@agentica/core 0.18.0 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.mjs +32 -14
- package/lib/index.mjs.map +1 -1
- package/lib/utils/AsyncQueue.d.ts +10 -0
- package/lib/utils/AsyncQueue.js +33 -9
- package/lib/utils/AsyncQueue.js.map +1 -1
- package/lib/utils/AsyncQueue.spec.d.ts +1 -0
- package/lib/utils/AsyncQueue.spec.js +280 -0
- package/lib/utils/AsyncQueue.spec.js.map +1 -0
- package/lib/utils/MPSC.spec.d.ts +1 -0
- package/lib/utils/MPSC.spec.js +222 -0
- package/lib/utils/MPSC.spec.js.map +1 -0
- package/lib/utils/StreamUtil.spec.d.ts +1 -0
- package/lib/utils/StreamUtil.spec.js +471 -0
- package/lib/utils/StreamUtil.spec.js.map +1 -0
- package/package.json +1 -1
- package/src/utils/AsyncQueue.spec.ts +355 -0
- package/src/utils/AsyncQueue.ts +36 -8
- package/src/utils/MPSC.spec.ts +276 -0
- package/src/utils/StreamUtil.spec.ts +520 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { AsyncQueue } from "./AsyncQueue";
|
|
2
|
+
|
|
3
|
+
describe("the AsyncQueue", () => {
|
|
4
|
+
describe("basic functionality", () => {
|
|
5
|
+
it("enqueue and dequeue test", async () => {
|
|
6
|
+
const queue = new AsyncQueue<number>();
|
|
7
|
+
|
|
8
|
+
// Enqueue items
|
|
9
|
+
queue.enqueue(1);
|
|
10
|
+
queue.enqueue(2);
|
|
11
|
+
queue.enqueue(3);
|
|
12
|
+
|
|
13
|
+
// Dequeue items
|
|
14
|
+
const result1 = await queue.dequeue();
|
|
15
|
+
const result2 = await queue.dequeue();
|
|
16
|
+
const result3 = await queue.dequeue();
|
|
17
|
+
|
|
18
|
+
expect(result1.value).toBe(1);
|
|
19
|
+
expect(result1.done).toBe(false);
|
|
20
|
+
expect(result2.value).toBe(2);
|
|
21
|
+
expect(result2.done).toBe(false);
|
|
22
|
+
expect(result3.value).toBe(3);
|
|
23
|
+
expect(result3.done).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("isEmpty test", async () => {
|
|
27
|
+
const queue = new AsyncQueue<number>();
|
|
28
|
+
|
|
29
|
+
expect(queue.isEmpty()).toBe(true);
|
|
30
|
+
|
|
31
|
+
queue.enqueue(1);
|
|
32
|
+
expect(queue.isEmpty()).toBe(false);
|
|
33
|
+
|
|
34
|
+
await queue.dequeue();
|
|
35
|
+
expect(queue.isEmpty()).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("isClosed test", () => {
|
|
39
|
+
const queue = new AsyncQueue<number>();
|
|
40
|
+
|
|
41
|
+
expect(queue.isClosed()).toBe(false);
|
|
42
|
+
|
|
43
|
+
queue.close();
|
|
44
|
+
expect(queue.isClosed()).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("done test", async () => {
|
|
48
|
+
const queue = new AsyncQueue<number>();
|
|
49
|
+
|
|
50
|
+
expect(queue.done()).toBe(false);
|
|
51
|
+
|
|
52
|
+
queue.enqueue(1);
|
|
53
|
+
expect(queue.done()).toBe(false);
|
|
54
|
+
|
|
55
|
+
await queue.dequeue();
|
|
56
|
+
expect(queue.done()).toBe(false);
|
|
57
|
+
|
|
58
|
+
queue.close();
|
|
59
|
+
expect(queue.done()).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("close functionality", () => {
|
|
64
|
+
it("close test with empty queue", async () => {
|
|
65
|
+
const queue = new AsyncQueue<number>();
|
|
66
|
+
|
|
67
|
+
queue.close();
|
|
68
|
+
|
|
69
|
+
const result = await queue.dequeue();
|
|
70
|
+
expect(result.done).toBe(true);
|
|
71
|
+
expect(result.value).toBeUndefined();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("close test with non-empty queue", async () => {
|
|
75
|
+
const queue = new AsyncQueue<number>();
|
|
76
|
+
|
|
77
|
+
queue.enqueue(1);
|
|
78
|
+
queue.enqueue(2);
|
|
79
|
+
queue.close();
|
|
80
|
+
|
|
81
|
+
const result1 = await queue.dequeue();
|
|
82
|
+
const result2 = await queue.dequeue();
|
|
83
|
+
const result3 = await queue.dequeue();
|
|
84
|
+
|
|
85
|
+
expect(result1.value).toBe(1);
|
|
86
|
+
expect(result1.done).toBe(false);
|
|
87
|
+
expect(result2.value).toBe(2);
|
|
88
|
+
expect(result2.done).toBe(false);
|
|
89
|
+
expect(result3.done).toBe(true);
|
|
90
|
+
expect(result3.value).toBeUndefined();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("close test with waiting dequeue", async () => {
|
|
94
|
+
const queue = new AsyncQueue<number>();
|
|
95
|
+
|
|
96
|
+
// Start dequeue before enqueue
|
|
97
|
+
const dequeuePromise = queue.dequeue();
|
|
98
|
+
|
|
99
|
+
// Close the queue
|
|
100
|
+
queue.close();
|
|
101
|
+
|
|
102
|
+
const result = await dequeuePromise;
|
|
103
|
+
expect(result.done).toBe(true);
|
|
104
|
+
expect(result.value).toBeUndefined();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("waitUntilEmpty functionality", () => {
|
|
109
|
+
it("waitUntilEmpty test with empty queue", async () => {
|
|
110
|
+
const queue = new AsyncQueue<number>();
|
|
111
|
+
|
|
112
|
+
// Should resolve immediately since queue is empty
|
|
113
|
+
await queue.waitUntilEmpty();
|
|
114
|
+
|
|
115
|
+
queue.enqueue(1);
|
|
116
|
+
const result = await queue.dequeue();
|
|
117
|
+
expect(result.value).toBe(1);
|
|
118
|
+
expect(result.done).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("waitUntilEmpty test with non-empty queue", async () => {
|
|
122
|
+
const queue = new AsyncQueue<number>();
|
|
123
|
+
|
|
124
|
+
queue.enqueue(1);
|
|
125
|
+
queue.enqueue(2);
|
|
126
|
+
|
|
127
|
+
// waitUntilEmpty should not resolve since queue is not empty
|
|
128
|
+
const waitPromise = queue.waitUntilEmpty();
|
|
129
|
+
|
|
130
|
+
// Dequeue first value
|
|
131
|
+
const result1 = await queue.dequeue();
|
|
132
|
+
expect(result1.value).toBe(1);
|
|
133
|
+
|
|
134
|
+
// Dequeue second value
|
|
135
|
+
const result2 = await queue.dequeue();
|
|
136
|
+
expect(result2.value).toBe(2);
|
|
137
|
+
|
|
138
|
+
// Now queue is empty, waitUntilEmpty should resolve\
|
|
139
|
+
await waitPromise;
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("waitClosed functionality", () => {
|
|
144
|
+
it("waitClosed test with unclosed queue", async () => {
|
|
145
|
+
const queue = new AsyncQueue<number>();
|
|
146
|
+
|
|
147
|
+
// waitClosed should not resolve since queue is not closed
|
|
148
|
+
const waitPromise = queue.waitClosed();
|
|
149
|
+
|
|
150
|
+
queue.enqueue(1);
|
|
151
|
+
const result = await queue.dequeue();
|
|
152
|
+
expect(result.value).toBe(1);
|
|
153
|
+
|
|
154
|
+
// Close the queue
|
|
155
|
+
queue.close();
|
|
156
|
+
|
|
157
|
+
// Now queue is closed, waitClosed should resolve
|
|
158
|
+
await waitPromise;
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("waitClosed test with already closed queue", async () => {
|
|
162
|
+
const queue = new AsyncQueue<number>();
|
|
163
|
+
|
|
164
|
+
queue.close();
|
|
165
|
+
|
|
166
|
+
// waitClosed should resolve immediately since queue is already closed
|
|
167
|
+
await queue.waitClosed();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("multiple waitClosed calls test", async () => {
|
|
171
|
+
const queue = new AsyncQueue<number>();
|
|
172
|
+
|
|
173
|
+
// Create multiple waitClosed promises
|
|
174
|
+
const waitPromises = [queue.waitClosed(), queue.waitClosed(), queue.waitClosed()];
|
|
175
|
+
|
|
176
|
+
// Close the queue
|
|
177
|
+
queue.close();
|
|
178
|
+
|
|
179
|
+
// All promises should resolve
|
|
180
|
+
await Promise.all(waitPromises);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("waitClosed test with delayed close", async () => {
|
|
184
|
+
const queue = new AsyncQueue<string>();
|
|
185
|
+
|
|
186
|
+
// Start waiting for close
|
|
187
|
+
const closePromise = queue.waitClosed();
|
|
188
|
+
|
|
189
|
+
// Close after delay
|
|
190
|
+
setTimeout(() => {
|
|
191
|
+
queue.close();
|
|
192
|
+
}, 10);
|
|
193
|
+
|
|
194
|
+
await closePromise; // Should resolve when queue is closed
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe("dequeue behavior", () => {
|
|
199
|
+
it("dequeue before enqueue test", async () => {
|
|
200
|
+
const queue = new AsyncQueue<number>();
|
|
201
|
+
|
|
202
|
+
// Start dequeue before enqueue
|
|
203
|
+
const dequeuePromise = queue.dequeue();
|
|
204
|
+
|
|
205
|
+
// Enqueue after a small delay
|
|
206
|
+
setTimeout(() => {
|
|
207
|
+
queue.enqueue(42);
|
|
208
|
+
}, 10);
|
|
209
|
+
|
|
210
|
+
const result = await dequeuePromise;
|
|
211
|
+
expect(result.value).toBe(42);
|
|
212
|
+
expect(result.done).toBe(false);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("multiple dequeue calls test", async () => {
|
|
216
|
+
const queue = new AsyncQueue<number>();
|
|
217
|
+
|
|
218
|
+
// Start multiple dequeue calls
|
|
219
|
+
const dequeuePromises = [
|
|
220
|
+
queue.dequeue(),
|
|
221
|
+
queue.dequeue(),
|
|
222
|
+
queue.dequeue(),
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
// Enqueue values
|
|
226
|
+
queue.enqueue(1);
|
|
227
|
+
queue.enqueue(2);
|
|
228
|
+
queue.enqueue(3);
|
|
229
|
+
|
|
230
|
+
const results = await Promise.all(dequeuePromises);
|
|
231
|
+
|
|
232
|
+
expect(results[0]?.value).toBe(1);
|
|
233
|
+
expect(results[0]?.done).toBe(false);
|
|
234
|
+
expect(results[1]?.value).toBe(2);
|
|
235
|
+
expect(results[1]?.done).toBe(false);
|
|
236
|
+
expect(results[2]?.value).toBe(3);
|
|
237
|
+
expect(results[2]?.done).toBe(false);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("dequeue after close test", async () => {
|
|
241
|
+
const queue = new AsyncQueue<number>();
|
|
242
|
+
|
|
243
|
+
queue.enqueue(1);
|
|
244
|
+
queue.close();
|
|
245
|
+
|
|
246
|
+
const result1 = await queue.dequeue();
|
|
247
|
+
expect(result1.value).toBe(1);
|
|
248
|
+
expect(result1.done).toBe(false);
|
|
249
|
+
|
|
250
|
+
const result2 = await queue.dequeue();
|
|
251
|
+
expect(result2.done).toBe(true);
|
|
252
|
+
expect(result2.value).toBeUndefined();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("duplicate dequeue test", async () => {
|
|
256
|
+
const queue = new AsyncQueue<string>();
|
|
257
|
+
|
|
258
|
+
// Start dequeue operation that will wait for an item
|
|
259
|
+
const pendingDequeue = queue.dequeue();
|
|
260
|
+
|
|
261
|
+
// Add item after a small delay
|
|
262
|
+
setTimeout(() => {
|
|
263
|
+
queue.enqueue("delayed item");
|
|
264
|
+
}, 10);
|
|
265
|
+
|
|
266
|
+
const delayedResult = await pendingDequeue;
|
|
267
|
+
expect(delayedResult.value).toBe("delayed item");
|
|
268
|
+
expect(delayedResult.done).toBe(false);
|
|
269
|
+
|
|
270
|
+
// Check for duplicate dequeue
|
|
271
|
+
const duplicatedResult = await Promise.race([
|
|
272
|
+
queue.dequeue(),
|
|
273
|
+
new Promise(resolve => setTimeout(resolve, 0, false)),
|
|
274
|
+
]) as false | IteratorResult<string, undefined>;
|
|
275
|
+
|
|
276
|
+
// If duplicatedResult is false, it means the race timed out (expected)
|
|
277
|
+
// If it's an IteratorResult, it should not have the same value
|
|
278
|
+
if (duplicatedResult !== false) {
|
|
279
|
+
expect(duplicatedResult.value).not.toBe("delayed item");
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
describe("edge cases and error handling", () => {
|
|
285
|
+
it("enqueue after close test", async () => {
|
|
286
|
+
const queue = new AsyncQueue<number>();
|
|
287
|
+
|
|
288
|
+
queue.close();
|
|
289
|
+
queue.enqueue(1); // Should still work, but dequeue will return done: true
|
|
290
|
+
|
|
291
|
+
const result = await queue.dequeue();
|
|
292
|
+
expect(result.done).toBe(true);
|
|
293
|
+
expect(result.value).toBeUndefined();
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("multiple close calls test", async () => {
|
|
297
|
+
const queue = new AsyncQueue<number>();
|
|
298
|
+
|
|
299
|
+
queue.close();
|
|
300
|
+
queue.close(); // Second close should not cause issues
|
|
301
|
+
|
|
302
|
+
const result = await queue.dequeue();
|
|
303
|
+
expect(result.done).toBe(true);
|
|
304
|
+
expect(result.value).toBeUndefined();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("waitUntilEmpty with multiple calls test", async () => {
|
|
308
|
+
const queue = new AsyncQueue<number>();
|
|
309
|
+
|
|
310
|
+
queue.enqueue(1);
|
|
311
|
+
|
|
312
|
+
// Create multiple waitUntilEmpty promises
|
|
313
|
+
const waitPromises = [queue.waitUntilEmpty(), queue.waitUntilEmpty()];
|
|
314
|
+
|
|
315
|
+
// Dequeue the value
|
|
316
|
+
await queue.dequeue();
|
|
317
|
+
|
|
318
|
+
// All promises should resolve
|
|
319
|
+
await Promise.all(waitPromises);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("concurrent enqueue and dequeue test", async () => {
|
|
323
|
+
const queue = new AsyncQueue<number>();
|
|
324
|
+
const results: number[] = [];
|
|
325
|
+
|
|
326
|
+
// Start multiple dequeue operations
|
|
327
|
+
const dequeuePromises = Array.from({ length: 5 }).fill(0).map(async () => queue.dequeue());
|
|
328
|
+
|
|
329
|
+
// Enqueue values with small delays
|
|
330
|
+
for (let i = 0; i < 5; i++) {
|
|
331
|
+
setTimeout(() => {
|
|
332
|
+
queue.enqueue(i);
|
|
333
|
+
}, i * 10);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Wait for all dequeue operations to complete
|
|
337
|
+
const dequeuedResults = await Promise.all(dequeuePromises);
|
|
338
|
+
|
|
339
|
+
// Collect values
|
|
340
|
+
dequeuedResults.forEach((result) => {
|
|
341
|
+
if (result.value !== undefined) {
|
|
342
|
+
results.push(result.value);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// Check that all values were dequeued
|
|
347
|
+
expect(results.length).toBe(5);
|
|
348
|
+
expect(results).toContain(0);
|
|
349
|
+
expect(results).toContain(1);
|
|
350
|
+
expect(results).toContain(2);
|
|
351
|
+
expect(results).toContain(3);
|
|
352
|
+
expect(results).toContain(4);
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
});
|
package/src/utils/AsyncQueue.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
export class AsyncQueueClosedError extends Error {
|
|
2
|
+
constructor(message: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "AsyncQueueClosedError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
1
8
|
export class AsyncQueue<T> {
|
|
2
9
|
private queue: T[] = [];
|
|
3
10
|
private resolvers: ((value: IteratorResult<T, undefined>) => void)[] = [];
|
|
@@ -6,6 +13,11 @@ export class AsyncQueue<T> {
|
|
|
6
13
|
private closed = false;
|
|
7
14
|
|
|
8
15
|
enqueue(item: T) {
|
|
16
|
+
if (this.closed) {
|
|
17
|
+
console.error(new AsyncQueueClosedError("Cannot enqueue item: queue is closed."));
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
9
21
|
this.queue.push(item);
|
|
10
22
|
if (this.resolvers.length > 0) {
|
|
11
23
|
this.resolvers.shift()?.({ value: this.queue.shift()!, done: false });
|
|
@@ -13,16 +25,25 @@ export class AsyncQueue<T> {
|
|
|
13
25
|
}
|
|
14
26
|
|
|
15
27
|
async dequeue(): Promise<IteratorResult<T, undefined>> {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (this.
|
|
21
|
-
|
|
22
|
-
this.emptyResolvers = [];
|
|
28
|
+
const item = (() => {
|
|
29
|
+
if (!this.isEmpty()) {
|
|
30
|
+
return { value: this.queue.shift()!, done: false } as const;
|
|
31
|
+
}
|
|
32
|
+
if (this.isClosed()) {
|
|
33
|
+
return { value: undefined, done: true } as const;
|
|
23
34
|
}
|
|
24
|
-
return
|
|
35
|
+
return null;
|
|
36
|
+
})();
|
|
37
|
+
|
|
38
|
+
if (this.isEmpty() && this.emptyResolvers.length !== 0) {
|
|
39
|
+
this.emptyResolvers.forEach(resolve => resolve());
|
|
40
|
+
this.emptyResolvers = [];
|
|
25
41
|
}
|
|
42
|
+
|
|
43
|
+
if (item !== null) {
|
|
44
|
+
return item;
|
|
45
|
+
}
|
|
46
|
+
|
|
26
47
|
return new Promise(resolve => this.resolvers.push(resolve));
|
|
27
48
|
}
|
|
28
49
|
|
|
@@ -46,6 +67,13 @@ export class AsyncQueue<T> {
|
|
|
46
67
|
this.closeResolvers.forEach(resolve => resolve());
|
|
47
68
|
}
|
|
48
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Wait until the queue is empty
|
|
72
|
+
*
|
|
73
|
+
* if the queue is closed, it will not resolve promise
|
|
74
|
+
* this function only check the queue is empty
|
|
75
|
+
* @returns A promise that resolves when the queue is empty
|
|
76
|
+
*/
|
|
49
77
|
async waitUntilEmpty() {
|
|
50
78
|
if (this.isEmpty()) {
|
|
51
79
|
return Promise.resolve();
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { MPSC } from "./MPSC";
|
|
2
|
+
|
|
3
|
+
describe("the MPSC", () => {
|
|
4
|
+
describe("basic functionality", () => {
|
|
5
|
+
it("basic MPSC functionality test", async () => {
|
|
6
|
+
const mpsc = new MPSC<number>();
|
|
7
|
+
const reader = mpsc.consumer.getReader();
|
|
8
|
+
|
|
9
|
+
// Produce values
|
|
10
|
+
mpsc.produce(10);
|
|
11
|
+
mpsc.produce(20);
|
|
12
|
+
mpsc.produce(30);
|
|
13
|
+
|
|
14
|
+
const read1 = await reader.read();
|
|
15
|
+
const read2 = await reader.read();
|
|
16
|
+
const read3 = await reader.read();
|
|
17
|
+
|
|
18
|
+
expect(read1.value).toBe(10);
|
|
19
|
+
expect(read1.done).toBe(false);
|
|
20
|
+
expect(read2.value).toBe(20);
|
|
21
|
+
expect(read2.done).toBe(false);
|
|
22
|
+
expect(read3.value).toBe(30);
|
|
23
|
+
expect(read3.done).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("close functionality test", async () => {
|
|
27
|
+
const mpsc = new MPSC<number>();
|
|
28
|
+
const reader = mpsc.consumer.getReader();
|
|
29
|
+
|
|
30
|
+
mpsc.produce(10);
|
|
31
|
+
mpsc.close();
|
|
32
|
+
|
|
33
|
+
await reader.read();
|
|
34
|
+
|
|
35
|
+
expect(mpsc.done()).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("multiple producers scenario test", async () => {
|
|
39
|
+
const multiMpsc = new MPSC<string>();
|
|
40
|
+
const multiReader = multiMpsc.consumer.getReader();
|
|
41
|
+
|
|
42
|
+
// Simulate multiple producers
|
|
43
|
+
for (let i = 1; i <= 5; i++) {
|
|
44
|
+
multiMpsc.produce(`producer-${i}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Read all values
|
|
48
|
+
const multiResults: string[] = [];
|
|
49
|
+
for (let i = 0; i < 5; i++) {
|
|
50
|
+
const { value } = await multiReader.read();
|
|
51
|
+
if (value != null) {
|
|
52
|
+
multiResults.push(value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
expect(multiResults.length).toBe(5);
|
|
57
|
+
expect(multiResults).toContain("producer-1");
|
|
58
|
+
expect(multiResults).toContain("producer-2");
|
|
59
|
+
expect(multiResults).toContain("producer-3");
|
|
60
|
+
expect(multiResults).toContain("producer-4");
|
|
61
|
+
expect(multiResults).toContain("producer-5");
|
|
62
|
+
|
|
63
|
+
multiMpsc.close();
|
|
64
|
+
|
|
65
|
+
const multiAfterClose = await multiReader.read();
|
|
66
|
+
expect(multiAfterClose.done).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("reading before producing test", async () => {
|
|
70
|
+
const delayMpsc = new MPSC<number>();
|
|
71
|
+
const delayReader = delayMpsc.consumer.getReader();
|
|
72
|
+
|
|
73
|
+
// Start reading before producing
|
|
74
|
+
const readPromise = delayReader.read();
|
|
75
|
+
|
|
76
|
+
// Produce after a small delay
|
|
77
|
+
setTimeout(() => {
|
|
78
|
+
delayMpsc.produce(42);
|
|
79
|
+
}, 10);
|
|
80
|
+
|
|
81
|
+
const delayResult = await readPromise;
|
|
82
|
+
expect(delayResult.value).toBe(42);
|
|
83
|
+
expect(delayResult.done).toBe(false);
|
|
84
|
+
|
|
85
|
+
delayMpsc.close();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("producer-first, then consumer test", async () => {
|
|
89
|
+
const laterMpsc = new MPSC<string>();
|
|
90
|
+
|
|
91
|
+
// Produce values first
|
|
92
|
+
laterMpsc.produce("first");
|
|
93
|
+
laterMpsc.produce("second");
|
|
94
|
+
laterMpsc.produce("third");
|
|
95
|
+
|
|
96
|
+
// Then get reader and read
|
|
97
|
+
const laterReader = laterMpsc.consumer.getReader();
|
|
98
|
+
const laterResult1 = await laterReader.read();
|
|
99
|
+
const laterResult2 = await laterReader.read();
|
|
100
|
+
const laterResult3 = await laterReader.read();
|
|
101
|
+
|
|
102
|
+
expect(laterResult1.value).toBe("first");
|
|
103
|
+
expect(laterResult1.done).toBe(false);
|
|
104
|
+
expect(laterResult2.value).toBe("second");
|
|
105
|
+
expect(laterResult2.done).toBe(false);
|
|
106
|
+
expect(laterResult3.value).toBe("third");
|
|
107
|
+
expect(laterResult3.done).toBe(false);
|
|
108
|
+
|
|
109
|
+
laterMpsc.close();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("waitClosed functionality", () => {
|
|
114
|
+
it("basic waitClosed functionality test", async () => {
|
|
115
|
+
const mpsc = new MPSC<string>();
|
|
116
|
+
const reader = mpsc.consumer.getReader();
|
|
117
|
+
|
|
118
|
+
mpsc.produce("message");
|
|
119
|
+
const readResult = await reader.read();
|
|
120
|
+
|
|
121
|
+
expect(readResult.value).toBe("message");
|
|
122
|
+
expect(readResult.done).toBe(false);
|
|
123
|
+
|
|
124
|
+
// Call waitClose then execute close
|
|
125
|
+
const closePromise = mpsc.waitClosed();
|
|
126
|
+
mpsc.close();
|
|
127
|
+
await closePromise; // Should resolve when closed
|
|
128
|
+
|
|
129
|
+
const afterClose = await reader.read();
|
|
130
|
+
expect(afterClose.done).toBe(true);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("call waitClose on already closed queue test", async () => {
|
|
134
|
+
const mpsc2 = new MPSC<number>();
|
|
135
|
+
|
|
136
|
+
mpsc2.close(); // Close first
|
|
137
|
+
const alreadyClosedPromise = mpsc2.waitClosed();
|
|
138
|
+
// Should resolve immediately since already closed
|
|
139
|
+
await alreadyClosedPromise;
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("multiple waitClose calls test", async () => {
|
|
143
|
+
const mpsc3 = new MPSC<number>();
|
|
144
|
+
|
|
145
|
+
// Create multiple waitClose promises
|
|
146
|
+
const waitPromises = [mpsc3.waitClosed(), mpsc3.waitClosed(), mpsc3.waitClosed()];
|
|
147
|
+
|
|
148
|
+
// All promises should resolve
|
|
149
|
+
mpsc3.close();
|
|
150
|
+
await Promise.all(waitPromises);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("verify waitClose doesn't block other operations", async () => {
|
|
154
|
+
const mpsc4 = new MPSC<string>();
|
|
155
|
+
const reader4 = mpsc4.consumer.getReader();
|
|
156
|
+
|
|
157
|
+
// Call waitClose
|
|
158
|
+
const waitPromise4 = mpsc4.waitClosed();
|
|
159
|
+
|
|
160
|
+
// Check if value production and consumption still work
|
|
161
|
+
mpsc4.produce("first");
|
|
162
|
+
mpsc4.produce("second");
|
|
163
|
+
|
|
164
|
+
const read1 = await reader4.read();
|
|
165
|
+
const read2 = await reader4.read();
|
|
166
|
+
|
|
167
|
+
expect(read1.value).toBe("first");
|
|
168
|
+
expect(read1.done).toBe(false);
|
|
169
|
+
expect(read2.value).toBe("second");
|
|
170
|
+
expect(read2.done).toBe(false);
|
|
171
|
+
|
|
172
|
+
mpsc4.close();
|
|
173
|
+
await waitPromise4;
|
|
174
|
+
|
|
175
|
+
const afterClose4 = await reader4.read();
|
|
176
|
+
expect(afterClose4.done).toBe(true);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("waitClose resolution after async close test", async () => {
|
|
180
|
+
const mpsc5 = new MPSC<number>();
|
|
181
|
+
const reader5 = mpsc5.consumer.getReader();
|
|
182
|
+
|
|
183
|
+
mpsc5.produce(100);
|
|
184
|
+
|
|
185
|
+
// Call waitClose
|
|
186
|
+
const waitPromise5 = mpsc5.waitClosed();
|
|
187
|
+
|
|
188
|
+
// Perform async close
|
|
189
|
+
setTimeout(() => {
|
|
190
|
+
mpsc5.close();
|
|
191
|
+
}, 50);
|
|
192
|
+
|
|
193
|
+
// Wait for waitClose to resolve
|
|
194
|
+
await waitPromise5;
|
|
195
|
+
await reader5.read();
|
|
196
|
+
|
|
197
|
+
const afterClose5 = await reader5.read();
|
|
198
|
+
expect(afterClose5.done).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe("waitUntilEmpty functionality", () => {
|
|
203
|
+
it("waitUntilEmpty test when queue is empty", async () => {
|
|
204
|
+
const mpsc = new MPSC<number>();
|
|
205
|
+
const reader = mpsc.consumer.getReader();
|
|
206
|
+
|
|
207
|
+
// Should resolve immediately since queue is empty
|
|
208
|
+
await mpsc.waitUntilEmpty();
|
|
209
|
+
|
|
210
|
+
mpsc.produce(1);
|
|
211
|
+
const readResult = await reader.read();
|
|
212
|
+
expect(readResult.value).toBe(1);
|
|
213
|
+
expect(readResult.done).toBe(false);
|
|
214
|
+
|
|
215
|
+
mpsc.close();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("waitUntilEmpty test when queue is not empty", async () => {
|
|
219
|
+
const mpsc = new MPSC<number>();
|
|
220
|
+
const reader = mpsc.consumer.getReader();
|
|
221
|
+
|
|
222
|
+
mpsc.produce(1);
|
|
223
|
+
mpsc.produce(2);
|
|
224
|
+
|
|
225
|
+
// waitUntilEmpty should not resolve since queue is not empty
|
|
226
|
+
const waitPromise = mpsc.waitUntilEmpty();
|
|
227
|
+
|
|
228
|
+
// Read first value
|
|
229
|
+
const read1 = await reader.read();
|
|
230
|
+
expect(read1.value).toBe(1);
|
|
231
|
+
|
|
232
|
+
// Read second value
|
|
233
|
+
const read2 = await reader.read();
|
|
234
|
+
expect(read2.value).toBe(2);
|
|
235
|
+
mpsc.close();
|
|
236
|
+
|
|
237
|
+
const read3 = await reader.read();
|
|
238
|
+
expect(read3.done).toBe(true);
|
|
239
|
+
|
|
240
|
+
// Now queue is empty, waitUntilEmpty should resolve
|
|
241
|
+
await waitPromise;
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("waitUntilEmpty test on closed queue", async () => {
|
|
245
|
+
const mpsc = new MPSC<number>();
|
|
246
|
+
const reader = mpsc.consumer.getReader();
|
|
247
|
+
|
|
248
|
+
mpsc.produce(1);
|
|
249
|
+
mpsc.close();
|
|
250
|
+
|
|
251
|
+
// waitUntilEmpty should resolve since queue is closed
|
|
252
|
+
await mpsc.waitUntilEmpty();
|
|
253
|
+
|
|
254
|
+
const readResult = await reader.read();
|
|
255
|
+
expect(readResult.value).toBe(1);
|
|
256
|
+
expect(readResult.done).toBe(false);
|
|
257
|
+
|
|
258
|
+
const afterClose = await reader.read();
|
|
259
|
+
expect(afterClose.done).toBe(true);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
describe("done functionality", () => {
|
|
264
|
+
it("done test on unclosed queue", async () => {
|
|
265
|
+
const mpsc = new MPSC<number>();
|
|
266
|
+
expect(mpsc.done()).toBe(false);
|
|
267
|
+
|
|
268
|
+
mpsc.produce(1);
|
|
269
|
+
expect(mpsc.done()).toBe(false);
|
|
270
|
+
|
|
271
|
+
mpsc.close();
|
|
272
|
+
await mpsc.consumer.getReader().read();
|
|
273
|
+
expect(mpsc.done()).toBe(true);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
});
|