-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathticker_test.go
140 lines (119 loc) · 2.96 KB
/
ticker_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package crong
import (
"context"
"testing"
"time"
)
func TestTicker(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
s, err := New("* * * * *", nil) // every minute
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
ticker := NewTicker(ctx, s, 5*time.Second)
if ticker == nil {
t.Fatalf("expected ticker")
}
defer ticker.Stop()
nextTick := s.Next(time.Now())
select {
case <-ctx.Done():
t.Fatalf("expected tick")
case tick := <-ticker.C:
tickMin := tick.Truncate(time.Minute)
nextMin := nextTick.Truncate(time.Minute)
if !tickMin.Equal(nextMin) {
t.Fatalf("expected tick to be %s, got %s", tickMin, nextMin)
}
}
}
func TestEarlyTicker(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s, err := New("* * * * *", nil) // every minute
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
ticker := NewTicker(ctx, s, 5*time.Second)
if ticker == nil {
t.Fatalf("expected ticker")
}
defer ticker.Stop()
nextTick := s.Next(time.Now())
go func() {
ticker.tick(ctx)
}()
select {
case <-ctx.Done():
t.Fatalf("expected tick")
case tick := <-ticker.C:
tickSecs := tick.Unix()
nextSecs := nextTick.Unix()
if nextSecs <= tickSecs {
t.Fatalf("expected tick to be %d, got %d", tickSecs, nextSecs)
}
}
}
// TestTickerCanceled verifies that we no longer receive ticks after
// canceling the context provided to the ticker
func TestTickerCanceled(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s, err := New("* * * * *", nil) // every minute
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
ticker := NewTicker(ctx, s, 5*time.Second)
if ticker == nil {
t.Fatalf("expected ticker")
}
defer ticker.Stop()
tctx, tcancel := context.WithCancel(context.Background())
defer tcancel()
sawTick := make(chan time.Time, 1)
go func() {
select {
case <-tctx.Done():
return
case <-ticker.C:
sawTick <- time.Now()
}
}()
// cancel the context, which should prevent the tick from being emitted
cancel()
go func() {
time.Sleep(500 * time.Millisecond)
if sent := ticker.tick(ctx); sent {
t.Errorf("expected no tick to be sent")
}
}()
cctx, ccancel := context.WithTimeout(context.Background(), 6*time.Second)
defer ccancel()
select {
case <-sawTick:
t.Fatalf("shouldn't have received tick")
case <-cctx.Done():
tcancel()
return
}
}
func TestTickerSendTimeout(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s, err := New("* * * * *", nil) // every minute
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
ticker := NewTicker(ctx, s, 3*time.Second)
if ticker == nil {
t.Fatalf("expected ticker")
}
defer ticker.Stop()
ticker.tick(ctx)
time.Sleep(5 * time.Second)
assertEqual(t, ticker.ticksDropped.Load(), int64(1))
}