MCPcopy
hub / github.com/grpc/grpc-go / TestReportError

Method TestReportError

internal/resolver/dns/dns_resolver_test.go:1317–1365  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
1317func (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.
1368func overrideResolveTimeoutDuration(t *testing.T, dur time.Duration) {

Callers

nothing calls this directly

Calls 8

overrideNetResolverFunction · 0.85
NowMethod · 0.80
FatalMethod · 0.65
ErrorMethod · 0.65
FatalfMethod · 0.65
DoneMethod · 0.45

Tested by

no test coverage detected