-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_test.go
65 lines (51 loc) · 1.29 KB
/
timer_test.go
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
package chrono_test
import (
"context"
"testing"
"time"
"github.com/nnikolash/go-chrono"
"github.com/stretchr/testify/require"
)
func TestSimTimer(t *testing.T) {
t.Parallel()
s := chrono.NewSimulator(time.Now())
timer1fired := false
timer2fired := false
timer3fired := false
timer4fired := false
s.AfterFunc(2*time.Minute, func(now time.Time) {
timer1fired = true
})
timer2 := s.AfterFunc(2*time.Minute, func(now time.Time) {
timer2fired = true
})
timer3 := s.AfterFunc(2*time.Minute, func(now time.Time) {
timer3fired = true
})
timer4 := s.AfterFunc(2*time.Minute, func(now time.Time) {
timer4fired = true
})
s.AfterFunc(1*time.Minute, func(now time.Time) {
timer2.Stop()
timer3.Stop()
timer4.Reset(3 * time.Minute)
})
s.AfterFunc(3*time.Minute, func(now time.Time) {
require.True(t, timer1fired)
require.False(t, timer2fired)
require.False(t, timer3fired)
require.False(t, timer4fired)
timer3.Reset(1 * time.Minute)
})
s.AfterFunc(4*time.Minute, func(now time.Time) {
require.True(t, timer1fired)
require.False(t, timer2fired)
require.True(t, timer3fired)
require.True(t, timer4fired)
})
s.ProcessAll(context.Background())
require.True(t, timer1fired)
require.False(t, timer2fired)
require.True(t, timer3fired)
require.True(t, timer4fired)
}