Skip to content

Commit

Permalink
text: add cycle test (#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusschiesser authored Sep 30, 2024
1 parent 5c0c8b2 commit 4c07a26
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/core/tests/workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,41 @@ describe("Workflow", () => {
expect(duration).toBeLessThan(200);
expect(result.data.result).toBe("Step 2 completed");
});

test("workflow with two concurrent cyclic steps", async () => {
const concurrentCyclicFlow = new Workflow({ verbose: true });

class Step1Event extends WorkflowEvent {}
class Step2Event extends WorkflowEvent {}

let step2Count = 0;

const step1 = vi.fn(async (_context, ev: StartEvent | Step1Event) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return new Step1Event({ result: "Step 1 completed" });
});

const step2 = vi.fn(async (_context, ev: StartEvent | Step2Event) => {
await new Promise((resolve) => setTimeout(resolve, 100));
step2Count++;
if (step2Count >= 5) {
return new StopEvent({ result: "Step 2 completed 5 times" });
}
return new Step2Event({ result: "Step 2 completed" });
});

concurrentCyclicFlow.addStep([StartEvent, Step1Event], step1);
concurrentCyclicFlow.addStep([StartEvent, Step2Event], step2);

const startTime = new Date();
const result = await concurrentCyclicFlow.run("start");
const endTime = new Date();
const duration = endTime.getTime() - startTime.getTime();

expect(step1).toHaveBeenCalledTimes(1);
expect(step2).toHaveBeenCalledTimes(5);
expect(duration).toBeGreaterThan(500); // At least 5 * 100ms for step2
expect(duration).toBeLessThan(1000); // Less than 1 second
expect(result.data.result).toBe("Step 2 completed 5 times");
});
});

0 comments on commit 4c07a26

Please sign in to comment.