|
| 1 | +"use strict"; |
| 2 | +const { promisify, format } = require("util"); |
| 3 | + |
| 4 | +const { Semaphore } = require("../../src/shared/semaphore"); |
| 5 | + |
| 6 | +const sleep = promisify(setTimeout); |
| 7 | + |
| 8 | +describe("semaphore", () => { |
| 9 | + let executionLog; |
| 10 | + const n = 3; |
| 11 | + const m = 2; |
| 12 | + const result = "finished"; |
| 13 | + const runner = async (index) => { |
| 14 | + executionLog.push(format("started %d", index)); |
| 15 | + await sleep(10); |
| 16 | + executionLog.push(format("finished %d", index)); |
| 17 | + return result; |
| 18 | + }; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + executionLog = []; |
| 22 | + }); |
| 23 | + |
| 24 | + test("non-exclusive", async () => { |
| 25 | + const resultsPrimary = await Promise.all(Array.from({ length: n }, (_, i) => runner(i + 1))); |
| 26 | + |
| 27 | + const resultsSecondary = await Promise.all(Array.from({ length: m }, (_, i) => runner(n + i + 1))); |
| 28 | + expect(resultsPrimary).toMatchInlineSnapshot(` |
| 29 | + [ |
| 30 | + "finished", |
| 31 | + "finished", |
| 32 | + "finished", |
| 33 | + ] |
| 34 | + `); |
| 35 | + expect(resultsSecondary).toMatchInlineSnapshot(` |
| 36 | + [ |
| 37 | + "finished", |
| 38 | + "finished", |
| 39 | + ] |
| 40 | + `); |
| 41 | + expect(executionLog).toMatchInlineSnapshot(` |
| 42 | + [ |
| 43 | + "started 1", |
| 44 | + "started 2", |
| 45 | + "started 3", |
| 46 | + "finished 1", |
| 47 | + "finished 2", |
| 48 | + "finished 3", |
| 49 | + "started 4", |
| 50 | + "started 5", |
| 51 | + "finished 4", |
| 52 | + "finished 5", |
| 53 | + ] |
| 54 | + `); |
| 55 | + }); |
| 56 | + |
| 57 | + test("exclusive queueing", async () => { |
| 58 | + const exclusiveRunner = Semaphore.makeExclusiveQueuing(runner); |
| 59 | + const resultsPrimary = await Promise.all(Array.from({ length: n }, (_, i) => exclusiveRunner(i + 1))); |
| 60 | + |
| 61 | + const resultsSecondary = await Promise.all(Array.from({ length: m }, (_, i) => exclusiveRunner(n + i + 1))); |
| 62 | + expect(resultsPrimary).toMatchInlineSnapshot(` |
| 63 | + [ |
| 64 | + "finished", |
| 65 | + "finished", |
| 66 | + "finished", |
| 67 | + ] |
| 68 | + `); |
| 69 | + expect(resultsSecondary).toMatchInlineSnapshot(` |
| 70 | + [ |
| 71 | + "finished", |
| 72 | + "finished", |
| 73 | + ] |
| 74 | + `); |
| 75 | + expect(executionLog).toMatchInlineSnapshot(` |
| 76 | + [ |
| 77 | + "started 1", |
| 78 | + "finished 1", |
| 79 | + "started 2", |
| 80 | + "finished 2", |
| 81 | + "started 3", |
| 82 | + "finished 3", |
| 83 | + "started 4", |
| 84 | + "finished 4", |
| 85 | + "started 5", |
| 86 | + "finished 5", |
| 87 | + ] |
| 88 | + `); |
| 89 | + }); |
| 90 | + |
| 91 | + test("exclusive returning", async () => { |
| 92 | + const exclusiveRunner = Semaphore.makeExclusiveReturning(runner); |
| 93 | + const resultsPrimary = await Promise.all(Array.from({ length: n }, (_, i) => exclusiveRunner(i + 1))); |
| 94 | + |
| 95 | + const resultsSecondary = await Promise.all(Array.from({ length: m }, (_, i) => exclusiveRunner(n + i + 1))); |
| 96 | + expect(resultsPrimary).toMatchInlineSnapshot(` |
| 97 | + [ |
| 98 | + "finished", |
| 99 | + undefined, |
| 100 | + undefined, |
| 101 | + ] |
| 102 | + `); |
| 103 | + expect(resultsSecondary).toMatchInlineSnapshot(` |
| 104 | + [ |
| 105 | + "finished", |
| 106 | + undefined, |
| 107 | + ] |
| 108 | + `); |
| 109 | + expect(executionLog).toMatchInlineSnapshot(` |
| 110 | + [ |
| 111 | + "started 1", |
| 112 | + "finished 1", |
| 113 | + "started 4", |
| 114 | + "finished 4", |
| 115 | + ] |
| 116 | + `); |
| 117 | + }); |
| 118 | +}); |
0 commit comments