Repository: fastify/fastify
File: test/close-pipelining.test.js (JavaScript)

Code before:
  })

  await fastify.listen({ port: 0 })

  const instance = new Client('http://localhost:' + fastify.server.address().port, {
    pipelining: 2
  })

  const [firstRequest, secondRequest, thirdRequest] = await Promise.allSettled([
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false })
  ])
  t.assert.strictEqual(firstRequest.status, 'fulfilled')
  t.assert.strictEqual(secondRequest.status, 'fulfilled')

  t.assert.strictEqual(firstRequest.value.statusCode, 200)
  t.assert.strictEqual(secondRequest.value.statusCode, 200)

  t.assert.strictEqual(thirdRequest.status, 'fulfilled')
  t.assert.strictEqual(thirdRequest.value.statusCode, 503)

  await instance.close()
})

test('Should close the socket abruptly - pipelining - return503OnClosing: false', { skip: 'flaky test' }, async t => {
  // Since Node v20, we will always invoke server.closeIdleConnections()
  // therefore our socket will be closed
  const fastify = Fastify({
    return503OnClosing: false,
    forceCloseConnections: false
  })

  fastify.get('/', async (req, reply) => {
    // Simulate a delay to allow pipelining to kick in
    await new Promise(resolve => setTimeout(resolve, 5))
    reply.send({ hello: 'world' })
    fastify.close()
  })

  await fastify.listen({ port: 0 })

  const instance = new Client('http://localhost:' + fastify.server.address().port, {
    pipelining: 1
  })

  const responses = await Promise.allSettled([
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false })

Review comment (suggestion):
```suggestion
test('Should close the socket abruptly - pipelining - return503OnClosing: false', async t => {
```

Code after:
  })

  await fastify.listen({ port: 0 })

  const instance = new Client('http://localhost:' + fastify.server.address().port, {
    pipelining: 2
  })

  const [firstRequest, secondRequest, thirdRequest] = await Promise.allSettled([
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false })
  ])
  t.assert.strictEqual(firstRequest.status, 'fulfilled')
  t.assert.strictEqual(secondRequest.status, 'fulfilled')

  t.assert.strictEqual(firstRequest.value.statusCode, 200)
  t.assert.strictEqual(secondRequest.value.statusCode, 200)

  t.assert.strictEqual(thirdRequest.status, 'fulfilled')
  t.assert.strictEqual(thirdRequest.value.statusCode, 503)

  await instance.close()
})

test('Should close the socket abruptly - pipelining - return503OnClosing: false', async t => {
  // Since Node v20, we will always invoke server.closeIdleConnections()
  // therefore our socket will be closed
  const fastify = Fastify({
    return503OnClosing: false,
    forceCloseConnections: false
  })

  fastify.get('/', async (req, reply) => {
    // Simulate a delay to allow pipelining to kick in
    await new Promise(resolve => setTimeout(resolve, 5))
    reply.send({ hello: 'world' })
    fastify.close()
  })

  await fastify.listen({ port: 0 })

  const instance = new Client('http://localhost:' + fastify.server.address().port, {
    pipelining: 1
  })

  const responses = await Promise.allSettled([
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false }),
    instance.request({ path: '/', method: 'GET', blocking: false })