-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathworkflow_internal_test.go
More file actions
296 lines (256 loc) · 7.72 KB
/
workflow_internal_test.go
File metadata and controls
296 lines (256 loc) · 7.72 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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 := t.Context()
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.NoError(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.NoError(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.NoError(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.NoError(t, err)
expected := []string{StateIdle.String(), StateRunning.String()}
require.Equal(t, expected, stateChanges)
})
}
func TestWorkflow_RunStopRace(t *testing.T) {
// This test verifies that calling Run() and Stop() concurrently doesn't cause a data race.
// It specifically tests that w.cancel is properly protected by a mutex during concurrent
// access from Run() (write) and Stop() (read).
// Run with: go test -race
ctx := context.Background()
// Run multiple iterations to increase the chance of detecting the race condition
for range 100 {
// Build a minimal workflow using the Builder
b := NewBuilder[string, testStatus]("race-test")
b.AddStep(statusStart, func(ctx context.Context, r *Run[string, testStatus]) (testStatus, error) {
return statusEnd, nil
}, statusEnd)
wf := b.Build(
&noopEventStreamer{},
&noopRecordStore{},
&noopScheduler{},
WithoutOutbox(),
)
// Start Run in a goroutine - this will write to w.cancel inside once.Do
done := make(chan struct{})
go func() {
wf.Run(ctx)
close(done)
}()
// Immediately call Stop - this reads w.cancel
// Without the mutex protection, this would race with the write in Run()
wf.Stop()
// Wait for Run to complete
<-done
// Call Stop again to ensure cleanup of any goroutines that were started.
// This handles the case where the first Stop() call happened before Run()
// had set the cancel function (so it returned early without stopping anything).
wf.Stop()
}
}
// noopScheduler implements RoleScheduler for testing
type noopScheduler struct{}
func (n *noopScheduler) Await(ctx context.Context, role string) (context.Context, context.CancelFunc, error) {
return ctx, func() {}, nil
}
// noopEventStreamer implements EventStreamer for testing
type noopEventStreamer struct{}
func (n *noopEventStreamer) NewSender(ctx context.Context, topic string) (EventSender, error) {
return &noopEventSender{}, nil
}
func (n *noopEventStreamer) NewReceiver(ctx context.Context, topic string, consumerName string, opts ...ReceiverOption) (EventReceiver, error) {
// Return a receiver that blocks until context is cancelled
return &noopEventReceiver{ctx: ctx}, nil
}
// noopEventSender implements EventSender for testing
type noopEventSender struct{}
func (n *noopEventSender) Send(ctx context.Context, foreignID string, statusType int, headers map[Header]string) error {
return nil
}
func (n *noopEventSender) Close() error {
return nil
}
// noopEventReceiver implements EventReceiver for testing
type noopEventReceiver struct {
ctx context.Context
}
func (n *noopEventReceiver) Recv(ctx context.Context) (*Event, Ack, error) {
<-ctx.Done()
return nil, nil, ctx.Err()
}
func (n *noopEventReceiver) Close() error {
return nil
}
// noopRecordStore implements RecordStore for testing
type noopRecordStore struct{}
func (n *noopRecordStore) Store(ctx context.Context, record *Record) error {
return nil
}
func (n *noopRecordStore) Latest(ctx context.Context, workflowName, foreignID string) (*Record, error) {
return nil, errors.New("not found")
}
func (n *noopRecordStore) Lookup(ctx context.Context, id string) (*Record, error) {
return nil, errors.New("not found")
}
func (n *noopRecordStore) List(ctx context.Context, workflowName string, offsetID int64, limit int, order OrderType, filters ...RecordFilter) ([]Record, error) {
return nil, nil
}
func (n *noopRecordStore) ListOutboxEvents(ctx context.Context, workflowName string, limit int64) ([]OutboxEvent, error) {
return nil, nil
}
func (n *noopRecordStore) DeleteOutboxEvent(ctx context.Context, id string) error {
return nil
}