Skip to content

Commit 7e415c0

Browse files
committed
test: clean up diagnostics_channel suppression coverage
Signed-off-by: Divyanshu Sharma <divyanshu88999@gmail.com>
1 parent a5c4940 commit 7e415c0

1 file changed

Lines changed: 74 additions & 129 deletions

File tree

test/parallel/test-diagnostics-channel-suppression.js

Lines changed: 74 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -5,216 +5,187 @@ const assert = require('assert');
55
const { channel, suppressed } = require('node:diagnostics_channel');
66
const { AsyncLocalStorage } = require('async_hooks');
77

8-
// Helper to run a function and capture whether a handler was called
9-
function makeHandler() {
10-
let called = false;
11-
const handler = (msg) => { called = true; };
12-
return {
13-
handler,
14-
called: () => called,
15-
reset: () => { called = false; }
16-
};
17-
}
18-
19-
// Test 1: Basic suppression - subscriber with suppressedBy is skipped inside suppressed()
20-
(function testBasicSuppression() {
8+
// Test 1: Basic suppression - subscriber with subscriberId is skipped inside suppressed()
9+
{
2110
const key = Symbol('tracer');
2211
const ch = channel('test-suppression-basic');
23-
const h = makeHandler();
24-
ch.subscribe(h.handler, { suppressedBy: key });
12+
const handler = common.mustNotCall();
13+
ch.subscribe(handler, { subscriberId: key });
2514

2615
suppressed(key, () => {
2716
ch.publish({});
2817
});
2918

30-
assert.strictEqual(h.called(), false, 'suppressed subscriber should not be called');
31-
// cleanup
32-
ch.unsubscribe(h.handler);
33-
})();
19+
ch.unsubscribe(handler);
20+
}
3421

3522
// Test 2: Non-opted subscriber fires even inside suppressed() scope
36-
(function testNonOptedFires() {
23+
{
3724
const key = Symbol('tracer2');
3825
const ch = channel('test-suppression-nonopted');
39-
const h1 = makeHandler();
40-
const h2 = makeHandler();
41-
ch.subscribe(h1.handler, { suppressedBy: key });
42-
ch.subscribe(h2.handler); // no suppression
26+
const optedHandler = common.mustNotCall();
27+
const regularHandler = common.mustCall(() => {}, 1);
28+
ch.subscribe(optedHandler, { subscriberId: key });
29+
ch.subscribe(regularHandler); // no suppression
4330

4431
suppressed(key, () => {
4532
ch.publish({});
4633
});
4734

48-
assert.strictEqual(h1.called(), false, 'opted subscriber should be skipped');
49-
assert.strictEqual(h2.called(), true, 'non-opted subscriber should be called');
50-
51-
ch.unsubscribe(h1.handler);
52-
ch.unsubscribe(h2.handler);
53-
})();
35+
ch.unsubscribe(optedHandler);
36+
ch.unsubscribe(regularHandler);
37+
}
5438

5539
// Test 3: Two APMs with different keys don't suppress each other
56-
(function testTwoKeysIndependent() {
40+
{
5741
const k1 = Symbol('k1');
5842
const k2 = Symbol('k2');
5943
const ch = channel('test-suppression-two-keys');
60-
const h1 = makeHandler();
61-
const h2 = makeHandler();
62-
ch.subscribe(h1.handler, { suppressedBy: k1 });
63-
ch.subscribe(h2.handler, { suppressedBy: k2 });
44+
let h1Calls = 0;
45+
let h2Calls = 0;
46+
const h1 = common.mustCall(() => { h1Calls++; }, 1);
47+
const h2 = common.mustCall(() => { h2Calls++; }, 1);
48+
ch.subscribe(h1, { subscriberId: k1 });
49+
ch.subscribe(h2, { subscriberId: k2 });
6450

6551
suppressed(k1, () => {
6652
ch.publish({});
6753
});
6854

69-
assert.strictEqual(h1.called(), false);
70-
assert.strictEqual(h2.called(), true);
71-
72-
h1.reset(); h2.reset();
55+
assert.strictEqual(h1Calls, 0);
56+
assert.strictEqual(h2Calls, 1);
7357

7458
suppressed(k2, () => {
7559
ch.publish({});
7660
});
7761

78-
assert.strictEqual(h1.called(), true);
79-
assert.strictEqual(h2.called(), false);
62+
assert.strictEqual(h1Calls, 1);
63+
assert.strictEqual(h2Calls, 1);
8064

81-
ch.unsubscribe(h1.handler);
82-
ch.unsubscribe(h2.handler);
83-
})();
65+
ch.unsubscribe(h1);
66+
ch.unsubscribe(h2);
67+
}
8468

