Skip to content

test: add unexpected disconnect guards to more client test files#4844

Open
samayer12 wants to merge 12 commits intonodejs:mainfrom
samayer12:251-disconnect-checks
Open

test: add unexpected disconnect guards to more client test files#4844
samayer12 wants to merge 12 commits intonodejs:mainfrom
samayer12:251-disconnect-checks

Conversation

@samayer12
Copy link
Contributor

@samayer12 samayer12 commented Feb 27, 2026

This relates to...

Relates to #251 and #4833.

Rationale

Adds disconnect guard assertions to unit tests that follow the pattern seen in #4833. This PR adds the guard to additional tests that do not already adopt the pattern, and documents it.

Changes

Disconnect guards

This PR structures tests along these lines:

  client.on('disconnect', () => {
    if (!client.closed && !client.destroyed) {
      t.fail('unexpected disconnect')
    }
  })

Fix encoding test (Node 20 + macOS)

I noticed that there were CI issues with test/fetch/encoding.js on Node.js 20 + macOS. As part of this PR, I also changed the chain limit test server from server.listen(0, 'listening') to specify 127.0.0.1 which fixed the test failure's ECONNRESET error. I also added flushHeaders() and per-socket setNoDelay() as seen in #4496.

Features

N/A

Bug Fixes

N/A

Breaking Changes and Deprecations

None

Benchmarks

[bench:run] ┌─────────┬───────────────────────┬─────────┬────────────────────┬────────────┬─────────────────────────┐
[bench:run] │ (index) │ Tests                 │ Samples │ Result             │ Tolerance  │ Difference with slowest │
[bench:run] ├─────────┼───────────────────────┼─────────┼────────────────────┼────────────┼─────────────────────────┤
[bench:run] │ 0       │ 'http - no keepalive' │ 85      │ '4648.39 req/sec'  │ '± 2.91 %' │ '-'                     │
[bench:run] │ 1       │ 'node-fetch'          │ 20      │ '11330.13 req/sec' │ '± 2.88 %' │ '+ 143.74 %'            │
[bench:run] │ 2       │ 'axios'               │ 15      │ '11494.72 req/sec' │ '± 2.82 %' │ '+ 147.28 %'            │
[bench:run] │ 3       │ 'got'                 │ 20      │ '12015.76 req/sec' │ '± 2.92 %' │ '+ 158.49 %'            │
[bench:run] │ 4       │ 'undici - fetch'      │ 40      │ '14663.42 req/sec' │ '± 2.82 %' │ '+ 215.45 %'            │
[bench:run] │ 5       │ 'request'             │ 35      │ '14892.99 req/sec' │ '± 2.82 %' │ '+ 220.39 %'            │
[bench:run] │ 6       │ 'superagent'          │ 15      │ '18511.94 req/sec' │ '± 2.25 %' │ '+ 298.24 %'            │
[bench:run] │ 7       │ 'http - keepalive'    │ 15      │ '19108.18 req/sec' │ '± 2.74 %' │ '+ 311.07 %'            │
[bench:run] │ 8       │ 'undici - pipeline'   │ 55      │ '22599.61 req/sec' │ '± 2.99 %' │ '+ 386.18 %'            │
[bench:run] │ 9       │ 'undici - stream'     │ 30      │ '25741.88 req/sec' │ '± 2.96 %' │ '+ 453.78 %'            │
[bench:run] │ 10      │ 'undici - request'    │ 10      │ '28529.09 req/sec' │ '± 2.17 %' │ '+ 513.74 %'            │
[bench:run] │ 11      │ 'undici - dispatch'   │ 20      │ '29355.94 req/sec' │ '± 2.96 %' │ '+ 531.53 %'            │
[bench:run] └─────────┴───────────────────────┴─────────┴────────────────────┴────────────┴─────────────────────────┘

Status

@samayer12 samayer12 force-pushed the 251-disconnect-checks branch 2 times, most recently from 5c94a36 to 8e69a9f Compare February 27, 2026 22:39
@samayer12 samayer12 marked this pull request as ready for review February 27, 2026 22:57
@codecov-commenter
Copy link

codecov-commenter commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.01%. Comparing base (b12081c) to head (ba79c89).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4844      +/-   ##
==========================================
- Coverage   93.02%   93.01%   -0.02%     
==========================================
  Files         112      112              
  Lines       35157    35157              
==========================================
- Hits        32705    32701       -4     
- Misses       2452     2456       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@samayer12 samayer12 force-pushed the 251-disconnect-checks branch 3 times, most recently from a88b414 to 8e69a9f Compare March 2, 2026 15:39
samayer12 and others added 10 commits March 6, 2026 12:39
Signed-off-by: Sam Mayer <sam.mayer@datadoghq.com>
Signed-off-by: Sam Mayer <sam.mayer@defenseunicorns.com>
Signed-off-by: Sam Mayer <sam.mayer@defenseunicorns.com>
…est files

Signed-off-by: Sam Mayer <sam.mayer@defenseunicorns.com>
The first describe block used the global agent, leaving pooled connections alive after server.closeAllConnections() sent RST. On macOS, the async RST delivery could surface as ECONNRESET in the next describe block's fetch call. Give each block its own Client and await cleanup to eliminate the race.

Signed-off-by: Sam Mayer <sam.mayer@defenseunicorns.com>
…ervers

The chain limit tests ran concurrently (node:test uses Promise.all within
a Suite), so a shared server was unsafe: an aborted connection's async RST
delivery on macOS could corrupt the server state for a sibling test.

Replace the shared before/after server with a setupChainServer(t) helper
that creates an isolated server+client pair per test and binds cleanup to
t.after, which is scoped to each individual test context. Also removes the
keepalive: false no-op (undici documents this flag as a noop outside of
browser context).
@samayer12 samayer12 force-pushed the 251-disconnect-checks branch from 2737414 to 5cf144d Compare March 6, 2026 18:39
@samayer12 samayer12 force-pushed the 251-disconnect-checks branch from 13e6c47 to 02ce816 Compare March 6, 2026 19:05
@samayer12 samayer12 requested a review from metcoder95 March 6, 2026 20:36

await t.assert.rejects(
fetch(`http://localhost:${server.address().port}`, {
fetch(`http://127.0.0.1:${server.address().port}`, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the loopback change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants