-
Notifications
You must be signed in to change notification settings - Fork 312
/
uncancel_context_test.go
91 lines (84 loc) · 2.23 KB
/
uncancel_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
package fsm
import (
"context"
"testing"
)
func TestUncancel(t *testing.T) {
t.Run("create a new context", func(t *testing.T) {
t.Run("and cancel it", func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, "key1", "value1")
ctx, cancelFunc := context.WithCancel(ctx)
cancelFunc()
if ctx.Err() != context.Canceled {
t.Errorf("expected context error 'context canceled', got %v", ctx.Err())
}
select {
case <-ctx.Done():
default:
t.Error("expected context to be done but it wasn't")
}
t.Run("and uncancel it", func(t *testing.T) {
ctx, newCancelFunc := uncancelContext(ctx)
if ctx.Err() != nil {
t.Errorf("expected context error to be nil, got %v", ctx.Err())
}
select {
case <-ctx.Done():
t.Fail()
default:
}
t.Run("now it should still contain the values", func(t *testing.T) {
if ctx.Value("key1") != "value1" {
t.Errorf("expected context value of key 'key1' to be 'value1', got %v", ctx.Value("key1"))
}
})
t.Run("and cancel the child", func(t *testing.T) {
newCancelFunc()
if ctx.Err() != context.Canceled {
t.Errorf("expected context error 'context canceled', got %v", ctx.Err())
}
select {
case <-ctx.Done():
default:
t.Error("expected context to be done but it wasn't")
}
})
})
})
t.Run("and uncancel it", func(t *testing.T) {
ctx := context.Background()
parent := ctx
ctx, newCancelFunc := uncancelContext(ctx)
if ctx.Err() != nil {
t.Errorf("expected context error to be nil, got %v", ctx.Err())
}
select {
case <-ctx.Done():
t.Fail()
default:
}
t.Run("and cancel the child", func(t *testing.T) {
newCancelFunc()
if ctx.Err() != context.Canceled {
t.Errorf("expected context error 'context canceled', got %v", ctx.Err())
}
select {
case <-ctx.Done():
default:
t.Error("expected context to be done but it wasn't")
}
t.Run("and ensure the parent is not affected", func(t *testing.T) {
if parent.Err() != nil {
t.Errorf("expected parent context error to be nil, got %v", ctx.Err())
}
select {
case <-parent.Done():
t.Fail()
default:
}
})
})
})
})
}