Skip to content

Commit fc17fa7

Browse files
committed
Revert "create guardDisconnect() helper"
This reverts commit 78d583d.
1 parent f2ba9b6 commit fc17fa7

26 files changed

Lines changed: 350 additions & 140 deletions

docs/docs/best-practices/writing-tests.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,33 @@ setGlobalDispatcher(agent)
2424
Undici's `Client` automatically reconnects after a socket error. This means
2525
a test can silently disconnect, reconnect, and still pass. Unfortunately,
2626
this could mask bugs like unexpected parser errors or protocol violations.
27-
To catch these silent reconnections, use the `guardDisconnect` helper from
28-
`test/guard-disconnect.js` after creating a `Client` (or `Pool`):
27+
To catch these silent reconnections, add a disconnect guard after creating
28+
a `Client`:
2929

3030
```js
3131
const { Client } = require('undici')
3232
const { test, after } = require('node:test')
3333
const { tspl } = require('@matteo.collina/tspl')
34-
const { guardDisconnect } = require('./guard-disconnect')
3534

3635
test('example with disconnect guard', async (t) => {
3736
t = tspl(t, { plan: 1 })
3837

3938
const client = new Client('http://localhost:3000')
4039
after(() => client.close())
4140

42-
guardDisconnect(client, t)
41+
client.on('disconnect', () => {
42+
if (!client.closed && !client.destroyed) {
43+
t.fail('unexpected disconnect')
44+
}
45+
})
4346

4447
// ... test logic ...
4548
})
4649
```
4750

48-
The guard listens for `'disconnect'` events and calls `t.fail()` unless the
49-
client is already closing or destroyed. `client.close()` and `client.destroy()`
50-
both emit `'disconnect'` events, but those are expected — the guard skips them
51-
by checking `!client.closed && !client.destroyed`.
51+
`client.close()` and `client.destroy()` both emit `'disconnect'` events, but
52+
those are expected. The guard only fails when a disconnect happens during the
53+
active test (i.e., `!client.closed && !client.destroyed` is true).
5254

5355
Skip the guard for tests where a disconnect is expected behavior, such as:
5456

test/client-connect.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const { Client, errors } = require('..')
77
const http = require('node:http')
88
const EE = require('node:events')
99
const { kBusy } = require('../lib/core/symbols')
10-
const { guardDisconnect } = require('./guard-disconnect')
1110

