|
1 |
| -import { test, suite } from 'node:test'; |
2 |
| -import { fs, mount, resolveMountConfig, SingleBuffer, umount } from '../../dist/index.js'; |
3 | 1 | import assert from 'node:assert';
|
| 2 | +import { suite, test } from 'node:test'; |
4 | 3 | import { Worker } from 'worker_threads';
|
| 4 | +import { fs, mount, resolveMountConfig, SingleBuffer } from '../../dist/index.js'; |
| 5 | +import { setupLogs } from '../logs.js'; |
| 6 | + |
| 7 | +setupLogs(); |
5 | 8 |
|
6 | 9 | await suite('SingleBuffer', () => {
|
7 |
| - test('should be able to restore filesystem (with same metadata) from original buffer', async () => { |
| 10 | + test('filesystem restoration from original buffer (with same metadata)', async () => { |
8 | 11 | const buffer = new ArrayBuffer(0x100000);
|
9 | 12 |
|
10 |
| - umount('/'); |
11 | 13 | const writable = await resolveMountConfig({ backend: SingleBuffer, buffer });
|
12 |
| - mount('/', writable); |
| 14 | + mount('/mnt', writable); |
13 | 15 |
|
14 |
| - fs.writeFileSync('/example.ts', 'console.log("hello world")', 'utf-8'); |
15 |
| - const stats = fs.statSync('/example.ts'); |
| 16 | + fs.writeFileSync('/mnt/example.ts', 'console.log("hello world")', 'utf-8'); |
| 17 | + const stats = fs.statSync('/mnt/example.ts'); |
16 | 18 |
|
17 |
| - umount('/'); |
18 | 19 | const snapshot = await resolveMountConfig({ backend: SingleBuffer, buffer });
|
19 |
| - mount('/', snapshot); |
| 20 | + mount('/snapshot', snapshot); |
20 | 21 |
|
21 |
| - const snapshotStats = fs.statSync('/example.ts'); |
| 22 | + const snapshotStats = fs.statSync('/snapshot/example.ts'); |
22 | 23 |
|
23 | 24 | assert.deepEqual(snapshotStats, stats);
|
24 | 25 | });
|
25 | 26 |
|
26 |
| - test('should support SharedArrayBuffer across threads', async () => { |
| 27 | + test('cross-thread SharedArrayBuffer', async () => { |
27 | 28 | const sharedBuffer = new SharedArrayBuffer(0x100000);
|
28 | 29 |
|
29 |
| - umount('/'); |
30 | 30 | const writable = await resolveMountConfig({ backend: SingleBuffer, buffer: sharedBuffer });
|
31 |
| - mount('/', writable); |
| 31 | + mount('/shared', writable); |
32 | 32 |
|
33 | 33 | const worker = new Worker(import.meta.dirname + '/single-buffer.worker.js', { workerData: sharedBuffer });
|
34 | 34 |
|
35 | 35 | // Pause while we wait for the worker to emit the 'continue' message, which
|
36 | 36 | // means it has mounted the filesystem and created /worker-file.ts
|
37 |
| - await new Promise<void>(resolve => { |
38 |
| - worker.on('message', message => { |
39 |
| - if (message === 'continue') resolve(); |
40 |
| - }); |
| 37 | + const { promise, resolve, reject } = Promise.withResolvers<void>(); |
| 38 | + |
| 39 | + setTimeout(reject, 1000); |
| 40 | + worker.on('message', message => { |
| 41 | + if (message === 'continue') resolve(); |
41 | 42 | });
|
42 | 43 |
|
43 |
| - worker.terminate(); |
| 44 | + await promise; |
| 45 | + |
| 46 | + await worker.terminate(); |
44 | 47 | worker.unref();
|
45 | 48 |
|
46 |
| - assert(fs.existsSync('/worker-file.ts')); |
| 49 | + assert(fs.existsSync('/shared/worker-file.ts')); |
47 | 50 | });
|
48 | 51 | });
|
0 commit comments