Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@tapjs/clock": "3.0.0",
"@types/debug": "4.1.12",
"@types/ms": "0.7.34",
"@types/node": "20.16.11",
"@types/node": "18.19.55",
"@types/sinonjs__fake-timers": "8.1.5",
"@types/stoppable": "1.1.3",
"@types/tap": "15.0.12",
Expand Down
36 changes: 24 additions & 12 deletions test/unit/undici-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ test('Timeout support / 1', async t => {
t.plan(1)

function handler (_req: http.IncomingMessage, res: http.ServerResponse) {
setTimeout(() => res.end('ok'), 100)
setTimeout(() => res.end('ok'), 1000)
}

const [{ port }, server] = await buildServer(handler)
const connection = new UndiciConnection({
url: new URL(`http://localhost:${port}`),
timeout: 50
timeout: 600
})

try {
Expand All @@ -148,22 +148,26 @@ test('Timeout support / 1', async t => {

test('Timeout support / 2', async t => {
t.plan(2)
t.clock.enter()
t.teardown(() => t.clock.exit())

function handler (_req: http.IncomingMessage, res: http.ServerResponse) {
res.writeHead(200, { 'content-type': 'text/plain' })
setTimeout(() => res.end('ok'), 100)
setTimeout(() => res.end('ok'), 1000)
}

const [{ port }, server] = await buildServer(handler)
const connection = new UndiciConnection({
url: new URL(`http://localhost:${port}`),
timeout: 50
timeout: 600
})
try {
await connection.request({
const res = connection.request({
path: '/hello',
method: 'GET'
}, options)
t.clock.advance(600)
await res
} catch (err: any) {
t.ok(err instanceof TimeoutError)
t.equal(err.message, 'Request timed out')
Expand All @@ -173,24 +177,28 @@ test('Timeout support / 2', async t => {

test('Timeout support / 3', async t => {
t.plan(2)
t.clock.enter()
t.teardown(() => t.clock.exit())

function handler (_req: http.IncomingMessage, res: http.ServerResponse) {
setTimeout(() => res.end('ok'), 100)
setTimeout(() => res.end('ok'), 1000)
}

const [{ port }, server] = await buildServer(handler)
const connection = new UndiciConnection({
url: new URL(`http://localhost:${port}`),
timeout: 200
timeout: 600
})
try {
await connection.request({
const res = connection.request({
path: '/hello',
method: 'GET'
}, {
timeout: 50,
timeout: 600,
...options
})
t.clock.advance(600)
await res
} catch (err: any) {
t.ok(err instanceof TimeoutError)
t.equal(err.message, 'Request timed out')
Expand All @@ -200,26 +208,30 @@ test('Timeout support / 3', async t => {

test('Timeout support / 4', async t => {
t.plan(2)
t.clock.enter()
t.teardown(() => t.clock.exit())

function handler (_req: http.IncomingMessage, res: http.ServerResponse) {
setTimeout(() => res.end('ok'), 100)
setTimeout(() => res.end('ok'), 1000)
}

const [{ port }, server] = await buildServer(handler)
const connection = new UndiciConnection({
url: new URL(`http://localhost:${port}`),
timeout: 200
timeout: 2000
})
const abortController = new AbortController()
try {
await connection.request({
const res = connection.request({
path: '/hello',
method: 'GET',
}, {
signal: abortController.signal,
timeout: 50,
...options
})
t.clock.advance(1000)
await res
t.fail('Timeout was not reached')
} catch (err: any) {
t.ok(err instanceof TimeoutError)
Expand Down