-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_context_done_test.go
84 lines (71 loc) · 2.06 KB
/
check_context_done_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
package contextz
import (
"context"
"errors"
"io"
"testing"
"time"
)
type testContext struct {
DeadlineFunc func() (deadline time.Time, ok bool)
DoneFunc func() <-chan struct{}
ErrFunc func() error
ValueFunc func(key interface{}) interface{}
}
var _ context.Context = (*testContext)(nil)
func (c *testContext) Deadline() (deadline time.Time, ok bool) { return c.DeadlineFunc() }
func (c *testContext) Done() <-chan struct{} { return c.DoneFunc() }
func (c *testContext) Err() error { return c.ErrFunc() }
func (c *testContext) Value(key interface{}) interface{} { return c.ValueFunc(key) }
func TestCheckContextDone(t *testing.T) {
t.Parallel()
t.Run("success,", func(t *testing.T) {
t.Parallel()
err := CheckContext(context.Background())
if err != nil {
t.Fatalf("❌: err != nil: %v", err)
}
})
t.Run("error,context.Canceled", func(t *testing.T) {
t.Parallel()
contextCanceled, cancelCause := context.WithCancelCause(context.Background())
cancelCause(nil)
err := CheckContext(contextCanceled)
if !errors.Is(err, context.Canceled) {
t.Errorf("❌: !errors.Is(err, context.Canceled): %v", err)
}
})
t.Run("error,io.ErrUnexpectedEOF", func(t *testing.T) {
t.Parallel()
contextCanceled, cancelCause := context.WithCancelCause(context.Background())
cancelCause(io.ErrUnexpectedEOF)
err := CheckContext(contextCanceled)
if !errors.Is(err, io.ErrUnexpectedEOF) {
t.Errorf("❌: !errors.Is(err, io.ErrUnexpectedEOF): %v", err)
}
})
t.Run("error,nil", func(t *testing.T) {
t.Parallel()
errCalledCount := 0
err := CheckContext(&testContext{
DoneFunc: func() <-chan struct{} {
closed := make(chan struct{})
close(closed)
return closed
},
ErrFunc: func() error {
errCalledCount++
if errCalledCount == 1 {
return nil
}
return context.Canceled
},
ValueFunc: func(key interface{}) interface{} {
return nil
},
})
if !errors.Is(err, context.Canceled) {
t.Errorf("❌: !errors.Is(err, contextCanceled): %v", err)
}
})
}