|
1 |
| -import * as assert from "node:assert"; |
2 |
| -import { Queue } from "../../src/git/queue.js"; |
3 |
| -import * as Sinon from "sinon"; |
4 |
| -import { afterEach, beforeEach } from "mocha"; |
5 |
| - |
6 |
| -const sleep = <T>(time: number, response: T): Promise<T> => { |
7 |
| - return new Promise((resolve) => |
8 |
| - setTimeout(() => { |
9 |
| - resolve(response); |
10 |
| - }, time), |
11 |
| - ); |
12 |
| -}; |
13 |
| - |
14 |
| -suite("Promise Queue", (): void => { |
15 |
| - let clock: Sinon.SinonFakeTimers | undefined = undefined; |
16 |
| - |
17 |
| - beforeEach(() => { |
18 |
| - clock = Sinon.useFakeTimers(); |
19 |
| - }); |
20 |
| - |
21 |
| - afterEach(() => { |
22 |
| - clock?.restore(); |
23 |
| - }); |
24 |
| - |
25 |
| - test("Create Instance", (): void => { |
26 |
| - const instance = new Queue(); |
27 |
| - |
28 |
| - assert.deepStrictEqual( |
29 |
| - instance instanceof Queue, |
30 |
| - true, |
31 |
| - "is instance of Queue", |
32 |
| - ); |
33 |
| - }); |
34 |
| - |
35 |
| - test("Pass through when queue is short", async (): Promise<void> => { |
36 |
| - const instance = new Queue(); |
37 |
| - const spy = Sinon.spy(() => Promise.resolve()); |
38 |
| - |
39 |
| - await instance.add(spy); |
40 |
| - |
41 |
| - Sinon.assert.calledOnce(spy); |
42 |
| - }); |
43 |
| - |
44 |
| - test("Don't run when adding to queue and it is to long, then run it when ready", async (): Promise<void> => { |
45 |
| - const instance = new Queue<undefined>(); |
46 |
| - |
47 |
| - const spy1 = Sinon.spy(() => sleep(100, undefined)); |
48 |
| - const spy2 = Sinon.spy(() => sleep(100, undefined)); |
49 |
| - const spy3 = Sinon.spy(() => sleep(100, undefined)); |
50 |
| - |
51 |
| - instance.add(spy1); |
52 |
| - instance.add(spy2); |
53 |
| - instance.add(spy3); |
54 |
| - |
55 |
| - await clock?.tickAsync(50); |
56 |
| - |
57 |
| - Sinon.assert.calledOnce(spy1); |
58 |
| - Sinon.assert.calledOnce(spy2); |
59 |
| - Sinon.assert.notCalled(spy3); |
60 |
| - |
61 |
| - await clock?.tickAsync(50); |
62 |
| - |
63 |
| - Sinon.assert.calledOnce(spy2); |
64 |
| - |
65 |
| - await clock?.runAllAsync(); |
66 |
| - }); |
67 |
| - |
68 |
| - test("Being able to use the value from the original promise", async (): Promise<void> => { |
69 |
| - const instance = new Queue<string>(); |
70 |
| - const myFunc = () => Promise.resolve("UNIQUE_VALUE"); |
71 |
| - |
72 |
| - const result = await instance.add(myFunc); |
73 |
| - |
74 |
| - assert.strictEqual(result, "UNIQUE_VALUE"); |
75 |
| - }); |
76 |
| - |
77 |
| - test("Being able to use the value from the original promise after queue", async (): Promise<void> => { |
78 |
| - const instance = new Queue<string>(); |
79 |
| - const myFunc1 = () => sleep(100, "UNIQUE_VALUE_1"); |
80 |
| - const myFunc2 = () => sleep(100, "UNIQUE_VALUE_2"); |
81 |
| - const myFunc3 = () => sleep(100, "UNIQUE_VALUE_3"); |
82 |
| - |
83 |
| - const call1 = instance.add(myFunc1); |
84 |
| - const call2 = instance.add(myFunc2); |
85 |
| - const call3 = instance.add(myFunc3); |
86 |
| - |
87 |
| - await clock?.tickAsync(200); |
88 |
| - |
89 |
| - assert.strictEqual(await call1, "UNIQUE_VALUE_1"); |
90 |
| - assert.strictEqual(await call2, "UNIQUE_VALUE_2"); |
91 |
| - assert.strictEqual(await call3, "UNIQUE_VALUE_3"); |
92 |
| - }); |
93 |
| - |
94 |
| - test("Minimum 1 parallel queue size", async (): Promise<void> => { |
95 |
| - const instance = new Queue<void>(-1); |
96 |
| - |
97 |
| - assert.strictEqual(await instance.add(() => Promise.resolve()), undefined); |
98 |
| - }); |
99 |
| - |
100 |
| - test("Increase max parallel runs more things", async (): Promise<void> => { |
101 |
| - const instance = new Queue<undefined>(1); |
102 |
| - |
103 |
| - const spy1 = Sinon.spy(() => sleep(100, undefined)); |
104 |
| - const spy2 = Sinon.spy(() => sleep(100, undefined)); |
105 |
| - const spy3 = Sinon.spy(() => sleep(100, undefined)); |
106 |
| - |
107 |
| - instance.add(spy1); |
108 |
| - instance.add(spy2); |
109 |
| - instance.add(spy3); |
110 |
| - |
111 |
| - Sinon.assert.calledOnce(spy1); |
112 |
| - Sinon.assert.notCalled(spy2); |
113 |
| - Sinon.assert.notCalled(spy3); |
114 |
| - |
115 |
| - instance.updateParallel(3); |
116 |
| - |
117 |
| - Sinon.assert.calledOnce(spy2); |
118 |
| - Sinon.assert.calledOnce(spy3); |
119 |
| - |
120 |
| - await clock?.runAllAsync(); |
121 |
| - }); |
122 |
| - |
123 |
| - test("Decrease max parallel does not run more things", async (): Promise<void> => { |
124 |
| - const instance = new Queue<undefined>(2); |
125 |
| - |
126 |
| - const spy1 = Sinon.spy(() => sleep(100, undefined)); |
127 |
| - const spy2 = Sinon.spy(() => sleep(100, undefined)); |
128 |
| - const spy3 = Sinon.spy(() => sleep(100, undefined)); |
129 |
| - const spy4 = Sinon.spy(() => sleep(100, undefined)); |
130 |
| - |
131 |
| - instance.add(spy1); |
132 |
| - instance.add(spy2); |
133 |
| - instance.add(spy3); |
134 |
| - |
135 |
| - Sinon.assert.calledOnce(spy1); |
136 |
| - Sinon.assert.calledOnce(spy2); |
137 |
| - Sinon.assert.notCalled(spy3); |
138 |
| - |
139 |
| - instance.updateParallel(1); |
140 |
| - instance.add(spy4); |
141 |
| - |
142 |
| - await clock?.tickAsync(50); |
143 |
| - |
144 |
| - Sinon.assert.notCalled(spy3); |
145 |
| - Sinon.assert.notCalled(spy4); |
146 |
| - |
147 |
| - await clock?.tickAsync(100); |
148 |
| - |
149 |
| - Sinon.assert.calledOnce(spy3); |
150 |
| - Sinon.assert.notCalled(spy4); |
151 |
| - |
152 |
| - await clock?.runAllAsync(); |
153 |
| - }); |
154 |
| -}); |
| 1 | +import * as assert from "node:assert"; |
| 2 | +import { Queue } from "../../src/git/queue.js"; |
| 3 | +import * as Sinon from "sinon"; |
| 4 | +import { afterEach, beforeEach } from "mocha"; |
| 5 | + |
| 6 | +const sleep = <T>(time: number, response: T): Promise<T> => { |
| 7 | + return new Promise((resolve) => |
| 8 | + setTimeout(() => { |
| 9 | + resolve(response); |
| 10 | + }, time), |
| 11 | + ); |
| 12 | +}; |
| 13 | + |
| 14 | +suite("Promise Queue", (): void => { |
| 15 | + let clock: Sinon.SinonFakeTimers | undefined = undefined; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + clock = Sinon.useFakeTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + clock?.restore(); |
| 23 | + }); |
| 24 | + |
| 25 | + test("Create Instance", (): void => { |
| 26 | + const instance = new Queue(); |
| 27 | + |
| 28 | + assert.deepStrictEqual( |
| 29 | + instance instanceof Queue, |
| 30 | + true, |
| 31 | + "is instance of Queue", |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + test("Pass through when queue is short", async (): Promise<void> => { |
| 36 | + const instance = new Queue(); |
| 37 | + const spy = Sinon.spy(() => Promise.resolve()); |
| 38 | + |
| 39 | + await instance.add(spy); |
| 40 | + |
| 41 | + Sinon.assert.calledOnce(spy); |
| 42 | + }); |
| 43 | + |
| 44 | + test("Don't run when adding to queue and it is to long, then run it when ready", async (): Promise<void> => { |
| 45 | + const instance = new Queue<undefined>(); |
| 46 | + |
| 47 | + const spy1 = Sinon.spy(() => sleep(100, undefined)); |
| 48 | + const spy2 = Sinon.spy(() => sleep(100, undefined)); |
| 49 | + const spy3 = Sinon.spy(() => sleep(100, undefined)); |
| 50 | + |
| 51 | + instance.add(spy1); |
| 52 | + instance.add(spy2); |
| 53 | + instance.add(spy3); |
| 54 | + |
| 55 | + await clock?.tickAsync(50); |
| 56 | + |
| 57 | + Sinon.assert.calledOnce(spy1); |
| 58 | + Sinon.assert.calledOnce(spy2); |
| 59 | + Sinon.assert.notCalled(spy3); |
| 60 | + |
| 61 | + await clock?.tickAsync(50); |
| 62 | + |
| 63 | + Sinon.assert.calledOnce(spy2); |
| 64 | + |
| 65 | + await clock?.runAllAsync(); |
| 66 | + }); |
| 67 | + |
| 68 | + test("Being able to use the value from the original promise", async (): Promise<void> => { |
| 69 | + const instance = new Queue<string>(); |
| 70 | + const myFunc = () => Promise.resolve("UNIQUE_VALUE"); |
| 71 | + |
| 72 | + const result = await instance.add(myFunc); |
| 73 | + |
| 74 | + assert.strictEqual(result, "UNIQUE_VALUE"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("Being able to use the value from the original promise after queue", async (): Promise<void> => { |
| 78 | + const instance = new Queue<string>(); |
| 79 | + const myFunc1 = () => sleep(100, "UNIQUE_VALUE_1"); |
| 80 | + const myFunc2 = () => sleep(100, "UNIQUE_VALUE_2"); |
| 81 | + const myFunc3 = () => sleep(100, "UNIQUE_VALUE_3"); |
| 82 | + |
| 83 | + const call1 = instance.add(myFunc1); |
| 84 | + const call2 = instance.add(myFunc2); |
| 85 | + const call3 = instance.add(myFunc3); |
| 86 | + |
| 87 | + await clock?.tickAsync(200); |
| 88 | + |
| 89 | + assert.strictEqual(await call1, "UNIQUE_VALUE_1"); |
| 90 | + assert.strictEqual(await call2, "UNIQUE_VALUE_2"); |
| 91 | + assert.strictEqual(await call3, "UNIQUE_VALUE_3"); |
| 92 | + }); |
| 93 | + |
| 94 | + test("Minimum 1 parallel queue size", async (): Promise<void> => { |
| 95 | + const instance = new Queue<void>(-1); |
| 96 | + |
| 97 | + assert.strictEqual(await instance.add(() => Promise.resolve()), undefined); |
| 98 | + }); |
| 99 | + |
| 100 | + test("Increase max parallel runs more things", async (): Promise<void> => { |
| 101 | + const instance = new Queue<undefined>(1); |
| 102 | + |
| 103 | + const spy1 = Sinon.spy(() => sleep(100, undefined)); |
| 104 | + const spy2 = Sinon.spy(() => sleep(100, undefined)); |
| 105 | + const spy3 = Sinon.spy(() => sleep(100, undefined)); |
| 106 | + |
| 107 | + instance.add(spy1); |
| 108 | + instance.add(spy2); |
| 109 | + instance.add(spy3); |
| 110 | + |
| 111 | + Sinon.assert.calledOnce(spy1); |
| 112 | + Sinon.assert.notCalled(spy2); |
| 113 | + Sinon.assert.notCalled(spy3); |
| 114 | + |
| 115 | + instance.updateParallel(3); |
| 116 | + |
| 117 | + Sinon.assert.calledOnce(spy2); |
| 118 | + Sinon.assert.calledOnce(spy3); |
| 119 | + |
| 120 | + await clock?.runAllAsync(); |
| 121 | + }); |
| 122 | + |
| 123 | + test("Decrease max parallel does not run more things", async (): Promise<void> => { |
| 124 | + const instance = new Queue<undefined>(2); |
| 125 | + |
| 126 | + const spy1 = Sinon.spy(() => sleep(100, undefined)); |
| 127 | + const spy2 = Sinon.spy(() => sleep(100, undefined)); |
| 128 | + const spy3 = Sinon.spy(() => sleep(100, undefined)); |
| 129 | + const spy4 = Sinon.spy(() => sleep(100, undefined)); |
| 130 | + |
| 131 | + instance.add(spy1); |
| 132 | + instance.add(spy2); |
| 133 | + instance.add(spy3); |
| 134 | + |
| 135 | + Sinon.assert.calledOnce(spy1); |
| 136 | + Sinon.assert.calledOnce(spy2); |
| 137 | + Sinon.assert.notCalled(spy3); |
| 138 | + |
| 139 | + instance.updateParallel(1); |
| 140 | + instance.add(spy4); |
| 141 | + |
| 142 | + await clock?.tickAsync(50); |
| 143 | + |
| 144 | + Sinon.assert.notCalled(spy3); |
| 145 | + Sinon.assert.notCalled(spy4); |
| 146 | + |
| 147 | + await clock?.tickAsync(100); |
| 148 | + |
| 149 | + Sinon.assert.calledOnce(spy3); |
| 150 | + Sinon.assert.notCalled(spy4); |
| 151 | + |
| 152 | + await clock?.runAllAsync(); |
| 153 | + }); |
| 154 | +}); |
0 commit comments