Skip to content

Commit

Permalink
lib: fix diagnostics channel
Browse files Browse the repository at this point in the history
PR-URL: #44154
Fixes: #44143
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information
theanarkh authored and juanarbol committed Oct 10, 2022
1 parent 3c30962 commit 5e56f86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
7 changes: 2 additions & 5 deletions test/parallel/test-diagnostics-channel-http-server-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ const dc = require('diagnostics_channel');
const assert = require('assert');
const http = require('http');

const incomingStartChannel = dc.channel('http.server.request.start');
const outgoingFinishChannel = dc.channel('http.server.response.finish');

const als = new AsyncLocalStorage();
let context;

// Bind requests to an AsyncLocalStorage context
incomingStartChannel.subscribe(common.mustCall((message) => {
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
outgoingFinishChannel.subscribe(common.mustCall((message) => {
dc.subscribe('http.server.response.finish', common.mustCall((message) => {
const data = {
request,
response,
Expand Down
16 changes: 7 additions & 9 deletions test/parallel/test-diagnostics-channel-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@ const http = require('http');
const net = require('net');
const dc = require('diagnostics_channel');

const onClientRequestStart = dc.channel('http.client.request.start');
const onClientResponseFinish = dc.channel('http.client.response.finish');
const onServerRequestStart = dc.channel('http.server.request.start');
const onServerResponseFinish = dc.channel('http.server.response.finish');

const isHTTPServer = (server) => server instanceof http.Server;
const isIncomingMessage = (object) => object instanceof http.IncomingMessage;
const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage;
const isNetSocket = (socket) => socket instanceof net.Socket;

onClientRequestStart.subscribe(common.mustCall(({ request }) => {
dc.subscribe('http.client.request.start', common.mustCall(({ request }) => {
assert.strictEqual(isOutgoingMessage(request), true);
}));

onClientResponseFinish.subscribe(common.mustCall(({ request, response }) => {
dc.subscribe('http.client.response.finish', common.mustCall(({
request,
response
}) => {
assert.strictEqual(isOutgoingMessage(request), true);
assert.strictEqual(isIncomingMessage(response), true);
}));

onServerRequestStart.subscribe(common.mustCall(({
dc.subscribe('http.server.request.start', common.mustCall(({
request,
response,
socket,
Expand All @@ -36,7 +34,7 @@ onServerRequestStart.subscribe(common.mustCall(({
assert.strictEqual(isHTTPServer(server), true);
}));

onServerResponseFinish.subscribe(common.mustCall(({
dc.subscribe('http.server.response.finish', common.mustCall(({
request,
response,
socket,
Expand Down

0 comments on commit 5e56f86

Please sign in to comment.