-
Notifications
You must be signed in to change notification settings - Fork 3
/
clock_test.go
111 lines (90 loc) · 2.93 KB
/
clock_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
package clocks
import (
"context"
"runtime"
"testing"
"time"
)
func TestDefaultClock(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := DefaultClock()
base := c.Now()
// Sleep for a millisecond as some clocks only have ms resolution
if v := c.SleepFor(ctx, time.Millisecond); !v {
t.Errorf("unexpected return value: %t; expected true", v)
}
if negElapsed := c.Until(base); negElapsed < -time.Hour {
t.Errorf("c.Until returned a much more negative duration than expected: %s; expected at least %s",
negElapsed, time.Millisecond)
} else if negElapsed > -time.Millisecond {
t.Errorf("c.Until returned a much less negative duration than expected: %s; expected at least %s",
negElapsed, time.Millisecond)
}
// we'll cancel our context to awaken our sleeper.
wakeUpTime := base.Add(time.Hour * 24)
ch := make(chan bool)
go func() {
ch <- c.SleepUntil(ctx, wakeUpTime)
}()
// Give the goroutine a chance to run
runtime.Gosched()
cancel()
if v := <-ch; v {
t.Errorf("unexpected return value for SleepUntil (and by extension SleepFor): %t; expected false", v)
}
{
afCh := make(chan struct{})
c.AfterFunc(time.Millisecond, func() { close(afCh) })
<-afCh
}
}
func TestDefaultClockContext(t *testing.T) {
c := DefaultClock()
t.Run("ContextWithDeadlineExceeded", func(t *testing.T) {
base := c.Now()
ctx, cancel := c.ContextWithDeadline(context.Background(), base.Add(time.Millisecond))
t.Cleanup(cancel)
if v := c.SleepUntil(ctx, base.Add(time.Second)); v {
t.Errorf("unexpected return value: %t; expected false", v)
} else {
if ctx.Err() != context.DeadlineExceeded {
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.DeadlineExceeded)
}
}
})
t.Run("ContextWithDeadlineNotExceeded", func(t *testing.T) {
base := c.Now()
ctx, cancel := c.ContextWithDeadline(context.Background(), base.Add(3*time.Second))
t.Cleanup(cancel)
if v := c.SleepUntil(ctx, base.Add(time.Millisecond)); !v {
t.Errorf("unexpected return value: %t; expected true", v)
} else {
if ctx.Err() != nil {
t.Errorf("unexpected error: %v; expected nil", ctx.Err())
}
}
})
t.Run("ContextWithTimeoutExceeded", func(t *testing.T) {
ctx, cancel := c.ContextWithTimeout(context.Background(), time.Millisecond)
t.Cleanup(cancel)
if v := c.SleepFor(ctx, time.Second); v {
t.Errorf("unexpected return value: %t; expected false", v)
} else {
if ctx.Err() != context.DeadlineExceeded {
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.DeadlineExceeded)
}
}
})
t.Run("ContextWithTimeoutNotExceeded", func(t *testing.T) {
ctx, cancel := c.ContextWithTimeout(context.Background(), 3*time.Second)
t.Cleanup(cancel)
if v := c.SleepFor(ctx, time.Millisecond); !v {
t.Errorf("unexpected return value: %t; expected false", v)
} else {
if ctx.Err() != nil {
t.Errorf("unexpected error: %v; expected nil", ctx.Err())
}
}
})
}