-
Notifications
You must be signed in to change notification settings - Fork 62
/
fastest_test.go
126 lines (105 loc) · 2.89 KB
/
fastest_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
package rdns
import (
"errors"
"net"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestFastest(t *testing.T) {
// Build 2 resolvers that count the number of invocations
var ci ClientInfo
r1 := &TestResolver{ // slow resolver, with one A record in the response
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
time.Sleep(10 * time.Millisecond)
a := new(dns.Msg)
a.SetReply(q)
a.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: q.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
},
A: net.IP{127, 0, 0, 1},
},
}
return a, nil
},
}
r2 := new(TestResolver) // fast resolver
g := NewFastest("fastest", r1, r2)
q := new(dns.Msg)
q.SetQuestion("test.com.", dns.TypeA)
// Send the first query, it should go to both and the fast response (with A record) should come back.
a, err := g.Resolve(q, ci)
require.NoError(t, err)
time.Sleep(time.Millisecond) // Wait to make sure both resolvers are actually hit before checking the hit-count
require.Equal(t, 1, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
require.Equal(t, 0, len(a.Answer))
}
func TestFastestFail(t *testing.T) {
var ci ClientInfo
// Fast resolver that fails
opt := StaticResolverOptions{
RCode: dns.RcodeServerFailure,
}
r1, err := NewStaticResolver("test-static", opt)
require.NoError(t, err)
// Slow resolver that succeeds
r2 := &TestResolver{
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
time.Sleep(10 * time.Millisecond)
a := new(dns.Msg)
a.SetReply(q)
a.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: q.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
},
A: net.IP{127, 0, 0, 1},
},
}
return a, nil
},
}
g := NewFastest("fastest", r1, r2)
q := new(dns.Msg)
q.SetQuestion("test.com.", dns.TypeA)
// We have a fast failing, and a slow succeeding one. Expect success
a, err := g.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r2.HitCount())
require.Equal(t, 1, len(a.Answer))
}
func TestFastestFailAll(t *testing.T) {
var ci ClientInfo
// Fast resolver that fails with an error
r1 := &TestResolver{
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
return nil, errors.New("failed")
},
}
// Slow resolver that fails with SERVFAIL
r2 := &TestResolver{
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
time.Sleep(10 * time.Millisecond)
a := new(dns.Msg)
a.SetRcode(q, dns.RcodeServerFailure)
return a, nil
},
}
g := NewFastest("fastest", r1, r2)
q := new(dns.Msg)
q.SetQuestion("test.com.", dns.TypeA)
// Expect the response to be from the slow SERVFAIL
a, err := g.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
require.Equal(t, dns.RcodeServerFailure, a.Rcode)
}