Skip to content

Commit

Permalink
test(instr-undici): fix TAV failures with Node.js 14 and 16 (#2111)
Browse files Browse the repository at this point in the history
Test all versions (TAV) tests were failing with Node.js 14 and 16
because of subtle behaviour changes in socket end handling in
Node.js and undici for a test that tries a bogus request. This
restores earlier work-around code mistakenly removed in #2085.
  • Loading branch information
trentm authored Apr 18, 2024
1 parent 9f5a867 commit 3621e09
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions plugins/node/instrumentation-undici/test/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ describe('UndiciInstrumentation `undici` tests', function () {
propagation.disable();
mockServer.mockListener(undefined);
mockServer.stop(done);

// Close kept-alive sockets. This can save a 4s keep-alive delay before the
// process exits.
(undici as any).getGlobalDispatcher().close();
});

beforeEach(function () {
Expand Down Expand Up @@ -216,11 +220,27 @@ describe('UndiciInstrumentation `undici` tests', function () {
};

const queryRequestUrl = `${protocol}://${hostname}:${mockServer.port}/?query=test`;
const firstQueryResponse = await undici.request(queryRequestUrl, {
headers,
// @ts-expect-error - method type expects in uppercase
method: 'get',
});
let firstQueryResponse;
try {
firstQueryResponse = await undici.request(queryRequestUrl, {
headers,
// @ts-expect-error - method type expects in uppercase
method: 'get',
});
} catch (err: any) {
// This request is using a bogus HTTP method `get`. If (a) using Node.js
// v14, v16, or early v18.x versions and (b) this request is re-using
// a socket (from an earlier keep-alive request in this test file),
// then Node.js will emit 'end' on the socket. Undici then throws
// `SocketError: other side closed`. Given this is only for old Node.js
// versions and for this rare case of using a bogus HTTP method, we will
// skip out of this test instead of attempting to fully understand it.
assert.strictEqual(err.message, 'other side closed');
this.skip();
}
if (!firstQueryResponse) {
return;
}
await consumeResponseBody(firstQueryResponse.body);

const secondQueryResponse = await undici.request(queryRequestUrl, {
Expand Down

0 comments on commit 3621e09

Please sign in to comment.