node/test/parallel/test-diagnostics-channel-sync-unsubscribe.js
Gabriel Schulhof eb504c9406 diagnostics_channel: fix last subscriber removal
When iterating over diagnostics channel subscribers, assume their count
is zero if the list of subscribers becomes undefined, because there may
be only one subscriber which may unsubscribe itself as part of its
onMessage handler.

Signed-off-by: Gabriel Schulhof <gabrielschulhof@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/48933
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: theanarkh <theratliter@gmail.com>
2023-07-31 10:13:59 -07:00

15 lines
396 B
JavaScript

'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);