8569
// Test 4: Nested suppressed() calls (same key, different keys)
86-
(function testNestedSuppressed() {
70+
{
8771
const k1 = Symbol('nested1');
8872
const k2 = Symbol('nested2');
8973
const ch = channel('test-suppression-nested');
90-
const h1 = makeHandler();
91-
const h2 = makeHandler();
92-
ch.subscribe(h1.handler, { suppressedBy: k1 });
93-
ch.subscribe(h2.handler, { suppressedBy: k2 });
74+
const h1 = common.mustNotCall();
75+
let h2Calls = 0;
76+
const h2 = common.mustCall(() => { h2Calls++; }, 2);
77+
ch.subscribe(h1, { subscriberId: k1 });
78+
ch.subscribe(h2, { subscriberId: k2 });
9479

9580
suppressed(k1, () => {
9681
// inside k1, h1 skipped, h2 runs
9782
ch.publish({});
98-
assert.strictEqual(h1.called(), false);
99-
assert.strictEqual(h2.called(), true);
100-
h2.reset();
83+
assert.strictEqual(h2Calls, 1);
10184

10285
suppressed(k2, () => {
10386
// inside both, both skipped
10487
ch.publish({});
105-
assert.strictEqual(h1.called(), false);
106-
assert.strictEqual(h2.called(), false);
88+
assert.strictEqual(h2Calls, 1);
10789
});
10890

10991
// back to only k1
11092
ch.publish({});
111-
assert.strictEqual(h1.called(), false);
112-
assert.strictEqual(h2.called(), true);
93+
assert.strictEqual(h2Calls, 2);
11394
});
11495

115-
ch.unsubscribe(h1.handler);
116-
ch.unsubscribe(h2.handler);
117-
})();
96+
ch.unsubscribe(h1);
97+
ch.unsubscribe(h2);
98+
}
11899

119-
// Test 5: suppressed() across a Promise boundary (async/await)
120-
(async function testSuppressedAcrossPromise() {
100+
// Test 5: suppressed() across a Promise boundary
101+
{
121102
const key = Symbol('promise');
122103
const ch = channel('test-suppression-promise');
123-
const h = makeHandler();
124-
ch.subscribe(h.handler, { suppressedBy: key });
104+
const handler = common.mustNotCall();
105+
ch.subscribe(handler, { subscriberId: key });
125106

126-
await suppressed(key, async () => {
107+
suppressed(key, async () => {
127108
await Promise.resolve();
128109
ch.publish({});
129-
});
130-
131-
assert.strictEqual(h.called(), false);
132-
ch.unsubscribe(h.handler);
133-
})();
110+
}).then(common.mustCall(() => {
111+
ch.unsubscribe(handler);
112+
}));
113+
}
134114

135115
// Test 6: suppressed() across setImmediate and queueMicrotask
136-
(async function testSuppressedAcrossTimers() {
116+
{
137117
const key = Symbol('timers');
138118
const ch = channel('test-suppression-timers');
139-
const h = makeHandler();
140-
ch.subscribe(h.handler, { suppressedBy: key });
119+
const handler = common.mustNotCall();
120+
ch.subscribe(handler, { subscriberId: key });
141121

142-
await suppressed(key, async () => {
122+
suppressed(key, async () => {
143123
await new Promise((resolve) => {
144124
setImmediate(() => {
145125
ch.publish({});
146-
assert.strictEqual(h.called(), false);
147-
h.reset();
148126

149127
queueMicrotask(() => {
150128
ch.publish({});
151-
assert.strictEqual(h.called(), false);
152-
153-
ch.unsubscribe(h.handler);
154129
resolve();
155130
});
156131
});
157132
});
158-
});
159-
})();
133+
}).then(common.mustCall(() => {
134+
ch.unsubscribe(handler);
135+
}));
136+
}
160137

