-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwrappers_test.go
83 lines (64 loc) · 1.93 KB
/
wrappers_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
package repeat
import (
"context"
"errors"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var (
errGolden = errors.New("golden")
)
func TestWrStopOnContextError_CallReturnOp(t *testing.T) {
opNilError := func(e error) error {
require.NoError(t, e)
return e
}
require.NoError(t, WrStopOnContextError(context.Background())(opNilError)(nil))
opError := func(e error) error {
require.EqualError(t, Cause(e), errGolden.Error())
return e
}
require.Error(t, WrStopOnContextError(context.Background())(opError)(HintTemporary(errGolden)))
}
func TestWrStopOnContextError_CancelOp(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
op := func(e error) error {
require.Fail(t, "should be never called")
return nil
}
cancel()
require.EqualError(t, WrStopOnContextError(ctx)(op)(nil), "repeat.stop: context canceled")
}
func TestWrStopOnContextError_CancelOpWithError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
op := func(e error) error {
require.Fail(t, "should be never called")
return nil
}
cancel()
require.EqualError(t, WrStopOnContextError(ctx)(op)(HintTemporary(errGolden)), "repeat.stop: golden")
require.EqualError(t, WrStopOnContextError(ctx)(op)(HintStop(errGolden)), "repeat.stop: golden")
require.EqualError(t, WrStopOnContextError(ctx)(op)(errGolden), "golden")
}
func TestWrStopOnContextError_SuccessIfDone(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
called := false
op := func(e error) error {
time.Sleep(time.Millisecond * 10)
called = true
return nil
}
go func() {
time.Sleep(time.Millisecond * 5)
cancel()
}()
require.NoError(t, WrStopOnContextError(ctx)(op)(HintTemporary(errGolden)))
require.True(t, called)
}
func TestForward(t *testing.T) {
op := func(e error) error { return nil }
require.Nil(t, Forward(nil))
require.Equal(t, reflect.ValueOf(op).Pointer(), reflect.ValueOf(op).Pointer())
}