From b8b5d0471112263a13f2a5549a43ab3dc3f230a2 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Fri, 24 Jul 2026 22:51:36 -0700 Subject: [PATCH] test: fix flaky HTTP/2 reset callback check The test assumed that all pending write callbacks would run within two event loop iterations after the stream closed. Callbacks for writes already handed to the socket can run later, causing a flaky assertion. Wait for the stream to close and all expected write callbacks to run before closing the server. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: codex:gpt-5.6-sol --- test/parallel/test-http2-reset-happy-close.js | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-http2-reset-happy-close.js b/test/parallel/test-http2-reset-happy-close.js index d4778de8c81d97..9e2e77aa1d8117 100644 --- a/test/parallel/test-http2-reset-happy-close.js +++ b/test/parallel/test-http2-reset-happy-close.js @@ -43,8 +43,17 @@ function runScenario(rstCode, expectedErrorCode, next) { stream.respond({ ':status': 200 }); let writeCbCount = 0; + let streamClosed = false; + const maybeCloseServer = () => { + if (streamClosed && writeCbCount === N_WRITES) + server.close(next); + }; + const onWrite = common.mustCall(() => { + writeCbCount++; + maybeCloseServer(); + }, N_WRITES); for (let i = 0; i < N_WRITES; i++) { - stream.write(Buffer.alloc(CHUNK_BYTES), () => { writeCbCount++; }); + stream.write(Buffer.alloc(CHUNK_BYTES), onWrite); } stream.on('aborted', common.mustCall(() => { @@ -69,16 +78,11 @@ function runScenario(rstCode, expectedErrorCode, next) { // 'finish' must not have been emitted (writes were aborted). assert.strictEqual(stream.writableFinished, false); assert.strictEqual(stream.destroyed, true); - // The C++ Http2Stream::Destroy SetImmediate flushes any - // still-queued WriteWraps with UV_ECANCELED, which fires the - // remaining `_write` callbacks. Those land *after* 'close'; - // verify the full set has fired by the next two immediates. - setImmediate(common.mustCall(() => setImmediate(common.mustCall(() => { - assert.strictEqual(writeCbCount, N_WRITES, - `all ${N_WRITES} write callbacks must fire ` + - `(got ${writeCbCount})`); - server.close(next); - })))); + // Write callbacks for data already handed to the socket can land + // after 'close'. Wait for all of them rather than assuming a fixed + // number of event loop iterations is sufficient. + streamClosed = true; + maybeCloseServer(); })); })); }));