◐ Shell
clean mode source ↗

test: add workerdata-sharedarraybuffer test · nodejs/node@792335f

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,32 @@

1+

// Flags: --expose-gc --experimental-worker

2+

'use strict';

3+
4+

const common = require('../common');

5+

const assert = require('assert');

6+

const { Worker } = require('worker_threads');

7+
8+

{

9+

const sharedArrayBuffer = new SharedArrayBuffer(12);

10+

const local = Buffer.from(sharedArrayBuffer);

11+
12+

const w = new Worker(`

13+

const { parentPort, workerData } = require('worker_threads');

14+

const local = Buffer.from(workerData.sharedArrayBuffer);

15+
16+

parentPort.on('message', () => {

17+

local.write('world!', 6);

18+

parentPort.postMessage('written!');

19+

});

20+

`, {

21+

eval: true,

22+

workerData: { sharedArrayBuffer }

23+

});

24+

w.on('message', common.mustCall(() => {

25+

assert.strictEqual(local.toString(), 'Hello world!');

26+

global.gc();

27+

w.terminate();

28+

}));

29+

w.postMessage({});

30+

// This would be a race condition if the memory regions were overlapping

31+

local.write('Hello ');

32+

}