/
fsm_test.go
1003 lines (905 loc) · 29.9 KB
/
fsm_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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package swf
import (
"fmt"
"log"
"math/rand"
"reflect"
"strconv"
"testing"
"time"
"code.google.com/p/goprotobuf/proto"
)
//Todo add tests of error handling mechanism
//assert that the decisions have the mark and the signal external...hmm need workflow id for signal external.
var testActivityInfo = ActivityInfo{ActivityID: "activityId", ActivityType: ActivityType{Name: "activity", Version: "activityVersion"}}
var typedFuncs = Typed(new(TestData))
func TestFSM(t *testing.T) {
fsm := FSM{
Name: "test-fsm",
DataType: TestData{},
Serializer: JSONStateSerializer{},
}
fsm.AddInitialState(&FSMState{
Name: "start",
Decider: func(f *FSMContext, lastEvent HistoryEvent, data interface{}) Outcome {
testData := data.(*TestData)
testData.States = append(testData.States, "start")
serialized := f.Serialize(testData)
decision := Decision{
DecisionType: DecisionTypeScheduleActivityTask,
ScheduleActivityTaskDecisionAttributes: &ScheduleActivityTaskDecisionAttributes{
ActivityID: testActivityInfo.ActivityID,
ActivityType: testActivityInfo.ActivityType,
TaskList: &TaskList{Name: "taskList"},
Input: serialized,
},
}
return f.Goto("working", testData, []Decision{decision})
},
})
fsm.AddState(&FSMState{
Name: "working",
Decider: typedFuncs.Decider(func(f *FSMContext, lastEvent HistoryEvent, testData *TestData) Outcome {
testData.States = append(testData.States, "working")
serialized := f.Serialize(testData)
var decisions = f.EmptyDecisions()
if lastEvent.EventType == EventTypeActivityTaskCompleted {
decision := Decision{
DecisionType: DecisionTypeCompleteWorkflowExecution,
CompleteWorkflowExecutionDecisionAttributes: &CompleteWorkflowExecutionDecisionAttributes{
Result: serialized,
},
}
decisions = append(decisions, decision)
} else if lastEvent.EventType == EventTypeActivityTaskFailed {
decision := Decision{
DecisionType: DecisionTypeScheduleActivityTask,
ScheduleActivityTaskDecisionAttributes: &ScheduleActivityTaskDecisionAttributes{
ActivityID: testActivityInfo.ActivityID,
ActivityType: testActivityInfo.ActivityType,
TaskList: &TaskList{Name: "taskList"},
Input: serialized,
},
}
decisions = append(decisions, decision)
}
return f.Stay(testData, decisions)
}),
})
events := []HistoryEvent{
HistoryEvent{EventType: "DecisionTaskStarted", EventID: 3},
HistoryEvent{EventType: "DecisionTaskScheduled", EventID: 2},
HistoryEvent{
EventID: 1,
EventType: "WorkflowExecutionStarted",
WorkflowExecutionStartedEventAttributes: &WorkflowExecutionStartedEventAttributes{
Input: StartFSMWorkflowInput(fsm.Serializer, new(TestData)),
},
},
}
first := &PollForDecisionTaskResponse{
Events: events,
PreviousStartedEventID: 0,
}
decisions, _ := fsm.Tick(first)
if !Find(decisions, stateMarkerPredicate) {
t.Fatal("No Record State Marker")
}
if !Find(decisions, scheduleActivityPredicate) {
t.Fatal("No ScheduleActivityTask")
}
secondEvents := DecisionsToEvents(decisions)
secondEvents = append(secondEvents, events...)
if state, _ := fsm.findSerializedState(secondEvents); state.ReplicationData.StateName != "working" {
t.Fatal("current state is not 'working'", secondEvents)
}
second := &PollForDecisionTaskResponse{
Events: secondEvents,
PreviousStartedEventID: 3,
}
secondDecisions, _ := fsm.Tick(second)
if !Find(secondDecisions, stateMarkerPredicate) {
t.Fatal("No Record State Marker")
}
if !Find(secondDecisions, completeWorkflowPredicate) {
t.Fatal("No CompleteWorkflow")
}
}
func Find(decisions []Decision, predicate func(Decision) bool) bool {
return FindDecision(decisions, predicate) != nil
}
func FindDecision(decisions []Decision, predicate func(Decision) bool) *Decision {
for _, d := range decisions {
if predicate(d) {
return &d
}
}
return nil
}
func stateMarkerPredicate(d Decision) bool {
return d.DecisionType == "RecordMarker" && d.RecordMarkerDecisionAttributes.MarkerName == StateMarker
}
func scheduleActivityPredicate(d Decision) bool {
return d.DecisionType == "ScheduleActivityTask"
}
func completeWorkflowPredicate(d Decision) bool {
return d.DecisionType == "CompleteWorkflowExecution"
}
func startTimerPredicate(d Decision) bool {
return d.DecisionType == "StartTimer"
}
func DecisionsToEvents(decisions []Decision) []HistoryEvent {
var events []HistoryEvent
for _, d := range decisions {
if scheduleActivityPredicate(d) {
event := HistoryEvent{
EventType: "ActivityTaskCompleted",
EventID: 7,
ActivityTaskCompletedEventAttributes: &ActivityTaskCompletedEventAttributes{
ScheduledEventID: 6,
},
}
events = append(events, event)
event = HistoryEvent{
EventType: "ActivityTaskScheduled",
EventID: 6,
ActivityTaskScheduledEventAttributes: &ActivityTaskScheduledEventAttributes{
ActivityID: testActivityInfo.ActivityID,
ActivityType: testActivityInfo.ActivityType,
},
}
events = append(events, event)
}
if stateMarkerPredicate(d) {
event := HistoryEvent{
EventType: "MarkerRecorded",
EventID: 5,
MarkerRecordedEventAttributes: &MarkerRecordedEventAttributes{
MarkerName: StateMarker,
Details: d.RecordMarkerDecisionAttributes.Details,
},
}
events = append(events, event)
}
}
return events
}
type TestData struct {
States []string
}
func TestMarshalledDecider(t *testing.T) {
typedDecider := func(f *FSMContext, h HistoryEvent, d *TestData) Outcome {
if d.States[0] != "marshalled" {
t.Fail()
}
return f.Goto("ok", d, nil)
}
wrapped := typedFuncs.Decider(typedDecider)
outcome := wrapped(&FSMContext{}, HistoryEvent{}, &TestData{States: []string{"marshalled"}})
if outcome.(TransitionOutcome).state != "ok" {
t.Fatal("NextState was not 'ok'")
}
}
func TestPanicRecovery(t *testing.T) {
s := &FSMState{
Name: "panic",
Decider: func(f *FSMContext, e HistoryEvent, data interface{}) Outcome {
panic("can you handle it?")
},
}
f := &FSM{}
f.AddInitialState(s)
_, err := f.panicSafeDecide(s, new(FSMContext), HistoryEvent{}, nil)
if err == nil {
t.Fatal("fatallz")
} else {
log.Println(err)
}
}
func TestErrorHandling(t *testing.T) {
fsm := FSM{
Name: "test-fsm",
DataType: TestData{},
Serializer: JSONStateSerializer{},
allowPanics: true,
}
fsm.AddInitialState(&FSMState{
Name: "ok",
Decider: func(f *FSMContext, h HistoryEvent, d interface{}) Outcome {
if h.EventType == EventTypeWorkflowExecutionStarted {
return f.Stay(d, nil)
}
if h.EventType == EventTypeWorkflowExecutionSignaled && d.(*TestData).States[0] == "recovered" {
log.Println("recovered")
return f.Stay(d, nil)
}
t.Fatalf("ok state did not get recovered %s", h)
return nil
},
})
fsm.AddErrorState(&FSMState{
Name: "error",
Decider: func(f *FSMContext, h HistoryEvent, d interface{}) Outcome {
if h.EventType == EventTypeWorkflowExecutionSignaled && h.WorkflowExecutionSignaledEventAttributes.SignalName == ErrorSignal {
log.Println("in error recovery")
return f.Goto("ok", &TestData{States: []string{"recovered"}}, nil)
}
t.Fatalf("error handler got unexpected event")
return nil
},
})
events := []HistoryEvent{
HistoryEvent{
EventID: 3,
EventType: EventTypeWorkflowExecutionSignaled,
WorkflowExecutionSignaledEventAttributes: &WorkflowExecutionSignaledEventAttributes{
SignalName: "NOT AN ERROR",
Input: "Hi",
},
},
HistoryEvent{
EventID: 2,
EventType: EventTypeWorkflowExecutionSignaled,
WorkflowExecutionSignaledEventAttributes: &WorkflowExecutionSignaledEventAttributes{
SignalName: ErrorSignal,
Input: "{\"workflowEpoch\":2}",
},
},
HistoryEvent{
EventID: 1,
EventType: EventTypeWorkflowExecutionStarted,
WorkflowExecutionStartedEventAttributes: &WorkflowExecutionStartedEventAttributes{
Input: StartFSMWorkflowInput(fsm.Serializer, ""),
},
},
}
resp := &PollForDecisionTaskResponse{
Events: events,
StartedEventID: 1,
PreviousStartedEventID: 0,
}
decisions, _ := fsm.Tick(resp)
if len(decisions) != 1 && decisions[0].DecisionType != DecisionTypeRecordMarker {
t.Fatalf("no state marker!")
}
//Try with TypedDecider
id := fsm.initialState.Decider
fsm.initialState.Decider = typedFuncs.Decider(func(f *FSMContext, h HistoryEvent, d *TestData) Outcome { return id(f, h, d) })
ie := fsm.errorState.Decider
fsm.errorState.Decider = typedFuncs.Decider(func(f *FSMContext, h HistoryEvent, d *TestData) Outcome { return ie(f, h, d) })
decisions2, _ := fsm.Tick(resp)
if len(decisions2) != 1 && decisions2[0].DecisionType != DecisionTypeRecordMarker {
t.Fatalf("no state marker for typed decider!")
}
}
func TestProtobufSerialization(t *testing.T) {
ser := &ProtobufStateSerializer{}
key := "FOO"
val := "BAR"
init := &ConfigVar{Key: &key, Str: &val}
serialized, err := ser.Serialize(init)
if err != nil {
t.Fatal(err)
}
log.Println(serialized)
deserialized := new(ConfigVar)
err = ser.Deserialize(serialized, deserialized)
if err != nil {
t.Fatal(err)
}
if init.GetKey() != deserialized.GetKey() {
t.Fatal(init, deserialized)
}
if init.GetStr() != deserialized.GetStr() {
t.Fatal(init, deserialized)
}
}
//This is c&p from som generated code
type ConfigVar struct {
Key *string `protobuf:"bytes,1,req,name=key" json:"key,omitempty"`
Str *string `protobuf:"bytes,2,opt,name=str" json:"str,omitempty"`
XXXunrecognized []byte `json:"-"`
}
func (m *ConfigVar) Reset() { *m = ConfigVar{} }
func (m *ConfigVar) String() string { return proto.CompactTextString(m) }
func (*ConfigVar) ProtoMessage() {}
func (m *ConfigVar) GetKey() string {
if m != nil && m.Key != nil {
return *m.Key
}
return ""
}
func (m *ConfigVar) GetStr() string {
if m != nil && m.Str != nil {
return *m.Str
}
return ""
}
func ExampleFSM() {
// create with swf.NewClient
var client *Client
// data type that will be managed by the FSM
type StateData struct {
Message string `json:"message,omitempty"`
Count int `json:"count,omitempty"`
}
//event type that will be signalled to the FSM with signal name "hello"
type Hello struct {
Message string `json:"message,omitempty"`
}
//the FSM we will create will oscillate between 2 states,
//waitForSignal -> will wait till the workflow is started or signalled, and update the StateData based on the Hello message received, set a timer, and transition to waitForTimer
//waitForTimer -> will wait till the timer set by waitForSignal fires, and will signal the workflow with a Hello message, and transition to waitFotSignal
waitForSignal := func(f *FSMContext, h HistoryEvent, d *StateData) Outcome {
decisions := f.EmptyDecisions()
switch h.EventType {
case EventTypeWorkflowExecutionStarted, EventTypeWorkflowExecutionSignaled:
if h.EventType == EventTypeWorkflowExecutionSignaled && h.WorkflowExecutionSignaledEventAttributes.SignalName == "hello" {
hello := &Hello{}
f.EventData(h, &Hello{})
d.Count++
d.Message = hello.Message
}
timeoutSeconds := "5" //swf uses stringy numbers in many places
timerDecision := Decision{
DecisionType: DecisionTypeStartTimer,
StartTimerDecisionAttributes: &StartTimerDecisionAttributes{
StartToFireTimeout: timeoutSeconds,
TimerID: "timeToSignal",
},
}
decisions = append(decisions, timerDecision)
return f.Goto("waitForTimer", d, decisions)
}
//if the event was unexpected just stay here
return f.Stay(d, decisions)
}
waitForTimer := func(f *FSMContext, h HistoryEvent, d *StateData) Outcome {
decisions := f.EmptyDecisions()
switch h.EventType {
case EventTypeTimerFired:
//every time the timer fires, signal the workflow with a Hello
message := strconv.FormatInt(time.Now().Unix(), 10)
signalInput := &Hello{message}
signalDecision := Decision{
DecisionType: DecisionTypeSignalExternalWorkflowExecution,
SignalExternalWorkflowExecutionDecisionAttributes: &SignalExternalWorkflowExecutionDecisionAttributes{
SignalName: "hello",
Input: f.Serialize(signalInput),
RunID: f.RunID,
WorkflowID: f.WorkflowID,
},
}
decisions = append(decisions, signalDecision)
return f.Goto("waitForSignal", d, decisions)
}
//if the event was unexpected just stay here
return f.Stay(d, decisions)
}
//create the FSMState by passing the decider function through TypedDecider(),
//which lets you use d *StateData rather than d interface{} in your decider.
waitForSignalState := &FSMState{Name: "waitForSignal", Decider: typedFuncs.Decider(waitForSignal)}
waitForTimerState := &FSMState{Name: "waitForTimer", Decider: typedFuncs.Decider(waitForTimer)}
//wire it up in an fsm
fsm := &FSM{
Name: "example-fsm",
Client: client,
DataType: StateData{},
Domain: "exaple-swf-domain",
TaskList: "example-decision-task-list-to-poll",
Serializer: &JSONStateSerializer{},
}
//add states to FSM
fsm.AddInitialState(waitForSignalState)
fsm.AddState(waitForTimerState)
//start it up!
fsm.Start()
//To start workflows using this fsm
client.StartWorkflow(StartWorkflowRequest{
Domain: "exaple-swf-domain",
WorkflowID: "your-id",
//you will have previously regiestered a WorkflowType that this FSM will work.
WorkflowType: WorkflowType{Name: "the-name", Version: "the-version"},
// It is *very* important to use StartFSMWorkflowInput so the state management works properly
Input: StartFSMWorkflowInput(fsm.Serializer, &StateData{Count: 0, Message: "starting message"}),
})
}
func TestContinuedWorkflows(t *testing.T) {
fsm := FSM{
Name: "test-fsm",
DataType: TestData{},
Serializer: JSONStateSerializer{},
}
fsm.AddInitialState(&FSMState{
Name: "ok",
Decider: func(f *FSMContext, h HistoryEvent, d interface{}) Outcome {
if h.EventType == EventTypeWorkflowExecutionSignaled && d.(*TestData).States[0] == "continuing" {
return f.Stay(d, nil)
}
panic("broken")
},
})
stateData := fsm.Serialize(TestData{States: []string{"continuing"}})
state := SerializedState{
ReplicationData: ReplicationData{
StateVersion: 23,
StateName: "ok",
StateData: stateData,
},
PendingActivities: ActivityCorrelator{},
}
serializedState := fsm.Serialize(state)
resp := &PollForDecisionTaskResponse{
Events: []HistoryEvent{HistoryEvent{
EventType: EventTypeWorkflowExecutionStarted,
WorkflowExecutionStartedEventAttributes: &WorkflowExecutionStartedEventAttributes{
Input: serializedState,
},
}},
StartedEventID: 5,
}
decisions, updatedState := fsm.Tick(resp)
log.Println(updatedState)
if updatedState.ReplicationData.StateVersion != 24 {
t.Fatal("StateVersion !=24 ", updatedState.ReplicationData.StateVersion)
}
if len(decisions) != 1 && decisions[0].RecordMarkerDecisionAttributes.MarkerName != StateMarker {
t.Fatal("unexpected decisions")
}
}
type MockKinesisClient struct {
*Client
putRecords []PutRecordRequest
seqNumber int
}
func (c *MockKinesisClient) PutRecord(req PutRecordRequest) (*PutRecordResponse, error) {
if c.putRecords == nil {
c.seqNumber = rand.Int()
c.putRecords = make([]PutRecordRequest, 0)
}
c.putRecords = append(c.putRecords, req)
c.seqNumber++
return &PutRecordResponse{
SequenceNumber: strconv.Itoa(c.seqNumber),
ShardID: req.PartitionKey,
}, nil
}
func (c *MockKinesisClient) RespondDecisionTaskCompleted(request RespondDecisionTaskCompletedRequest) error {
return nil
}
func TestKinesisReplication(t *testing.T) {
client := &MockKinesisClient{}
fsm := FSM{
Client: client,
Name: "test-fsm",
DataType: TestData{},
KinesisStream: "test-stream",
Serializer: JSONStateSerializer{},
KinesisReplicator: defaultKinesisReplicator(),
}
fsm.AddInitialState(&FSMState{
Name: "initial",
Decider: func(f *FSMContext, h HistoryEvent, d interface{}) Outcome {
if h.EventType == EventTypeWorkflowExecutionStarted {
return f.Goto("done", d, f.EmptyDecisions())
}
t.Fatal("unexpected")
return nil // unreachable
},
})
fsm.AddState(&FSMState{
Name: "done",
Decider: func(f *FSMContext, h HistoryEvent, d interface{}) Outcome {
go fsm.PollerShutdownManager.StopPollers()
return f.Stay(d, f.EmptyDecisions())
},
})
events := []HistoryEvent{
HistoryEvent{EventType: "DecisionTaskStarted", EventID: 3},
HistoryEvent{EventType: "DecisionTaskScheduled", EventID: 2},
HistoryEvent{
EventID: 1,
EventType: "WorkflowExecutionStarted",
WorkflowExecutionStartedEventAttributes: &WorkflowExecutionStartedEventAttributes{
Input: StartFSMWorkflowInput(fsm.Serializer, new(TestData)),
},
},
}
decisionTask := &PollForDecisionTaskResponse{
Events: events,
PreviousStartedEventID: 0,
}
fsm.handleDecisionTask(decisionTask)
if client.putRecords == nil || len(client.putRecords) != 1 {
t.Fatalf("expected only one state to be replicated, got: %v", client.putRecords)
}
replication := client.putRecords[0]
if replication.StreamName != fsm.KinesisStream {
t.Fatalf("expected Kinesis stream: %q, got %q", fsm.KinesisStream, replication.StreamName)
}
var replicatedState ReplicationData
if err := fsm.Serializer.Deserialize(string(replication.Data), &replicatedState); err != nil {
t.Fatal(err)
}
if replicatedState.StateVersion != 1 {
t.Fatalf("state.StateVersion != 1, got: %d", replicatedState.StateVersion)
}
if replicatedState.StateName != "done" {
t.Fatalf("current state being replicated is not 'done', got %q", replicatedState.StateName)
}
}
func TestTrackPendingActivities(t *testing.T) {
fsm := &FSM{
Name: "test-fsm",
DataType: TestData{},
Serializer: JSONStateSerializer{},
}
fsm.AddInitialState(&FSMState{
Name: "start",
Decider: func(f *FSMContext, lastEvent HistoryEvent, data interface{}) Outcome {
testData := data.(*TestData)
testData.States = append(testData.States, "start")
serialized := f.Serialize(testData)
decision := Decision{
DecisionType: DecisionTypeScheduleActivityTask,
ScheduleActivityTaskDecisionAttributes: &ScheduleActivityTaskDecisionAttributes{
ActivityID: testActivityInfo.ActivityID,
ActivityType: testActivityInfo.ActivityType,
TaskList: &TaskList{Name: "taskList"},
Input: serialized,
},
}
return f.Goto("working", testData, []Decision{decision})
},
})
// Deciders should be able to retrieve info about the pending activity
fsm.AddState(&FSMState{
Name: "working",
Decider: typedFuncs.Decider(func(f *FSMContext, lastEvent HistoryEvent, testData *TestData) Outcome {
testData.States = append(testData.States, "working")
serialized := f.Serialize(testData)
var decisions = f.EmptyDecisions()
if lastEvent.EventType == EventTypeActivityTaskCompleted {
trackedActivityInfo := f.ActivityInfo(lastEvent)
if !reflect.DeepEqual(*trackedActivityInfo, testActivityInfo) {
t.Fatalf("pending activity not being tracked\nExpected:\n%+v\nGot:\n%+v",
testActivityInfo, trackedActivityInfo,
)
}
timeoutSeconds := "5" //swf uses stringy numbers in many places
decision := Decision{
DecisionType: DecisionTypeStartTimer,
StartTimerDecisionAttributes: &StartTimerDecisionAttributes{
StartToFireTimeout: timeoutSeconds,
TimerID: "timeToComplete",
},
}
return f.Goto("done", testData, []Decision{decision})
} else if lastEvent.EventType == EventTypeActivityTaskFailed {
trackedActivityInfo := f.ActivityInfo(lastEvent)
if !reflect.DeepEqual(*trackedActivityInfo, testActivityInfo) {
t.Fatalf("pending activity not being tracked\nExpected:\n%+v\nGot:\n%+v",
testActivityInfo, trackedActivityInfo,
)
}
decision := Decision{
DecisionType: DecisionTypeScheduleActivityTask,
ScheduleActivityTaskDecisionAttributes: &ScheduleActivityTaskDecisionAttributes{
ActivityID: testActivityInfo.ActivityID,
ActivityType: testActivityInfo.ActivityType,
TaskList: &TaskList{Name: "taskList"},
Input: serialized,
},
}
decisions = append(decisions, decision)
}
return f.Stay(testData, decisions)
}),
})
// Pending activities are cleared after finished
fsm.AddState(&FSMState{
Name: "done",
Decider: typedFuncs.Decider(func(f *FSMContext, lastEvent HistoryEvent, testData *TestData) Outcome {
decisions := f.EmptyDecisions()
if lastEvent.EventType == EventTypeTimerFired {
testData.States = append(testData.States, "done")
serialized := f.Serialize(testData)
trackedActivityInfo := f.ActivityInfo(lastEvent)
if trackedActivityInfo != nil {
t.Fatalf("pending activity not being cleared\nGot:\n%+v", trackedActivityInfo)
}
decision := Decision{
DecisionType: DecisionTypeCompleteWorkflowExecution,
CompleteWorkflowExecutionDecisionAttributes: &CompleteWorkflowExecutionDecisionAttributes{
Result: serialized,
},
}
decisions = append(decisions, decision)
}
return f.Stay(testData, decisions)
}),
})
// Schedule a task
events := []HistoryEvent{
HistoryEvent{EventType: "DecisionTaskStarted", EventID: 3},
HistoryEvent{EventType: "DecisionTaskScheduled", EventID: 2},
HistoryEvent{
EventID: 1,
EventType: "WorkflowExecutionStarted",
WorkflowExecutionStartedEventAttributes: &WorkflowExecutionStartedEventAttributes{
Input: StartFSMWorkflowInput(fsm.Serializer, new(TestData)),
},
},
}
first := &PollForDecisionTaskResponse{
Events: events,
PreviousStartedEventID: 0,
}
decisions, _ := fsm.Tick(first)
recordMarker := FindDecision(decisions, stateMarkerPredicate)
if recordMarker == nil {
t.Fatal("No Record State Marker")
}
if !Find(decisions, scheduleActivityPredicate) {
t.Fatal("No ScheduleActivityTask")
}
// Fail the task
secondEvents := []HistoryEvent{
{
EventType: "ActivityTaskFailed",
EventID: 7,
ActivityTaskFailedEventAttributes: &ActivityTaskFailedEventAttributes{
ScheduledEventID: 6,
},
},
{
EventType: "ActivityTaskScheduled",
EventID: 6,
ActivityTaskScheduledEventAttributes: &ActivityTaskScheduledEventAttributes{
ActivityID: testActivityInfo.ActivityID,
ActivityType: testActivityInfo.ActivityType,
},
},
{
EventType: "MarkerRecorded",
EventID: 5,
MarkerRecordedEventAttributes: &MarkerRecordedEventAttributes{
MarkerName: StateMarker,
Details: recordMarker.RecordMarkerDecisionAttributes.Details,
},
},
}
secondEvents = append(secondEvents, events...)
if state, _ := fsm.findSerializedState(secondEvents); state.ReplicationData.StateName != "working" {
t.Fatal("current state is not 'working'", secondEvents)
}
second := &PollForDecisionTaskResponse{
Events: secondEvents,
PreviousStartedEventID: 3,
}
secondDecisions, _ := fsm.Tick(second)
recordMarker = FindDecision(secondDecisions, stateMarkerPredicate)
if recordMarker == nil {
t.Fatal("No Record State Marker")
}
if !Find(secondDecisions, scheduleActivityPredicate) {
t.Fatal("No ScheduleActivityTask (retry)")
}
// Complete the task
thirdEvents := []HistoryEvent{
{
EventType: "ActivityTaskCompleted",
EventID: 11,
ActivityTaskCompletedEventAttributes: &ActivityTaskCompletedEventAttributes{
ScheduledEventID: 10,
},
},
{
EventType: "ActivityTaskScheduled",
EventID: 10,
ActivityTaskScheduledEventAttributes: &ActivityTaskScheduledEventAttributes{
ActivityID: testActivityInfo.ActivityID,
ActivityType: testActivityInfo.ActivityType,
},
},
{
EventType: "MarkerRecorded",
EventID: 9,
MarkerRecordedEventAttributes: &MarkerRecordedEventAttributes{
MarkerName: StateMarker,
Details: recordMarker.RecordMarkerDecisionAttributes.Details,
},
},
}
thirdEvents = append(thirdEvents, secondEvents...)
if state, _ := fsm.findSerializedState(thirdEvents); state.ReplicationData.StateName != "working" {
t.Fatal("current state is not 'working'", thirdEvents)
}
third := &PollForDecisionTaskResponse{
Events: thirdEvents,
PreviousStartedEventID: 7,
}
thirdDecisions, _ := fsm.Tick(third)
recordMarker = FindDecision(thirdDecisions, stateMarkerPredicate)
if recordMarker == nil {
t.Fatal("No Record State Marker")
}
if !Find(thirdDecisions, startTimerPredicate) {
t.Fatal("No StartTimer")
}
// Finish the workflow, check if pending activities were cleared
fourthEvents := []HistoryEvent{
{
EventType: "TimerFired",
EventID: 14,
},
{
EventType: "MarkerRecorded",
EventID: 13,
MarkerRecordedEventAttributes: &MarkerRecordedEventAttributes{
MarkerName: StateMarker,
Details: recordMarker.RecordMarkerDecisionAttributes.Details,
},
},
}
fourthEvents = append(fourthEvents, thirdEvents...)
if state, _ := fsm.findSerializedState(fourthEvents); state.ReplicationData.StateName != "done" {
t.Fatal("current state is not 'done'", fourthEvents)
}
fourth := &PollForDecisionTaskResponse{
Events: fourthEvents,
PreviousStartedEventID: 11,
}
fourthDecisions, _ := fsm.Tick(fourth)
recordMarker = FindDecision(fourthDecisions, stateMarkerPredicate)
if recordMarker == nil {
t.Fatal("No Record State Marker")
}
if !Find(fourthDecisions, completeWorkflowPredicate) {
t.Fatal("No CompleteWorkflow")
}
}
func TestFSMContextActivityTracking(t *testing.T) {
ctx := NewFSMContext(
&FSM{
Name: "test-fsm",
DataType: TestData{},
Serializer: JSONStateSerializer{},
},
WorkflowType{Name: "test-workflow", Version: "1"},
WorkflowExecution{WorkflowID: "test-workflow-1", RunID: "123123"},
&ActivityCorrelator{},
"InitialState", &TestData{}, 0,
)
scheduledEventID := rand.Int()
activityID := fmt.Sprintf("test-activity-%d", scheduledEventID)
taskScheduled := HistoryEvent{
EventType: "ActivityTaskScheduled",
EventID: scheduledEventID,
ActivityTaskScheduledEventAttributes: &ActivityTaskScheduledEventAttributes{
ActivityID: activityID,
ActivityType: ActivityType{
Name: "test-activity",
Version: "1",
},
},
}
ctx.Decide(taskScheduled, &TestData{}, func(ctx *FSMContext, h HistoryEvent, data interface{}) Outcome {
if len(ctx.ActivitiesInfo()) != 0 {
t.Fatal("There should be no activies being tracked yet")
}
if !reflect.DeepEqual(h, taskScheduled) {
t.Fatal("Got an unexpected event")
}
return ctx.Stay(data, ctx.EmptyDecisions())
})
if len(ctx.ActivitiesInfo()) < 1 {
t.Fatal("Pending activity task is not being tracked")
}
// the pending activity can now be retrieved
taskCompleted := HistoryEvent{
EventType: "ActivityTaskCompleted",
EventID: rand.Int(),
ActivityTaskCompletedEventAttributes: &ActivityTaskCompletedEventAttributes{
ScheduledEventID: scheduledEventID,
},
}
taskFailed := HistoryEvent{
EventType: "ActivityTaskFailed",
EventID: rand.Int(),
ActivityTaskFailedEventAttributes: &ActivityTaskFailedEventAttributes{
ScheduledEventID: scheduledEventID,
},
}
infoOnCompleted := ctx.ActivityInfo(taskCompleted)
infoOnFailed := ctx.ActivityInfo(taskFailed)
if infoOnCompleted.ActivityID != activityID ||
infoOnCompleted.Name != "test-activity" ||
infoOnCompleted.Version != "1" {
t.Fatal("Pending activity can not be retrieved when completed")
}
if infoOnFailed.ActivityID != activityID ||
infoOnFailed.Name != "test-activity" ||
infoOnFailed.Version != "1" {
t.Fatal("Pending activity can not be retrieved when failed")
}
// pending activities are also cleared after terminated
ctx.Decide(taskCompleted, &TestData{}, func(ctx *FSMContext, h HistoryEvent, data interface{}) Outcome {
if len(ctx.ActivitiesInfo()) != 1 {
t.Fatal("There should be one activity being tracked")
}
if !reflect.DeepEqual(h, taskCompleted) {
t.Fatal("Got an unexpected event")
}
infoOnCompleted := ctx.ActivityInfo(taskCompleted)
if infoOnCompleted.ActivityID != activityID ||
infoOnCompleted.Name != "test-activity" ||
infoOnCompleted.Version != "1" {
t.Fatal("Pending activity can not be retrieved when completed")
}
return ctx.Stay(data, ctx.EmptyDecisions())
})
if len(ctx.ActivitiesInfo()) > 0 {
t.Fatal("Pending activity task is not being cleared after completed")
}
}
func TestContinueWorkflowDecision(t *testing.T) {
fsm := &FSM{
Name: "test-fsm",
DataType: TestData{},
Serializer: JSONStateSerializer{},
}
ctx := NewFSMContext(
fsm,
WorkflowType{Name: "test-workflow", Version: "1"},
WorkflowExecution{WorkflowID: "test-workflow-1", RunID: "123123"},
&ActivityCorrelator{},
"InitialState", &TestData{}, uint64(7),
)
ctx.stateData = &TestData{States: []string{"continuing"}}
fsm.AddInitialState(&FSMState{
Name: "InitialState",
Decider: func(ctx *FSMContext, h HistoryEvent, data interface{}) Outcome {
return nil
},
},
)
cont := ctx.ContinueWorkflowDecision("InitialState")
testData := new(TestData)
serState := new(SerializedState)
ctx.Deserialize(cont.ContinueAsNewWorkflowExecutionDecisionAttributes.Input, serState)
ctx.Deserialize(serState.ReplicationData.StateData, testData)
if len(testData.States) != 1 || testData.States[0] != "continuing" || serState.ReplicationData.StateVersion != 7 || serState.ReplicationData.StateName != "InitialState" {
t.Fatal(testData, cont)
}
}
func TestCompleteState(t *testing.T) {
fsm := &FSM{
Name: "test-fsm",
DataType: TestData{},
Serializer: JSONStateSerializer{},
}
ctx := NewFSMContext(
fsm,
WorkflowType{Name: "test-workflow", Version: "1"},
WorkflowExecution{WorkflowID: "test-workflow-1", RunID: "123123"},
&ActivityCorrelator{},
"InitialState", &TestData{}, 0,
)
event := HistoryEvent{
EventID: 1,
EventType: "WorkflowExecutionStarted",
WorkflowExecutionStartedEventAttributes: &WorkflowExecutionStartedEventAttributes{
Input: StartFSMWorkflowInput(ctx.Serializer(), new(TestData)),
},
}
fsm.AddInitialState(fsm.DefaultCompleteState())
fsm.Init()
outcome := fsm.completeState.Decider(ctx, event, new(TestData))
if len(outcome.Decisions()) != 1 {
t.Fatal(outcome)
}
if outcome.Decisions()[0].DecisionType != DecisionTypeCompleteWorkflowExecution {