-
Notifications
You must be signed in to change notification settings - Fork 37
/
dnscache_test.go
152 lines (134 loc) · 3.3 KB
/
dnscache_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package dnscache
import (
"context"
"net"
"net/http/httptrace"
"testing"
"time"
)
func TestResolver_LookupHost(t *testing.T) {
r := &Resolver{}
var cacheMiss bool
r.OnCacheMiss = func() {
cacheMiss = true
}
hosts := []string{"google.com", "google.com.", "netflix.com"}
for _, host := range hosts {
t.Run(host, func(t *testing.T) {
for _, wantMiss := range []bool{true, false, false} {
cacheMiss = false
addrs, err := r.LookupHost(context.Background(), host)
if err != nil {
t.Fatal(err)
}
if len(addrs) == 0 {
t.Error("got no record")
}
for _, addr := range addrs {
if net.ParseIP(addr) == nil {
t.Errorf("got %q; want a literal IP address", addr)
}
}
if wantMiss != cacheMiss {
t.Errorf("got cache miss=%v, want %v", cacheMiss, wantMiss)
}
}
})
}
}
func TestClearCache(t *testing.T) {
r := &Resolver{}
_, _ = r.LookupHost(context.Background(), "google.com")
if e := r.cache["hgoogle.com"]; e != nil && !e.used {
t.Error("cache entry used flag is false, want true")
}
r.Refresh(true)
if e := r.cache["hgoogle.com"]; e != nil && e.used {
t.Error("cache entry used flag is true, want false")
}
r.Refresh(true)
if e := r.cache["hgoogle.com"]; e != nil {
t.Error("cache entry is not cleared")
}
options := ResolverRefreshOptions{}
options.ClearUnused = true
options.PersistOnFailure = false
_, _ = r.LookupHost(context.Background(), "google.com")
if e := r.cache["hgoogle.com"]; e != nil && !e.used {
t.Error("cache entry used flag is false, want true")
}
r.RefreshWithOptions(options)
if e := r.cache["hgoogle.com"]; e != nil && e.used {
t.Error("cache entry used flag is true, want false")
}
r.RefreshWithOptions(options)
if e := r.cache["hgoogle.com"]; e != nil {
t.Error("cache entry is not cleared")
}
options.ClearUnused = false
options.PersistOnFailure = true
br := &Resolver{}
br.Resolver = BadResolver{}
_, _ = br.LookupHost(context.Background(), "google.com")
br.Resolver = BadResolver{choke: true}
br.RefreshWithOptions(options)
if len(br.cache["hgoogle.com"].rrs) == 0 {
t.Error("cache entry is cleared")
}
}
func TestRaceOnDelete(t *testing.T) {
r := &Resolver{}
ls := make(chan bool)
rs := make(chan bool)
go func() {
for {
select {
case <-ls:
return
default:
r.LookupHost(context.Background(), "google.com")
time.Sleep(2 * time.Millisecond)
}
}
}()
go func() {
for {
select {
case <-rs:
return
default:
r.Refresh(true)
time.Sleep(time.Millisecond)
}
}
}()
time.Sleep(1 * time.Second)
ls <- true
rs <- true
}
func TestResolver_LookupHost_DNSHooksGetTriggerd(t *testing.T) {
var (
dnsStartInfo *httptrace.DNSStartInfo
dnsDoneInfo *httptrace.DNSDoneInfo
)
trace := &httptrace.ClientTrace{
DNSStart: func(info httptrace.DNSStartInfo) {
dnsStartInfo = &info
},
DNSDone: func(info httptrace.DNSDoneInfo) {
dnsDoneInfo = &info
},
}
ctx := httptrace.WithClientTrace(context.Background(), trace)
r := &Resolver{}
_, err := r.LookupHost(ctx, "example.com")
if err != nil {
t.Fatal(err)
}
if dnsStartInfo == nil {
t.Error("dnsStartInfo is nil, indicating that DNSStart callback has not been invoked")
}
if dnsDoneInfo == nil {
t.Error("dnsDoneInfo is nil, indicating that DNSDone callback has not been invoked")
}
}