-
Notifications
You must be signed in to change notification settings - Fork 8
/
workflow_internal_test.go
181 lines (167 loc) · 4.28 KB
/
workflow_internal_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package workflow
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
"k8s.io/utils/clock"
clock_testing "k8s.io/utils/clock/testing"
internal_logger "github.com/luno/workflow/internal/logger"
)
func Test_runOnce(t *testing.T) {
ctx := context.Background()
t.Run("Returns non-nil error (context.Canceled) when parent ctx is cancelled", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
cancel()
err := runOnce(
ctx,
"workflow-1",
"role-1",
"process-1",
nil,
nil,
nil,
nil,
clock_testing.NewFakeClock(time.Now()),
time.Minute,
)
require.True(t, errors.Is(err, context.Canceled))
})
t.Run("Returns awaitRole's context cancellation", func(t *testing.T) {
err := runOnce(
ctx,
"workflow-1",
"role-1",
"process-1",
func(processName string, s State) {},
func(ctx context.Context, role string) (context.Context, context.CancelFunc, error) {
return nil, nil, context.Canceled
},
func(ctx context.Context) error {
return nil
},
nil,
clock_testing.NewFakeClock(time.Now()),
time.Minute,
)
require.True(t, errors.Is(err, context.Canceled))
})
t.Run("Retry awaitRole error that is not context.Canceled", func(t *testing.T) {
testErr := errors.New("test error")
buf := bytes.NewBuffer([]byte{})
err := runOnce(
ctx,
"workflow-1",
"role-1",
"process-1",
func(processName string, s State) {},
func(ctx context.Context, role string) (context.Context, context.CancelFunc, error) {
return nil, nil, testErr
},
func(ctx context.Context) error {
return nil
},
&logger{
debugMode: false,
inner: internal_logger.New(buf),
},
clock.RealClock{},
time.Minute,
)
require.Nil(t, err)
require.Contains(t, buf.String(), `"msg":"run error [role=role-1], [process=process-1]: test error"`)
})
t.Run("Cancelled parent context during process execution retries and exits with context.Canceled ", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
err := runOnce(
ctx,
"workflow-1",
"role-1",
"process-1",
func(processName string, s State) {},
func(ctx context.Context, role string) (context.Context, context.CancelFunc, error) {
ctx, cancel := context.WithCancel(ctx)
return ctx, cancel, nil
},
func(ctx context.Context) error {
// Cancel parent context and return context error
cancel()
return ctx.Err()
},
nil,
clock_testing.NewFakeClock(time.Now()),
time.Minute,
)
// If the err is nil then it will be retried
require.Nil(t, err)
// Context has been cancelled previously in the last runOnce so we expect
// this to immediately return context.Canceled and need any parameters.
err = runOnce(
ctx,
"",
"",
"",
nil,
nil,
nil,
nil,
nil,
time.Minute,
)
require.True(t, errors.Is(err, context.Canceled))
})
t.Run("Retry process error that is not context.Canceled", func(t *testing.T) {
testErr := errors.New("test error")
buf := bytes.NewBuffer([]byte{})
err := runOnce(
ctx,
"workflow-1",
"role-1",
"process-1",
func(processName string, s State) {},
func(ctx context.Context, role string) (context.Context, context.CancelFunc, error) {
ctx, cancel := context.WithCancel(ctx)
return ctx, cancel, nil
},
func(ctx context.Context) error {
return testErr
},
&logger{
debugMode: false,
inner: internal_logger.New(buf),
},
clock.RealClock{},
time.Millisecond,
)
require.Nil(t, err)
require.Contains(t, buf.String(), `"msg":"run error [role=role-1], [process=process-1]: test error`)
})
t.Run("Updates process state", func(t *testing.T) {
var stateChanges []string
err := runOnce(
ctx,
"workflow-1",
"role-1",
"process-1",
func(processName string, s State) {
stateChanges = append(stateChanges, s.String())
},
func(ctx context.Context, role string) (context.Context, context.CancelFunc, error) {
ctx, cancel := context.WithCancel(ctx)
return ctx, cancel, nil
},
func(ctx context.Context) error {
return nil
},
nil,
clock_testing.NewFakeClock(time.Now()),
time.Minute,
)
require.Nil(t, err)
expected := []string{StateIdle.String(), StateRunning.String()}
require.Equal(t, expected, stateChanges)
})
}