@@ -102,13 +102,25 @@ func overrideTimeAfterFuncWithChannel(t *testing.T) (durChan chan time.Duration,
102102 return durChan , timeChan
103103}
104104
105- // Override the remaining time used by the DNS resolver to allow resolution
106- func overrideTimeUntilFunc (t * testing.T , now time.Time ) {
105+ // Override the current time used by the DNS resolver.
106+ func overrideTimeNowFunc (t * testing.T , now time.Time ) {
107+ origTimeNowFunc := dnsinternal .TimeNowFunc
108+ dnsinternal .TimeNowFunc = func () time.Time { return now }
109+ t .Cleanup (func () { dnsinternal .TimeNowFunc = origTimeNowFunc })
110+ }
111+
112+ // Override the remaining wait time to allow re-resolution by DNS resolver.
113+ // Use the timeChan to read the time until resolver needs to wait for
114+ // and return immediately without any delay.
115+ func overrideTimeUntilFuncWithChannel (t * testing.T ) (timeChan chan time.Time ) {
116+ timeCh := make (chan time.Time , 1 )
107117 origTimeUntil := dnsinternal .TimeUntilFunc
108- dnsinternal .TimeUntilFunc = func (tm time.Time ) time.Duration {
109- return now .Sub (tm )
118+ dnsinternal .TimeUntilFunc = func (t time.Time ) time.Duration {
119+ timeCh <- t
120+ return 0
110121 }
111122 t .Cleanup (func () { dnsinternal .TimeUntilFunc = origTimeUntil })
123+ return timeCh
112124}
113125
114126func enableSRVLookups (t * testing.T ) {
@@ -1302,11 +1314,7 @@ func (s) TestMinResolutionInterval(t *testing.T) {
13021314
13031315// TestMinResolutionInterval_NoExtraDelay verifies that there is no extra delay
13041316// between two resolution requests apart from [minResolutionInterval].
1305- // It sets the minResolutionInterval 1s and overrides timeUntilFunc to
1306- // calculate remaining time to allow resolution after 1s and verifies that
1307- // remaining time to allow resolution is very small
13081317func (s ) TestMinResolutionInterval_NoExtraDelay (t * testing.T ) {
1309- durChan , timeChan := overrideTimeAfterFuncWithChannel (t )
13101318 tr := & testNetResolver {
13111319 hostLookupTable : map [string ][]string {
13121320 "foo.bar.com" : {"1.2.3.4" , "5.6.7.8" },
@@ -1316,13 +1324,18 @@ func (s) TestMinResolutionInterval_NoExtraDelay(t *testing.T) {
13161324 },
13171325 }
13181326 overrideNetResolver (t , tr )
1319- var minResolutionInterval = 1 * time .Second
1320- overrideResolutionInterval (t , minResolutionInterval )
1327+ // Override time.Now() to return a zero value for time. This will allow us
1328+ // to verify that the call to time.Until is made with the exact
1329+ // [minResolutionInterval] that we expect.
1330+ overrideTimeNowFunc (t , time.Time {})
1331+ timeCh := overrideTimeUntilFuncWithChannel (t )
13211332
13221333 r , stateCh , errorCh := buildResolverWithTestClientConn (t , "foo.bar.com" )
13231334
1324- ctx , ctxCancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1325- defer ctxCancel ()
1335+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1336+ defer cancel ()
1337+
1338+ // Ensure that the first resolution happens.
13261339 select {
13271340 case <- ctx .Done ():
13281341 t .Fatal ("Timeout when waiting for DNS resolver" )
@@ -1331,25 +1344,20 @@ func (s) TestMinResolutionInterval_NoExtraDelay(t *testing.T) {
13311344 case <- stateCh :
13321345 }
13331346
1334- // Make next resolution request after 1s.
1335- var now = time .Now ().Add (minResolutionInterval )
1336- overrideTimeUntilFunc (t , now )
1347+ // Request re-resolution and verify that the resolver waits for
1348+ // [minResolutionInterval].
13371349 r .ResolveNow (resolver.ResolveNowOptions {})
1338-
1339- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1340- defer cancel ()
13411350 select {
13421351 case <- ctx .Done ():
13431352 t .Fatal ("Timeout when waiting for DNS resolver" )
1344- case dur := <- durChan :
1345- if dur > 1 * time .Millisecond {
1346- t .Fatalf ("Remaining time duration to allow next resolution is higher than expected" )
1353+ case gotTime := <- timeCh :
1354+ wantTime := time.Time {}.Add (dns .MinResolutionInterval )
1355+ if ! gotTime .Equal (wantTime ) {
1356+ t .Fatalf ("DNS resolver waits for %v time before re-resolution, want %v" , gotTime , wantTime )
13471357 }
13481358 }
13491359
1350- // Unblock the DNS resolver's backoff by pushing the current time.
1351- timeChan <- time .Now ()
1352-
1360+ // Ensure that the re-resolution request actually happens.
13531361 select {
13541362 case <- ctx .Done ():
13551363 t .Fatal ("Timeout when waiting for an error from the resolver" )
0 commit comments