1
1
'use strict'
2
2
3
+ const FakeTimers = require ( '@sinonjs/fake-timers' )
3
4
const { test, after } = require ( 'node:test' )
4
5
const { isIP } = require ( 'node:net' )
5
6
const { lookup } = require ( 'node:dns' )
6
7
const { createServer } = require ( 'node:http' )
7
8
const { createServer : createSecureServer } = require ( 'node:https' )
8
9
const { once } = require ( 'node:events' )
9
- const { setTimeout : sleep } = require ( 'node:timers/promises' )
10
10
11
11
const { tspl } = require ( '@matteo.collina/tspl' )
12
12
const pem = require ( 'https-pem' )
@@ -613,6 +613,7 @@ test('Should automatically resolve IPs (dual stack disabled - 6)', async t => {
613
613
test ( 'Should we handle TTL (4)' , async t => {
614
614
t = tspl ( t , { plan : 10 } )
615
615
616
+ const clock = FakeTimers . install ( )
616
617
let counter = 0
617
618
let lookupCounter = 0
618
619
const server = createServer ( )
@@ -674,6 +675,7 @@ test('Should we handle TTL (4)', async t => {
674
675
] )
675
676
676
677
after ( async ( ) => {
678
+ clock . uninstall ( )
677
679
await client . close ( )
678
680
server . close ( )
679
681
@@ -688,7 +690,7 @@ test('Should we handle TTL (4)', async t => {
688
690
t . equal ( response . statusCode , 200 )
689
691
t . equal ( await response . body . text ( ) , 'hello world!' )
690
692
691
- await sleep ( 200 )
693
+ clock . tick ( 200 )
692
694
693
695
const response2 = await client . request ( {
694
696
...requestOptions ,
@@ -698,7 +700,7 @@ test('Should we handle TTL (4)', async t => {
698
700
t . equal ( response2 . statusCode , 200 )
699
701
t . equal ( await response2 . body . text ( ) , 'hello world!' )
700
702
701
- await sleep ( 300 )
703
+ clock . tick ( 300 )
702
704
703
705
const response3 = await client . request ( {
704
706
...requestOptions ,
@@ -714,6 +716,7 @@ test('Should we handle TTL (4)', async t => {
714
716
test ( 'Should we handle TTL (6)' , async t => {
715
717
t = tspl ( t , { plan : 10 } )
716
718
719
+ const clock = FakeTimers . install ( )
717
720
let counter = 0
718
721
let lookupCounter = 0
719
722
const server = createServer ( )
@@ -778,6 +781,7 @@ test('Should we handle TTL (6)', async t => {
778
781
] )
779
782
780
783
after ( async ( ) => {
784
+ clock . uninstall ( )
781
785
await client . close ( )
782
786
server . close ( )
783
787
@@ -792,7 +796,7 @@ test('Should we handle TTL (6)', async t => {
792
796
t . equal ( response . statusCode , 200 )
793
797
t . equal ( await response . body . text ( ) , 'hello world!' )
794
798
795
- await sleep ( 200 )
799
+ clock . tick ( 200 )
796
800
797
801
const response2 = await client . request ( {
798
802
...requestOptions ,
@@ -802,7 +806,7 @@ test('Should we handle TTL (6)', async t => {
802
806
t . equal ( response2 . statusCode , 200 )
803
807
t . equal ( await response2 . body . text ( ) , 'hello world!' )
804
808
805
- await sleep ( 300 )
809
+ clock . tick ( 300 )
806
810
807
811
const response3 = await client . request ( {
808
812
...requestOptions ,
@@ -817,6 +821,7 @@ test('Should we handle TTL (6)', async t => {
817
821
test ( 'Should set lowest TTL between resolved and option maxTTL' , async t => {
818
822
t = tspl ( t , { plan : 9 } )
819
823
824
+ const clock = FakeTimers . install ( )
820
825
let lookupCounter = 0
821
826
const server = createServer ( )
822
827
const requestOptions = {
@@ -855,6 +860,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
855
860
)
856
861
857
862
after ( async ( ) => {
863
+ clock . uninstall ( )
858
864
await client . close ( )
859
865
server . close ( )
860
866
@@ -869,7 +875,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
869
875
t . equal ( response . statusCode , 200 )
870
876
t . equal ( await response . body . text ( ) , 'hello world!' )
871
877
872
- await sleep ( 100 )
878
+ clock . tick ( 100 )
873
879
874
880
// 100ms: lookup since ttl = Math.min(50, maxTTL: 200)
875
881
const response2 = await client . request ( {
@@ -880,7 +886,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
880
886
t . equal ( response2 . statusCode , 200 )
881
887
t . equal ( await response2 . body . text ( ) , 'hello world!' )
882
888
883
- await sleep ( 100 )
889
+ clock . tick ( 100 )
884
890
885
891
// 100ms: cached since ttl = Math.min(500, maxTTL: 200)
886
892
const response3 = await client . request ( {
@@ -891,7 +897,7 @@ test('Should set lowest TTL between resolved and option maxTTL', async t => {
891
897
t . equal ( response3 . statusCode , 200 )
892
898
t . equal ( await response3 . body . text ( ) , 'hello world!' )
893
899
894
- await sleep ( 150 )
900
+ clock . tick ( 150 )
895
901
896
902
// 250ms: lookup since ttl = Math.min(500, maxTTL: 200)
897
903
const response4 = await client . request ( {
@@ -1191,6 +1197,7 @@ test('Should use all dns entries (dual stack disabled - 6)', async t => {
1191
1197
test ( 'Should handle single family resolved (dual stack)' , async t => {
1192
1198
t = tspl ( t , { plan : 7 } )
1193
1199
1200
+ const clock = FakeTimers . install ( )
1194
1201
let counter = 0
1195
1202
let lookupCounter = 0
1196
1203
const server = createServer ( )
@@ -1250,6 +1257,7 @@ test('Should handle single family resolved (dual stack)', async t => {
1250
1257
] )
1251
1258
1252
1259
after ( async ( ) => {
1260
+ clock . uninstall ( )
1253
1261
await client . close ( )
1254
1262
server . close ( )
1255
1263
@@ -1264,7 +1272,7 @@ test('Should handle single family resolved (dual stack)', async t => {
1264
1272
t . equal ( response . statusCode , 200 )
1265
1273
t . equal ( await response . body . text ( ) , 'hello world!' )
1266
1274
1267
- await sleep ( 100 )
1275
+ clock . tick ( 100 )
1268
1276
1269
1277
const response2 = await client . request ( {
1270
1278
...requestOptions ,
@@ -1280,6 +1288,7 @@ test('Should handle single family resolved (dual stack)', async t => {
1280
1288
test ( 'Should prefer affinity (dual stack - 4)' , async t => {
1281
1289
t = tspl ( t , { plan : 10 } )
1282
1290
1291
+ const clock = FakeTimers . install ( )
1283
1292
let counter = 0
1284
1293
let lookupCounter = 0
1285
1294
const server = createServer ( )
@@ -1342,6 +1351,7 @@ test('Should prefer affinity (dual stack - 4)', async t => {
1342
1351
] )
1343
1352
1344
1353
after ( async ( ) => {
1354
+ clock . uninstall ( )
1345
1355
await client . close ( )
1346
1356
server . close ( )
1347
1357
@@ -1356,7 +1366,7 @@ test('Should prefer affinity (dual stack - 4)', async t => {
1356
1366
t . equal ( response . statusCode , 200 )
1357
1367
t . equal ( await response . body . text ( ) , 'hello world!' )
1358
1368
1359
- await sleep ( 100 )
1369
+ clock . tick ( 100 )
1360
1370
1361
1371
const response2 = await client . request ( {
1362
1372
...requestOptions ,
@@ -1380,6 +1390,7 @@ test('Should prefer affinity (dual stack - 4)', async t => {
1380
1390
test ( 'Should prefer affinity (dual stack - 6)' , async t => {
1381
1391
t = tspl ( t , { plan : 10 } )
1382
1392
1393
+ const clock = FakeTimers . install ( )
1383
1394
let counter = 0
1384
1395
let lookupCounter = 0
1385
1396
const server = createServer ( )
@@ -1442,6 +1453,7 @@ test('Should prefer affinity (dual stack - 6)', async t => {
1442
1453
] )
1443
1454
1444
1455
after ( async ( ) => {
1456
+ clock . uninstall ( )
1445
1457
await client . close ( )
1446
1458
server . close ( )
1447
1459
@@ -1456,7 +1468,7 @@ test('Should prefer affinity (dual stack - 6)', async t => {
1456
1468
t . equal ( response . statusCode , 200 )
1457
1469
t . equal ( await response . body . text ( ) , 'hello world!' )
1458
1470
1459
- await sleep ( 100 )
1471
+ clock . tick ( 100 )
1460
1472
1461
1473
const response2 = await client . request ( {
1462
1474
...requestOptions ,
0 commit comments