Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix the use of diagnostics channel #44154

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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