-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex5.test.js
More file actions
49 lines (36 loc) · 1.29 KB
/
ex5.test.js
File metadata and controls
49 lines (36 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const { toAsyncIterator } = require('./ex5');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
(async () => {
let handler = null;
let unsubCalls = 0;
function subscribe(fn) {
handler = fn;
return () => { unsubCalls++; handler = null; };
}
const it = toAsyncIterator(subscribe, { highWaterMark: 2 });
// push 3 values synchronously -> should not buffer beyond 2
handler(1);
handler(2);
handler(3);
const a = await it.next();
const b = await it.next();
assert(a.value === 1 && b.value === 2, 'must yield values in order');
// third value behavior: depending on implementation it may error or drop;
// enforce that buffer does not exceed HWM by ensuring we don't see 3 here.
const c = await it.next();
assert(c.value !== 3, 'must not deliver beyond highWaterMark without explicit policy');
await it.return();
assert(unsubCalls === 1, 'unsubscribe must be called exactly once on return()');
// pushing after return must not deliver anything
const saved = handler;
if (saved) saved(99);
const done = await it.next();
assert(done.done === true, 'iterator must be done after return');
console.log('ex5 tests passed');
})().catch((err) => {
console.error(err);
process.exit(1);
});