Skip to content
File

Blob: src/workerd/api/node/tests/diagnostics-channel-getter-test.js

javascript68 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4//
5// Tests the `hasSubscribers` getter behavior of `Channel` and `TracingChannel`
6// when the `diagnostics_channel_has_subscribers_getter` compat flag is enabled.
7// Legacy method behavior is covered by `diagnostics-channel-test`.
8 
9import { ok, strictEqual } from 'node:assert';
10 
11import { channel, tracingChannel } from 'node:diagnostics_channel';
12 
13export const test_channel_hasSubscribers_is_a_getter = {
14 async test() {
15 const ch = channel('getter-test');
16 
17 // It is a boolean, not a function (matches Node.js).
18 strictEqual(typeof ch.hasSubscribers, 'boolean');
19 strictEqual(ch.hasSubscribers, false);
20 
21 const listener = () => {};
22 ch.subscribe(listener);
23 strictEqual(ch.hasSubscribers, true);
24 
25 ch.unsubscribe(listener);
26 strictEqual(ch.hasSubscribers, false);
27 
28 // Defined on the prototype as a read-only getter.
29 const desc = Object.getOwnPropertyDescriptor(
30 Object.getPrototypeOf(ch),
31 'hasSubscribers'
32 );
33 ok(desc);
34 strictEqual(typeof desc.get, 'function');
35 strictEqual(desc.set, undefined);
36 strictEqual(desc.value, undefined);
37 },
38};
39 
40export const test_tracingChannel_hasSubscribers_is_a_getter = {
41 async test() {
42 const tc = tracingChannel('tracing-getter-test');
43 
44 strictEqual(typeof tc.hasSubscribers, 'boolean');
45 strictEqual(tc.hasSubscribers, false);
46 
47 const listener = () => {};
48 
49 // Each sub-channel independently flips the aggregate getter.
50 for (const sub of ['start', 'end', 'asyncStart', 'asyncEnd', 'error']) {
51 tc[sub].subscribe(listener);
52 strictEqual(tc.hasSubscribers, true, `via ${sub}`);
53 tc[sub].unsubscribe(listener);
54 strictEqual(tc.hasSubscribers, false, `after unsubscribing ${sub}`);
55 }
56 
57 // TracingChannel.hasSubscribers should also be a prototype getter.
58 const desc = Object.getOwnPropertyDescriptor(
59 Object.getPrototypeOf(tc),
60 'hasSubscribers'
61 );
62 ok(desc);
63 strictEqual(typeof desc.get, 'function');
64 strictEqual(desc.set, undefined);
65 strictEqual(desc.value, undefined);
66 },
67};