1211
// TODO: move to test/node-test/client-connect.js
1312
test('connect aborted after connect', async (t) => {
@@ -30,7 +29,11 @@ test('connect aborted after connect', async (t) => {
3029
pipelining: 3
3130
})
3231
after(() => client.close())
33-
guardDisconnect(client, t)
32+
client.on('disconnect', () => {
33+
if (!client.closed && !client.destroyed) {
34+
t.fail('unexpected disconnect')
35+
}
36+
})
3437

3538
client.connect({
3639
path: '/',

test/client-pipelining.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const { kConnect } = require('../lib/core/symbols')
99
const EE = require('node:events')
1010
const { kBusy, kRunning, kSize } = require('../lib/core/symbols')
1111
const { maybeWrapStream, consts } = require('./utils/async-iterators')
12-
const { guardDisconnect } = require('./guard-disconnect')
1312

1413
test('20 times GET with pipelining 10', async (t) => {
1514
const num = 20
@@ -42,7 +41,11 @@ test('20 times GET with pipelining 10', async (t) => {
4241
pipelining: 10
4342
})
4443
after(() => client.close())
45-
guardDisconnect(client, t)
44+
client.on('disconnect', () => {
45+
if (!client.closed && !client.destroyed) {
46+
t.fail('unexpected disconnect')
47+
}
48+
})
4649

4750
for (let i = 0; i < num; i++) {
4851
makeRequest(i)
@@ -100,7 +103,11 @@ test('A client should enqueue as much as twice its pipelining factor', async (t)
100103
pipelining: 2
101104
})
102105
after(() => client.close())
103-
guardDisconnect(client, t)
106+
client.on('disconnect', () => {
107+
if (!client.closed && !client.destroyed) {
108+
t.fail('unexpected disconnect')
109+
}
110+
})
104111

105112
for (; sent < 2;) {
106113
t.ok(client[kSize] <= client.pipelining, 'client is not full')
@@ -151,7 +158,11 @@ test('pipeline 1 is 1 active request', async (t) => {
151158
pipelining: 1
152159
})
153160
after(() => client.destroy())
154-
guardDisconnect(client, t)
161+
client.on('disconnect', () => {
162+
if (!client.closed && !client.destroyed) {
163+
t.fail('unexpected disconnect')
164+
}
165+
})
155166
client.request({
156167
path: '/',
157168
method: 'GET'
@@ -205,7 +216,11 @@ test('pipelined chunked POST stream', async (t) => {
205216
pipelining: 2
206217
})
207218
after(() => client.close())
208-
guardDisconnect(client, t)
219+
client.on('disconnect', () => {
220+
if (!client.closed && !client.destroyed) {
221+
t.fail('unexpected disconnect')
222+
}
223+
})
209224

210225
client.request({
211226
path: '/',
@@ -275,7 +290,11 @@ test('pipelined chunked POST iterator', async (t) => {
275290
pipelining: 2
276291
})
277292
after(() => client.close())
278-
guardDisconnect(client, t)
293+
client.on('disconnect', () => {
294+
if (!client.closed && !client.destroyed) {
295+
t.fail('unexpected disconnect')
296+
}
297+
})
279298

280299
client.request({
281300
path: '/',
@@ -399,7 +418,11 @@ test('pipelining non-idempotent', async (t) => {
399418
pipelining: 2
400419
})
401420
after(() => client.close())
402-
guardDisconnect(client, t)
421+
client.on('disconnect', () => {
422+
if (!client.closed && !client.destroyed) {
423+
t.fail('unexpected disconnect')
424+
}
425+
})
403426

404427
let ended = false
405428
client.request({
@@ -446,7 +469,11 @@ function pipeliningNonIdempotentWithBody (bodyType) {
446469
pipelining: 2
447470
})
448471
after(() => client.close())
449-
guardDisconnect(client, t)
472+
client.on('disconnect', () => {
473+
if (!client.closed && !client.destroyed) {
474+
t.fail('unexpected disconnect')
475+
}
476+
})
450477

451478
let ended = false
452479
let reading = false
@@ -755,7 +782,11 @@ test('pipelining blocked', async (t) => {
755782
pipelining: 10
756783
})
757784
after(() => client.close())
758-
guardDisconnect(client, t)
785+
client.on('disconnect', () => {
786+
if (!client.closed && !client.destroyed) {
787+
t.fail('unexpected disconnect')
788+
}
789+
})
759790
client.request({
760791
path: '/',
761792
method: 'GET',

test/client-post.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const { test, after } = require('node:test')
55
const { once } = require('node:events')
66
const { Client } = require('..')
77
const { createServer } = require('node:http')
8-
const { guardDisconnect } = require('./guard-disconnect')
98

109
test('request post blob', async (t) => {
1110
t = tspl(t, { plan: 3 })
@@ -27,7 +26,11 @@ test('request post blob', async (t) => {
2726

2827
const client = new Client(`http://localhost:${server.address().port}`)
2928
after(client.destroy.bind(client))
30-
guardDisconnect(client, t)
29+
client.on('disconnect', () => {
30+
if (!client.closed && !client.destroyed) {
31+
t.fail('unexpected disconnect')
32+
}
33+
})
3134

3235
client.request({
3336
path: '/',
@@ -64,7 +67,11 @@ test('request post arrayBuffer', async (t) => {
6467

6568
const client = new Client(`http://localhost:${server.address().port}`)
6669
after(() => client.destroy())
67-
guardDisconnect(client, t)
70+
client.on('disconnect', () => {
71+
if (!client.closed && !client.destroyed) {
72+
t.fail('unexpected disconnect')
73+
}
74+
})
6875

6976
const buf = Buffer.from('asd')
7077
const dst = new ArrayBuffer(buf.byteLength)

test/client-stream.js

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { Client, errors } = require('..')
66
const { createServer } = require('node:http')
77
const { PassThrough, Writable, Readable } = require('node:stream')
88
const EE = require('node:events')
9-
const { guardDisconnect } = require('./guard-disconnect')
109

1110
test('stream get', async (t) => {
1211
t = tspl(t, { plan: 9 })
@@ -23,7 +22,11 @@ test('stream get', async (t) => {
2322
server.listen(0, () => {
2423
const client = new Client(`http://localhost:${server.address().port}`)
2524
after(() => client.close())
26-
guardDisconnect(client, t)
25+
client.on('disconnect', () => {
26+
if (!client.closed && !client.destroyed) {
27+
t.fail('unexpected disconnect')
28+
}
29+
})
2730

2831
const signal = new EE()
2932
client.stream({
@@ -67,7 +70,11 @@ test('stream promise get', async (t) => {
6770
server.listen(0, async () => {
6871
const client = new Client(`http://localhost:${server.address().port}`)
6972
after(() => client.close())
70-
guardDisconnect(client, t)
73+
client.on('disconnect', () => {
74+
if (!client.closed && !client.destroyed) {
75+
t.fail('unexpected disconnect')
76+
}
77+
})
7178

7279
await client.stream({
7380
path: '/',
@@ -257,7 +264,11 @@ test('stream waits only for writable side', async (t) => {
257264
server.listen(0, () => {
258265
const client = new Client(`http://localhost:${server.address().port}`)
259266
after(() => client.close())
260-
guardDisconnect(client, t)
267+
client.on('disconnect', () => {
268+
if (!client.closed && !client.destroyed) {
269+
t.fail('unexpected disconnect')
270+
}
271+
})
261272

262273
const pt = new PassThrough({ autoDestroy: false })
263274
client.stream({
@@ -325,7 +336,11 @@ test('stream destroy if not readable', async (t) => {
325336
server.listen(0, () => {
326337
const client = new Client(`http://localhost:${server.address().port}`)
327338
after(client.destroy.bind(client))
328-
guardDisconnect(client, t)
339+
client.on('disconnect', () => {
340+
if (!client.closed && !client.destroyed) {
341+
t.fail('unexpected disconnect')
342+
}
343+
})
329344

330345
client.stream({
331346
path: '/',
@@ -402,7 +417,11 @@ test('stream body without destroy', async (t) => {
402417
server.listen(0, () => {
403418
const client = new Client(`http://localhost:${server.address().port}`)
404419
after(client.destroy.bind(client))
405-
guardDisconnect(client, t)
420+
client.on('disconnect', () => {
421+
if (!client.closed && !client.destroyed) {
422+
t.fail('unexpected disconnect')
423+
}
424+
})
406425

407426
client.stream({
408427
path: '/',
@@ -526,7 +545,11 @@ test('stream abort after complete', async (t) => {
526545
server.listen(0, () => {
527546
const client = new Client(`http://localhost:${server.address().port}`)
528547
after(client.destroy.bind(client))
529-
guardDisconnect(client, t)
548+
client.on('disconnect', () => {
549+
if (!client.closed && !client.destroyed) {
550+
t.fail('unexpected disconnect')
551+
}
552+
})
530553

531554
const pt = new PassThrough()
532555
const signal = new EE()
@@ -587,7 +610,11 @@ test('trailers', async (t) => {
587610
server.listen(0, () => {
588611
const client = new Client(`http://localhost:${server.address().port}`)
589612
after(() => client.close())
590-
guardDisconnect(client, t)
613+
client.on('disconnect', () => {
614+
if (!client.closed && !client.destroyed) {
615+
t.fail('unexpected disconnect')
616+
}
617+
})
591618

592619
client.stream({
593620
path: '/',
@@ -613,7 +640,11 @@ test('stream ignore 1xx', async (t) => {
613640
server.listen(0, () => {
614641
const client = new Client(`http://localhost:${server.address().port}`)
615642
after(() => client.close())
616-
guardDisconnect(client, t)
643+
client.on('disconnect', () => {
644+
if (!client.closed && !client.destroyed) {
645+
t.fail('unexpected disconnect')
646+
}
647+
})
617648

618649
let buf = ''
619650
client.stream({
@@ -646,7 +677,11 @@ test('stream ignore 1xx and use onInfo', async (t) => {
646677
server.listen(0, () => {
647678
const client = new Client(`http://localhost:${server.address().port}`)
648679
after(() => client.close())
649-
guardDisconnect(client, t)
680+
client.on('disconnect', () => {
681+
if (!client.closed && !client.destroyed) {
682+
t.fail('unexpected disconnect')
683+
}
684+
})
650685

651686
let buf = ''
652687
client.stream({
@@ -685,7 +720,11 @@ test('stream backpressure', async (t) => {
685720
server.listen(0, () => {
686721
const client = new Client(`http://localhost:${server.address().port}`)
687722
after(() => client.close())
688-
guardDisconnect(client, t)
723+
client.on('disconnect', () => {
724+
if (!client.closed && !client.destroyed) {
725+
t.fail('unexpected disconnect')
726+
}
727+
})
689728

690729
let buf = ''
691730
client.stream({
@@ -747,7 +786,11 @@ test('stream needDrain', async (t) => {
747786
after(() => {
748787
client.destroy()
749788
})
750-
guardDisconnect(client, t)
789+
client.on('disconnect', () => {
790+
if (!client.closed && !client.destroyed) {
791+
t.fail('unexpected disconnect')
792+
}
793+
})
751794

752795
const dst = new PassThrough()
753796
dst.pause()
@@ -804,7 +847,11 @@ test('stream legacy needDrain', async (t) => {
804847
after(() => {
805848
client.destroy()
806849
})
807-
guardDisconnect(client, t)
850+
client.on('disconnect', () => {
851+
if (!client.closed && !client.destroyed) {
852+
t.fail('unexpected disconnect')
853+
}
854+
})
808855

809856
const dst = new PassThrough()
810857
dst.pause()

test/client-timeout.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const { createServer } = require('node:http')
77
const { Readable } = require('node:stream')
88
const FakeTimers = require('@sinonjs/fake-timers')
99
const timers = require('../lib/util/timers')
10-
const { guardDisconnect } = require('./guard-disconnect')
1110

1211
test('refresh timeout on pause', async (t) => {
1312
t = tspl(t, { plan: 1 })
@@ -181,7 +180,11 @@ test('parser resume with no body timeout', async (t) => {
181180
})
182181
after(() => client.destroy())
183182

184-
guardDisconnect(client, t)
183+
client.on('disconnect', () => {
184+
if (!client.closed && !client.destroyed) {
185+
t.fail('unexpected disconnect')
186+
}
187+
})
185188

186189
client.dispatch({
187190
path: '/',

0 commit comments

Comments
 (0)