|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const Countdown = require('../common/countdown'); |
| 4 | +const assert = require('assert'); |
| 5 | +const { Worker } = require('worker_threads'); |
| 6 | +const { describe, it, mock } = require('node:test'); |
| 7 | + |
| 8 | +describe('Allocating uninitialized ArrayBuffers ...', () => { |
| 9 | + it('...should not affect zero-fill in other threads', () => { |
| 10 | + const w = new Worker(` |
| 11 | + const { parentPort } = require('worker_threads'); |
| 12 | +
|
| 13 | + function post() { |
| 14 | + const uint32array = new Uint32Array(64); |
| 15 | + parentPort.postMessage(uint32array.reduce((a, b) => a + b)); |
| 16 | + } |
| 17 | +
|
| 18 | + setInterval(post, 0); |
| 19 | + `, { eval: true }); |
| 20 | + |
| 21 | + const fn = mock.fn(() => { |
| 22 | + // Continuously allocate memory in the main thread. The allocUnsafe |
| 23 | + // here sets a scope internally that indicates that the memory should |
| 24 | + // not be initialized. While this is happening, the other thread is |
| 25 | + // also allocating buffers that must remain zero-filled. The purpose |
| 26 | + // of this test is to ensure that the scope used to determine whether |
| 27 | + // to zero-fill or not does not impact the other thread. |
| 28 | + setInterval(() => Buffer.allocUnsafe(32 * 1024 * 1024), 0).unref(); |
| 29 | + }); |
| 30 | + |
| 31 | + w.on('online', fn); |
| 32 | + |
| 33 | + const countdown = new Countdown(100, () => { |
| 34 | + w.terminate(); |
| 35 | + assert(fn.mock.calls.length > 0); |
| 36 | + }); |
| 37 | + |
| 38 | + w.on('message', (sum) => { |
| 39 | + assert.strictEqual(sum, 0); |
| 40 | + if (countdown.remaining) countdown.dec(); |
| 41 | + }); |
| 42 | + }); |
| 43 | +}); |
0 commit comments