From a6f7f6e40020e5b287d95ab25c96b4a0796f0735 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Sun, 17 May 2026 13:42:41 -0700 Subject: [PATCH] test: make Brotli 16GB test wait for backpressure Wait for the Brotli decoder to fill its readable buffer before checking that decompression stops at the high water mark. This avoids racing the fixed timeout against libuv worker-pool scheduling. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 --- test/parallel/test-zlib-brotli-16GB.js | 33 +++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-zlib-brotli-16GB.js b/test/parallel/test-zlib-brotli-16GB.js index 7bd5a137f908fd..ace8bfaf0a09cb 100644 --- a/test/parallel/test-zlib-brotli-16GB.js +++ b/test/parallel/test-zlib-brotli-16GB.js @@ -15,9 +15,30 @@ const buf = Buffer.from(content, 'hex'); const decoder = createBrotliDecompress(); decoder.end(buf); -// We need to wait to verify that the libuv thread pool had time -// to process the data and the buffer is not empty. -setTimeout(common.mustCall(() => { - // There is only one chunk in the buffer - assert.strictEqual(decoder._readableState.buffer.length, getDefaultHighWaterMark() / (16 * 1024)); -}), common.platformTimeout(500)); +const expectedBufferedChunks = getDefaultHighWaterMark() / (16 * 1024); + +const timeout = setTimeout(() => { + assert.fail(`Timed out waiting for Brotli output. ` + + `Buffered ${decoder._readableState.buffer.length} chunks.`); +}, common.platformTimeout(5000)); + +function waitForBackpressure() { + const bufferedChunks = decoder._readableState.buffer.length; + + assert.ok(bufferedChunks <= expectedBufferedChunks); + + if (bufferedChunks < expectedBufferedChunks) { + setImmediate(waitForBackpressure); + return; + } + + clearTimeout(timeout); + + // Verify that decompression stopped once the readable buffer filled up. + setTimeout(common.mustCall(() => { + assert.strictEqual(decoder._readableState.buffer.length, + expectedBufferedChunks); + }), common.platformTimeout(500)); +} + +waitForBackpressure();