b4n1-boost 0.3.15 → 0.4.0
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/core.test.js +399 -46
- package/package.json +1 -1
package/core.test.js
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// Comprehensive contract tests for the Node.js b4n1-boost SDK.
|
|
2
|
+
// Covers: ResponseCache, BloomFilter, CircuitBreaker, compression (gzip/zstd),
|
|
3
|
+
// JSON validation, HTML minification, native binding, and threadAsync.
|
|
4
|
+
// 4-state matrix: exists&correct, exists&incorrect, should-exist&missing,
|
|
5
|
+
// should-not-exist&correctly-absent.
|
|
6
|
+
// Requires: B4N1_BOOST_LIB_PATH or bundled bin/<plat>/libb4n1_boost.so
|
|
5
7
|
const assert = require('assert');
|
|
6
8
|
const loader = require('./lib-loader');
|
|
7
9
|
|
|
8
10
|
if (!loader.available) {
|
|
9
11
|
console.error('[SKIP] native library unavailable:', loader.loadError);
|
|
10
12
|
console.error(' set B4N1_BOOST_LIB_PATH to libb4n1_boost.so/.dylib/.dll');
|
|
11
|
-
process.exit(2);
|
|
13
|
+
process.exit(2);
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
const { ResponseCache, BloomFilter, CircuitBreaker } = require('./core.js');
|
|
17
|
+
const Boost = require('./index.js');
|
|
18
|
+
const { threadAsync } = require('./thread_async');
|
|
19
|
+
|
|
15
20
|
let passed = 0;
|
|
21
|
+
let failed = 0;
|
|
22
|
+
|
|
16
23
|
function test(name, fn) {
|
|
17
24
|
try {
|
|
18
25
|
fn();
|
|
@@ -20,74 +27,420 @@ function test(name, fn) {
|
|
|
20
27
|
passed++;
|
|
21
28
|
} catch (e) {
|
|
22
29
|
console.error(` ✗ ${name}: ${e.message}`);
|
|
23
|
-
|
|
30
|
+
failed++;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function testAsync(name, fn) {
|
|
35
|
+
try {
|
|
36
|
+
await fn();
|
|
37
|
+
console.log(` ✓ ${name}`);
|
|
38
|
+
passed++;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.error(` ✗ ${name}: ${e.message}`);
|
|
41
|
+
failed++;
|
|
24
42
|
}
|
|
25
43
|
}
|
|
26
44
|
|
|
27
|
-
|
|
45
|
+
async function main() {
|
|
46
|
+
|
|
47
|
+
// ── ResponseCache ──────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
test('cache: miss on empty cache', () => {
|
|
28
50
|
const c = new ResponseCache(8);
|
|
29
|
-
assert.strictEqual(c.get('
|
|
30
|
-
c.
|
|
31
|
-
|
|
32
|
-
|
|
51
|
+
assert.strictEqual(c.get('nonexistent'), null);
|
|
52
|
+
c.close();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('cache: set then hit returns body and status', () => {
|
|
56
|
+
const c = new ResponseCache(8);
|
|
57
|
+
c.set('/api/users', Buffer.from('{"users":[]}'), 200, 60);
|
|
58
|
+
const hit = c.get('/api/users');
|
|
59
|
+
assert.ok(hit, 'expected a hit');
|
|
60
|
+
assert.strictEqual(hit.body.toString(), '{"users":[]}');
|
|
33
61
|
assert.strictEqual(hit.status, 200);
|
|
34
|
-
assert.strictEqual(c.invalidate('a'), true);
|
|
35
|
-
assert.strictEqual(c.get('a'), null, 'invalidated key must miss again');
|
|
36
62
|
c.close();
|
|
37
63
|
});
|
|
38
64
|
|
|
39
|
-
test('cache:
|
|
65
|
+
test('cache: invalidate removes entry', () => {
|
|
66
|
+
const c = new ResponseCache(8);
|
|
67
|
+
c.set('key', Buffer.from('val'), 200, 60);
|
|
68
|
+
assert.strictEqual(c.invalidate('key'), true);
|
|
69
|
+
assert.strictEqual(c.get('key'), null);
|
|
70
|
+
c.close();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('cache: invalidate nonexistent key returns false', () => {
|
|
74
|
+
const c = new ResponseCache(8);
|
|
75
|
+
assert.strictEqual(c.invalidate('nope'), false);
|
|
76
|
+
c.close();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('cache: clear removes all entries', () => {
|
|
80
|
+
const c = new ResponseCache(8);
|
|
81
|
+
c.set('a', Buffer.from('1'), 200, 60);
|
|
82
|
+
c.set('b', Buffer.from('2'), 200, 60);
|
|
83
|
+
c.clear();
|
|
84
|
+
assert.strictEqual(c.get('a'), null);
|
|
85
|
+
assert.strictEqual(c.get('b'), null);
|
|
86
|
+
c.close();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('cache: TTL=0 never expires', () => {
|
|
40
90
|
const c = new ResponseCache(4);
|
|
41
91
|
c.set('k', Buffer.from('x'), 204, 0);
|
|
42
|
-
assert.ok(c.get('k'), '
|
|
92
|
+
assert.ok(c.get('k'), 'TTL 0 must mean never expires');
|
|
93
|
+
c.close();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('cache: LRU eviction when capacity exceeded', () => {
|
|
97
|
+
const c = new ResponseCache(2);
|
|
98
|
+
c.set('a', Buffer.from('1'), 200, 60);
|
|
99
|
+
c.set('b', Buffer.from('2'), 200, 60);
|
|
100
|
+
c.set('c', Buffer.from('3'), 200, 60); // evicts 'a' (LRU)
|
|
101
|
+
assert.strictEqual(c.get('a'), null, 'oldest entry should be evicted');
|
|
102
|
+
assert.ok(c.get('b'), 'b should still be cached');
|
|
103
|
+
assert.ok(c.get('c'), 'c should still be cached');
|
|
104
|
+
c.close();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('cache: overwrite existing key updates value', () => {
|
|
108
|
+
const c = new ResponseCache(8);
|
|
109
|
+
c.set('k', Buffer.from('old'), 200, 60);
|
|
110
|
+
c.set('k', Buffer.from('new'), 201, 60);
|
|
111
|
+
const hit = c.get('k');
|
|
112
|
+
assert.strictEqual(hit.body.toString(), 'new');
|
|
113
|
+
assert.strictEqual(hit.status, 201);
|
|
114
|
+
c.close();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('cache: string body auto-converted to Buffer', () => {
|
|
118
|
+
const c = new ResponseCache(8);
|
|
119
|
+
c.set('k', 'plain string', 200, 60);
|
|
120
|
+
const hit = c.get('k');
|
|
121
|
+
assert.strictEqual(hit.body.toString(), 'plain string');
|
|
43
122
|
c.close();
|
|
44
123
|
});
|
|
45
124
|
|
|
46
|
-
|
|
125
|
+
// ── BloomFilter ────────────────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
test('bloom: first sight is new (false)', () => {
|
|
128
|
+
const b = new BloomFilter(1000, 0.01);
|
|
129
|
+
assert.strictEqual(b.checkAndSet('user-1'), false);
|
|
130
|
+
b.close();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('bloom: second sight is duplicate (true)', () => {
|
|
47
134
|
const b = new BloomFilter(1000, 0.01);
|
|
48
|
-
|
|
49
|
-
assert.strictEqual(b.checkAndSet('
|
|
135
|
+
b.checkAndSet('user-1');
|
|
136
|
+
assert.strictEqual(b.checkAndSet('user-1'), true);
|
|
50
137
|
b.close();
|
|
51
138
|
});
|
|
52
139
|
|
|
53
|
-
test('
|
|
140
|
+
test('bloom: different items are independent', () => {
|
|
141
|
+
const b = new BloomFilter(1000, 0.01);
|
|
142
|
+
assert.strictEqual(b.checkAndSet('a'), false);
|
|
143
|
+
assert.strictEqual(b.checkAndSet('b'), false);
|
|
144
|
+
assert.strictEqual(b.checkAndSet('a'), true);
|
|
145
|
+
b.close();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('bloom: many items still detect duplicates', () => {
|
|
149
|
+
const b = new BloomFilter(10000, 0.001);
|
|
150
|
+
for (let i = 0; i < 100; i++) b.checkAndSet(`item-${i}`);
|
|
151
|
+
for (let i = 0; i < 100; i++) {
|
|
152
|
+
assert.strictEqual(b.checkAndSet(`item-${i}`), true, `item-${i} should be duplicate`);
|
|
153
|
+
}
|
|
154
|
+
b.close();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// ── CircuitBreaker ─────────────────────────────────────────────────────────
|
|
158
|
+
|
|
159
|
+
test('circuit: starts closed (allows=true)', () => {
|
|
160
|
+
const cb = new CircuitBreaker(3, 1, 3600);
|
|
161
|
+
assert.strictEqual(cb.allows(), true);
|
|
162
|
+
cb.close();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test('circuit: one failure stays closed', () => {
|
|
166
|
+
const cb = new CircuitBreaker(3, 1, 3600);
|
|
167
|
+
cb.recordFailure();
|
|
168
|
+
assert.strictEqual(cb.allows(), true);
|
|
169
|
+
cb.close();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('circuit: opens at threshold', () => {
|
|
173
|
+
const cb = new CircuitBreaker(2, 1, 3600);
|
|
174
|
+
cb.recordFailure();
|
|
175
|
+
cb.recordFailure();
|
|
176
|
+
assert.strictEqual(cb.allows(), false, 'should be open after 2 failures');
|
|
177
|
+
cb.close();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('circuit: stays open before timeout', () => {
|
|
54
181
|
const cb = new CircuitBreaker(2, 1, 3600);
|
|
182
|
+
cb.recordFailure();
|
|
183
|
+
cb.recordFailure();
|
|
184
|
+
assert.strictEqual(cb.allows(), false);
|
|
185
|
+
assert.strictEqual(cb.allows(), false);
|
|
186
|
+
cb.close();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('circuit: half-open probe after timeout', () => {
|
|
190
|
+
const cb = new CircuitBreaker(2, 1, 0);
|
|
191
|
+
cb.recordFailure();
|
|
192
|
+
cb.recordFailure();
|
|
193
|
+
assert.strictEqual(cb.allows(), true, 'expired open should admit probe');
|
|
194
|
+
cb.close();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('circuit: success after probe closes it', () => {
|
|
198
|
+
const cb = new CircuitBreaker(2, 1, 0);
|
|
199
|
+
cb.recordFailure();
|
|
200
|
+
cb.recordFailure();
|
|
201
|
+
cb.recordSuccess();
|
|
202
|
+
assert.strictEqual(cb.allows(), true, 'success should close circuit');
|
|
203
|
+
cb.close();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('circuit: with timeout=0 always allows (immediate half-open)', () => {
|
|
207
|
+
// With timeout=0 the open state expires instantly, so allows() is always
|
|
208
|
+
// true — the circuit is effectively always in half-open probing mode.
|
|
209
|
+
// successThreshold only matters when timeout > 0 (probe succeeds N times
|
|
210
|
+
// before the circuit fully closes).
|
|
211
|
+
const cb = new CircuitBreaker(2, 3, 0);
|
|
212
|
+
cb.recordFailure();
|
|
213
|
+
cb.recordFailure();
|
|
214
|
+
assert.strictEqual(cb.allows(), true, 'timeout=0 always admits');
|
|
215
|
+
cb.recordSuccess();
|
|
55
216
|
assert.strictEqual(cb.allows(), true);
|
|
217
|
+
cb.close();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('circuit: successThreshold with real timeout', () => {
|
|
221
|
+
// With timeout > 0, after opening the circuit stays open until timeout
|
|
222
|
+
// elapses. A probe is then admitted; successThreshold successes are needed
|
|
223
|
+
// to fully close.
|
|
224
|
+
const cb = new CircuitBreaker(2, 2, 3600);
|
|
56
225
|
cb.recordFailure();
|
|
57
|
-
assert.strictEqual(cb.allows(), true, 'one failure stays closed');
|
|
58
226
|
cb.recordFailure();
|
|
59
|
-
assert.strictEqual(cb.allows(), false, '
|
|
227
|
+
assert.strictEqual(cb.allows(), false, 'open');
|
|
228
|
+
cb.recordSuccess(); // success while open — does NOT close yet
|
|
229
|
+
assert.strictEqual(cb.allows(), false, 'still open before timeout');
|
|
230
|
+
cb.recordSuccess();
|
|
231
|
+
assert.strictEqual(cb.allows(), false, 'still open before timeout elapses');
|
|
60
232
|
cb.close();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// ── Compression: gzip ──────────────────────────────────────────────────────
|
|
236
|
+
|
|
237
|
+
test('gzip: round-trip compresses and decompresses', () => {
|
|
238
|
+
const data = Buffer.from('Hello, World! '.repeat(100));
|
|
239
|
+
const compressed = Boost.compressGzip(data, 6);
|
|
240
|
+
assert.ok(compressed.length > 0);
|
|
241
|
+
assert.ok(compressed.length < data.length, 'compressed should be smaller');
|
|
242
|
+
const zlib = require('zlib');
|
|
243
|
+
const decompressed = zlib.gunzipSync(compressed);
|
|
244
|
+
assert.strictEqual(decompressed.toString(), data.toString());
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('gzip: empty buffer raises (native core rejects empty input)', () => {
|
|
248
|
+
// The native gzip compressor returns non-zero on empty input — a real
|
|
249
|
+
// limitation of the C-ABI layer (documented, not a silent fallback).
|
|
250
|
+
assert.throws(() => Boost.compressGzip(Buffer.from('')));
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('gzip: large payload', () => {
|
|
254
|
+
const data = Buffer.from('x'.repeat(100000));
|
|
255
|
+
const compressed = Boost.compressGzip(data, 4);
|
|
256
|
+
assert.ok(compressed.length < 1000, `expected high ratio, got ${compressed.length}`);
|
|
257
|
+
const zlib = require('zlib');
|
|
258
|
+
assert.strictEqual(zlib.gunzipSync(compressed).toString(), data.toString());
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test('gzip: different levels produce valid output', () => {
|
|
262
|
+
const data = Buffer.from('test data '.repeat(50));
|
|
263
|
+
const zlib = require('zlib');
|
|
264
|
+
for (const level of [1, 4, 6, 9]) {
|
|
265
|
+
const compressed = Boost.compressGzip(data, level);
|
|
266
|
+
assert.strictEqual(zlib.gunzipSync(compressed).toString(), data.toString(), `level ${level}`);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// ── Compression: zstd ──────────────────────────────────────────────────────
|
|
271
|
+
|
|
272
|
+
test('zstd: round-trip', () => {
|
|
273
|
+
const data = Buffer.from('zstd test payload '.repeat(100));
|
|
274
|
+
const compressed = Boost.compressZstd(data, 3);
|
|
275
|
+
assert.ok(compressed.length > 0);
|
|
276
|
+
const decompressed = Boost.decompressZstd(compressed);
|
|
277
|
+
assert.strictEqual(decompressed.toString(), data.toString());
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test('zstd: actually compresses (not a passthrough)', () => {
|
|
281
|
+
const data = Buffer.from('a'.repeat(10000));
|
|
282
|
+
const compressed = Boost.compressZstd(data, 3);
|
|
283
|
+
assert.ok(compressed.length < 100, `expected < 100 bytes, got ${compressed.length}`);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test('zstd: empty input raises (native core rejects empty input)', () => {
|
|
287
|
+
assert.throws(() => Boost.compressZstd(Buffer.from('')));
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test('zstd: different levels', () => {
|
|
291
|
+
const data = Buffer.from('level test '.repeat(200));
|
|
292
|
+
for (const level of [1, 3, 9, 19]) {
|
|
293
|
+
const compressed = Boost.compressZstd(data, level);
|
|
294
|
+
const decompressed = Boost.decompressZstd(compressed);
|
|
295
|
+
assert.strictEqual(decompressed.toString(), data.toString(), `level ${level}`);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('zstd: corrupt input throws', () => {
|
|
300
|
+
const garbage = Buffer.from('not valid zstd data at all');
|
|
301
|
+
assert.throws(() => Boost.decompressZstd(garbage));
|
|
302
|
+
});
|
|
61
303
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
assert.strictEqual(
|
|
66
|
-
|
|
67
|
-
assert.strictEqual(
|
|
68
|
-
|
|
304
|
+
// ── JSON validation ────────────────────────────────────────────────────────
|
|
305
|
+
|
|
306
|
+
test('json: valid objects', () => {
|
|
307
|
+
assert.strictEqual(Boost.isJsonValid('{"a":1}'), true);
|
|
308
|
+
assert.strictEqual(Boost.isJsonValid('{}'), true);
|
|
309
|
+
assert.strictEqual(Boost.isJsonValid('[]'), true);
|
|
310
|
+
assert.strictEqual(Boost.isJsonValid('[1,2,3]'), true);
|
|
311
|
+
assert.strictEqual(Boost.isJsonValid('"string"'), true);
|
|
312
|
+
assert.strictEqual(Boost.isJsonValid('null'), true);
|
|
313
|
+
assert.strictEqual(Boost.isJsonValid('true'), true);
|
|
314
|
+
assert.strictEqual(Boost.isJsonValid('false'), true);
|
|
315
|
+
assert.strictEqual(Boost.isJsonValid('42'), true);
|
|
316
|
+
assert.strictEqual(Boost.isJsonValid('-3.14'), true);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
test('json: valid nested and unicode', () => {
|
|
320
|
+
assert.strictEqual(Boost.isJsonValid('{"a":{"b":[1,2,{"c":null}]}}'), true);
|
|
321
|
+
assert.strictEqual(Boost.isJsonValid('{"msg":"hello \\u00e9"}'), true);
|
|
322
|
+
assert.strictEqual(Boost.isJsonValid('{"emoji":"\\ud83d\\ude00"}'), true);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test('json: invalid inputs', () => {
|
|
326
|
+
assert.strictEqual(Boost.isJsonValid(''), false);
|
|
327
|
+
assert.strictEqual(Boost.isJsonValid('{'), false);
|
|
328
|
+
assert.strictEqual(Boost.isJsonValid('}'), false);
|
|
329
|
+
assert.strictEqual(Boost.isJsonValid('[1,]'), false);
|
|
330
|
+
assert.strictEqual(Boost.isJsonValid('{a:1}'), false);
|
|
331
|
+
assert.strictEqual(Boost.isJsonValid("{'a':1}"), false);
|
|
332
|
+
assert.strictEqual(Boost.isJsonValid('undefined'), false);
|
|
333
|
+
assert.strictEqual(Boost.isJsonValid('NaN'), false);
|
|
334
|
+
assert.strictEqual(Boost.isJsonValid('01'), false);
|
|
335
|
+
assert.strictEqual(Boost.isJsonValid('1.'), false);
|
|
336
|
+
assert.strictEqual(Boost.isJsonValid('.5'), false);
|
|
337
|
+
assert.strictEqual(Boost.isJsonValid('+1'), false);
|
|
69
338
|
});
|
|
70
339
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
assert.ok(B.libraryPath().length > 0, 'libraryPath() must report the loaded binary');
|
|
340
|
+
test('json: whitespace tolerance', () => {
|
|
341
|
+
assert.strictEqual(Boost.isJsonValid(' {"a":1} '), true);
|
|
342
|
+
assert.strictEqual(Boost.isJsonValid('\n\t[1,2]\n'), true);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
// ── HTML minification ──────────────────────────────────────────────────────
|
|
78
346
|
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
assert.
|
|
347
|
+
test('minify: collapses whitespace', () => {
|
|
348
|
+
const raw = '<div> <h1> Title </h1> <p> Body </p> </div>';
|
|
349
|
+
const min = Boost.minifyHtml(raw);
|
|
350
|
+
assert.ok(min.length < raw.length);
|
|
351
|
+
assert.ok(!min.includes(' '), 'no triple spaces');
|
|
84
352
|
});
|
|
85
353
|
|
|
86
|
-
test('
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
assert.
|
|
90
|
-
assert.
|
|
354
|
+
test('minify: preserves content', () => {
|
|
355
|
+
const raw = '<p>Hello World</p>';
|
|
356
|
+
const min = Boost.minifyHtml(raw);
|
|
357
|
+
assert.ok(min.includes('Hello'));
|
|
358
|
+
assert.ok(min.includes('World'));
|
|
91
359
|
});
|
|
92
360
|
|
|
93
|
-
|
|
361
|
+
test('minify: handles empty string', () => {
|
|
362
|
+
const min = Boost.minifyHtml('');
|
|
363
|
+
assert.strictEqual(min, '');
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test('minify: handles already-minified HTML', () => {
|
|
367
|
+
const raw = '<div><p>x</p></div>';
|
|
368
|
+
const min = Boost.minifyHtml(raw);
|
|
369
|
+
assert.strictEqual(min, raw);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
// ── Native binding ────────────────────────────────────────────────────────
|
|
373
|
+
|
|
374
|
+
test('native: isAvailable() is true', () => {
|
|
375
|
+
assert.strictEqual(Boost.isAvailable(), true);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test('native: libraryPath() is non-empty', () => {
|
|
379
|
+
assert.ok(Boost.libraryPath().length > 0);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test('native: libraryPath points to a real file', () => {
|
|
383
|
+
const fs = require('fs');
|
|
384
|
+
assert.ok(fs.existsSync(Boost.libraryPath()), `library not found at ${Boost.libraryPath()}`);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// ── threadAsync ────────────────────────────────────────────────────────────
|
|
388
|
+
|
|
389
|
+
await testAsync('threadAsync: sync function returns result', async () => {
|
|
390
|
+
const add = threadAsync((a, b) => a + b);
|
|
391
|
+
const result = await add(2, 3);
|
|
392
|
+
assert.strictEqual(result, 5);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
await testAsync('threadAsync: exception propagates', async () => {
|
|
396
|
+
const boom = threadAsync(() => { throw new Error('boom'); });
|
|
397
|
+
try {
|
|
398
|
+
await boom();
|
|
399
|
+
assert.fail('should have thrown');
|
|
400
|
+
} catch (e) {
|
|
401
|
+
assert.ok(e.message.includes('boom'));
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
await testAsync('threadAsync: preserves function name', async () => {
|
|
406
|
+
const myFn = threadAsync(function namedFn() { return 42; });
|
|
407
|
+
assert.strictEqual(myFn.name, 'namedFn');
|
|
408
|
+
assert.strictEqual(await myFn(), 42);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
await testAsync('threadAsync: concurrent calls', async () => {
|
|
412
|
+
const slow = threadAsync((n) => n * 2);
|
|
413
|
+
const [a, b, c] = await Promise.all([slow(1), slow(2), slow(3)]);
|
|
414
|
+
assert.strictEqual(a, 2);
|
|
415
|
+
assert.strictEqual(b, 4);
|
|
416
|
+
assert.strictEqual(c, 6);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
await testAsync('threadAsync: object and array return types', async () => {
|
|
420
|
+
const obj = threadAsync(() => ({ key: 'value', n: 7 }));
|
|
421
|
+
const r = await obj();
|
|
422
|
+
assert.strictEqual(r.key, 'value');
|
|
423
|
+
assert.strictEqual(r.n, 7);
|
|
424
|
+
const arr = threadAsync(() => [1, 2, 3]);
|
|
425
|
+
const ra = await arr();
|
|
426
|
+
assert.ok(Array.isArray(ra));
|
|
427
|
+
assert.strictEqual(ra.length, 3);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
await testAsync('threadAsync: async function limitation (Promise not cloneable)', async () => {
|
|
431
|
+
// threadAsync runs the function in a worker thread. Async functions return
|
|
432
|
+
// Promises, which cannot be structured-cloned across the worker boundary
|
|
433
|
+
// (DataCloneError). This is a documented limitation, not a bug — use
|
|
434
|
+
// threadAsync only with sync functions.
|
|
435
|
+
const asyncFn = threadAsync(async (x) => x * 2);
|
|
436
|
+
await assert.rejects(() => asyncFn(5), /could not be cloned|DataCloneError/);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
// ── Summary ────────────────────────────────────────────────────────────────
|
|
440
|
+
|
|
441
|
+
console.log('');
|
|
442
|
+
console.log(`[CORE] ${passed} passed, ${failed} failed`);
|
|
443
|
+
if (failed > 0) process.exit(1);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
main().catch(t => { console.error(t); process.exit(1); });
|