-
Notifications
You must be signed in to change notification settings - Fork 5
/
timeline_test.go
138 lines (114 loc) · 3.02 KB
/
timeline_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
package timers
import (
"context"
"sync"
"testing"
"time"
)
func TestTimeline(t *testing.T) {
tests := []struct {
scenario string
function func(*testing.T)
}{
{
scenario: "scheduling a deadline every 10ms triggers as expected",
function: testTimeline10ms,
},
{
scenario: "multiple goroutines waiting on the same timeline are all notified when the deadline expires",
function: testTimelineMulti,
},
{
scenario: "canceling a timeline cancels all contexts of this timeline",
function: testTimelineCancel,
},
{
scenario: "canceling the background context of a timeline also cancels contexts that it created",
function: testTimelineBackground,
},
}
for _, test := range tests {
testFunc := test.function
t.Run(test.scenario, func(t *testing.T) {
t.Parallel()
testFunc(t)
})
}
}
func testTimeline10ms(t *testing.T) {
timeline := Timeline{Resolution: 1 * time.Millisecond}
defer timeline.Cancel()
for i := 0; i != 100; i++ {
t0 := time.Now()
ctx := timeline.Timeout(10 * time.Millisecond)
<-ctx.Done()
t1 := time.Now()
d, _ := ctx.Deadline()
for j, delay := range []time.Duration{d.Sub(t0), t1.Sub(t0)} {
if delay < (10 * time.Millisecond) {
t.Error("the delay is too short, expected > 10ms, got", delay, "at", j, "/", i)
}
if delay > (15 * time.Millisecond) {
t.Error("the delay is too large, expected < 13ms, got", delay, "at", j, "/", i)
}
}
if err := ctx.Err(); err != context.DeadlineExceeded {
t.Error("bad context error:", err)
}
}
}
func testTimelineMulti(t *testing.T) {
timeline := Timeline{Resolution: 10 * time.Millisecond}
defer timeline.Cancel()
wg := sync.WaitGroup{}
deadline := time.Now().Add(100 * time.Millisecond)
for i := 0; i != 10; i++ {
wg.Add(1)
go func(ctx context.Context) {
<-ctx.Done()
wg.Done()
}(timeline.Deadline(deadline))
}
wg.Wait()
}
func testTimelineCancel(t *testing.T) {
timeline := Timeline{}
ctx1 := timeline.Timeout(1 * time.Second)
ctx2 := timeline.Timeout(2 * time.Second)
ctx3 := timeline.Timeout(3 * time.Second)
timeline.Cancel()
for _, ctx := range []context.Context{ctx1, ctx2, ctx3} {
if err := ctx.Err(); err != context.Canceled {
t.Error("bad context error:", err)
}
}
}
func testTimelineBackground(t *testing.T) {
background, cancel := context.WithCancel(context.Background())
timeline := Timeline{Background: background}
ctx1 := timeline.Timeout(1 * time.Second)
ctx2 := timeline.Timeout(2 * time.Second)
ctx3 := timeline.Timeout(3 * time.Second)
cancel()
for _, ctx := range []context.Context{ctx1, ctx2, ctx3} {
if err := ctx.Err(); err != context.Canceled {
t.Error("bad context error:", err)
}
}
}
func BenchmarkTimeline(b *testing.B) {
timeouts := []time.Duration{
100 * time.Millisecond,
250 * time.Millisecond,
500 * time.Millisecond,
1 * time.Second,
10 * time.Second,
}
timeline := Timeline{}
defer timeline.Cancel()
b.RunParallel(func(pb *testing.PB) {
for i := 0; pb.Next(); i++ {
timeline.Timeout(timeouts[i%len(timeouts)])
}
})
}