|
| 1 | +import { Definition, Step } from 'sequential-workflow-model'; |
| 2 | +import { createSignalActivity, signalSignalActivity } from './signal-activity'; |
| 3 | +import { createActivitySet } from '../../core'; |
| 4 | +import { createWorkflowMachineBuilder } from '../../workflow-machine-builder'; |
| 5 | +import { createAtomActivityFromHandler } from '../atom-activity'; |
| 6 | +import { SerializedWorkflowMachineSnapshot } from '../../types'; |
| 7 | + |
| 8 | +interface TestGlobalState { |
| 9 | + logs: string[]; |
| 10 | +} |
| 11 | + |
| 12 | +const activitySet = createActivitySet<TestGlobalState>([ |
| 13 | + createAtomActivityFromHandler<Step, TestGlobalState>('ping', async (step, globalState) => { |
| 14 | + globalState.logs.push(`ping:${step.name}`); |
| 15 | + }), |
| 16 | + createSignalActivity<Step, TestGlobalState>('waitForSignal', { |
| 17 | + init: () => ({}), |
| 18 | + beforeSignal: async (step, globalState) => { |
| 19 | + globalState.logs.push(`beforeSignal:${step.name}`); |
| 20 | + }, |
| 21 | + afterSignal: async (step, globalState) => { |
| 22 | + globalState.logs.push(`afterSignal:${step.name}`); |
| 23 | + } |
| 24 | + }) |
| 25 | +]); |
| 26 | + |
| 27 | +function createPingStep(id: string, name: string): Step { |
| 28 | + return { |
| 29 | + id, |
| 30 | + componentType: 'task', |
| 31 | + type: 'ping', |
| 32 | + name, |
| 33 | + properties: {} |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe('SignalActivity - persistence', () => { |
| 38 | + it('saves state, restores state', done => { |
| 39 | + const definition: Definition = { |
| 40 | + sequence: [ |
| 41 | + createPingStep('0x1', 'p1'), |
| 42 | + { |
| 43 | + id: '0x2', |
| 44 | + componentType: 'task', |
| 45 | + type: 'waitForSignal', |
| 46 | + name: 'w1', |
| 47 | + properties: {} |
| 48 | + }, |
| 49 | + createPingStep('0x3', 'p2'), |
| 50 | + createPingStep('0x4', 'p3') |
| 51 | + ], |
| 52 | + properties: {} |
| 53 | + }; |
| 54 | + |
| 55 | + const builder = createWorkflowMachineBuilder(activitySet); |
| 56 | + const machine = builder.build(definition); |
| 57 | + |
| 58 | + let isIsInstance1Stopped = false; |
| 59 | + |
| 60 | + function runInstance2(serializedSnapshot: SerializedWorkflowMachineSnapshot<TestGlobalState>) { |
| 61 | + const size = JSON.stringify(serializedSnapshot).length; |
| 62 | + |
| 63 | + expect(size).toBe(2395); |
| 64 | + |
| 65 | + const interpreter = machine.deserializeSnapshot(serializedSnapshot); |
| 66 | + const paths: (string[] | null)[] = []; |
| 67 | + |
| 68 | + interpreter.onChange(() => { |
| 69 | + const path = interpreter.getSnapshot().tryGetStatePath(); |
| 70 | + paths.push(path); |
| 71 | + if (path && path.includes('WAIT_FOR_SIGNAL')) { |
| 72 | + expect(paths.length).toBe(1); // First path should be the same as the one from the serialized snapshot |
| 73 | + signalSignalActivity(interpreter, {}); |
| 74 | + } |
| 75 | + }); |
| 76 | + interpreter.onDone(() => { |
| 77 | + const snapshot = interpreter.getSnapshot(); |
| 78 | + expect(isIsInstance1Stopped).toBe(true); |
| 79 | + expect(snapshot.globalState.logs).toEqual(['ping:p1', 'beforeSignal:w1', 'afterSignal:w1', 'ping:p2', 'ping:p3']); |
| 80 | + expect(paths).toStrictEqual([ |
| 81 | + ['MAIN', 'STEP_0x2', 'WAIT_FOR_SIGNAL'], |
| 82 | + ['MAIN', 'STEP_0x2', 'AFTER_SIGNAL'], |
| 83 | + ['MAIN', 'STEP_0x3'], |
| 84 | + ['MAIN', 'STEP_0x4'], |
| 85 | + ['FINISHED'] |
| 86 | + ]); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + interpreter.start(); |
| 90 | + } |
| 91 | + |
| 92 | + function runInstance1() { |
| 93 | + const interpreter = machine.create({ |
| 94 | + init: () => ({ |
| 95 | + logs: [] |
| 96 | + }) |
| 97 | + }); |
| 98 | + const paths: (string[] | null)[] = []; |
| 99 | + |
| 100 | + interpreter.onChange(() => { |
| 101 | + const snapshot = interpreter.getSnapshot(); |
| 102 | + const path = snapshot.tryGetStatePath(); |
| 103 | + paths.push(path); |
| 104 | + |
| 105 | + if (path && path.includes('WAIT_FOR_SIGNAL')) { |
| 106 | + expect(snapshot.globalState.logs).toEqual(['ping:p1', 'beforeSignal:w1']); |
| 107 | + expect(paths).toStrictEqual([ |
| 108 | + ['MAIN', 'STEP_0x1'], |
| 109 | + ['MAIN', 'STEP_0x2', 'BEFORE_SIGNAL'], |
| 110 | + ['MAIN', 'STEP_0x2', 'WAIT_FOR_SIGNAL'] |
| 111 | + ]); |
| 112 | + runInstance2(interpreter.serializeSnapshot()); |
| 113 | + expect(interpreter.isRunning()).toBe(true); |
| 114 | + expect(interpreter.tryStop()).toBe(true); |
| 115 | + } |
| 116 | + }); |
| 117 | + interpreter.onDone(() => { |
| 118 | + const snapshot = interpreter.getSnapshot(); |
| 119 | + const path = snapshot.tryGetStatePath(); |
| 120 | + expect(path).toStrictEqual(['MAIN', 'STEP_0x2', 'WAIT_FOR_SIGNAL']); |
| 121 | + isIsInstance1Stopped = true; |
| 122 | + }); |
| 123 | + interpreter.start(); |
| 124 | + } |
| 125 | + |
| 126 | + runInstance1(); |
| 127 | + }); |
| 128 | +}); |
0 commit comments