From ec5b67f96ca975e10eb5c66b78550f10132a0fdf Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sat, 25 Jan 2025 10:39:41 +0100 Subject: [PATCH] test: fix invalid common.mustSucceed() usage By its own nature, the function returned by `common.mustSucceed()` cannot be used as a listener for `'error'` events. Write errors like `read ECONNRESET` or `write EPIPE`, should be ignored because the socket might be closed by the other peer while the request is sent. Refs: https://github.com/nodejs/node/commit/3caa2c1a005652fdb3e8 --- ...-server-headers-timeout-delayed-headers.js | 12 ++++++---- ...ver-headers-timeout-interrupted-headers.js | 12 ++++++---- ...ttp-server-request-timeout-delayed-body.js | 24 ++++++++++--------- ...-server-request-timeout-delayed-headers.js | 12 ++++++---- ...server-request-timeout-interrupted-body.js | 12 ++++++---- ...ver-request-timeout-interrupted-headers.js | 12 ++++++---- 6 files changed, 48 insertions(+), 36 deletions(-) diff --git a/test/parallel/test-http-server-headers-timeout-delayed-headers.js b/test/parallel/test-http-server-headers-timeout-delayed-headers.js index af4a5df4b7f530..844dfebdd8c5de 100644 --- a/test/parallel/test-http-server-headers-timeout-delayed-headers.js +++ b/test/parallel/test-http-server-headers-timeout-delayed-headers.js @@ -38,16 +38,18 @@ server.listen(0, common.mustCall(() => { response += chunk; })); - const errOrEnd = common.mustSucceed(function(err) { + client.on('error', () => { + // Ignore errors like 'write EPIPE' that might occur while the request is + // sent. + }); + + client.on('close', common.mustCall(() => { assert.strictEqual( response, 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' ); server.close(); - }); - - client.on('end', errOrEnd); - client.on('error', errOrEnd); + })); client.resume(); diff --git a/test/parallel/test-http-server-headers-timeout-interrupted-headers.js b/test/parallel/test-http-server-headers-timeout-interrupted-headers.js index 9d25940ec65cd1..790e98499f9529 100644 --- a/test/parallel/test-http-server-headers-timeout-interrupted-headers.js +++ b/test/parallel/test-http-server-headers-timeout-interrupted-headers.js @@ -38,16 +38,18 @@ server.listen(0, common.mustCall(() => { response += chunk; })); - const errOrEnd = common.mustSucceed(function(err) { + client.on('error', () => { + // Ignore errors like 'write EPIPE' that might occur while the request is + // sent. + }); + + client.on('close', common.mustCall(() => { assert.strictEqual( response, 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' ); server.close(); - }); - - client.on('end', errOrEnd); - client.on('error', errOrEnd); + })); client.resume(); client.write('GET / HTTP/1.1\r\n'); diff --git a/test/parallel/test-http-server-request-timeout-delayed-body.js b/test/parallel/test-http-server-request-timeout-delayed-body.js index fdfbbb2f4cb4e8..053b4f849b6191 100644 --- a/test/parallel/test-http-server-request-timeout-delayed-body.js +++ b/test/parallel/test-http-server-request-timeout-delayed-body.js @@ -45,6 +45,19 @@ server.listen(0, common.mustCall(() => { response += chunk; })); + client.on('error', () => { + // Ignore errors like 'write EPIPE' that might occur while the request is + // sent. + }); + + client.on('close', common.mustCall(() => { + assert.strictEqual( + response, + 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' + ); + server.close(); + })); + client.resume(); client.write('POST / HTTP/1.1\r\n'); client.write('Host: example.com\r\n'); @@ -57,15 +70,4 @@ server.listen(0, common.mustCall(() => { client.write('12345678901234567890\r\n\r\n'); }, common.platformTimeout(requestTimeout * 2)).unref(); }); - - const errOrEnd = common.mustSucceed(function(err) { - assert.strictEqual( - response, - 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' - ); - server.close(); - }); - - client.on('end', errOrEnd); - client.on('error', errOrEnd); })); diff --git a/test/parallel/test-http-server-request-timeout-delayed-headers.js b/test/parallel/test-http-server-request-timeout-delayed-headers.js index 304a78b96a77d3..5176adc8e5a249 100644 --- a/test/parallel/test-http-server-request-timeout-delayed-headers.js +++ b/test/parallel/test-http-server-request-timeout-delayed-headers.js @@ -33,16 +33,18 @@ server.listen(0, common.mustCall(() => { response += chunk; })); - const errOrEnd = common.mustSucceed(function(err) { + client.on('error', () => { + // Ignore errors like 'write EPIPE' that might occur while the request is + // sent. + }); + + client.on('close', common.mustCall(() => { assert.strictEqual( response, 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' ); server.close(); - }); - - client.on('end', errOrEnd); - client.on('error', errOrEnd); + })); client.resume(); diff --git a/test/parallel/test-http-server-request-timeout-interrupted-body.js b/test/parallel/test-http-server-request-timeout-interrupted-body.js index 82c2f3171736a6..02d81d7bb7d8be 100644 --- a/test/parallel/test-http-server-request-timeout-interrupted-body.js +++ b/test/parallel/test-http-server-request-timeout-interrupted-body.js @@ -45,16 +45,18 @@ server.listen(0, common.mustCall(() => { response += chunk; })); - const errOrEnd = common.mustSucceed(function(err) { + client.on('error', () => { + // Ignore errors like 'write EPIPE' that might occur while the request is + // sent. + }); + + client.on('close', common.mustCall(() => { assert.strictEqual( response, 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' ); server.close(); - }); - - client.on('error', errOrEnd); - client.on('end', errOrEnd); + })); client.resume(); client.write('POST / HTTP/1.1\r\n'); diff --git a/test/parallel/test-http-server-request-timeout-interrupted-headers.js b/test/parallel/test-http-server-request-timeout-interrupted-headers.js index eb9914dad1f56a..856f45d8e117e4 100644 --- a/test/parallel/test-http-server-request-timeout-interrupted-headers.js +++ b/test/parallel/test-http-server-request-timeout-interrupted-headers.js @@ -33,16 +33,18 @@ server.listen(0, common.mustCall(() => { response += chunk; })); - const errOrEnd = common.mustSucceed(function(err) { + client.on('error', () => { + // Ignore errors like 'write EPIPE' that might occur while the request is + // sent. + }); + + client.on('close', common.mustCall(() => { assert.strictEqual( response, 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' ); server.close(); - }); - - client.on('end', errOrEnd); - client.on('error', errOrEnd); + })); client.resume(); client.write('GET / HTTP/1.1\r\n');