strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('..')5const { Client } = require('undici')6 7test('Should return 503 while closing - pipelining', async t => {8 const fastify = Fastify({9 return503OnClosing: true,10 forceCloseConnections: false11 })12 13 fastify.get('/', async (req, reply) => {14 fastify.close()15 return { hello: 'world' }16 })17 18 await fastify.listen({ port: 0 })19 20 const instance = new Client('http://localhost:' + fastify.server.address().port, {21 pipelining: 222 })23 24 const codes = [200, 200, 503]25 const responses = await Promise.all([26 instance.request({ path: '/', method: 'GET' }),27 instance.request({ path: '/', method: 'GET' }),28 instance.request({ path: '/', method: 'GET' })29 ])30 const actual = responses.map(r => r.statusCode)31 t.assert.deepStrictEqual(actual, codes)32 33 await instance.close()34})35 36test('Should close the socket abruptly - pipelining - return503OnClosing: false', async t => {37 // Since Node v20, we will always invoke server.closeIdleConnections()38 // therefore our socket will be closed39 const fastify = Fastify({40 return503OnClosing: false,41 forceCloseConnections: false42 })43 44 fastify.get('/', async (req, reply) => {45 reply.send({ hello: 'world' })46 fastify.close()47 })48 49 await fastify.listen({ port: 0 })50 51 const instance = new Client('http://localhost:' + fastify.server.address().port, {52 pipelining: 253 })54 55 const responses = await Promise.allSettled([56 instance.request({ path: '/', method: 'GET' }),57 instance.request({ path: '/', method: 'GET' }),58 instance.request({ path: '/', method: 'GET' }),59 instance.request({ path: '/', method: 'GET' })60 ])61 62 t.assert.strictEqual(responses[0].status, 'fulfilled')63 t.assert.strictEqual(responses[1].status, 'fulfilled')64 t.assert.strictEqual(responses[2].status, 'rejected')65 t.assert.strictEqual(responses[3].status, 'rejected')66 67 await instance.close()68})69 