MCPcopy
hub / github.com/coredns/coredns / TestNegativeStaleMaskingPositiveCache

Function TestNegativeStaleMaskingPositiveCache

plugin/cache/cache_test.go:622–706  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

620}
621
622func TestNegativeStaleMaskingPositiveCache(t *testing.T) {
623 c := New()
624 c.staleUpTo = time.Minute * 10
625 c.Next = nxDomainBackend(60)
626
627 req := new(dns.Msg)
628 qname := "cached.org."
629 req.SetQuestion(qname, dns.TypeA)
630 ctx := context.TODO()
631
632 // Add an entry to Negative Cache": cached.org. = NXDOMAIN
633 expectedResult := dns.RcodeNameError
634 if ret, _ := c.ServeDNS(ctx, &test.ResponseWriter{}, req); ret != expectedResult {
635 t.Errorf("Test 0 Negative Cache Population: expecting %v; got %v", expectedResult, ret)
636 }
637
638 // Confirm item was added to negative cache and not to positive cache
639 if c.ncache.Len() == 0 {
640 t.Errorf("Test 0 Negative Cache Population: item not added to negative cache")
641 }
642 if c.pcache.Len() != 0 {
643 t.Errorf("Test 0 Negative Cache Population: item added to positive cache")
644 }
645
646 // Set the Backend to return non-cachable errors only
647 c.Next = plugin.HandlerFunc(func(context.Context, dns.ResponseWriter, *dns.Msg) (int, error) {
648 return 255, nil // Below, a 255 means we tried querying upstream.
649 })
650
651 // Confirm we get the NXDOMAIN from the negative cache, not the error form the backend
652 rec := dnstest.NewRecorder(&test.ResponseWriter{})
653 req = new(dns.Msg)
654 req.SetQuestion(qname, dns.TypeA)
655 expectedResult = dns.RcodeNameError
656 if c.ServeDNS(ctx, rec, req); rec.Rcode != expectedResult {
657 t.Errorf("Test 1 NXDOMAIN from Negative Cache: expecting %v; got %v", expectedResult, rec.Rcode)
658 }
659
660 // Jump into the future beyond when the negative cache item would go stale
661 // but before the item goes rotten (exceeds serve stale time)
662 c.now = func() time.Time { return time.Now().Add(time.Duration(5) * time.Minute) }
663
664 // Set Backend to return a positive NOERROR + A record response
665 c.Next = BackendHandler()
666
667 // Make a query for the stale cache item
668 rec = dnstest.NewRecorder(&test.ResponseWriter{})
669 req = new(dns.Msg)
670 req.SetQuestion(qname, dns.TypeA)
671 expectedResult = dns.RcodeNameError
672 if c.ServeDNS(ctx, rec, req); rec.Rcode != expectedResult {
673 t.Errorf("Test 2 NOERROR from Backend: expecting %v; got %v", expectedResult, rec.Rcode)
674 }
675
676 // Confirm that prefetch removes the negative cache item.
677 waitFor := 3
678 for i := 1; i <= waitFor; i++ {
679 if c.ncache.Len() != 0 {

Callers

nothing calls this directly

Calls 9

HandlerFuncFuncType · 0.92
NewRecorderFunction · 0.92
nxDomainBackendFunction · 0.85
BackendHandlerFunction · 0.85
NewFunction · 0.70
ServeDNSMethod · 0.65
ErrorfMethod · 0.65
LenMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…