forked from campoy/justforfunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
187 lines (155 loc) · 4.47 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2016 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to writing, software distributed
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
package contextimpl
import (
"fmt"
"math"
"testing"
"time"
)
func TestBackgroundNotTODO(t *testing.T) {
todo := fmt.Sprint(TODO())
bg := fmt.Sprint(Background())
if todo == bg {
t.Errorf("TODO and Background are equal: %q vs %q", todo, bg)
}
}
func TestWithCancel(t *testing.T) {
ctx, cancel := WithCancel(Background())
if err := ctx.Err(); err != nil {
t.Errorf("error should be nil first, got %v", err)
}
cancel()
<-ctx.Done()
if err := ctx.Err(); err != Canceled {
t.Errorf("error should be canceled now, got %v", err)
}
}
func TestWithCancelConcurrent(t *testing.T) {
ctx, cancel := WithCancel(Background())
time.AfterFunc(1*time.Second, cancel)
if err := ctx.Err(); err != nil {
t.Errorf("error should be nil first, got %v", err)
}
<-ctx.Done()
if err := ctx.Err(); err != Canceled {
t.Errorf("error should be canceled now, got %v", err)
}
}
func TestWithCancelPropagation(t *testing.T) {
ctxA, cancelA := WithCancel(Background())
ctxB, cancelB := WithCancel(ctxA)
defer cancelB()
cancelA()
select {
case <-ctxB.Done():
case <-time.After(1 * time.Second):
t.Errorf("time out")
}
if err := ctxB.Err(); err != Canceled {
t.Errorf("error should be canceled now, got %v", err)
}
}
func TestWithDeadline(t *testing.T) {
deadline := time.Now().Add(2 * time.Second)
ctx, cancel := WithDeadline(Background(), deadline)
if d, ok := ctx.Deadline(); !ok || d != deadline {
t.Errorf("expected deadline %v; got %v", deadline, d)
}
then := time.Now()
<-ctx.Done()
if d := time.Since(then); math.Abs(d.Seconds()-2.0) > 0.1 {
t.Errorf("should have been done after 2.0 seconds, took %v", d)
}
if err := ctx.Err(); err != DeadlineExceeded {
t.Errorf("error should be DeadlineExceeded, got %v", err)
}
cancel()
if err := ctx.Err(); err != DeadlineExceeded {
t.Errorf("error should still be DeadlineExceeded, got %v", err)
}
}
func TestWithTimeout(t *testing.T) {
timeout := 2 * time.Second
deadline := time.Now().Add(timeout)
ctx, cancel := WithTimeout(Background(), timeout)
if d, ok := ctx.Deadline(); !ok || d.Sub(deadline) > time.Millisecond {
t.Errorf("expected deadline %v; got %v", deadline, d)
}
then := time.Now()
<-ctx.Done()
if d := time.Since(then); math.Abs(d.Seconds()-2.0) > 0.1 {
t.Errorf("should have been done after 2.0 seconds, took %v", d)
}
if err := ctx.Err(); err != DeadlineExceeded {
t.Errorf("error should be DeadlineExceeded, got %v", err)
}
cancel()
if err := ctx.Err(); err != DeadlineExceeded {
t.Errorf("error should still be DeadlineExceeded, got %v", err)
}
}
func TestWithValue(t *testing.T) {
tc := []struct {
key, val, keyRet, valRet interface{}
shouldPanic bool
}{
{"a", "b", "a", "b", false},
{"a", "b", "c", nil, false},
{42, true, 42, true, false},
{42, true, int64(42), nil, false},
{nil, true, nil, nil, true},
{[]int{1, 2, 3}, true, []int{1, 2, 3}, nil, true},
}
for _, tt := range tc {
var panicked interface{}
func() {
defer func() { panicked = recover() }()
ctx := WithValue(Background(), tt.key, tt.val)
if val := ctx.Value(tt.keyRet); val != tt.valRet {
t.Errorf("expected value %v, got %v", tt.valRet, val)
}
}()
if panicked != nil && !tt.shouldPanic {
t.Errorf("unexpected panic: %v", panicked)
}
if panicked == nil && tt.shouldPanic {
t.Errorf("expected panic, but didn't get it")
}
}
}
func TestDeadlineExceededIsTimeouter(t *testing.T) {
f := func(ctx Context) error {
<-ctx.Done()
return ctx.Err()
}
type timeouter interface {
Timeout() bool
}
ctx, cancel := WithCancel(Background())
cancel()
err := f(ctx)
if _, ok := err.(timeouter); ok {
t.Errorf("canceled context should not have Timeout method")
}
ctx, cancel = WithTimeout(Background(), 1*time.Millisecond)
defer cancel()
err = f(ctx)
if _, ok := err.(timeouter); !ok {
t.Errorf("deadline exceeded context should have Timeout method")
}
}
var (
_ Context
_ testing.T
)