-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
143 lines (128 loc) · 3.6 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
package process_test
import (
"context"
"fmt"
"log"
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/kevindejong/process"
)
func TestContextNoCancel(t *testing.T) {
ctx, _ := process.NewContext()
require.Equal(t, nil, ctx.Err())
select {
case <-ctx.Done():
t.Fatal("context unexpectedly canceld")
default:
}
}
func TestContextCancelErr(t *testing.T) {
ctx, cancel := process.NewContext()
expectedErr := errors.New("TestContextCancelErr")
cancel(expectedErr)
select {
case <-ctx.Done():
require.EqualError(t, ctx.Err(), fmt.Sprintf("context canceled: %s", expectedErr.Error()))
default:
t.Fatal("context unexpectedly canceld")
}
}
func TestContextCancelNoErr(t *testing.T) {
ctx, cancel := process.NewContext()
cancel(nil)
select {
case <-ctx.Done():
require.EqualError(t, ctx.Err(), "context canceled: <nil>")
require.Equal(t, nil, errors.Cause(ctx.Err()))
default:
t.Fatal("context unexpectedly canceled")
}
}
func TestContextMultipleCancel(t *testing.T) {
ctx, cancel := process.NewContext()
expectedErr := errors.New("TestContextCancelErr")
cancel(expectedErr)
require.NotPanics(t, func() {
cancel(nil)
})
select {
case <-ctx.Done():
require.EqualError(t, ctx.Err(), fmt.Sprintf("context canceled: %s", expectedErr.Error()))
default:
t.Fatal("context not canceld as expected")
}
}
func TestContextContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
customCtx, _ := process.WithCancel(ctx)
cancel()
select {
case <-customCtx.Done():
require.EqualError(t, customCtx.Err(), context.Canceled.Error())
case <-time.After(10 * time.Millisecond): // context cancelation is not instantaneous
t.Fatal("context not canceled as expected")
}
}
func TestContextContextCancelThenCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
customCtx, cancelFunc := process.WithCancel(ctx)
done := make(chan struct{})
go func() {
<-ctx.Done()
cancelFunc(errors.New("unexpected error"))
close(done)
}()
cancel()
<-done
select {
case <-customCtx.Done():
require.EqualError(t, customCtx.Err(), context.Canceled.Error())
case <-time.After(10 * time.Millisecond): // context cancelation is not instantaneous
t.Fatal("context not canceld as expected")
}
}
func TestContextNoDeadline(t *testing.T) {
customCtx, _ := process.NewContext()
ctxDeadline, ok := customCtx.Deadline()
if ok {
log.Fatal("expected invalid deadline")
}
require.Equal(t, time.Time{}, ctxDeadline)
}
func TestContextContextDeadline(t *testing.T) {
deadline := time.Time{}.Add(1 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
customCtx, _ := process.WithCancel(ctx)
ctxDeadline, ok := customCtx.Deadline()
if !ok {
log.Fatal("expected valid deadline")
}
require.Equal(t, deadline, ctxDeadline)
}
func TestContextNoValues(t *testing.T) {
customCtx, _ := process.NewContext()
val := customCtx.Value("abc")
require.Equal(t, nil, val)
}
type contextTestKey string
func TestContextContextValues(t *testing.T) {
key := contextTestKey("key")
value := "value"
ctx := context.WithValue(context.Background(), key, value)
customCtx, _ := process.WithCancel(ctx)
val := customCtx.Value(key)
require.Equal(t, value, val)
}
func TestContextInner(t *testing.T) {
customCtx, cancelFunc := process.NewContext()
defer cancelFunc(nil)
ctx, cancel := context.WithCancel(customCtx)
defer cancel()
expectedErr := errors.New("TestContextInner")
cancelFunc(expectedErr)
<-ctx.Done()
require.EqualError(t, ctx.Err(), fmt.Sprintf("context canceled: %s", expectedErr.Error()))
}