-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_test.go
90 lines (83 loc) · 1.91 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package clock
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFakeTimer(t *testing.T) {
tests := []struct {
duration time.Duration
count int32
}{
{time.Second, 0},
{time.Second * 59, 0},
{time.Minute, 1},
{time.Hour, 1},
}
for _, test := range tests {
executed := int32(0)
clock := NewMock()
timer := clock.NewTimer(time.Minute)
go func() {
<-timer.Chan()
atomic.AddInt32(&executed, 1)
}()
clock.Forward(test.duration)
time.Sleep(time.Millisecond * 10)
assert.Equal(t, test.count, atomic.LoadInt32(&executed))
}
}
func TestFakeTimer_Stop(t *testing.T) {
tests := []struct {
duration time.Duration
stopped bool
count int32
}{
{time.Second, true, 0},
{time.Second * 2, false, 1},
}
for _, test := range tests {
executed := int32(0)
clock := NewMock()
timer := clock.NewTimer(time.Second * 2)
go func() {
<-timer.Chan()
atomic.AddInt32(&executed, 1)
}()
clock.Forward(test.duration)
assert.Equal(t, test.stopped, timer.Stop())
assert.Equal(t, test.count, atomic.LoadInt32(&executed))
}
}
func TestFakeTimer_Reset(t *testing.T) {
tests := []struct {
duration1 time.Duration
duration2 time.Duration
stopped bool
count1 int32
count2 int32
}{
{time.Second * 59, time.Minute, true, 0, 1},
{time.Second * 59, time.Second * 59, true, 0, 0},
{time.Minute, time.Minute, false, 1, 2},
}
for _, test := range tests {
executed := int32(0)
clock := NewMock()
timer := clock.NewTimer(time.Minute)
go func() {
for {
<-timer.Chan()
atomic.AddInt32(&executed, 1)
}
}()
clock.Forward(test.duration1)
time.Sleep(time.Millisecond * 10)
assert.Equal(t, test.count1, atomic.LoadInt32(&executed))
assert.Equal(t, test.stopped, timer.Reset(time.Minute))
clock.Forward(test.duration2)
time.Sleep(time.Millisecond * 10)
assert.Equal(t, test.count2, atomic.LoadInt32(&executed))
}
}