Skip to content

Commit df25ea8

Browse files
authored
Added a SingleBuffer test with SharedArrayBuffer (#211)
1 parent 9594207 commit df25ea8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

tests/backend/single-buffer.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test, suite } from 'node:test';
22
import { fs, mount, resolveMountConfig, SingleBuffer, umount } from '../../dist/index.js';
33
import assert from 'node:assert';
4+
import { Worker } from 'worker_threads';
45

56
await suite('SingleBuffer', () => {
67
test('should be able to restore filesystem (with same metadata) from original buffer', async () => {
@@ -21,4 +22,27 @@ await suite('SingleBuffer', () => {
2122

2223
assert.deepEqual(snapshotStats, stats);
2324
});
25+
26+
test('should support SharedArrayBuffer across threads', async () => {
27+
const sharedBuffer = new SharedArrayBuffer(0x100000);
28+
29+
umount('/');
30+
const writable = await resolveMountConfig({ backend: SingleBuffer, buffer: sharedBuffer });
31+
mount('/', writable);
32+
33+
const worker = new Worker(import.meta.dirname + '/single-buffer.worker.js', { workerData: sharedBuffer });
34+
35+
// Pause while we wait for the worker to emit the 'continue' message, which
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+
});
41+
});
42+
43+
worker.terminate();
44+
worker.unref();
45+
46+
assert(fs.existsSync('/worker-file.ts'));
47+
});
2448
});

tests/backend/single-buffer.worker.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { parentPort, workerData } from 'node:worker_threads';
2+
import { configure, SingleBuffer, fs } from '../../dist/index.js';
3+
4+
await configure({
5+
mounts: {
6+
'/': {
7+
backend: SingleBuffer,
8+
buffer: workerData,
9+
},
10+
},
11+
});
12+
13+
fs.writeFileSync('/worker-file.ts', 'console.log("this file was created by the worker")', 'utf-8');
14+
15+
parentPort.postMessage('continue');

0 commit comments

Comments
 (0)