nodejs / undici

An HTTP/1.1 client, written from scratch for Node.js
https://nodejs.github.io/undici
MIT License
6.19k stars 540 forks source link

Reading request body never consumes anything when using Jest mock timers #1251

Open vlovich opened 2 years ago

vlovich commented 2 years ago

Bug Description

Modern Jest fake timers seem to cause problems for reading the body of Undici Requests.

Reproducible By

The following code deadlocks:

import { Request } from 'undici'

beforeEach(() => {
  const mockTimeBase = 1643948235000

  jest.useFakeTimers('modern')
  jest.setSystemTime(mockTimeBase)
})

afterEach(() => {
  jest.useRealTimers()
})

test('request and jest fake timers problem', async () => {
    const req = new Request('http://example.com', { method: 'POST', body: 'some message' })
    console.log('Awaiting undici request')
    console.log(await req.text())
    console.log('Done')
})

Expected Behavior

The request body should be consumed during reading even when using fake timers.

Logs & Screenshots

Environment

Node 17.6 Jest 27.2.5 Undici 4.14.1 (also reproduced on 4.13.0)

Additional context

Removing the use of fake timers or using the Node Request builtin (i.e. comment out the first line) causes the test to complete as expected. If you convert the .text() invocation to instead call read() on the reader for the body, read() always returns 'some message' as the value and done is always false. Jest fake timers say they impact queueMicrotask & I noticed the streams in undici use queueMicrotask which may be related to the problem. Even if there's no way to strictly fix this within undici, it would be nice for there to be an escape hatch of some kind (e.g. some callback I can register to call jest.runAllTimers probably fixes the issue)

mcollina commented 2 years ago

An option is to completely bypass jest fake objects and use Node primordials (I'm probably surprised I'm saying this). We might consider exposing them (cc @aduh95).

A PR to fix this would be welcomed.

vlovich commented 2 years ago

Hmm... I'm not entirely certain I understood what that comment means. Are you saying to use Node's Request/Response objects directly rather than the undici implementation? Or are you saying to somehow make sure to grab the true queueMicrotask implementation at global scope to avoid Jest injecting fake timers having an impact? Or something else entirely?

mcollina commented 2 years ago

Are you saying to use Node's Request/Response objects directly rather than the undici implementation?

I don't exactly know what are Node.js Request and Response object. Node.js implementations of those are based on the undici ones, so some other libraries might be providing them for you.

Or are you saying to somehow make sure to grab the true queueMicrotask implementation at global scope to avoid Jest injecting fake timers having an impact?

This would be my idea. Try to bypass jest completely.

(I would actually recommend you to stop using it).

vlovich commented 2 years ago

FWIW Sinon.JS is only mildly better. I can't tell if it's undici now or Miniflare, but even with Sinon.JS fake timers, a second request to a Durable Object in Miniflare just hangs.

mcollina commented 2 years ago

I think you'd need to advanced time when mocking it. We use a few process.nextTick() in the Node.js WHATWG implementations.

aduh95 commented 2 years ago

FWIW queueMicrotask is not available in primordials (it's not an API provided by V8, Node.js does its own implementation). I think we could definitely expose it, maybe as import { queueMicrotask } from 'node:timers/promises'.