-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdiagnostics.js
More file actions
197 lines (173 loc) · 5.36 KB
/
diagnostics.js
File metadata and controls
197 lines (173 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict'
const expect = require('expect.js')
const EventEmitter = require('events').EventEmitter
const describe = require('mocha').describe
const it = require('mocha').it
const dc = require('diagnostics_channel')
const Pool = require('../')
// TracingChannel exists on Node 18+ but the aggregated hasSubscribers getter
// and stable unsubscribe behavior require Node 19.9+/20.5+. Skip tracing
// tests on older versions where TracingChannel is missing or has internal bugs.
const hasStableTracingChannel =
typeof dc.tracingChannel === 'function' && typeof dc.tracingChannel('pg:pool:test:probe').hasSubscribers === 'boolean'
function mockClient(methods) {
return function () {
const client = new EventEmitter()
client.end = function (cb) {
if (cb) process.nextTick(cb)
}
client._queryable = true
client._ending = false
client.processID = 12345
Object.assign(client, methods)
return client
}
}
describe('diagnostics channels', function () {
describe('pg:pool:connect', function () {
;(hasStableTracingChannel ? it : it.skip)('publishes start event when connect is called', function (done) {
const pool = new Pool({
Client: mockClient({
connect: function (cb) {
process.nextTick(() => cb(null))
},
}),
})
let capturedContext
const channel = dc.tracingChannel('pg:pool:connect')
const subs = {
start: (ctx) => {
capturedContext = ctx
},
end: () => {},
asyncStart: () => {},
asyncEnd: () => {},
error: () => {},
}
channel.subscribe(subs)
pool.connect(function (err, client, release) {
if (err) return done(err)
release()
pool.end(() => {
expect(capturedContext).to.be.ok()
expect(capturedContext.pool).to.be.ok()
expect(capturedContext.pool.maxSize).to.be(10)
expect(capturedContext.pool.totalCount).to.be.a('number')
channel.unsubscribe(subs)
done()
})
})
})
;(hasStableTracingChannel ? it : it.skip)('enriches context with client info on asyncEnd', function (done) {
const pool = new Pool({
Client: mockClient({
connect: function (cb) {
process.nextTick(() => cb(null))
},
}),
})
const channel = dc.tracingChannel('pg:pool:connect')
const subs = {
start: () => {},
end: () => {},
asyncStart: () => {},
asyncEnd: (ctx) => {
expect(ctx.client).to.be.ok()
expect(ctx.client.processID).to.be(12345)
channel.unsubscribe(subs)
done()
},
error: () => {},
}
channel.subscribe(subs)
pool.connect(function (err, client, release) {
if (err) return done(err)
release()
pool.end()
})
})
})
describe('pg:pool:release', function () {
it('publishes when a client is released', function (done) {
const pool = new Pool({
Client: mockClient({
connect: function (cb) {
process.nextTick(() => cb(null))
},
}),
})
let releaseMessage
const channel = dc.channel('pg:pool:release')
const onMessage = (msg) => {
releaseMessage = msg
}
channel.subscribe(onMessage)
pool.connect(function (err, client, release) {
if (err) return done(err)
release()
pool.end(() => {
expect(releaseMessage).to.be.ok()
expect(releaseMessage.client).to.be.ok()
expect(releaseMessage.client.processID).to.be(12345)
channel.unsubscribe(onMessage)
done()
})
})
})
it('includes error when released with error', function (done) {
const pool = new Pool({
Client: mockClient({
connect: function (cb) {
process.nextTick(() => cb(null))
},
}),
})
let releaseMessage
const channel = dc.channel('pg:pool:release')
const onMessage = (msg) => {
releaseMessage = msg
}
channel.subscribe(onMessage)
pool.connect(function (err, client, release) {
if (err) return done(err)
const releaseError = new Error('test error')
release(releaseError)
pool.end(() => {
expect(releaseMessage).to.be.ok()
expect(releaseMessage.error).to.be(releaseError)
channel.unsubscribe(onMessage)
done()
})
})
})
})
describe('pg:pool:remove', function () {
it('publishes when a client is removed', function (done) {
const pool = new Pool({
Client: mockClient({
connect: function (cb) {
process.nextTick(() => cb(null))
},
}),
})
let removeMessage
const channel = dc.channel('pg:pool:remove')
const onMessage = (msg) => {
removeMessage = msg
}
channel.subscribe(onMessage)
pool.connect(function (err, client, release) {
if (err) return done(err)
// release with error to trigger removal
release(new Error('force remove'))
pool.end(() => {
expect(removeMessage).to.be.ok()
expect(removeMessage.client).to.be.ok()
expect(removeMessage.client.processID).to.be(12345)
channel.unsubscribe(onMessage)
done()
})
})
})
})
})