@@ -500,7 +500,7 @@ test('Should automatically resolve IPs (dual stack disabled - 6)', async t => {
500
500
} )
501
501
502
502
test ( 'Should we handle TTL (4)' , async t => {
503
- t = tspl ( t , { plan : 7 } )
503
+ t = tspl ( t , { plan : 10 } )
504
504
505
505
let counter = 0
506
506
let lookupCounter = 0
@@ -536,6 +536,10 @@ test('Should we handle TTL (4)', async t => {
536
536
case 2 :
537
537
t . equal ( isIP ( url . hostname ) , 4 )
538
538
break
539
+
540
+ case 3 :
541
+ t . equal ( isIP ( url . hostname ) , 4 )
542
+ break
539
543
default :
540
544
t . fail ( 'should not reach this point' )
541
545
}
@@ -546,31 +550,13 @@ test('Should we handle TTL (4)', async t => {
546
550
dns ( {
547
551
dualStack : false ,
548
552
affinity : 4 ,
549
- maxTTL : 100 ,
553
+ maxTTL : 400 ,
550
554
lookup : ( origin , opts , cb ) => {
551
555
++ lookupCounter
552
556
lookup (
553
557
origin . hostname ,
554
558
{ all : true , family : opts . affinity } ,
555
- ( err , addresses ) => {
556
- if ( err ) {
557
- return cb ( err )
558
- }
559
-
560
- const results = new Map ( )
561
-
562
- for ( const addr of addresses ) {
563
- const record = {
564
- address : addr . address ,
565
- ttl : opts . maxTTL ,
566
- family : addr . family
567
- }
568
-
569
- results . set ( `${ record . address } :${ record . family } ` , record )
570
- }
571
-
572
- cb ( null , results . values ( ) )
573
- }
559
+ cb
574
560
)
575
561
}
576
562
} )
@@ -591,7 +577,7 @@ test('Should we handle TTL (4)', async t => {
591
577
t . equal ( response . statusCode , 200 )
592
578
t . equal ( await response . body . text ( ) , 'hello world!' )
593
579
594
- await sleep ( 500 )
580
+ await sleep ( 200 )
595
581
596
582
const response2 = await client . request ( {
597
583
...requestOptions ,
@@ -600,11 +586,22 @@ test('Should we handle TTL (4)', async t => {
600
586
601
587
t . equal ( response2 . statusCode , 200 )
602
588
t . equal ( await response2 . body . text ( ) , 'hello world!' )
589
+
590
+ await sleep ( 300 )
591
+
592
+ const response3 = await client . request ( {
593
+ ...requestOptions ,
594
+ origin : `http://localhost:${ server . address ( ) . port } `
595
+ } )
596
+
597
+ t . equal ( response3 . statusCode , 200 )
598
+ t . equal ( await response3 . body . text ( ) , 'hello world!' )
599
+
603
600
t . equal ( lookupCounter , 2 )
604
601
} )
605
602
606
603
test ( 'Should we handle TTL (6)' , async t => {
607
- t = tspl ( t , { plan : 7 } )
604
+ t = tspl ( t , { plan : 10 } )
608
605
609
606
let counter = 0
610
607
let lookupCounter = 0
@@ -642,6 +639,11 @@ test('Should we handle TTL (6)', async t => {
642
639
// [::1] -> ::1
643
640
t . equal ( isIP ( url . hostname . slice ( 1 , 4 ) ) , 6 )
644
641
break
642
+
643
+ case 3 :
644
+ // [::1] -> ::1
645
+ t . equal ( isIP ( url . hostname . slice ( 1 , 4 ) ) , 6 )
646
+ break
645
647
default :
646
648
t . fail ( 'should not reach this point' )
647
649
}
@@ -652,35 +654,94 @@ test('Should we handle TTL (6)', async t => {
652
654
dns ( {
653
655
dualStack : false ,
654
656
affinity : 6 ,
655
- maxTTL : 100 ,
657
+ maxTTL : 400 ,
656
658
lookup : ( origin , opts , cb ) => {
657
659
++ lookupCounter
658
660
lookup (
659
661
origin . hostname ,
660
662
{ all : true , family : opts . affinity } ,
661
- ( err , addresses ) => {
662
- if ( err ) {
663
- return cb ( err )
664
- }
663
+ cb
664
+ )
665
+ }
666
+ } )
667
+ ] )
668
+
669
+ after ( async ( ) => {
670
+ await client . close ( )
671
+ server . close ( )
665
672
666
- const results = [ ]
673
+ await once ( server , 'close' )
674
+ } )
667
675
668
- for ( const addr of addresses ) {
669
- const record = {
670
- address : addr . address ,
671
- ttl : opts . maxTTL ,
672
- family : addr . family
673
- }
676
+ const response = await client . request ( {
677
+ ...requestOptions ,
678
+ origin : `http://localhost:${ server . address ( ) . port } `
679
+ } )
680
+
681
+ t . equal ( response . statusCode , 200 )
682
+ t . equal ( await response . body . text ( ) , 'hello world!' )
683
+
684
+ await sleep ( 200 )
685
+
686
+ const response2 = await client . request ( {
687
+ ...requestOptions ,
688
+ origin : `http://localhost:${ server . address ( ) . port } `
689
+ } )
690
+
691
+ t . equal ( response2 . statusCode , 200 )
692
+ t . equal ( await response2 . body . text ( ) , 'hello world!' )
674
693
675
- results . push ( record )
676
- }
694
+ await sleep ( 300 )
677
695
678
- cb ( null , results )
696
+ const response3 = await client . request ( {
697
+ ...requestOptions ,
698
+ origin : `http://localhost:${ server . address ( ) . port } `
699
+ } )
700
+
701
+ t . equal ( response3 . statusCode , 200 )
702
+ t . equal ( await response3 . body . text ( ) , 'hello world!' )
703
+ t . equal ( lookupCounter , 2 )
704
+ } )
705
+
706
+ test ( 'Should set lowest TTL between resolved and option maxTTL' , async t => {
707
+ t = tspl ( t , { plan : 9 } )
708
+
709
+ let lookupCounter = 0
710
+ const server = createServer ( )
711
+ const requestOptions = {
712
+ method : 'GET' ,
713
+ path : '/' ,
714
+ headers : {
715
+ 'content-type' : 'application/json'
716
+ }
717
+ }
718
+
719
+ server . on ( 'request' , ( req , res ) => {
720
+ res . writeHead ( 200 , { 'content-type' : 'text/plain' } )
721
+ res . end ( 'hello world!' )
722
+ } )
723
+
724
+ server . listen ( 0 , '127.0.0.1' )
725
+
726
+ await once ( server , 'listening' )
727
+
728
+ const client = new Agent ( ) . compose (
729
+ dns ( {
730
+ dualStack : false ,
731
+ affinity : 4 ,
732
+ maxTTL : 200 ,
733
+ lookup : ( origin , opts , cb ) => {
734
+ ++ lookupCounter
735
+ cb ( null , [
736
+ {
737
+ address : '127.0.0.1' ,
738
+ family : 4 ,
739
+ ttl : lookupCounter === 1 ? 50 : 500
679
740
}
680
- )
741
+ ] )
681
742
}
682
743
} )
683
- ] )
744
+ )
684
745
685
746
after ( async ( ) => {
686
747
await client . close ( )
@@ -697,16 +758,40 @@ test('Should we handle TTL (6)', async t => {
697
758
t . equal ( response . statusCode , 200 )
698
759
t . equal ( await response . body . text ( ) , 'hello world!' )
699
760
700
- await sleep ( 200 )
761
+ await sleep ( 100 )
701
762
763
+ // 100ms: lookup since ttl = Math.min(50, maxTTL: 200)
702
764
const response2 = await client . request ( {
703
765
...requestOptions ,
704
766
origin : `http://localhost:${ server . address ( ) . port } `
705
767
} )
706
768
707
769
t . equal ( response2 . statusCode , 200 )
708
770
t . equal ( await response2 . body . text ( ) , 'hello world!' )
709
- t . equal ( lookupCounter , 2 )
771
+
772
+ await sleep ( 100 )
773
+
774
+ // 100ms: cached since ttl = Math.min(500, maxTTL: 200)
775
+ const response3 = await client . request ( {
776
+ ...requestOptions ,
777
+ origin : `http://localhost:${ server . address ( ) . port } `
778
+ } )
779
+
780
+ t . equal ( response3 . statusCode , 200 )
781
+ t . equal ( await response3 . body . text ( ) , 'hello world!' )
782
+
783
+ await sleep ( 150 )
784
+
785
+ // 250ms: lookup since ttl = Math.min(500, maxTTL: 200)
786
+ const response4 = await client . request ( {
787
+ ...requestOptions ,
788
+ origin : `http://localhost:${ server . address ( ) . port } `
789
+ } )
790
+
791
+ t . equal ( response4 . statusCode , 200 )
792
+ t . equal ( await response4 . body . text ( ) , 'hello world!' )
793
+
794
+ t . equal ( lookupCounter , 3 )
710
795
} )
711
796
712
797
test ( 'Should handle max cached items' , async t => {
0 commit comments