• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const dc = require('diagnostics_channel');
5const assert = require('assert');
6const { Channel } = dc;
7
8const name = 'test';
9const input = {
10  foo: 'bar'
11};
12
13// Individual channel objects can be created to avoid future lookups
14const channel = dc.channel(name);
15assert.ok(channel instanceof Channel);
16
17// No subscribers yet, should not publish
18assert.ok(!channel.hasSubscribers);
19
20const subscriber = common.mustCall((message, name) => {
21  assert.strictEqual(name, channel.name);
22  assert.deepStrictEqual(message, input);
23});
24
25// Now there's a subscriber, should publish
26dc.subscribe(name, subscriber);
27assert.ok(channel.hasSubscribers);
28
29// The ActiveChannel prototype swap should not fail instanceof
30assert.ok(channel instanceof Channel);
31
32// Should trigger the subscriber once
33channel.publish(input);
34
35// Should not publish after subscriber is unsubscribed
36assert.ok(dc.unsubscribe(name, subscriber));
37assert.ok(!channel.hasSubscribers);
38
39// unsubscribe() should return false when subscriber is not found
40assert.ok(!dc.unsubscribe(name, subscriber));
41
42assert.throws(() => {
43  dc.subscribe(name, null);
44}, { code: 'ERR_INVALID_ARG_TYPE' });
45
46// Reaching zero subscribers should not delete from the channels map as there
47// will be no more weakref to incRef if another subscribe happens while the
48// channel object itself exists.
49channel.subscribe(subscriber);
50channel.unsubscribe(subscriber);
51channel.subscribe(subscriber);
52