-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkronika_test.go
116 lines (91 loc) · 2.74 KB
/
kronika_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
package kronika
import (
"context"
"testing"
"time"
)
func TestWaitFor(t *testing.T) {
interval := 200 * time.Millisecond
ctx := context.Background()
t1 := time.Now()
WaitFor(ctx, interval)
t2 := time.Now()
actualInterval := t2.Sub(t1)
if actualInterval < interval {
t.Fatalf("Difference between times should be more than %d, but was %d", interval, actualInterval)
}
}
func TestWaitForCancel(t *testing.T) {
interval := 200 * time.Millisecond
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately
t1 := time.Now()
WaitFor(ctx, interval) // this should return immediately and so not reach our interval
t2 := time.Now()
actualInterval := t2.Sub(t1)
if actualInterval > interval {
t.Fatalf("Difference between times should be less than %d, but was %d", interval, actualInterval)
}
}
func TestWaitUntil(t *testing.T) {
interval := 200 * time.Millisecond
ctx := context.Background()
t1 := time.Now().Add(interval)
WaitUntil(ctx, t1)
t2 := time.Now()
actualInterval := t2.Sub(t1)
if actualInterval < 0 {
t.Fatalf("t2 should be after t1 but was not. t2: %d, t1: %d", t2.UnixNano(), t1.UnixNano())
}
}
func TestWaitUntilCancel(t *testing.T) {
interval := 200 * time.Millisecond
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately
t1 := time.Now().Add(interval)
WaitUntil(ctx, t1) // this should return immediately and so not reach t1
t2 := time.Now()
actualInterval := t2.Sub(t1)
if actualInterval > 0 {
t.Fatalf("t2 should be before t1 but was not. t2: %d, t1: %d", t2.UnixNano(), t1.UnixNano())
}
}
func TestEvery(t *testing.T) {
interval := 200 * time.Millisecond
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // cancel whenever it exits
lastT := time.Now()
start := lastT.Add(interval)
i := 0
noOfTicks := 10
for currT := range Every(ctx, start, interval) {
if i == noOfTicks {
cancel() // cancel the context
} else {
i++
}
diff := currT.Sub(lastT)
slop := diff * 2 / 10 // variation allowance see https://golang.org/src/time/tick_test.go
if diff-slop > interval || diff+slop < interval {
t.Fatalf("Interval since last tick should be at least %d but was %d.", interval, diff)
}
lastT = currT
}
if i != noOfTicks {
t.Fatalf("Ticker should have run %d times but ran %d times.", noOfTicks, i)
}
}
func TestEveryCancelBeforeFistEvent(t *testing.T) {
interval := 5 * time.Second
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately
start := time.Now()
for range Every(ctx, start, interval) {
}
length := time.Now().Sub(start)
if length > time.Millisecond*10 {
t.Fatalf(
"Ticker should have stopped almost immediately, but took %dms",
length/time.Millisecond)
}
}