Skip to content

Commit

Permalink
Test caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Sep 14, 2023
1 parent 9e8cc56 commit f185daa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
47 changes: 47 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package dns_test
import (
"context"
"fmt"
"net"
"testing"
"time"

"github.com/ncruces/go-dns"
)
Expand All @@ -21,3 +24,47 @@ func ExampleNewCachingResolver() {
// 2606:4700:4700::1111
// 2606:4700:4700::1001
}

func TestNewCachingResolver(t *testing.T) {
// Prime recursive resolver cache.
e, err := net.LookupIP("nxdomain.test")
if err == nil {
t.Errorf("LookupIPAddr('nxdomain.test') = %v", e)
}

r := dns.NewCachingResolver(nil)
measure := func() time.Duration {
start := time.Now()
r.LookupIPAddr(context.TODO(), "nxdomain.test")
return time.Since(start)
}

uncached, cached := measure(), measure()
if uncached > cached*10 { // Expect order of magnitude difference.
t.Logf("uncached %v, cached %v", uncached, cached)
} else {
t.Errorf("uncached %v, cached %v", uncached, cached)
}
}

func TestNegativeCache(t *testing.T) {
// Prime recursive resolver cache.
e, err := net.LookupIP("nxdomain.test")
if err == nil {
t.Errorf("LookupIPAddr('nxdomain.test') = %v", e)
}

r := dns.NewCachingResolver(nil, dns.NegativeCache(false))
measure := func() time.Duration {
start := time.Now()
r.LookupIPAddr(context.TODO(), "nxdomain.test")
return time.Since(start)
}

first, second := measure(), measure()
if first/10 < second && second < first*10 { // Do not expect huge differences.
t.Logf("first %v, second %v", first, second)
} else {
t.Errorf("first %v, second %v", first, second)
}
}
2 changes: 1 addition & 1 deletion doh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestNewDoHResolver(t *testing.T) {
})
}

func TestNewDoH64Resolver(t *testing.T) {
func TestNewDoHResolver_64(t *testing.T) {
// Test IPv6 connectivity (broken on GitHub Actions).
if c, err := net.Dial("tcp", "ipv6.google.com:80"); err != nil {
t.Skip("IPv6 not supported.")
Expand Down
2 changes: 1 addition & 1 deletion dot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestNewDoTResolver(t *testing.T) {
})
}

func TestNewDoT64Resolver(t *testing.T) {
func TestNewDoTResolver_64(t *testing.T) {
// Test IPv6 connectivity (broken on GitHub Actions).
if c, err := net.Dial("tcp", "ipv6.google.com:80"); err != nil {
t.Skip("IPv6 not supported.")
Expand Down

0 comments on commit f185daa

Please sign in to comment.