-
Notifications
You must be signed in to change notification settings - Fork 0
/
stubs_test.go
113 lines (90 loc) · 2.25 KB
/
stubs_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
package main
import (
"github.com/miekg/dns"
"github.com/stretchr/testify/mock"
"net"
"time"
)
type StubResponseWriter struct {
}
func (s *StubResponseWriter) WriteMsg(m *dns.Msg) error {
return nil
}
type StubConnPool struct {
mock.Mock
}
func (s *StubConnPool) Get() (ce ConnEntry, upstream Upstream) {
server, c := net.Pipe()
server.Close()
ce = &connEntry{Conn: &dns.Conn{Conn: c}}
upstream = Upstream{}
return
}
// Adds a new connection to the pool
func (s *StubConnPool) Add(ce ConnEntry) (err error) {
return nil
}
// Add a new upstream to the pool
func (s *StubConnPool) AddUpstream(r *Upstream) {
}
func (s *StubConnPool) CloseConnection(ce ConnEntry) {}
func (s *StubConnPool) Lock() {}
func (s *StubConnPool) Unlock() {}
func (s *StubConnPool) NewConnection(upstream Upstream, dialFunc DialFunc) (ce ConnEntry, err error) {
server, c := net.Pipe()
server.Close()
ce = &connEntry{Conn: &dns.Conn{Conn: c}}
err = nil
return
}
// Returns the number of open connections in the pool
func (s *StubConnPool) Size() int {
return 0
}
type StubJanitor struct {
mock.Mock
}
// No need for the stub to do anything in this case
func (s *StubJanitor) Start(r *RecordCache) {
}
func (s *StubJanitor) Stop() {
}
type StubConn struct {
mock.Mock
}
func (s *StubConn) Close() error {
return nil
}
type StubDnsClient struct {
mock.Mock
}
func (m *StubDnsClient) ExchangeWithConn(s *dns.Msg, conn *dns.Conn) (r *dns.Msg, rtt time.Duration, err error) {
return &dns.Msg{}, time.Duration(0), nil
}
func (m *StubDnsClient) Dial(address string) (conn *dns.Conn, err error) {
server, client := net.Pipe()
server.Close()
return &dns.Conn{Conn: client}, nil
}
// builds a stub server, connects to a stub client, returns the stuff
// stub server = server with contents stubbed, not a stub of the server
// that's confusing, will fix eventually
func BuildStubServer() (Server, *StubDnsClient, error) {
testClient := new(StubDnsClient)
server, err := NewMutexServer(testClient, new(StubConnPool))
server.(*MutexServer).Cache.StopCleaningCrew()
if err != nil {
return server, testClient, err
}
server.AddUpstream(
&Upstream{
Name: "a.b.c.d.e.f.g",
},
)
server.AddUpstream(
&Upstream{
Name: "g.f.e.d.c.b.a",
},
)
return server, testClient, nil
}