-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathratelimitedserver_test.go
90 lines (66 loc) · 1.74 KB
/
ratelimitedserver_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
package resolvermt
import (
"errors"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
type spyLimiter struct {
calls int
}
func (s *spyLimiter) Take() time.Time {
s.calls++
return time.Time{}
}
type fakePool struct {
connectionPool
}
func (s *fakePool) Get() (*dns.Client, *dns.Conn, error) {
return nil, nil, errors.New("error")
}
func TestRateLimitedServerNewList(t *testing.T) {
tests := []struct {
name string
have []string
want int
}{
{name: "Valid Resolver", have: []string{"8.8.8.8:53"}, want: 1},
{name: "Empty Resolver", have: []string{}, want: 0},
{name: "Multiple Resolvers", have: []string{"8.8.8.8:53", "invalid", "127.0.0.1"}, want: 2},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gotList := newRateLimitedServerList(test.have, 10)
assert.Equal(t, test.want, len(gotList))
})
}
}
func TestRateLimitedServerQuery(t *testing.T) {
limiter := &spyLimiter{}
server, err := newRateLimitedServer("8.8.8.8:53", 10)
if err != nil {
t.Fatal("unable to connect to resolver")
}
server.limiter = limiter
_, _, gotErr := server.Query("www.google.com", TypeA)
assert.Equal(t, 1, limiter.calls)
assert.Nil(t, gotErr)
}
func TestRateLimitedServerQueryPoolErr(t *testing.T) {
server, err := newRateLimitedServer("8.8.8.8:53", 10)
if err != nil {
t.Fatal("unable to connect to resolver")
}
// Make sure the next Query receives an error from the connection pool
server.pool = &fakePool{}
_, _, gotErr := server.Query("www.google.com", TypeA)
assert.NotNil(t, gotErr)
}
func TestRateLimitedServerClose(t *testing.T) {
server, err := newRateLimitedServer("8.8.8.8:53", 10)
if err != nil {
t.Fatal("unable to connect to resolver")
}
server.Close()
}