Skip to content

Commit 4b85f4e

Browse files
authored
test: fix dns interceptor flakiness (#3902)
1 parent 7cd36d1 commit 4b85f4e

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

test/interceptors/dns.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

3+
const FakeTimers = require('@sinonjs/fake-timers')
34
const { test, after } = require('node:test')
45
const { isIP } = require('node:net')
56
const { lookup } = require('node:dns')
67
const { createServer } = require('node:http')
78
const { createServer: createSecureServer } = require('node:https')
89
const { once } = require('node:events')
9-
const { setTimeout: sleep } = require('node:timers/promises')
1010

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

616+
const clock = FakeTimers.install()
616617
let counter = 0
617618
let lookupCounter = 0
618619
const server = createServer()
@@ -674,6 +675,7 @@ test('Should we handle TTL (4)', async t => {
674675
])
675676

676677
after(async () => {
678+
clock.uninstall()
677679
await client.close()
678680
server.close()
679681

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

691-
await sleep(200)
693+
clock.tick(200)
692694

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

701-
await sleep(300)
703+
clock.tick(300)
702704

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

719+
const clock = FakeTimers.install()
717720
let counter = 0
718721
let lookupCounter = 0
719722
const server = createServer()
@@ -778,6 +781,7 @@ test('Should we handle TTL (6)', async t => {
778781
])
779782

780783
after(async () => {
784+
clock.uninstall()
781785
await client.close()
782786
server.close()
783787

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

795-
await sleep(200)
799+
clock.tick(200)
796800

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

805-
await sleep(300)
809+
clock.tick(300)
806810

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

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

857862
after(async () => {
863+
clock.uninstall()
858864
await client.close()
859865
server.close()
860866

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

872-
await sleep(100)
878+
clock.tick(100)
873879

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

883-
await sleep(100)
889+
clock.tick(100)
884890

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

894-
await sleep(150)
900+
clock.tick(150)
895901

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

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

12521259
after(async () => {
1260+
clock.uninstall()
12531261
await client.close()
12541262
server.close()
12551263

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

1267-
await sleep(100)
1275+
clock.tick(100)
12681276

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

1291+
const clock = FakeTimers.install()
12831292
let counter = 0
12841293
let lookupCounter = 0
12851294
const server = createServer()
@@ -1342,6 +1351,7 @@ test('Should prefer affinity (dual stack - 4)', async t => {
13421351
])
13431352

13441353
after(async () => {
1354+
clock.uninstall()
13451355
await client.close()
13461356
server.close()
13471357

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

1359-
await sleep(100)
1369+
clock.tick(100)
13601370

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

1393+
const clock = FakeTimers.install()
13831394
let counter = 0
13841395
let lookupCounter = 0
13851396
const server = createServer()
@@ -1442,6 +1453,7 @@ test('Should prefer affinity (dual stack - 6)', async t => {
14421453
])
14431454

14441455
after(async () => {
1456+
clock.uninstall()
14451457
await client.close()
14461458
server.close()
14471459

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

1459-
await sleep(100)
1471+
clock.tick(100)
14601472

14611473
const response2 = await client.request({
14621474
...requestOptions,

0 commit comments

Comments
 (0)