Test verifies that when the DNS resolver gets an error from the underlying net.Resolver, it reports the error to the channel and backs off and retries.
(t *testing.T)
| 1315 | // Test verifies that when the DNS resolver gets an error from the underlying |
| 1316 | // net.Resolver, it reports the error to the channel and backs off and retries. |
| 1317 | func (s) TestReportError(t *testing.T) { |
| 1318 | durChan, timeChan := overrideTimeAfterFuncWithChannel(t) |
| 1319 | overrideNetResolver(t, &testNetResolver{}) |
| 1320 | |
| 1321 | const target = "notfoundaddress" |
| 1322 | _, _, errorCh := buildResolverWithTestClientConn(t, target) |
| 1323 | |
| 1324 | // Should receive first error. |
| 1325 | ctx, ctxCancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 1326 | defer ctxCancel() |
| 1327 | select { |
| 1328 | case <-ctx.Done(): |
| 1329 | t.Fatal("Timeout when waiting for an error from the resolver") |
| 1330 | case err := <-errorCh: |
| 1331 | if !strings.Contains(err.Error(), "hostLookup error") { |
| 1332 | t.Fatalf(`ReportError(err=%v) called; want err contains "hostLookupError"`, err) |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | // Expect the DNS resolver to backoff and attempt to re-resolve. Every time, |
| 1337 | // the DNS resolver will receive the same error from the net.Resolver and is |
| 1338 | // expected to push it to the channel. |
| 1339 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 1340 | defer cancel() |
| 1341 | const retries = 10 |
| 1342 | var prevDur time.Duration |
| 1343 | for i := 0; i < retries; i++ { |
| 1344 | select { |
| 1345 | case <-ctx.Done(): |
| 1346 | t.Fatalf("(Iteration: %d): Timeout when waiting for DNS resolver to backoff", i) |
| 1347 | case dur := <-durChan: |
| 1348 | if dur <= prevDur { |
| 1349 | t.Fatalf("(Iteration: %d): Unexpected decrease in amount of time to backoff", i) |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | // Unblock the DNS resolver's backoff by pushing the current time. |
| 1354 | timeChan <- time.Now() |
| 1355 | |
| 1356 | select { |
| 1357 | case <-ctx.Done(): |
| 1358 | t.Fatal("Timeout when waiting for an error from the resolver") |
| 1359 | case err := <-errorCh: |
| 1360 | if !strings.Contains(err.Error(), "hostLookup error") { |
| 1361 | t.Fatalf(`ReportError(err=%v) called; want err contains "hostLookupError"`, err) |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | // Override the default dns.ResolvingTimeout with a test duration. |
| 1368 | func overrideResolveTimeoutDuration(t *testing.T, dur time.Duration) { |
nothing calls this directly
no test coverage detected