forked from benbjohnson/clock
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext_test.go
110 lines (102 loc) · 3.4 KB
/
context_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
package clock
import (
"context"
"errors"
"testing"
"time"
)
// delay is the extra delay use to ensure that enough time has passed
const delay = 10 * time.Millisecond
// Ensure that WithDeadline is cancelled when deadline exceeded.
func TestMock_WithDeadline(t *testing.T) {
c := New()
cause := errors.New("example cause")
ctx, _ := ContextWithDeadlineCause(context.Background(), c, c.Now().Add(time.Second), cause)
time.Sleep(time.Second + delay)
select {
case <-ctx.Done():
if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Error("invalid type of error returned when deadline exceeded")
}
if context.Cause(ctx) != cause {
t.Error("cause does not match expected cause")
}
default:
t.Error("context is not cancelled when deadline exceeded")
}
}
// Ensure that WithDeadline does nothing when the deadline is later than the current deadline.
func TestMock_WithDeadlineLaterThanCurrent(t *testing.T) {
c := New()
cause := errors.New("example cause")
ctx, _ := ContextWithDeadlineCause(context.Background(), c, c.Now().Add(time.Second), cause)
ctx, _ = ContextWithDeadlineCause(ctx, c, c.Now().Add(10*time.Second), cause)
time.Sleep(time.Second + delay)
select {
case <-ctx.Done():
if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Error("invalid type of error returned when deadline exceeded")
}
if context.Cause(ctx) != cause {
t.Error("cause does not match expected cause")
}
default:
t.Error("context is not cancelled when deadline exceeded")
}
}
// Ensure that WithDeadline cancel closes Done channel with context.Canceled error.
func TestMock_WithDeadlineCancel(t *testing.T) {
c := New()
ctx, cancel := ContextWithDeadline(context.Background(), c, c.Now().Add(time.Second))
cancel()
select {
case <-ctx.Done():
if !errors.Is(ctx.Err(), context.Canceled) {
t.Error("invalid type of error returned after cancellation")
}
case <-time.After(time.Second + delay):
t.Error("context is not cancelled after cancel was called")
}
}
// Ensure that WithDeadline closes child contexts after it was closed.
func TestMock_WithDeadlineCancelledWithParent(t *testing.T) {
c := New()
parent, cancel := context.WithCancel(context.Background())
ctx, _ := ContextWithDeadline(parent, c, c.Now().Add(time.Second))
cancel()
select {
case <-ctx.Done():
if !errors.Is(ctx.Err(), context.Canceled) {
t.Error("invalid type of error returned after cancellation")
}
case <-time.After(time.Second + delay):
t.Error("context is not cancelled when parent context is cancelled")
}
}
// Ensure that WithDeadline cancelled immediately when deadline has already passed.
func TestMock_WithDeadlineImmediate(t *testing.T) {
c := New()
ctx, _ := ContextWithDeadline(context.Background(), c, c.Now().Add(-time.Second))
select {
case <-ctx.Done():
if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Error("invalid type of error returned when deadline has already passed")
}
default:
t.Error("context is not cancelled when deadline has already passed")
}
}
// Ensure that WithTimeout is cancelled when deadline exceeded.
func TestMock_WithTimeout(t *testing.T) {
c := New()
ctx, _ := ContextWithTimeout(context.Background(), c, time.Second)
time.Sleep(time.Second + delay)
select {
case <-ctx.Done():
if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Error("invalid type of error returned when time is over")
}
default:
t.Error("context is not cancelled when time is over")
}
}