-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathschedule_test.go
More file actions
72 lines (60 loc) · 1.72 KB
/
Copy pathschedule_test.go
File metadata and controls
72 lines (60 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package emailverifier
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Waits for firings rather than sleeping a fixed window: the third tick of a
// three-period sleep lands as the sleep expires, so an exact count is a coin
// flip. The count after stop() is settled by construction -- stop() sends on
// an unbuffered channel and the goroutine receives only while parked in its
// select, never mid job, so nothing can fire after it returns.
func TestStartScheduleOK(t *testing.T) {
var ops int32
fired := make(chan struct{}, 16)
s := newSchedule(10*time.Millisecond, func() {
atomic.AddInt32(&ops, 1)
select {
case fired <- struct{}{}:
default:
}
})
s.start()
for range 3 {
select {
case <-fired:
case <-time.After(5 * time.Second):
t.Fatalf("fired %d times in 5s, expected at least 3", atomic.LoadInt32(&ops))
}
}
s.stop()
settled := atomic.LoadInt32(&ops)
time.Sleep(50 * time.Millisecond)
assert.Equal(t, settled, atomic.LoadInt32(&ops), "schedule kept firing after stop")
}
func TestNewScheduleOK(t *testing.T) {
f := func() {}
actual := newSchedule(time.Minute, f)
assert.NotNil(t, actual)
}
func TestNewScheduleOK_FuncWithParams(t *testing.T) {
f := func(a, b int) bool {
return a > b
}
actual := newSchedule(time.Minute, f, 3, 4)
assert.NotNil(t, actual)
assert.Equal(t, []interface{}{3, 4}, actual.jobParams)
}
func TestNewScheduleWithWrongFunc(t *testing.T) {
f := "test"
actual := newSchedule(time.Minute, f)
assert.Nil(t, actual.jobParams)
assert.Equal(t, actual.jobFunc, f)
}
func TestRunScheduleFailedWithWrongFunc(t *testing.T) {
f := "test"
actual := newSchedule(time.Minute, f)
assert.Equal(t, actual.jobFunc, f)
assert.NotPanics(t, actual.start)
}