Skip to content

server.mjs does not wait for listen() before test runner starts #161

@trivikr

Description

@trivikr

The Node test server starts asynchronously, but test-engine/server/server.mjs currently calls server.listen(port) without exposing or awaiting readiness.

In consumers that start the server via side-effect import, for example:

await import('./test-engine/server/server.mjs')
await runTests(...)

the import can resolve before the server has emitted listening. On slower platforms, especially Windows CI, the first client request can race server startup and fail intermittently.

Observed symptoms:

SERVER WARNING: State not found for <uuid>
Error: Process completed with exit code 1

The likely sequence is:

  1. Worker imports server.mjs.
  2. server.listen(port) is called, but the server is not ready yet.
  3. Test setup sends PUT /config/<uuid>.
  4. The request can fail before the server accepts connections.
  5. The harness continues to later GET /state/<uuid>.
  6. Since no config/state was created, the server logs State not found.

Suggested fix:

await new Promise((resolve, reject) => {
  server.once('listening', resolve)
  server.once('error', reject)
  server.listen(port)
})

This preserves the existing side-effect import behavior while making await import('./server.mjs') mean the server is actually listening before tests proceed.

This was found while running Undici’s cache interceptor test harness against the vendored cache-tests submodule in nodejs/undici#5199

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions