(t *testing.T)
| 523 | } |
| 524 | |
| 525 | func TestServeFromStaleCacheFetchVerifyTimeout(t *testing.T) { |
| 526 | // Verify that when verifyStaleTimeout is set and the upstream is slow, |
| 527 | // the client gets the stale entry within ~timeout, while the in-flight |
| 528 | // verify continues in the background and refreshes the cache. |
| 529 | c := New() |
| 530 | c.staleUpTo = 1 * time.Hour |
| 531 | c.verifyStale = true |
| 532 | c.verifyStaleTimeout = 50 * time.Millisecond |
| 533 | c.Next = ttlBackend(120) |
| 534 | |
| 535 | req := new(dns.Msg) |
| 536 | req.SetQuestion("cached.org.", dns.TypeA) |
| 537 | ctx := context.TODO() |
| 538 | |
| 539 | // Prime the cache with a 120s TTL entry. |
| 540 | rec := dnstest.NewRecorder(&test.ResponseWriter{}) |
| 541 | c.ServeDNS(ctx, rec, req) |
| 542 | if c.pcache.Len() != 1 { |
| 543 | t.Fatalf("Msg with > 0 TTL should have been cached") |
| 544 | } |
| 545 | |
| 546 | // Move forward past the TTL so the entry is stale. |
| 547 | c.now = func() time.Time { return time.Now().Add(3 * time.Minute) } |
| 548 | |
| 549 | // Swap in a slow backend that takes longer than the verify timeout. |
| 550 | bgDone := make(chan struct{}) |
| 551 | c.Next = slowTTLBackend(60, 200*time.Millisecond, bgDone) |
| 552 | |
| 553 | rec = dnstest.NewRecorder(&test.ResponseWriter{}) |
| 554 | start := time.Now() |
| 555 | ret, err := c.ServeDNS(ctx, rec, req.Copy()) |
| 556 | elapsed := time.Since(start) |
| 557 | |
| 558 | if err != nil { |
| 559 | t.Fatalf("unexpected error: %v", err) |
| 560 | } |
| 561 | if ret != dns.RcodeSuccess { |
| 562 | t.Fatalf("expected RcodeSuccess, got %d", ret) |
| 563 | } |
| 564 | if elapsed > 150*time.Millisecond { |
| 565 | t.Errorf("expected response within ~timeout (50ms); took %v", elapsed) |
| 566 | } |
| 567 | if rec.Msg == nil || len(rec.Msg.Answer) == 0 { |
| 568 | t.Fatalf("expected an answer, got %+v", rec.Msg) |
| 569 | } |
| 570 | // Stale serve sets TTL to 0. |
| 571 | if got := rec.Msg.Answer[0].Header().Ttl; got != 0 { |
| 572 | t.Errorf("expected stale TTL=0, got %d", got) |
| 573 | } |
| 574 | |
| 575 | // Wait for the background verify to complete. |
| 576 | select { |
| 577 | case <-bgDone: |
| 578 | case <-time.After(2 * time.Second): |
| 579 | t.Fatalf("background verify never completed") |
| 580 | } |
| 581 | } |
| 582 |
nothing calls this directly
no test coverage detected
searching dependent graphs…