-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_test.go
171 lines (140 loc) · 4.37 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
package challenge02
import (
"fmt"
"testing"
)
// This test is failing and needs to be fixed first.
// After that continue test by test until you completed implementing the package :-)
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)
}
t.Logf("%s %s", 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")
// }
// }