1'use strict'; 2 3const common = require('../common'); 4const dc = require('diagnostics_channel'); 5const assert = require('assert'); 6const { Channel } = dc; 7 8const input = { 9 foo: 'bar' 10}; 11 12// Should not have named channel 13assert.ok(!dc.hasSubscribers('test')); 14 15// Individual channel objects can be created to avoid future lookups 16const channel = dc.channel('test'); 17assert.ok(channel instanceof Channel); 18 19// No subscribers yet, should not publish 20assert.ok(!channel.hasSubscribers); 21 22const subscriber = common.mustCall((message, name) => { 23 assert.strictEqual(name, channel.name); 24 assert.deepStrictEqual(message, input); 25}); 26 27// Now there's a subscriber, should publish 28channel.subscribe(subscriber); 29assert.ok(channel.hasSubscribers); 30 31// The ActiveChannel prototype swap should not fail instanceof 32assert.ok(channel instanceof Channel); 33 34// Should trigger the subscriber once 35channel.publish(input); 36 37// Should not publish after subscriber is unsubscribed 38assert.ok(channel.unsubscribe(subscriber)); 39assert.ok(!channel.hasSubscribers); 40 41// unsubscribe() should return false when subscriber is not found 42assert.ok(!channel.unsubscribe(subscriber)); 43 44assert.throws(() => { 45 channel.subscribe(null); 46}, { code: 'ERR_INVALID_ARG_TYPE' }); 47