11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst dc = require('diagnostics_channel'); 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ciconst { Channel } = dc; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst input = { 91cb0ef41Sopenharmony_ci foo: 'bar' 101cb0ef41Sopenharmony_ci}; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci// Should not have named channel 131cb0ef41Sopenharmony_ciassert.ok(!dc.hasSubscribers('test')); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// Individual channel objects can be created to avoid future lookups 161cb0ef41Sopenharmony_ciconst channel = dc.channel('test'); 171cb0ef41Sopenharmony_ciassert.ok(channel instanceof Channel); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci// No subscribers yet, should not publish 201cb0ef41Sopenharmony_ciassert.ok(!channel.hasSubscribers); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst subscriber = common.mustCall((message, name) => { 231cb0ef41Sopenharmony_ci assert.strictEqual(name, channel.name); 241cb0ef41Sopenharmony_ci assert.deepStrictEqual(message, input); 251cb0ef41Sopenharmony_ci}); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci// Now there's a subscriber, should publish 281cb0ef41Sopenharmony_cichannel.subscribe(subscriber); 291cb0ef41Sopenharmony_ciassert.ok(channel.hasSubscribers); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci// The ActiveChannel prototype swap should not fail instanceof 321cb0ef41Sopenharmony_ciassert.ok(channel instanceof Channel); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci// Should trigger the subscriber once 351cb0ef41Sopenharmony_cichannel.publish(input); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci// Should not publish after subscriber is unsubscribed 381cb0ef41Sopenharmony_ciassert.ok(channel.unsubscribe(subscriber)); 391cb0ef41Sopenharmony_ciassert.ok(!channel.hasSubscribers); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci// unsubscribe() should return false when subscriber is not found 421cb0ef41Sopenharmony_ciassert.ok(!channel.unsubscribe(subscriber)); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciassert.throws(() => { 451cb0ef41Sopenharmony_ci channel.subscribe(null); 461cb0ef41Sopenharmony_ci}, { code: 'ERR_INVALID_ARG_TYPE' }); 47