diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-06-14 23:07:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-15 00:07:02 +0200 |
commit | 78b12a43b307c3405cd63cf4612517210330b487 (patch) | |
tree | ef9a6a258f13f7d4151e8152ed9702572126c165 /tests/node_compat | |
parent | 184a85eaecbe7055b5b3969391f49c4723ac44fb (diff) |
fix(ext/node): better support for `node:diagnostics_channel` module (#24088)
Closes https://github.com/denoland/deno/issues/24060
Diffstat (limited to 'tests/node_compat')
9 files changed, 279 insertions, 7 deletions
diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index bc5580c1d..27cf6afb8 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -261,9 +261,16 @@ "test-dgram-close-during-bind.js", "test-dgram-close-signal.js", "test-diagnostics-channel-has-subscribers.js", + "test-diagnostics-channel-net.js", "test-diagnostics-channel-object-channel-pub-sub.js", "test-diagnostics-channel-pub-sub.js", "test-diagnostics-channel-symbol-named.js", + "test-diagnostics-channel-sync-unsubscribe.js", + "test-diagnostics-channel-tracing-channel-args-types.js", + "test-diagnostics-channel-tracing-channel-callback-run-stores.js", + "test-diagnostics-channel-tracing-channel-promise-run-stores.js", + "test-diagnostics-channel-tracing-channel-sync-error.js", + "test-diagnostics-channel-tracing-channel-sync.js", "test-diagnostics-channel-udp.js", "test-dns-lookup.js", "test-dns-memory-error.js", diff --git a/tests/node_compat/runner/TODO.md b/tests/node_compat/runner/TODO.md index 0d5bcc626..e24c82b75 100644 --- a/tests/node_compat/runner/TODO.md +++ b/tests/node_compat/runner/TODO.md @@ -628,18 +628,11 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co - [parallel/test-diagnostics-channel-http-server-start.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-http-server-start.js) - [parallel/test-diagnostics-channel-http.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-http.js) - [parallel/test-diagnostics-channel-memory-leak.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-memory-leak.js) -- [parallel/test-diagnostics-channel-net.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-net.js) - [parallel/test-diagnostics-channel-process.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-process.js) - [parallel/test-diagnostics-channel-safe-subscriber-errors.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-safe-subscriber-errors.js) -- [parallel/test-diagnostics-channel-sync-unsubscribe.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-sync-unsubscribe.js) -- [parallel/test-diagnostics-channel-tracing-channel-args-types.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-args-types.js) - [parallel/test-diagnostics-channel-tracing-channel-async-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-async-error.js) - [parallel/test-diagnostics-channel-tracing-channel-async.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-async.js) -- [parallel/test-diagnostics-channel-tracing-channel-callback-run-stores.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-callback-run-stores.js) -- [parallel/test-diagnostics-channel-tracing-channel-promise-run-stores.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-promise-run-stores.js) - [parallel/test-diagnostics-channel-tracing-channel-run-stores.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-run-stores.js) -- [parallel/test-diagnostics-channel-tracing-channel-sync-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-sync-error.js) -- [parallel/test-diagnostics-channel-tracing-channel-sync.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-tracing-channel-sync.js) - [parallel/test-diagnostics-channel-worker-threads.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-diagnostics-channel-worker-threads.js) - [parallel/test-directory-import.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-directory-import.js) - [parallel/test-disable-proto-delete.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-disable-proto-delete.js) diff --git a/tests/node_compat/test/parallel/test-diagnostics-channel-net.js b/tests/node_compat/test/parallel/test-diagnostics-channel-net.js new file mode 100644 index 000000000..504c3e5dc --- /dev/null +++ b/tests/node_compat/test/parallel/test-diagnostics-channel-net.js @@ -0,0 +1,32 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); +const dc = require('diagnostics_channel'); + +const isNetSocket = (socket) => socket instanceof net.Socket; + +dc.subscribe('net.client.socket', common.mustCall(({ socket }) => { + assert.strictEqual(isNetSocket(socket), true); +})); + +dc.subscribe('net.server.socket', common.mustCall(({ socket }) => { + assert.strictEqual(isNetSocket(socket), true); +})); + +const server = net.createServer(common.mustCall((socket) => { + socket.destroy(); + server.close(); +})); + +server.listen(() => { + const { port } = server.address(); + net.connect(port); +}); diff --git a/tests/node_compat/test/parallel/test-diagnostics-channel-sync-unsubscribe.js b/tests/node_compat/test/parallel/test-diagnostics-channel-sync-unsubscribe.js new file mode 100644 index 000000000..767382476 --- /dev/null +++ b/tests/node_compat/test/parallel/test-diagnostics-channel-sync-unsubscribe.js @@ -0,0 +1,21 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; + +const common = require('../common'); +const dc = require('node:diagnostics_channel'); + +const channel_name = 'test:channel'; +const published_data = 'some message'; + +const onMessageHandler = common.mustCall(() => dc.unsubscribe(channel_name, onMessageHandler)); + +dc.subscribe(channel_name, onMessageHandler); + +// This must not throw. +dc.channel(channel_name).publish(published_data); diff --git a/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-args-types.js b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-args-types.js new file mode 100644 index 000000000..885c9d76b --- /dev/null +++ b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-args-types.js @@ -0,0 +1,46 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; + +require('../common'); +const dc = require('diagnostics_channel'); +const assert = require('assert'); + +let channel; + +// tracingChannel creating with name +channel = dc.tracingChannel('test'); +assert.strictEqual(channel.start.name, 'tracing:test:start'); + +// tracingChannel creating with channels +channel = dc.tracingChannel({ + start: dc.channel('tracing:test:start'), + end: dc.channel('tracing:test:end'), + asyncStart: dc.channel('tracing:test:asyncStart'), + asyncEnd: dc.channel('tracing:test:asyncEnd'), + error: dc.channel('tracing:test:error'), +}); + +// tracingChannel creating without nameOrChannels must throw TypeError +assert.throws(() => (channel = dc.tracingChannel(0)), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: + /The "nameOrChannels" argument must be of type string or an instance of Channel or Object/, +}); + +// tracingChannel creating without instance of Channel must throw error +assert.throws(() => (channel = dc.tracingChannel({ start: '' })), { + code: 'ERR_INVALID_ARG_TYPE', + message: /The "nameOrChannels\.start" property must be an instance of Channel/, +}); + +// tracingChannel creating with empty nameOrChannels must throw error +assert.throws(() => (channel = dc.tracingChannel({})), { + message: /Cannot convert undefined or null to object/, +}); diff --git a/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-callback-run-stores.js b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-callback-run-stores.js new file mode 100644 index 000000000..1160e6464 --- /dev/null +++ b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-callback-run-stores.js @@ -0,0 +1,36 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; + +const common = require('../common'); +const { AsyncLocalStorage } = require('async_hooks'); +const dc = require('diagnostics_channel'); +const assert = require('assert'); + +const channel = dc.tracingChannel('test'); +const store = new AsyncLocalStorage(); + +const firstContext = { foo: 'bar' }; +const secondContext = { baz: 'buz' }; + +channel.start.bindStore(store, common.mustCall(() => { + return firstContext; +})); + +channel.asyncStart.bindStore(store, common.mustCall(() => { + return secondContext; +})); + +assert.strictEqual(store.getStore(), undefined); +channel.traceCallback(common.mustCall((cb) => { + assert.deepStrictEqual(store.getStore(), firstContext); + setImmediate(cb); +}), 0, {}, null, common.mustCall(() => { + assert.deepStrictEqual(store.getStore(), secondContext); +})); +assert.strictEqual(store.getStore(), undefined); diff --git a/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-promise-run-stores.js b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-promise-run-stores.js new file mode 100644 index 000000000..3f015e192 --- /dev/null +++ b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-promise-run-stores.js @@ -0,0 +1,38 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; + +const common = require('../common'); +const { setTimeout } = require('node:timers/promises'); +const { AsyncLocalStorage } = require('async_hooks'); +const dc = require('diagnostics_channel'); +const assert = require('assert'); + +const channel = dc.tracingChannel('test'); +const store = new AsyncLocalStorage(); + +const firstContext = { foo: 'bar' }; +const secondContext = { baz: 'buz' }; + +channel.start.bindStore(store, common.mustCall(() => { + return firstContext; +})); + +channel.asyncStart.bindStore(store, common.mustNotCall(() => { + return secondContext; +})); + +assert.strictEqual(store.getStore(), undefined); +channel.tracePromise(common.mustCall(async () => { + assert.deepStrictEqual(store.getStore(), firstContext); + await setTimeout(1); + // Should _not_ switch to second context as promises don't have an "after" + // point at which to do a runStores. + assert.deepStrictEqual(store.getStore(), firstContext); +})); +assert.strictEqual(store.getStore(), undefined); diff --git a/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-sync-error.js b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-sync-error.js new file mode 100644 index 000000000..09fc10329 --- /dev/null +++ b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-sync-error.js @@ -0,0 +1,46 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'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), + asyncStart: common.mustNotCall(), + 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); +} diff --git a/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-sync.js b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-sync.js new file mode 100644 index 000000000..036bcce3b --- /dev/null +++ b/tests/node_compat/test/parallel/test-diagnostics-channel-tracing-channel-sync.js @@ -0,0 +1,53 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'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' }; +const arg = { baz: 'buz' }; + +function check(found) { + assert.strictEqual(found, input); +} + +const handlers = { + start: common.mustCall(check), + end: common.mustCall((found) => { + check(found); + assert.strictEqual(found.result, expectedResult); + }), + asyncStart: common.mustNotCall(), + asyncEnd: common.mustNotCall(), + error: common.mustNotCall() +}; + +assert.strictEqual(channel.start.hasSubscribers, false); +channel.subscribe(handlers); +assert.strictEqual(channel.start.hasSubscribers, true); +const result1 = channel.traceSync(function(arg1) { + assert.strictEqual(arg1, arg); + assert.strictEqual(this, thisArg); + return expectedResult; +}, input, thisArg, arg); +assert.strictEqual(result1, expectedResult); + +channel.unsubscribe(handlers); +assert.strictEqual(channel.start.hasSubscribers, false); +const result2 = channel.traceSync(function(arg1) { + assert.strictEqual(arg1, arg); + assert.strictEqual(this, thisArg); + return expectedResult; +}, input, thisArg, arg); +assert.strictEqual(result2, expectedResult); |