Skip to content

Commit

Permalink
attempt to add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 21, 2023
1 parent 1f07e5f commit 225e24a
Showing 1 changed file with 41 additions and 0 deletions.
Expand Up @@ -16,11 +16,13 @@
import {
SpanStatusCode,
context,
diag,
propagation,
Span as ISpan,
SpanKind,
trace,
SpanAttributes,
DiagConsoleLogger,
} from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
Expand Down Expand Up @@ -269,6 +271,14 @@ describe('HttpInstrumentation', () => {
// hang the request.
return;
}
if (request.url?.includes('/destroy-request')) {
// force flush http response header to trigger client response callback
response.write('');
setTimeout(() => {
request.socket.destroy();
}, 100);
return;
}
if (request.url?.includes('/ignored')) {
provider.getTracer('test').startSpan('some-span').end();
}
Expand Down Expand Up @@ -920,6 +930,37 @@ describe('HttpInstrumentation', () => {
assert.strictEqual(clientSpan.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(clientSpan.attributes).length >= 6);
});

it('should not end span multiple times if request socket destroyed before response completes', async () => {
const warnMessages: string[] = [];
diag.setLogger({
...new DiagConsoleLogger(),
warn: message => {
warnMessages.push(message);
},
});
const promise = new Promise<void>(resolve => {
const req = http.get(
`${protocol}://${hostname}:${serverPort}/destroy-request`,
res => {
res.on('end', () => {});
res.on('close', () => {});
res.on('error', () => {
resolve();
});
}
);
// force flush http request header to trigger client response callback
req.write('');
req.on('error', () => {});
});

await promise;

diag.disable();

assert.strictEqual(warnMessages, []);
});
});

describe('with require parent span', () => {
Expand Down

0 comments on commit 225e24a

Please sign in to comment.