-
Notifications
You must be signed in to change notification settings - Fork 5
/
funcs_disabled_test.go
123 lines (99 loc) · 2.09 KB
/
funcs_disabled_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// +build timex_disable
package timex_test
import (
"testing"
"time"
"github.com/cabify/timex"
"github.com/stretchr/testify/assert"
)
func TestNow(t *testing.T) {
diff := time.Since(timex.Now())
assert.True(t, diff < time.Second)
assert.True(t, diff > 0)
}
func TestSince(t *testing.T) {
diff := timex.Since(time.Now())
assert.True(t, diff < time.Second)
assert.True(t, diff > 0)
}
func TestUntil(t *testing.T) {
diff := timex.Until(time.Now())
assert.True(t, diff < 0)
assert.True(t, diff > -time.Second)
}
func TestAfterFunc(t *testing.T) {
timeout := time.After(time.Second)
ok := make(chan struct{})
timex.AfterFunc(time.Millisecond, func() { close(ok) })
select {
case <-ok:
// ok
case <-timeout:
t.Errorf("Timeout waiting for AfterFunc")
}
}
func TestSleep(t *testing.T) {
timeout := time.After(time.Second)
ok := make(chan struct{})
go func() {
timex.Sleep(time.Millisecond)
close(ok)
}()
select {
case <-ok:
// ok
case <-timeout:
t.Errorf("Timeout waiting for Sleep")
}
}
func TestAfter(t *testing.T) {
timeout := time.After(time.Second)
ok := timex.After(time.Millisecond)
select {
case <-ok:
// ok
case <-timeout:
t.Errorf("Timeout waiting for After")
}
}
func TestNewTicker(t *testing.T) {
timeout := time.After(time.Second)
ticker := timex.NewTicker(100 * time.Millisecond)
select {
case <-ticker.C():
// ok
case <-timeout:
t.Errorf("Timeout waiting for Ticker")
}
ticker.Stop()
ok := timex.After(200 * time.Millisecond)
select {
case <-ticker.C():
t.Errorf("Should not tick again since it's stopped")
case <-ok:
// ok
}
}
func TestNewTimer(t *testing.T) {
t.Run("tick", func(t *testing.T) {
timeout := time.After(time.Second)
timer := timex.NewTimer(100 * time.Millisecond)
select {
case <-timer.C():
// ok
case <-timeout:
t.Errorf("Timeout waiting for Mock")
}
})
t.Run("stop", func(t *testing.T) {
timer := timex.NewTimer(100 * time.Millisecond)
timer.Stop()
ok := timex.After(200 * time.Millisecond)
select {
case <-timer.C():
t.Errorf("Should not tick since it's stopped")
case <-ok:
// ok
}
})
}