161-
// Test 7: unsubscribe() works correctly after using suppressedBy
162-
(function testUnsubscribeCleansUp() {
138+
// Test 7: unsubscribe() works correctly after using subscriberId
139+
{
163140
const key = Symbol('unsub');
164141
const ch = channel('test-suppression-unsubscribe');
165-
const h = makeHandler();
166-
ch.subscribe(h.handler, { suppressedBy: key });
167-
ch.unsubscribe(h.handler);
142+
const handler = common.mustNotCall();
143+
ch.subscribe(handler, { subscriberId: key });
144+
ch.unsubscribe(handler);
168145

169146
// Should not throw and should not be called
170147
suppressed(key, () => {
171148
ch.publish({});
172149
});
173150

174-
assert.strictEqual(h.called(), false);
175-
})();
151+
}
176152

177-
// Test 8: bindStore with suppressedBy is skipped inside suppressed()
178-
(function testBindStoreSuppression() {
153+
// Test 8: bindStore with subscriberId is skipped inside suppressed()
154+
{
179155
const key = Symbol('store');
180156
const ch = channel('test-suppression-store');
181157
const als = new AsyncLocalStorage();
182158

183-
let transformCalls = 0;
184159
const handler = common.mustCall(() => {
185160
assert.strictEqual(als.getStore(), undefined);
186161
});
187162

188163
ch.subscribe(handler);
189-
ch.bindStore(als, (d) => {
190-
transformCalls++;
191-
return { foo: d };
192-
}, { suppressedBy: key });
164+
ch.bindStore(als, common.mustNotCall(), { subscriberId: key });
193165

194166
suppressed(key, () => {
195167
ch.publish({});
196168
});
197169

198-
assert.strictEqual(transformCalls, 0);
199170
ch.unsubscribe(handler);
200171
ch.unbindStore(als);
201-
})();
172+
}
202173

203-
// Test 9: Wrong type for suppressedBy throws ERR_INVALID_ARG_TYPE
204-
(function testWrongTypeThrows() {
174+
// Test 9: Wrong type for subscriberId throws ERR_INVALID_ARG_TYPE
175+
{
205176
const ch = channel('test-suppression-wrong-type');
206177
const bad = 'not-allowed';
207-
assert.throws(() => ch.subscribe(() => {}, { suppressedBy: bad }), {
178+
assert.throws(() => ch.subscribe(() => {}, { subscriberId: bad }), {
208179
name: 'TypeError'
209180
});
210181
const als = new AsyncLocalStorage();
211-
assert.throws(() => ch.bindStore(als, (d) => d, { suppressedBy: bad }), {
182+
assert.throws(() => ch.bindStore(als, (d) => d, { subscriberId: bad }), {
212183
name: 'TypeError'
213184
});
214-
})();
185+
}
215186

216187
// Test 10: suppressed() return value passes through fn's return value
217-
(function testSuppressedReturnValueAndContext() {
188+
{
218189
const key = Symbol('return');
219190
const receiver = { value: 41 };
220191
const result = suppressed(key, function(a, b) {
@@ -224,32 +195,6 @@ function makeHandler() {
224195
return this.value + 1;
225196
}, receiver, 'a', 'b');
226197
assert.strictEqual(result, 42);
227-
})();
228-
229-
// Test 11: null suppressedBy behaves like no suppression opt-in
230-
(function testNullSuppressedByIsIgnored() {
231-
const key = Symbol('null-suppressed-by');
232-
const ch = channel('test-suppression-null-suppressed-by');
233-
const h = makeHandler();
234-
235-
ch.subscribe(h.handler, { suppressedBy: null });
236-
237-
suppressed(key, () => {
238-
ch.publish({});
239-
});
240-
241-
assert.strictEqual(h.called(), true);
242-
ch.unsubscribe(h.handler);
243-
})();
244-
245-
// Test 12: suppressed() rejects null/undefined keys consistently
246-
(function testKeyValidation() {
247-
assert.throws(() => suppressed(null, () => {}), {
248-
name: 'TypeError'
249-
});
250-
assert.throws(() => suppressed(undefined, () => {}), {
251-
name: 'TypeError'
252-
});
253-
})();
198+
}
254199

255200
console.log('ok - diagnostics_channel suppression tests loaded');

0 commit comments

Comments
 (0)