-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvokers_test.go
78 lines (65 loc) · 1.85 KB
/
invokers_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
package shift
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDeadlineInvoker_Invoke(t *testing.T) {
t.Run("with timeout", func(t *testing.T) {
var called bool
invoker := &deadlineInvoker{
timeout: time.Millisecond,
timeoutCallback: func() { called = true },
}
var fn Operate = func(context.Context) (interface{}, error) {
time.Sleep(2 * time.Millisecond)
return nil, nil
}
res, err := invoker.invoke(context.Background(), fn)
assert.Error(t, err)
assert.IsType(t, &InvocationTimeoutError{}, err)
assert.Nil(t, res)
assert.Equal(t, true, called)
})
t.Run("without timeout", func(t *testing.T) {
var called bool
invoker := &deadlineInvoker{
timeout: time.Second,
timeoutCallback: func() { called = true },
}
t.Run("on failure", func(t *testing.T) {
var fn Operate = func(context.Context) (interface{}, error) {
return nil, errors.New("operation error")
}
res, err := invoker.invoke(context.Background(), fn)
assert.Error(t, err)
assert.EqualError(t, err, "operation error")
assert.Nil(t, res)
assert.Equal(t, false, called)
})
t.Run("on success", func(t *testing.T) {
const val = "test"
var fn Operate = func(context.Context) (interface{}, error) {
return val, nil
}
res, err := invoker.invoke(context.Background(), fn)
assert.NoError(t, err)
assert.Equal(t, val, res.(string))
assert.Equal(t, false, called)
})
})
}
func TestOnOpenInvoker_Invoke(t *testing.T) {
var called bool
invoker := &onOpenInvoker{rejectCallback: func() {
called = true
}}
var fn Operate = func(context.Context) (interface{}, error) { return nil, nil }
res, err := invoker.invoke(context.Background(), fn)
assert.Error(t, err)
assert.IsType(t, &IsOnOpenStateError{}, err)
assert.Nil(t, res)
assert.Equal(t, true, called)
}