Skip to content

Commit deff579

Browse files
committed
Addressed comment style and test improvement as suggested
1 parent a775921 commit deff579

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

internal/resolver/dns/dns_resolver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var (
6363
func init() {
6464
resolver.Register(NewBuilder())
6565
internal.TimeAfterFunc = time.After
66+
internal.TimeNowFunc = time.Now
6667
internal.TimeUntilFunc = time.Until
6768
internal.NewNetResolver = newNetResolver
6869
internal.AddressDialer = addressDialer
@@ -215,7 +216,7 @@ func (d *dnsResolver) watcher() {
215216
// Success resolving, wait for the next ResolveNow. However, also wait 30
216217
// seconds at the very least to prevent constantly re-resolving.
217218
backoffIndex = 1
218-
nextResolutionTime = time.Now().Add(MinResolutionInterval)
219+
nextResolutionTime = internal.TimeNowFunc().Add(MinResolutionInterval)
219220
select {
220221
case <-d.ctx.Done():
221222
return
@@ -224,7 +225,7 @@ func (d *dnsResolver) watcher() {
224225
} else {
225226
// Poll on an error found in DNS Resolver or an error received from
226227
// ClientConn.
227-
nextResolutionTime = time.Now().Add(backoff.DefaultExponential.Backoff(backoffIndex))
228+
nextResolutionTime = internal.TimeNowFunc().Add(backoff.DefaultExponential.Backoff(backoffIndex))
228229
backoffIndex++
229230
}
230231
select {

internal/resolver/dns/dns_resolver_test.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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 0 wait time.
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

114126
func 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].
13081317
func (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,20 @@ 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+
// Override time.Until() to read the time passed to it
1332+
// and return immediately without any delay
1333+
timeCh := overrideTimeUntilFuncWithChannel(t)
13211334

13221335
r, 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.
13261341
select {
13271342
case <-ctx.Done():
13281343
t.Fatal("Timeout when waiting for DNS resolver")
@@ -1331,25 +1346,20 @@ func (s) TestMinResolutionInterval_NoExtraDelay(t *testing.T) {
13311346
case <-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].
13371351
r.ResolveNow(resolver.ResolveNowOptions{})
1338-
1339-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1340-
defer cancel()
13411352
select {
13421353
case <-ctx.Done():
13431354
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")
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.
13531363
select {
13541364
case <-ctx.Done():
13551365
t.Fatal("Timeout when waiting for an error from the resolver")

internal/resolver/dns/internal/internal.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ var (
5656
// blocked waiting for the duration to elapse.
5757
TimeAfterFunc func(time.Duration) <-chan time.Time
5858

59-
// TimeUntilFunc is used by the DNS resolver to get the time remaining
60-
// to allow next resolution. In non-test code, this is implemented by
59+
// TimeNowFunc is used by the DNS resolver to get the current time.
60+
// In non-test code, this is implemented by time.Now. In test code,
61+
// this can be used to control the current time for the resolver.
62+
TimeNowFunc func() time.Time
63+
64+
// TimeUntilFunc is used by the DNS resolver to calculate the remaining
65+
// wait time for re-resolution. In non-test code, this is implemented by
6166
// time.Until. In test code, this can be used to control the remaining
62-
// time for resolver to allow next resolution.
67+
// time for resolver to wait for re-resolution.
6368
TimeUntilFunc func(time.Time) time.Duration
6469

6570
// NewNetResolver returns the net.Resolver instance for the given target.

0 commit comments

Comments
 (0)