-
Notifications
You must be signed in to change notification settings - Fork 1
/
abort_test.go
139 lines (125 loc) · 3.22 KB
/
abort_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
package futures
import (
"context"
"errors"
"log"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestAbortNoComplete(t *testing.T) {
ctx, _ := NewAbort()
require.Equal(t, nil, ctx.Err())
select {
case <-ctx.Done():
t.Fatal("abort unexpectedly completed")
default:
}
}
func TestAbortCompleteErr(t *testing.T) {
ctx, complete := NewAbort()
expectedErr := errors.New("TestAbortCompleteErr")
complete(expectedErr)
select {
case <-ctx.Done():
require.EqualError(t, ctx.Err(), expectedErr.Error())
default:
t.Fatal("abort unexpectedly completed")
}
}
func TestAbortCompleteNoErr(t *testing.T) {
ctx, complete := NewAbort()
complete(nil)
select {
case <-ctx.Done():
require.Equal(t, nil, ctx.Err())
default:
t.Fatal("abort unexpectedly completed")
}
}
func TestAbortMultipleComplete(t *testing.T) {
ctx, complete := NewAbort()
expectedErr := errors.New("TestAbortCompleteErr")
complete(expectedErr)
require.NotPanics(t, func() {
complete(nil)
})
select {
case <-ctx.Done():
require.EqualError(t, ctx.Err(), expectedErr.Error())
default:
t.Fatal("abort not completed as expected")
}
}
func TestAbortContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
abortCtx, _ := WithAbort(ctx)
cancel()
select {
case <-abortCtx.Done():
require.EqualError(t, abortCtx.Err(), context.Canceled.Error())
case <-time.After(10 * time.Millisecond): // context cancelation is not instantaneous
t.Fatal("abort not completed as expected")
}
}
func TestAbortContextCancelThenComplete(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
abortCtx, abortFunc := WithAbort(ctx)
done := make(chan struct{})
go func() {
<-ctx.Done()
abortFunc(errors.New("unexpected error"))
close(done)
}()
cancel()
<-done
select {
case <-abortCtx.Done():
require.EqualError(t, abortCtx.Err(), context.Canceled.Error())
case <-time.After(10 * time.Millisecond): // context cancelation is not instantaneous
t.Fatal("abort not completed as expected")
}
}
func TestAbortNoDeadline(t *testing.T) {
abortCtx, _ := NewAbort()
ctxDeadline, ok := abortCtx.Deadline()
if ok {
log.Fatal("expected invalid deadline")
}
require.Equal(t, time.Time{}, ctxDeadline)
}
func TestAbortContextDeadline(t *testing.T) {
deadline := time.Time{}.Add(1 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
abortCtx, _ := WithAbort(ctx)
ctxDeadline, ok := abortCtx.Deadline()
if !ok {
log.Fatal("expected valid deadline")
}
require.Equal(t, deadline, ctxDeadline)
}
func TestAbortNoValues(t *testing.T) {
abortCtx, _ := NewAbort()
val := abortCtx.Value("abc")
require.Equal(t, nil, val)
}
type abortTestKey string
func TestAbortContextValues(t *testing.T) {
key := abortTestKey("key")
value := "value"
ctx := context.WithValue(context.Background(), key, value)
abortCtx, _ := WithAbort(ctx)
val := abortCtx.Value(key)
require.Equal(t, value, val)
}
func TestAbortInner(t *testing.T) {
abortCtx, abortFunc := NewAbort()
defer abortFunc(nil)
ctx, cancel := context.WithCancel(abortCtx)
defer cancel()
expectedErr := errors.New("TestAbortInner")
abortFunc(expectedErr)
<-ctx.Done()
require.EqualError(t, ctx.Err(), expectedErr.Error())
}