-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontext_test.go
44 lines (41 loc) · 1.35 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
package utility
import (
"context"
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestIsContextError(t *testing.T) {
t.Run("ContextCanceledReturnsTrue", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
assert.True(t, IsContextError(ctx.Err()))
})
t.Run("ContextDeadlineExceededReturnsTrue", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel()
time.Sleep(10 * time.Millisecond)
assert.True(t, IsContextError(ctx.Err()))
})
t.Run("ContextWithoutErrorReturnsFalse", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
assert.False(t, IsContextError(ctx.Err()))
})
t.Run("NonContextErrorReturnsFalse", func(t *testing.T) {
assert.False(t, IsContextError(errors.New("custom error")))
})
t.Run("WrappedContextErrorReturnsFalse", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
wrappedErr := errors.Wrap(ctx.Err(), "wrapped error")
assert.False(t, IsContextError(wrappedErr))
})
t.Run("UnwrappedContextErrorReturnsTrue", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
wrappedErr := errors.Wrap(ctx.Err(), "wrapped error")
assert.True(t, IsContextError(errors.Cause(wrappedErr)))
})
}