Skip to content

Commit c31eab5

Browse files
logaretmclaude
andcommitted
Skip TracingChannel tests on Node < 19.9
Node 18 backported TracingChannel but with a buggy implementation — unsubscribing and resubscribing to the same channel crashes internally (`_subscribers` becomes undefined). Node 16 has no TracingChannel at all. Gate tests on `hasStableTracingChannel` which checks both that `dc.tracingChannel` exists AND that the aggregated `hasSubscribers` getter returns a boolean (only true on Node 19.9+/20.5+). TracingChannel tests: skipped on Node 16/18, run on Node 20+ Plain channel tests (release/remove): run on all versions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 73c9946 commit c31eab5

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

packages/pg-pool/test/diagnostics.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const it = require('mocha').it
77
const dc = require('diagnostics_channel')
88
const Pool = require('../')
99

10+
// TracingChannel exists on Node 18+ but the aggregated hasSubscribers getter
11+
// and stable unsubscribe behavior require Node 19.9+/20.5+. Skip tracing
12+
// tests on older versions where TracingChannel is missing or has internal bugs.
13+
const hasStableTracingChannel =
14+
typeof dc.tracingChannel === 'function' && typeof dc.tracingChannel('pg:pool:test:probe').hasSubscribers === 'boolean'
15+
1016
function mockClient(methods) {
1117
return function () {
1218
const client = new EventEmitter()
@@ -23,7 +29,7 @@ function mockClient(methods) {
2329

2430
describe('diagnostics channels', function () {
2531
describe('pg:pool:connect', function () {
26-
it('publishes start event when connect is called', function (done) {
32+
;(hasStableTracingChannel ? it : it.skip)('publishes start event when connect is called', function (done) {
2733
const pool = new Pool({
2834
Client: mockClient({
2935
connect: function (cb) {
@@ -60,7 +66,7 @@ describe('diagnostics channels', function () {
6066
})
6167
})
6268
})
63-
it('enriches context with client info on asyncEnd', function (done) {
69+
;(hasStableTracingChannel ? it : it.skip)('enriches context with client info on asyncEnd', function (done) {
6470
const pool = new Pool({
6571
Client: mockClient({
6672
connect: function (cb) {

packages/pg/test/unit/client/diagnostics-tests.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ const helper = require('./test-helper')
33
const assert = require('assert')
44
const dc = require('diagnostics_channel')
55

6+
// TracingChannel exists on Node 18+ but the aggregated hasSubscribers getter
7+
// and stable unsubscribe behavior require Node 19.9+/20.5+. Skip tests on
8+
// older versions where TracingChannel is missing or has internal bugs.
9+
const hasStableTracingChannel =
10+
typeof dc.tracingChannel === 'function' && typeof dc.tracingChannel('pg:test:probe').hasSubscribers === 'boolean'
11+
612
const suite = new helper.Suite()
713
const test = suite.test.bind(suite)
14+
// pass undefined as callback to skip when TracingChannel is unavailable/unstable
15+
const testTracing = (name, cb) => test(name, hasStableTracingChannel ? cb : undefined)
816

9-
test('query diagnostics channel', function () {
10-
test('publishes start and asyncEnd on successful query', function (done) {
17+
testTracing('query diagnostics channel', function () {
18+
testTracing('publishes start and asyncEnd on successful query', function (done) {
1119
const client = helper.client()
1220
client.connection.emit('readyForQuery')
1321

@@ -50,7 +58,7 @@ test('query diagnostics channel', function () {
5058
client.connection.emit('readyForQuery')
5159
})
5260

53-
test('publishes error on failed query', function (done) {
61+
testTracing('publishes error on failed query', function (done) {
5462
const client = helper.client()
5563
client.connection.emit('readyForQuery')
5664

@@ -87,7 +95,7 @@ test('query diagnostics channel', function () {
8795
})
8896
})
8997

90-
test('query context includes client info', function (done) {
98+
testTracing('query context includes client info', function (done) {
9199
const client = helper.client({ database: 'testdb', host: 'localhost', port: 5432, user: 'testuser' })
92100
client.connection.emit('readyForQuery')
93101

@@ -120,7 +128,7 @@ test('query diagnostics channel', function () {
120128
client.connection.emit('readyForQuery')
121129
})
122130

123-
test('promise query publishes diagnostics', function (done) {
131+
testTracing('promise query publishes diagnostics', function (done) {
124132
const client = helper.client()
125133
client.connection.emit('readyForQuery')
126134

@@ -154,8 +162,8 @@ test('query diagnostics channel', function () {
154162
})
155163
})
156164

157-
test('connection diagnostics channel', function () {
158-
test('publishes start on connect with callback', function (done) {
165+
testTracing('connection diagnostics channel', function () {
166+
testTracing('publishes start on connect with callback', function (done) {
159167
const Connection = require('../../../lib/connection')
160168
const { Client } = helper
161169

0 commit comments

Comments
 (0)