@@ -102,13 +102,25 @@ func overrideTimeAfterFuncWithChannel(t *testing.T) (durChan chan time.Duration,
102102return 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 0 wait time.
115+ func overrideTimeUntilFuncWithChannel (t * testing.T ) (timeChan chan time.Time ) {
116+ timeCh := make (chan time.Time , 1 )
107117origTimeUntil := 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}
111122t .Cleanup (func () { dnsinternal .TimeUntilFunc = origTimeUntil })
123+ return timeCh
112124}
113125
114126func enableSRVLookups (t * testing.T ) {
@@ -1301,12 +1313,8 @@ func (s) TestMinResolutionInterval(t *testing.T) {
13011313}
13021314
13031315// TestMinResolutionInterval_NoExtraDelay verifies that there is no extra delay
1304- // 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
1316+ // between two resolution requests apart from [MinResolutionInterval].
13081317func (s ) TestMinResolutionInterval_NoExtraDelay (t * testing.T ) {
1309- durChan , timeChan := overrideTimeAfterFuncWithChannel (t )
13101318tr := & testNetResolver {
13111319hostLookupTable : map [string ][]string {
13121320"foo.bar.com" : {"1.2.3.4" , "5.6.7.8" },
@@ -1316,13 +1324,20 @@ func (s) TestMinResolutionInterval_NoExtraDelay(t *testing.T) {
13161324},
13171325}
13181326overrideNetResolver (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+ // Override time.Until() to read the time passed to it
1332+ // and return immediately without any delay
1333+ timeCh := overrideTimeUntilFuncWithChannel (t )
13211334
13221335r , stateCh , errorCh := buildResolverWithTestClientConn (t , "foo.bar.com" )
13231336
1324- ctx , ctxCancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1325- defer ctxCancel ()
1337+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1338+ defer cancel ()
1339+
1340+ // Ensure that the first resolution happens.
13261341select {
13271342case <- ctx .Done ():
13281343t .Fatal ("Timeout when waiting for DNS resolver" )
@@ -1331,25 +1346,20 @@ func (s) TestMinResolutionInterval_NoExtraDelay(t *testing.T) {
13311346case <- stateCh :
13321347}
13331348
1334- // Make next resolution request after 1s.
1335- var now = time .Now ().Add (minResolutionInterval )
1336- overrideTimeUntilFunc (t , now )
1349+ // Request re-resolution and verify that the resolver waits for
1350+ // [MinResolutionInterval].
13371351r .ResolveNow (resolver.ResolveNowOptions {})
1338-
1339- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1340- defer cancel ()
13411352select {
13421353case <- ctx .Done ():
13431354t .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" )
1355+ case gotTime := <- timeCh :
1356+ wantTime := time.Time {}.Add (dns .MinResolutionInterval )
1357+ if ! gotTime .Equal (wantTime ) {
1358+ t .Fatalf ("DNS resolver waits for %v time before re-resolution, want %v" , gotTime , wantTime )
13471359}
13481360}
13491361
1350- // Unblock the DNS resolver's backoff by pushing the current time.
1351- timeChan <- time .Now ()
1352-
1362+ // Ensure that the re-resolution request actually happens.
13531363select {
13541364case <- ctx .Done ():
13551365t .Fatal ("Timeout when waiting for an error from the resolver" )
0 commit comments