Skip to content

Commit

Permalink
refactor: update nock and migrate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioregueira committed Sep 6, 2020
1 parent 95541bd commit a14e012
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 68 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-plugin-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"got": "9.6.0",
"gts": "2.0.2",
"mocha": "7.2.0",
"nock": "12.0.3",
"nock": "13.0.4",
"nyc": "15.1.0",
"request": "2.88.2",
"request-promise-native": "1.0.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,80 +606,92 @@ describe('HttpPlugin', () => {
}
});

it('should have 1 ended span when request is aborted', async () => {
nock(`${protocol}://my.server.com`)
.get('/')
.socketDelay(50)
.reply(200, '<html></html>');

const promiseRequest = new Promise((resolve, reject) => {
const req = http.request(
`${protocol}://my.server.com`,
(resp: http.IncomingMessage) => {
let data = '';
resp.on('data', chunk => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
describe('when request is aborted', () => {
describe('before the connection succeeds', () => {
it('should have 1 ended span', async () => {
nock(`${protocol}://my.server.com`)
.get('/')
.delayConnection(50)
.reply(200, '<html></html>');

const promiseRequest = new Promise((resolve, reject) => {
const req = http.request(
`${protocol}://my.server.com`,
(resp: http.IncomingMessage) => {
let data = '';
resp.on('data', chunk => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}
);
req.setTimeout(10, () => {
req.abort();
reject('timeout');
});
return req.end();
});

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, CanonicalCode.ABORTED);
assert.ok(Object.keys(span.attributes).length >= 6);
}
);
req.setTimeout(10, () => {
req.abort();
reject('timeout');
});
return req.end();
});

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, CanonicalCode.ABORTED);
assert.ok(Object.keys(span.attributes).length >= 6);
}
});
describe('after the response is received', () => {
let altServer: http.Server;
const altServerPort = 22346;

it('should have 1 ended span when request is aborted after receiving response', async () => {
nock(`${protocol}://my.server.com`)
.get('/')
.delay({
body: 50,
})
.replyWithFile(200, `${process.cwd()}/package.json`);
beforeEach(done => {
altServer = http.createServer((request, response) => {
response.write('First part of the response');
setTimeout(() => response.end(), 50);
});
altServer.listen(altServerPort, done);
});

const promiseRequest = new Promise((resolve, reject) => {
const req = http.request(
`${protocol}://my.server.com`,
(resp: http.IncomingMessage) => {
let data = '';
resp.on('data', chunk => {
req.abort();
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}
);
afterEach(() => {
altServer.close();
});

return req.end();
});
it('should have 1 ended span', async () => {
const promiseRequest = new Promise((resolve, reject) => {
const req = http.request(
`${protocol}://${hostname}:${altServerPort}`,
(resp: http.IncomingMessage) => {
resp.on('data', (data) => {
req.abort();
});
resp.on('close', () => {
resolve();
})
}
);

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, CanonicalCode.ABORTED);
assert.ok(Object.keys(span.attributes).length > 7);
}
return req.end();
});

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, CanonicalCode.ABORTED);
assert.ok(Object.keys(span.attributes).length > 7);
}
});
});
});

it("should have 1 ended span when request doesn't listening response", done => {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-plugin-https/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"got": "9.6.0",
"gts": "2.0.2",
"mocha": "7.2.0",
"nock": "12.0.3",
"nock": "13.0.4",
"nyc": "15.1.0",
"request": "2.88.2",
"request-promise-native": "1.0.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ describe('HttpsPlugin', () => {
it('should have 1 ended span when request is aborted', async () => {
nock(`${protocol}://my.server.com`)
.get('/')
.socketDelay(50)
.delayConnection(50)
.reply(200, '<html></html>');

const promiseRequest = new Promise((resolve, reject) => {
Expand Down

0 comments on commit a14e012

Please sign in to comment.