Skip to content

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

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

test: add unexpected disconnect guards to more client test files#4844
samayer12 wants to merge 10 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 a guardDisconnect() helper to unit tests that previously used the more verbose pattern seen in #4833. This PR also adds the guard function to additional tests that do not already adopt the pattern. Docs are also updated for future test authors.

I think that 8e69a9f also resolves a test cleanup issue in CI with node 20 tests on macos-latest, seen here.

Note that c8 fails coverage in CI and is related to bcoe/c8#581 and bcoe/c8#582.

Changes

This PR recommended structuring tests along these lines:

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

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

  guardDisconnect(client, t)

  // ... test logic ...
})

Following feedback, we prefer to make the function utility explicit and just provide a set of assertions calculated. Therefore, we'll stick with:

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

Here are the benchmark results from a local run:

[bench:run] ┌─────────┬───────────────────────┬─────────┬────────────────────┬────────────┬─────────────────────────┐
[bench:run] │ (index) │ Tests                 │ Samples │ Result             │ Tolerance  │ Difference with slowest │
[bench:run] ├─────────┼───────────────────────┼─────────┼────────────────────┼────────────┼─────────────────────────┤
[bench:run] │ 0       │ 'http - no keepalive' │ 50      │ '5752.85 req/sec'  │ '± 2.82 %' │ '-'                     │
[bench:run] │ 1       │ 'got'                 │ 70      │ '11935.17 req/sec' │ '± 2.93 %' │ '+ 107.47 %'            │
[bench:run] │ 2       │ 'node-fetch'          │ 30      │ '11967.99 req/sec' │ '± 2.80 %' │ '+ 108.04 %'            │
[bench:run] │ 3       │ 'undici - fetch'      │ 35      │ '14422.02 req/sec' │ '± 2.91 %' │ '+ 150.69 %'            │
[bench:run] │ 4       │ 'axios'               │ 15      │ '14573.42 req/sec' │ '± 2.56 %' │ '+ 153.33 %'            │
[bench:run] │ 5       │ 'request'             │ 25      │ '15760.95 req/sec' │ '± 2.87 %' │ '+ 173.97 %'            │
[bench:run] │ 6       │ 'http - keepalive'    │ 20      │ '17944.82 req/sec' │ '± 2.91 %' │ '+ 211.93 %'            │
[bench:run] │ 7       │ 'superagent'          │ 20      │ '18427.55 req/sec' │ '± 2.58 %' │ '+ 220.32 %'            │
[bench:run] │ 8       │ 'undici - pipeline'   │ 30      │ '22861.79 req/sec' │ '± 2.98 %' │ '+ 297.40 %'            │
[bench:run] │ 9       │ 'undici - request'    │ 20      │ '26651.34 req/sec' │ '± 2.94 %' │ '+ 363.27 %'            │
[bench:run] │ 10      │ 'undici - dispatch'   │ 25      │ '27216.23 req/sec' │ '± 2.64 %' │ '+ 373.09 %'            │
[bench:run] │ 11      │ 'undici - stream'     │ 15      │ '27222.67 req/sec' │ '± 2.74 %' │ '+ 373.20 %'            │
[bench:run] └─────────┴───────────────────────┴─────────┴────────────────────┴────────────┴─────────────────────────┘

Features

N/A

Bug Fixes

N/A

Breaking Changes and Deprecations

None

Status

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>
samayer12 and others added 2 commits February 27, 2026 15:30
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>
@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.14%. Comparing base (85cde5d) to head (2737414).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4844   +/-   ##
=======================================
  Coverage   93.14%   93.14%           
=======================================
  Files         109      109           
  Lines       34239    34239           
=======================================
  Hits        31891    31891           
  Misses       2348     2348           

☔ 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
…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).
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