-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock_fake_test.go
101 lines (87 loc) · 2.15 KB
/
clock_fake_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
91
92
93
94
95
96
97
98
99
100
101
package clock
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMock_Forward(t *testing.T) {
c := NewMock()
n := c.Now()
diff := time.Hour
c.Forward(diff)
assert.Equal(t, c.Now(), n.Add(diff))
}
func TestMock_Set(t *testing.T) {
c := NewMock()
target := time.Unix(1000, 0)
assert.NotEqual(t, c.Now(), target)
c.Set(target)
time.Sleep(time.Millisecond * 10)
assert.Equal(t, c.Now(), target)
}
func TestMock_RunUntilDone(t *testing.T) {
c := NewMock()
var fired int32
go incUponReceive(c.NewTicker(time.Second).Chan(), &fired)
go incUponReceive(c.NewTimer(time.Minute).Chan(), &fired)
before := c.Now()
sched()
c.RunUntilDone()
assert.Equal(t, time.Minute, c.Since(before))
assert.Equal(t, int32(61), atomic.LoadInt32(&fired))
}
func TestMock_Since(t *testing.T) {
c := NewMock()
target := time.Unix(1000, 0)
diff := time.Hour
c.Set(target.Add(diff))
assert.Equal(t, c.Since(target), diff)
}
func TestMock_Until(t *testing.T) {
c := NewMock()
target := time.Unix(1000, 0)
diff := time.Hour
c.Set(target)
assert.Equal(t, c.Until(target.Add(diff)), diff)
}
func TestMock_After(t *testing.T) {
received := int32(0)
clock := NewMock()
ch := clock.After(time.Minute)
go func() {
<-ch
atomic.AddInt32(&received, 1)
}()
clock.Forward(time.Second * 59)
assert.Zero(t, atomic.LoadInt32(&received))
clock.Forward(time.Second)
assert.NotZero(t, atomic.LoadInt32(&received))
}
func TestMock_AfterFunc(t *testing.T) {
received := int32(0)
clock := NewMock()
fn := func() {
atomic.AddInt32(&received, 1)
}
clock.AfterFunc(time.Minute, fn)
clock.Forward(time.Second * 59)
assert.Zero(t, atomic.LoadInt32(&received))
clock.Forward(time.Second)
assert.NotZero(t, atomic.LoadInt32(&received))
}
func TestMock_Sleep(t *testing.T) {
received := int32(0)
clock := NewMock()
go func() {
clock.Sleep(time.Hour + time.Second)
atomic.AddInt32(&received, 1)
}()
sched()
clock.Forward(time.Hour)
assert.Zero(t, atomic.LoadInt32(&received))
clock.Forward(time.Second)
// Go to sleep just in case the goroutine wasn't scheduled yet
time.Sleep(time.Millisecond)
assert.NotZero(t, atomic.LoadInt32(&received))
}