Skip to content

Commit 1bfb7ac

Browse files
committed
Clean up tests
1 parent df25ea8 commit 1bfb7ac

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

tests/backend/single-buffer.test.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,51 @@
1-
import { test, suite } from 'node:test';
2-
import { fs, mount, resolveMountConfig, SingleBuffer, umount } from '../../dist/index.js';
31
import assert from 'node:assert';
2+
import { suite, test } from 'node:test';
43
import { Worker } from 'worker_threads';
4+
import { fs, mount, resolveMountConfig, SingleBuffer } from '../../dist/index.js';
5+
import { setupLogs } from '../logs.js';
6+
7+
setupLogs();
58

69
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 () => {
811
const buffer = new ArrayBuffer(0x100000);
912

10-
umount('/');
1113
const writable = await resolveMountConfig({ backend: SingleBuffer, buffer });
12-
mount('/', writable);
14+
mount('/mnt', writable);
1315

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');
1618

17-
umount('/');
1819
const snapshot = await resolveMountConfig({ backend: SingleBuffer, buffer });
19-
mount('/', snapshot);
20+
mount('/snapshot', snapshot);
2021

21-
const snapshotStats = fs.statSync('/example.ts');
22+
const snapshotStats = fs.statSync('/snapshot/example.ts');
2223

2324
assert.deepEqual(snapshotStats, stats);
2425
});
2526

26-
test('should support SharedArrayBuffer across threads', async () => {
27+
test('cross-thread SharedArrayBuffer', async () => {
2728
const sharedBuffer = new SharedArrayBuffer(0x100000);
2829

29-
umount('/');
3030
const writable = await resolveMountConfig({ backend: SingleBuffer, buffer: sharedBuffer });
31-
mount('/', writable);
31+
mount('/shared', writable);
3232

3333
const worker = new Worker(import.meta.dirname + '/single-buffer.worker.js', { workerData: sharedBuffer });
3434

3535
// Pause while we wait for the worker to emit the 'continue' message, which
3636
// 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();
4142
});
4243

43-
worker.terminate();
44+
await promise;
45+
46+
await worker.terminate();
4447
worker.unref();
4548

46-
assert(fs.existsSync('/worker-file.ts'));
49+
assert(fs.existsSync('/shared/worker-file.ts'));
4750
});
4851
});

tests/backend/single-buffer.worker.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { parentPort, workerData } from 'node:worker_threads';
2-
import { configure, SingleBuffer, fs } from '../../dist/index.js';
2+
import { configureSingle, fs, SingleBuffer } from '../../dist/index.js';
33

4-
await configure({
5-
mounts: {
6-
'/': {
7-
backend: SingleBuffer,
8-
buffer: workerData,
9-
},
10-
},
4+
await configureSingle({
5+
backend: SingleBuffer,
6+
buffer: workerData,
117
});
128

139
fs.writeFileSync('/worker-file.ts', 'console.log("this file was created by the worker")', 'utf-8');

0 commit comments

Comments
 (0)