Skip to content

Commit

Permalink
fix: deflake connect-timeout test (#2851)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Feb 26, 2024
1 parent 6bb0c9b commit 334adc0
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions test/connect-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ const { test, after, describe } = require('node:test')
const { Client, Pool, errors } = require('..')
const net = require('node:net')
const assert = require('node:assert')
const sleep = ms => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, Number(ms))

// Using describe instead of test to avoid the timeout
describe('prioritize socket errors over timeouts', () => {
describe('prioritize socket errors over timeouts', async () => {
const t = tspl({ ...assert, after: () => {} }, { plan: 1 })
const connectTimeout = 1000
const client = new Pool('http://foobar.bar:1234', { connectTimeout: 2 })
const client = new Pool('http://foobar.bar:1234', { connectTimeout: 1 })

client.request({ method: 'GET', path: '/foobar' })
.then(() => t.fail())
.catch((err) => {
t.strictEqual(['ENOTFOUND', 'EAI_AGAIN'].includes(err.code), true)
t.strictEqual(err.code !== 'UND_ERR_CONNECT_TIMEOUT', true)
})

// block for 1s which is enough for the dns lookup to complete and TO to fire
sleep(connectTimeout)
// block for 1s which is enough for the dns lookup to complete and the
// Timeout to fire
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, Number(1000))

await t.completed
})

// never connect
// mock net.connect to avoid the dns lookup
net.connect = function (options) {
return new net.Socket(options)
}
Expand Down

0 comments on commit 334adc0

Please sign in to comment.