Skip to content

Add unit tests #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package timertask

import (
"testing"
"time"
)

func MockSleepInterval(t *testing.T, duration time.Duration) {
old := sleepDuration
sleepDuration = duration
t.Cleanup(func() { sleepDuration = old })
}

func MockUnixTimeNow(t *testing.T, timeFunc func() int64) {
old := unixTimeNow
unixTimeNow = timeFunc
t.Cleanup(func() { unixTimeNow = old })
}

func (m *Manager) WorkersByIntervals() map[int64]int64 {
return m.workersByIntervals
}

func (m *Manager) Workers() map[int64]map[int64]taskConfig {
return m.workers
}
9 changes: 7 additions & 2 deletions timertask.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"time"
)

var sleepDuration = 1 * time.Second // nolint:gochecknoglobals
var unixTimeNow = func() int64 { // nolint:gochecknoglobals
return time.Now().Unix()
}

type taskConfig struct {
interval time.Duration
callback func() error
Expand Down Expand Up @@ -84,7 +89,7 @@ func (m *Manager) Reset(id int64) {
func (m *Manager) Start() {
go func() {
for {
nowSeconds := time.Now().Unix()
nowSeconds := unixTimeNow()

m.mutex.Lock()
workers, ok := m.workers[nowSeconds]
Expand All @@ -110,7 +115,7 @@ func (m *Manager) Start() {
}
m.mutex.Unlock()

time.Sleep(1 * time.Second)
time.Sleep(sleepDuration)
}
}()
}
86 changes: 42 additions & 44 deletions timertask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,59 @@ import (
"github.com/xconnio/timertask"
)

func TestManager_Schedule(t *testing.T) {
func TestSchedule(t *testing.T) {
manager := timertask.NewManager()

messages1 := 0
manager.Schedule(1, 2*time.Second, func() error {
messages1++
callback := func() error {
return nil
})
}
manager.Schedule(1, 1*time.Second, callback)
require.NotNil(t, manager.WorkersByIntervals()[1])
require.Len(t, manager.Workers(), 1)
}

messages2 := 0
manager.Schedule(2, 1*time.Second, func() error {
messages2++
func TestCancel(t *testing.T) {
manager := timertask.NewManager()
callback := func() error {
return nil
})

go manager.Start()

// Wait for worker
time.Sleep(3 * time.Second)

require.NotZero(t, messages1)
require.NotZero(t, messages2)

// Cancel worker1 and check
}
manager.Schedule(1, 1*time.Second, callback)
manager.Cancel(1)
time.Sleep(3 * time.Second)

require.Equal(t, messages1, 1)

require.Greater(t, messages2, 2)
_, exists := manager.WorkersByIntervals()[1]
require.False(t, exists)
require.Len(t, manager.Workers(), 0)
}

func TestManager_Reset(t *testing.T) {
func TestReset(t *testing.T) {
manager := timertask.NewManager()

messages := 0
manager.Schedule(1, 2*time.Second, func() error {
messages++
callback := func() error {
return nil
})

manager.Start()
}
manager.Schedule(1, 1*time.Second, callback)
manager.Reset(1)

// Wait for first work
time.Sleep(3 * time.Second)
require.Equal(t, 1, messages)
require.NotNil(t, manager.WorkersByIntervals()[1])
require.Len(t, manager.Workers(), 1)
}

time.Sleep(900 * time.Millisecond)
// Reset worker1 and check work
manager.Reset(1)
func TestStart(t *testing.T) {
timertask.MockSleepInterval(t, 1*time.Millisecond)
initialTime := time.Now()
timertask.MockUnixTimeNow(t, func() int64 {
return initialTime.Unix()
})

time.Sleep(1 * time.Second)
require.Equal(t, 1, messages)
manager := timertask.NewManager()
callbackCalled := make(chan bool, 1)
callback := func() error {
callbackCalled <- true
return nil
}
manager.Schedule(1, 10*time.Millisecond, callback)
manager.Start()

// Wait for another work after reset
time.Sleep(2 * time.Second)
require.Equal(t, 2, messages)
select {
case <-callbackCalled:
case <-time.After(20 * time.Millisecond):
t.Fatal("Callback was not called in time")
}
}
Loading