Skip to content

Commit

Permalink
test: fix dns interceptor flakiness (#3902)
Browse files Browse the repository at this point in the history
  • Loading branch information
luddd3 authored Nov 29, 2024
1 parent 7cd36d1 commit 4b85f4e
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions test/interceptors/dns.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const FakeTimers = require('@sinonjs/fake-timers')
const { test, after } = require('node:test')
const { isIP } = require('node:net')
const { lookup } = require('node:dns')
const { createServer } = require('node:http')
const { createServer: createSecureServer } = require('node:https')
const { once } = require('node:events')
const { setTimeout: sleep } = require('node:timers/promises')

const { tspl } = require('@matteo.collina/tspl')
const pem = require('https-pem')
Expand Down Expand Up @@ -613,6 +613,7 @@ test('Should automatically resolve IPs (dual stack disabled - 6)', async t => {
test('Should we handle TTL (4)', async t => {
t = tspl(t, { plan: 10 })

const clock = FakeTimers.install()
let counter = 0
let lookupCounter = 0
const server = createServer()
Expand Down Expand Up @@ -674,6 +675,7 @@ test('Should we handle TTL (4)', async t => {
])

after(async () => {
clock.uninstall()
await client.close()
server.close()

Expand All @@ -688,7 +690,7 @@ test('Should we handle TTL (4)', async t => {
t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

await sleep(200)
clock.tick(200)

const response2 = await client.request({
...requestOptions,
Expand All @@ -698,7 +700,7 @@ test('Should we handle TTL (4)', async t => {
t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world!')

await sleep(300)
clock.tick(300)

const response3 = await client.request({
...requestOptions,
Expand All @@ -714,6 +716,7 @@ test('Should we handle TTL (4)', async t => {
test('Should we handle TTL (6)', async t => {
t = tspl(t, { plan: 10 })

const clock = FakeTimers.install()
let counter = 0
let lookupCounter = 0
const server = createServer()
Expand Down Expand Up @@ -778,6 +781,7 @@ test('Should we handle TTL (6)', async t => {
])

after(async () => {
clock.uninstall()
await client.close()
server.close()

Expand All @@ -792,7 +796,7 @@ test('Should we handle TTL (6)', async t => {
t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

await sleep(200)
clock.tick(200)

const response2 = await client.request({
...requestOptions,
Expand All @@ -802,7 +806,7 @@ test('Should we handle TTL (6)', async t => {
t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world!')

await sleep(300)
clock.tick(300)

const response3 = await client.request({
...requestOptions,
Expand All @@ -817,6 +821,7 @@ test('Should we handle TTL (6)', async t => {
test('Should set lowest TTL between resolved and option maxTTL', async t => {
t = tspl(t, { plan: 9 })

const clock = FakeTimers.install()
let lookupCounter = 0
const server = createServer()
const requestOptions = {
Expand Down Expand Up @@ -855,6 +860,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
)

after(async () => {
clock.uninstall()
await client.close()
server.close()

Expand All @@ -869,7 +875,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

await sleep(100)
clock.tick(100)

// 100ms: lookup since ttl = Math.min(50, maxTTL: 200)
const response2 = await client.request({
Expand All @@ -880,7 +886,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world!')

await sleep(100)
clock.tick(100)

// 100ms: cached since ttl = Math.min(500, maxTTL: 200)
const response3 = await client.request({
Expand All @@ -891,7 +897,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
t.equal(response3.statusCode, 200)
t.equal(await response3.body.text(), 'hello world!')

await sleep(150)
clock.tick(150)

// 250ms: lookup since ttl = Math.min(500, maxTTL: 200)
const response4 = await client.request({
Expand Down Expand Up @@ -1191,6 +1197,7 @@ test('Should use all dns entries (dual stack disabled - 6)', async t => {
test('Should handle single family resolved (dual stack)', async t => {
t = tspl(t, { plan: 7 })

const clock = FakeTimers.install()
let counter = 0
let lookupCounter = 0
const server = createServer()
Expand Down Expand Up @@ -1250,6 +1257,7 @@ test('Should handle single family resolved (dual stack)', async t => {
])

after(async () => {
clock.uninstall()
await client.close()
server.close()

Expand All @@ -1264,7 +1272,7 @@ test('Should handle single family resolved (dual stack)', async t => {
t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

await sleep(100)
clock.tick(100)

const response2 = await client.request({
...requestOptions,
Expand All @@ -1280,6 +1288,7 @@ test('Should handle single family resolved (dual stack)', async t => {
test('Should prefer affinity (dual stack - 4)', async t => {
t = tspl(t, { plan: 10 })

const clock = FakeTimers.install()
let counter = 0
let lookupCounter = 0
const server = createServer()
Expand Down Expand Up @@ -1342,6 +1351,7 @@ test('Should prefer affinity (dual stack - 4)', async t => {
])

after(async () => {
clock.uninstall()
await client.close()
server.close()

Expand All @@ -1356,7 +1366,7 @@ test('Should prefer affinity (dual stack - 4)', async t => {
t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

await sleep(100)
clock.tick(100)

const response2 = await client.request({
...requestOptions,
Expand All @@ -1380,6 +1390,7 @@ test('Should prefer affinity (dual stack - 4)', async t => {
test('Should prefer affinity (dual stack - 6)', async t => {
t = tspl(t, { plan: 10 })

const clock = FakeTimers.install()
let counter = 0
let lookupCounter = 0
const server = createServer()
Expand Down Expand Up @@ -1442,6 +1453,7 @@ test('Should prefer affinity (dual stack - 6)', async t => {
])

after(async () => {
clock.uninstall()
await client.close()
server.close()

Expand All @@ -1456,7 +1468,7 @@ test('Should prefer affinity (dual stack - 6)', async t => {
t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

await sleep(100)
clock.tick(100)

const response2 = await client.request({
...requestOptions,
Expand Down

0 comments on commit 4b85f4e

Please sign in to comment.