-
Notifications
You must be signed in to change notification settings - Fork 8
/
workflow_test.go
616 lines (502 loc) · 15.3 KB
/
workflow_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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package workflow_test
import (
"context"
"encoding/json"
"fmt"
"io"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
clock_testing "k8s.io/utils/clock/testing"
"github.com/luno/workflow"
"github.com/luno/workflow/adapters/memrecordstore"
"github.com/luno/workflow/adapters/memrolescheduler"
"github.com/luno/workflow/adapters/memstreamer"
"github.com/luno/workflow/adapters/memtimeoutstore"
)
// Ensures that workflow.Workflow always implements workflow.API
var _ workflow.API[MyType, status] = (*workflow.Workflow[MyType, status])(nil)
type MyType struct {
UserID int64
Name string
Email string
Cellphone string
OTP int
OTPVerified bool
}
func (m MyType) ForeignID() string {
return strconv.FormatInt(m.UserID, 10)
}
type status int
const (
StatusUnknown status = 0
StatusInitiated status = 1
StatusProfileCreated status = 2
StatusEmailConfirmationSent status = 3
StatusEmailVerified status = 4
StatusCellphoneNumberSubmitted status = 5
StatusOTPSent status = 6
StatusOTPVerified status = 7
StatusCompleted status = 8
StatusStart status = 9
StatusMiddle status = 10
StatusEnd status = 11
)
func (s status) String() string {
switch s {
case StatusInitiated:
return "Initiated"
case StatusProfileCreated:
return "Name Created"
case StatusEmailConfirmationSent:
return "Email Confirmation Sent"
case StatusEmailVerified:
return "Email Verified"
case StatusCellphoneNumberSubmitted:
return "Cellphone Number Submitted"
case StatusOTPSent:
return "OTP Sent"
case StatusOTPVerified:
return "OTP Verified"
case StatusCompleted:
return "Completed"
case StatusStart:
return "Start"
case StatusMiddle:
return "Middle"
case StatusEnd:
return "End"
default:
return "Unknown"
}
}
type ExternalEmailVerified struct {
IsVerified bool
}
type ExternalCellPhoneSubmitted struct {
DialingCode string
Number string
}
type ExternalOTP struct {
OTPCode int
}
func TestWorkflowAcceptanceTest(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
b := workflow.NewBuilder[MyType, status]("user sign up")
b.AddStep(StatusInitiated, createProfile, StatusProfileCreated)
b.AddStep(StatusProfileCreated, sendEmailConfirmation, StatusEmailConfirmationSent)
b.AddCallback(StatusEmailConfirmationSent, emailVerifiedCallback, StatusEmailVerified)
b.AddCallback(StatusEmailVerified, cellphoneNumberCallback, StatusCellphoneNumberSubmitted)
b.AddStep(StatusCellphoneNumberSubmitted, sendOTP, StatusOTPSent).WithOptions(workflow.ParallelCount(2))
b.AddCallback(StatusOTPSent, otpCallback, StatusOTPVerified)
b.AddTimeout(StatusOTPVerified, workflow.DurationTimerFunc[MyType, status](time.Hour), waitForAccountCoolDown, StatusCompleted)
b.OnComplete(func(ctx context.Context, record *workflow.TypedRecord[MyType, status]) error {
fmt.Println(fmt.Sprintf("Welcome %v 👋", record.Object.Name))
return nil
})
recordStore := memrecordstore.New()
clock := clock_testing.NewFakeClock(time.Now())
wf := b.Build(
memstreamer.New(),
recordStore,
memrolescheduler.New(),
workflow.WithTimeoutStore(memtimeoutstore.New()),
workflow.WithClock(clock),
workflow.WithDebugMode(),
)
wf.Run(ctx)
t.Cleanup(wf.Stop)
fid := strconv.FormatInt(expectedUserID, 10)
mt := MyType{
UserID: expectedUserID,
}
runID, err := wf.Trigger(ctx, fid, StatusInitiated, workflow.WithInitialValue[MyType, status](&mt))
require.Nil(t, err)
// Once in the correct status, trigger third party callbacks
workflow.TriggerCallbackOn(t, wf, fid, runID, StatusEmailConfirmationSent, ExternalEmailVerified{
IsVerified: true,
})
workflow.TriggerCallbackOn(t, wf, fid, runID, StatusEmailVerified, ExternalCellPhoneSubmitted{
DialingCode: "+44",
Number: "7467623292",
})
workflow.TriggerCallbackOn(t, wf, fid, runID, StatusOTPSent, ExternalOTP{
OTPCode: expectedOTP,
})
workflow.AwaitTimeoutInsert(t, wf, fid, runID, StatusOTPVerified)
// Advance time forward by one hour to trigger the timeout
clock.Step(time.Hour)
_, err = wf.Await(ctx, fid, runID, StatusCompleted)
require.Nil(t, err)
r, err := recordStore.Latest(ctx, "user sign up", fid)
require.Nil(t, err)
require.Equal(t, int(expectedFinalStatus), r.Status)
var actual MyType
err = workflow.Unmarshal(r.Object, &actual)
require.Nil(t, err)
require.Equal(t, expectedUserID, actual.UserID)
require.Equal(t, strconv.FormatInt(expectedUserID, 10), actual.ForeignID())
require.Equal(t, expectedProfile, actual.Name)
require.Equal(t, expectedEmail, actual.Email)
require.Equal(t, expectedCellphone, actual.Cellphone)
require.Equal(t, expectedOTP, actual.OTP)
require.Equal(t, expectedOTPVerified, actual.OTPVerified)
}
func BenchmarkWorkflow(b *testing.B) {
b.Run("1", func(b *testing.B) {
benchmarkWorkflow(b, 1)
})
b.Run("5", func(b *testing.B) {
benchmarkWorkflow(b, 5)
})
b.Run("10", func(b *testing.B) {
benchmarkWorkflow(b, 10)
})
}
func benchmarkWorkflow(b *testing.B, numberOfSteps int) {
ctx, cancel := context.WithCancel(context.Background())
b.Cleanup(func() {
cancel()
})
bldr := workflow.NewBuilder[MyType, status]("benchmark")
for i := range numberOfSteps {
bldr.AddStep(status(i), func(ctx context.Context, r *workflow.Run[MyType, status]) (status, error) {
return status(i + 1), nil
}, status(i+1))
}
recordStore := memrecordstore.New()
clock := clock_testing.NewFakeClock(time.Now())
wf := bldr.Build(
memstreamer.New(),
recordStore,
memrolescheduler.New(),
workflow.WithClock(clock),
workflow.WithOutboxPollingFrequency(1*time.Nanosecond),
workflow.WithDefaultOptions(
workflow.PollingFrequency(1*time.Nanosecond),
),
)
wf.Run(ctx)
b.Cleanup(wf.Stop)
fid := strconv.FormatInt(expectedUserID, 10)
mt := MyType{
UserID: expectedUserID,
}
for range b.N {
_, err := wf.Trigger(ctx, fid, 0, workflow.WithInitialValue[MyType, status](&mt))
if err != nil {
b.Fatal(err)
}
workflow.Require(b, wf, fid, status(numberOfSteps), MyType{
UserID: expectedUserID,
})
}
}
func TestTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
b := workflow.NewBuilder[MyType, status]("user sign up")
b.AddStep(StatusInitiated, func(ctx context.Context, t *workflow.Run[MyType, status]) (status, error) {
return StatusProfileCreated, nil
}, StatusProfileCreated)
b.AddTimeout(StatusProfileCreated, workflow.DurationTimerFunc[MyType, status](time.Hour), func(ctx context.Context, t *workflow.Run[MyType, status], now time.Time) (status, error) {
return StatusCompleted, nil
}, StatusCompleted).WithOptions(
workflow.PollingFrequency(100 * time.Millisecond),
)
recordStore := memrecordstore.New()
clock := clock_testing.NewFakeClock(time.Now())
wf := b.Build(
memstreamer.New(),
recordStore,
memrolescheduler.New(),
workflow.WithTimeoutStore(memtimeoutstore.New()),
workflow.WithClock(clock),
)
wf.Run(ctx)
t.Cleanup(wf.Stop)
start := time.Now()
runID, err := wf.Trigger(ctx, "example", StatusInitiated)
require.Nil(t, err)
workflow.AwaitTimeoutInsert(t, wf, "example", runID, StatusProfileCreated)
// Advance time forward by one hour to trigger the timeout
clock.Step(time.Hour)
_, err = wf.Await(ctx, "example", runID, StatusCompleted)
require.Nil(t, err)
end := time.Now()
require.True(t, end.Sub(start) < 1*time.Second)
}
var (
expectedUserID int64 = 984892374983743
expectedFinalStatus = StatusCompleted
expectedProfile = "Andrew Wormald"
expectedEmail = "[email protected]"
expectedCellphone = "+44 7467623292"
expectedOTP = 345345
expectedOTPVerified = true
)
func createProfile(ctx context.Context, mt *workflow.Run[MyType, status]) (status, error) {
mt.Object.Name = "Andrew Wormald"
return StatusProfileCreated, nil
}
func sendEmailConfirmation(ctx context.Context, mt *workflow.Run[MyType, status]) (status, error) {
return StatusEmailConfirmationSent, nil
}
func emailVerifiedCallback(ctx context.Context, mt *workflow.Run[MyType, status], r io.Reader) (status, error) {
b, err := io.ReadAll(r)
if err != nil {
return 0, err
}
var ev ExternalEmailVerified
err = json.Unmarshal(b, &ev)
if err != nil {
return 0, err
}
if !ev.IsVerified {
// Skip callback
return 0, nil
}
mt.Object.Email = "[email protected]"
return StatusEmailVerified, nil
}
func cellphoneNumberCallback(ctx context.Context, mt *workflow.Run[MyType, status], r io.Reader) (status, error) {
b, err := io.ReadAll(r)
if err != nil {
return 0, err
}
var ev ExternalCellPhoneSubmitted
err = json.Unmarshal(b, &ev)
if err != nil {
return 0, err
}
if ev.DialingCode != "" && ev.Number != "" {
mt.Object.Cellphone = fmt.Sprintf("%v %v", ev.DialingCode, ev.Number)
}
return StatusCellphoneNumberSubmitted, nil
}
func sendOTP(ctx context.Context, mt *workflow.Run[MyType, status]) (status, error) {
mt.Object.OTP = expectedOTP
return StatusOTPSent, nil
}
func otpCallback(ctx context.Context, mt *workflow.Run[MyType, status], r io.Reader) (status, error) {
b, err := io.ReadAll(r)
if err != nil {
return 0, err
}
var otp ExternalOTP
err = json.Unmarshal(b, &otp)
if err != nil {
return 0, err
}
if otp.OTPCode == expectedOTP {
mt.Object.OTPVerified = true
}
return StatusOTPVerified, nil
}
func waitForAccountCoolDown(ctx context.Context, mt *workflow.Run[MyType, status], now time.Time) (status, error) {
return StatusCompleted, nil
}
func TestWorkflow_ErrWorkflowNotRunning(t *testing.T) {
b := workflow.NewBuilder[MyType, status]("sync users")
b.AddStep(StatusStart, func(ctx context.Context, t *workflow.Run[MyType, status]) (status, error) {
return StatusMiddle, nil
}, StatusMiddle)
b.AddStep(StatusMiddle, func(ctx context.Context, t *workflow.Run[MyType, status]) (status, error) {
return StatusEnd, nil
}, StatusEnd)
recordStore := memrecordstore.New()
wf := b.Build(
memstreamer.New(),
recordStore,
memrolescheduler.New(),
)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
_, err := wf.Trigger(ctx, "andrew", StatusStart)
require.Equal(t, "trigger failed: workflow is not running", err.Error())
err = wf.Schedule("andrew", StatusStart, "@monthly")
require.Equal(t, "schedule failed: workflow is not running", err.Error())
}
func TestWorkflow_TestingRequire(t *testing.T) {
b := workflow.NewBuilder[MyType, status]("sync users")
b.AddStep(StatusStart, func(ctx context.Context, t *workflow.Run[MyType, status]) (status, error) {
t.Object.Email = "[email protected]"
return StatusMiddle, nil
}, StatusMiddle)
b.AddStep(StatusMiddle, func(ctx context.Context, t *workflow.Run[MyType, status]) (status, error) {
t.Object.Cellphone = "+44 349 8594"
return StatusEnd, nil
}, StatusEnd)
recordStore := memrecordstore.New()
wf := b.Build(
memstreamer.New(),
recordStore,
memrolescheduler.New(),
)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
wf.Run(ctx)
t.Cleanup(wf.Stop)
foreignID := "andrew"
_, err := wf.Trigger(ctx, foreignID, StatusStart)
require.Nil(t, err)
expected := MyType{
Email: "[email protected]",
}
workflow.Require(t, wf, foreignID, StatusMiddle, expected)
expected = MyType{
Email: "[email protected]",
Cellphone: "+44 349 8594",
}
workflow.Require(t, wf, foreignID, StatusEnd, expected)
}
func TestTimeTimerFunc(t *testing.T) {
type YinYang struct {
Yin bool
Yang bool
}
b := workflow.NewBuilder[YinYang, status]("timer_func")
launchDate := time.Date(1992, time.April, 9, 0, 0, 0, 0, time.UTC)
b.AddTimeout(StatusStart,
workflow.TimeTimerFunc[YinYang, status](launchDate),
func(ctx context.Context, t *workflow.Run[YinYang, status], now time.Time) (status, error) {
t.Object.Yin = true
t.Object.Yang = true
return StatusEnd, nil
},
StatusEnd,
)
now := time.Date(1991, time.December, 25, 8, 30, 0, 0, time.UTC)
clock := clock_testing.NewFakeClock(now)
recordStore := memrecordstore.New()
wf := b.Build(
memstreamer.New(),
recordStore,
memrolescheduler.New(),
workflow.WithTimeoutStore(memtimeoutstore.New()),
)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
wf.Run(ctx)
t.Cleanup(wf.Stop)
runID, err := wf.Trigger(ctx, "Andrew Wormald", StatusStart)
require.Nil(t, err)
workflow.AwaitTimeoutInsert(t, wf, "Andrew Wormald", runID, StatusStart)
clock.SetTime(launchDate)
expected := YinYang{
Yin: true,
Yang: true,
}
workflow.Require(t, wf, "Andrew Wormald", StatusEnd, expected)
}
func TestConnector(t *testing.T) {
type typeX struct {
Val string
}
events := []workflow.ConnectorEvent{
{
ID: "1",
ForeignID: "SDFJKH-SDKFJHBSD-SDKFJBS",
Type: "2",
},
{
ID: "2",
ForeignID: "XCVXCM-EIXCASDBJ-SDFBJKZ",
Type: "2",
},
}
connector := memstreamer.NewConnector(events)
buidler := workflow.NewBuilder[typeX, status]("workflow")
buidler.AddConnector(
"my-test-connector",
connector,
func(ctx context.Context, w *workflow.Workflow[typeX, status], e *workflow.ConnectorEvent) error {
_, err := w.Trigger(ctx, e.ForeignID, StatusStart, workflow.WithInitialValue[typeX, status](&typeX{
Val: "trigger set value",
}))
if err != nil {
return err
}
return nil
},
)
buidler.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[typeX, status]) (status, error) {
r.Object.Val = "workflow step set value"
return StatusEnd, nil
}, StatusEnd)
w := buidler.Build(
memstreamer.New(),
memrecordstore.New(),
memrolescheduler.New(),
)
ctx := context.Background()
w.Run(ctx)
t.Cleanup(w.Stop)
for _, event := range events {
workflow.Require(t, w, event.ForeignID, StatusStart, typeX{
Val: "trigger set value",
})
workflow.Require(t, w, event.ForeignID, StatusEnd, typeX{
Val: "workflow step set value",
})
}
}
func TestStepConsumerLag(t *testing.T) {
fixedNowTime := time.Date(2023, time.April, 9, 8, 30, 0, 0, time.UTC)
clock := clock_testing.NewFakeClock(fixedNowTime)
lagAmount := time.Hour
type TimeWatcher struct {
StartTime time.Time
ConsumeTime time.Time
}
b := workflow.NewBuilder[TimeWatcher, status]("step consumer lag")
b.AddStep(
StatusStart,
func(ctx context.Context, t *workflow.Run[TimeWatcher, status]) (status, error) {
t.Object.ConsumeTime = clock.Now()
return StatusEnd, nil
},
StatusEnd,
).WithOptions(
workflow.ConsumeLag(lagAmount),
)
recordStore := memrecordstore.New()
wf := b.Build(
memstreamer.New(memstreamer.WithClock(clock)),
recordStore,
memrolescheduler.New(),
workflow.WithClock(clock),
workflow.WithDebugMode(),
)
ctx := context.Background()
wf.Run(ctx)
t.Cleanup(wf.Stop)
foreignID := "1"
_, err := wf.Trigger(ctx, foreignID, StatusStart, workflow.WithInitialValue[TimeWatcher, status](&TimeWatcher{
StartTime: clock.Now(),
}))
require.Nil(t, err)
time.Sleep(time.Second)
latest, err := recordStore.Latest(ctx, wf.Name, foreignID)
require.Nil(t, err)
// Ensure that the record has not been consumer or updated
require.Equal(t, int(StatusStart), latest.Status)
clock.Step(lagAmount)
workflow.Require(t, wf, foreignID, StatusEnd, TimeWatcher{
StartTime: fixedNowTime,
ConsumeTime: fixedNowTime.Add(lagAmount),
})
}