0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-diagnostics-channel-http-server-start.js
theanarkh fa22337901
lib: fix diagnostics channel
PR-URL: https://github.com/nodejs/node/pull/44154
Fixes: https://github.com/nodejs/node/issues/44143
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2022-08-08 17:07:03 +00:00

63 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
const { AsyncLocalStorage } = require('async_hooks');
const dc = require('diagnostics_channel');
const assert = require('assert');
const http = require('http');
const als = new AsyncLocalStorage();
let context;
// Bind requests to an AsyncLocalStorage context
dc.subscribe('http.server.request.start', common.mustCall((message) => {
als.enterWith(message);
context = message;
}));
// When the request ends, verify the context has been maintained
// and that the messages contain the expected data
dc.subscribe('http.server.response.finish', common.mustCall((message) => {
const data = {
request,
response,
server,
socket: request.socket
};
// Context is maintained
compare(als.getStore(), context);
compare(context, data);
compare(message, data);
}));
let request;
let response;
const server = http.createServer(common.mustCall((req, res) => {
request = req;
response = res;
setTimeout(() => {
res.end('done');
}, 1);
}));
server.listen(() => {
const { port } = server.address();
http.get(`http://localhost:${port}`, (res) => {
res.resume();
res.on('end', () => {
server.close();
});
});
});
function compare(a, b) {
assert.strictEqual(a.request, b.request);
assert.strictEqual(a.response, b.response);
assert.strictEqual(a.socket, b.socket);
assert.strictEqual(a.server, b.server);
}