1 import { Orchestrator } from "@utils"
2 import { FakeLocking, FakeLogger } from "../__fixtures__"
3
4 const entities = ["brand", "product"]
5
6 describe("Orchestrator", () => {
7 const logger = new FakeLogger().asLogger()
8
9 test("process each entity via the task runner", async () => {
10 const processedEntities: string[] = []
11 const locking = new FakeLocking()
12
13 async function taskRunner(entity: string) {
14 processedEntities.push(entity)
15 }
16
17 const orchestrator = new Orchestrator(locking, entities, {
18 lockDuration: 60 * 1000,
19 logger,
20 })
21
22 await orchestrator.process(taskRunner)
23
24 expect(locking.locks.size).toEqual(0)
25 expect(orchestrator.state).toEqual("completed")
26 expect(processedEntities).toEqual(["brand", "product"])
27 })
28
29 test("do not process tasks when unable to acquire lock", async () => {
30 const processedEntities: string[] = []
31 const locking = new FakeLocking()
32
33 // Another process already holds the locks for every entity
34 await locking.acquire(entities, { ownerId: "another-process" })
35
36 const orchestrator = new Orchestrator(locking, entities, {
37 lockDuration: 60 * 1000,
38 logger,
39 })
40
41 async function taskRunner(entity: string) {
42 processedEntities.push(entity)
43 }
44
45 await orchestrator.process(taskRunner)
46
47 expect(processedEntities).toEqual([])
48 })
49
50 test("share tasks between multiple instances", async () => {
51 const processedEntities: { owner: string; entity: string }[] = []
52 const locking = new FakeLocking()
53
54 async function taskRunner(entity: string) {
55 processedEntities.push({ entity: entity, owner: "instance-1" })
56 }
57
58 const orchestrator = new Orchestrator(locking, entities, {
59 lockDuration: 60 * 1000,
60 logger,
61 })
62
63 async function taskRunner2(entity: string) {
64 processedEntities.push({ entity: entity, owner: "instance-2" })
65 }
66
67 const orchestrator1 = new Orchestrator(locking, entities, {
68 lockDuration: 60 * 1000,
69 logger,
70 })
71
72 await Promise.all([
73 orchestrator.process(taskRunner),
74 orchestrator1.process(taskRunner2),
75 ])
76
77 expect(processedEntities).toEqual(
78 expect.arrayContaining([
79 {
80 entity: "brand",
81 owner: "instance-1",
82 },
83 {
84 entity: "product",
85 owner: "instance-2",
86 },
87 ])
88 )
89 expect(locking.locks.size).toEqual(0)
90 })
91
92 test("stop processing when task runner throws error", async () => {
93 const processedEntities: string[] = []
94 const locking = new FakeLocking()
95
96 async function taskRunner(entity: string) {
97 if (entity === "product") {
98 throw new Error("Cannot process")
99 }
100 processedEntities.push(entity)
101 }
102
103 const orchestrator = new Orchestrator(locking, entities, {
104 lockDuration: 60 * 1000,
105 logger,
106 })
107
108 await expect(orchestrator.process(taskRunner)).rejects.toThrow(
109 "Cannot process"
110 )
111 expect(orchestrator.state).toEqual("error")
112 expect(processedEntities).toEqual(["brand"])
113 expect(locking.locks.size).toEqual(0)
114 })
115
116 test("throw error when the same instance is executed to process tasks parallely", async () => {
117 const processedEntities: string[] = []
118 const locking = new FakeLocking()
119
120 async function taskRunner(entity: string) {
121 expect(orchestrator.state).toEqual("processing")
122 processedEntities.push(entity)
123 }
124
125 const orchestrator = new Orchestrator(locking, entities, {
126 lockDuration: 60 * 1000,
127 logger,
128 })
129
130 await expect(
131 Promise.all([
132 orchestrator.process(taskRunner),
133 orchestrator.process(taskRunner),
134 ])
135 ).rejects.toThrow("Cannot re-run an already running orchestrator instance")
136
137 expect(locking.locks.size).toEqual(0)
138 })
139 })