-
Notifications
You must be signed in to change notification settings - Fork 8
/
cache_test.go
70 lines (59 loc) · 1.58 KB
/
cache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package dns_test
import (
"context"
"fmt"
"net"
"testing"
"time"
"github.com/ncruces/go-dns"
)
func ExampleNewCachingResolver() {
resolver := dns.NewCachingResolver(nil)
ips, _ := resolver.LookupIPAddr(context.TODO(), "one.one.one.one")
for _, ip := range ips {
fmt.Println(ip.String())
}
// Unordered output:
// 1.1.1.1
// 1.0.0.1
// 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)
}
}