-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add tracing channel to diagnostics_channel
- Loading branch information
Stephen Belanger
committed
Oct 18, 2022
1 parent
7e09c6c
commit 0438150
Showing
5 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
test/parallel/test-diagnostics-channel-tracing-channel-async-error.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const expectedError = new Error('test'); | ||
const input = { foo: 'bar' }; | ||
const thisArg = { baz: 'buz' }; | ||
|
||
function check(found) { | ||
assert.deepStrictEqual(found, input); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check, 2), | ||
end: common.mustCall(check, 2), | ||
asyncEnd: common.mustCall(check, 2), | ||
error: common.mustCall((found) => { | ||
check(found); | ||
assert.deepStrictEqual(found.error, expectedError); | ||
}, 2) | ||
}; | ||
|
||
channel.subscribe(handlers); | ||
|
||
channel.traceCallback(function (cb, err) { | ||
assert.deepStrictEqual(this, thisArg); | ||
setImmediate(cb, err); | ||
}, 0, input, thisArg, common.mustCall((err, res) => { | ||
assert.strictEqual(err, expectedError); | ||
assert.deepStrictEqual(res, undefined); | ||
}), expectedError); | ||
|
||
channel.tracePromise(function (value) { | ||
assert.deepStrictEqual(this, thisArg); | ||
return Promise.reject(value); | ||
}, input, thisArg, expectedError).then( | ||
common.mustNotCall(), | ||
common.mustCall(value => { | ||
assert.deepStrictEqual(value, expectedError); | ||
}) | ||
); |
46 changes: 46 additions & 0 deletions
46
test/parallel/test-diagnostics-channel-tracing-channel-async.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const expectedResult = { foo: 'bar' }; | ||
const input = { foo: 'bar' }; | ||
const thisArg = { baz: 'buz' }; | ||
|
||
function check(found) { | ||
assert.deepStrictEqual(found, input); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check, 2), | ||
end: common.mustCall(check, 2), | ||
asyncEnd: common.mustCall((found) => { | ||
check(found); | ||
assert.strictEqual(found.error, undefined); | ||
assert.deepStrictEqual(found.result, expectedResult); | ||
}, 2), | ||
error: common.mustNotCall() | ||
}; | ||
|
||
channel.subscribe(handlers); | ||
|
||
channel.traceCallback(function (cb, err, res) { | ||
assert.deepStrictEqual(this, thisArg); | ||
setImmediate(cb, err, res); | ||
}, 0, input, thisArg, common.mustCall((err, res) => { | ||
assert.strictEqual(err, null); | ||
assert.deepStrictEqual(res, expectedResult); | ||
}), null, expectedResult); | ||
|
||
channel.tracePromise(function (value) { | ||
assert.deepStrictEqual(this, thisArg); | ||
return Promise.resolve(value); | ||
}, input, thisArg, expectedResult).then( | ||
common.mustCall(value => { | ||
assert.deepStrictEqual(value, expectedResult); | ||
}), | ||
common.mustNotCall() | ||
); |
38 changes: 38 additions & 0 deletions
38
test/parallel/test-diagnostics-channel-tracing-channel-sync-error.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const expectedError = new Error('test'); | ||
const input = { foo: 'bar' }; | ||
const thisArg = { baz: 'buz' }; | ||
|
||
function check(found) { | ||
assert.deepStrictEqual(found, input); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check), | ||
end: common.mustCall(check), | ||
asyncEnd: common.mustNotCall(), | ||
error: common.mustCall((found) => { | ||
check(found); | ||
assert.deepStrictEqual(found.error, expectedError); | ||
}) | ||
}; | ||
|
||
channel.subscribe(handlers); | ||
try { | ||
channel.traceSync(function (err) { | ||
assert.deepStrictEqual(this, thisArg); | ||
assert.strictEqual(err, expectedError); | ||
throw err; | ||
}, input, thisArg, expectedError); | ||
|
||
throw new Error('It should not reach this error'); | ||
} catch (error) { | ||
assert.deepStrictEqual(error, expectedError); | ||
} |
37 changes: 37 additions & 0 deletions
37
test/parallel/test-diagnostics-channel-tracing-channel-sync.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const expectedResult = { foo: 'bar' }; | ||
const input = { foo: 'bar' }; | ||
|
||
function check(found) { | ||
assert.deepStrictEqual(found, input); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check), | ||
end: common.mustCall((found) => { | ||
check(found); | ||
assert.deepStrictEqual(found.result, expectedResult); | ||
}), | ||
asyncEnd: common.mustNotCall(), | ||
error: common.mustNotCall() | ||
}; | ||
|
||
assert.strictEqual(channel.hasSubscribers, false); | ||
channel.subscribe(handlers); | ||
assert.strictEqual(channel.hasSubscribers, true); | ||
channel.traceSync(() => { | ||
return expectedResult; | ||
}, input); | ||
|
||
channel.unsubscribe(handlers); | ||
assert.strictEqual(channel.hasSubscribers, false); | ||
channel.traceSync(() => { | ||
return expectedResult; | ||
}, input); |