-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsndriver_test.go
279 lines (244 loc) · 7.38 KB
/
dsndriver_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package dsndriver
import (
"context"
"database/sql/driver"
"fmt"
"testing"
"time"
"github.com/go-sql-driver/mysql"
)
var accessDeniedErr = &mysql.MySQLError{
Number: 1045,
Message: "access denied",
}
type mockConnector struct {
cfg *mysql.Config
connectFunc func(context.Context) (driver.Conn, error)
calls int
}
func (c *mockConnector) Driver() driver.Driver { return MySQLDriver{} }
func (c *mockConnector) Connect(ctx context.Context) (driver.Conn, error) {
c.calls++
if c.connectFunc != nil {
return c.connectFunc(ctx)
}
return &mockConn{cfg: c.cfg, def: true, connNo: c.calls}, nil
}
type mockConn struct {
cfg *mysql.Config
def bool // default from Connect() ^ default (not c.connectFunc)
connNo int
}
func (c mockConn) Prepare(query string) (driver.Stmt, error) { return nil, nil }
func (c mockConn) Close() error { return nil }
func (c mockConn) Begin() (driver.Tx, error) { return nil, nil }
func init() {
Debug = true
newConnector = func(cfg *mysql.Config) (driver.Connector, error) { return &mockConnector{cfg: cfg}, nil }
}
var swapperCalls int
var gotDSN, newDSN string
func testswapper(ctx context.Context, dsn string) string {
swapperCalls += 1
gotDSN = dsn
return newDSN
}
func setup() {
SetHotswapFunc(testswapper)
swapperCalls = 0
gotDSN = ""
newDSN = ""
}
func TestConnectNoError(t *testing.T) {
setup()
connectCalls := 0
dsn1 := "user:pass@tcp(127.0.0.1)/"
mct1 := &mockConnector{
connectFunc: func(context.Context) (driver.Conn, error) {
connectCalls += 1
return mockConn{}, nil
},
}
c := NewConnector(dsn1, mct1)
if connectCalls != 0 {
t.Errorf("Connect called %d times, expected 0", connectCalls)
}
if swapperCalls != 0 {
t.Errorf("Hotswap func called %d times, expected 0", swapperCalls)
}
gotConn, err := c.Connect(context.Background())
if err != nil {
t.Errorf("Connect returned error '%s', expected nil", err)
}
if gotConn == nil {
t.Errorf("Connect returned nil driver.Conn, expected non-nil value")
}
if connectCalls != 1 {
t.Errorf("Connect called %d times, expected 1", connectCalls)
}
if swapperCalls != 0 {
t.Errorf("Hotswap func called %d times, expected 0", swapperCalls)
}
}
func TestConnectNotAccessDeniedError(t *testing.T) {
setup()
connectCalls := 0
dsn1 := "user:pass@tcp(127.0.0.1)/"
mct1 := &mockConnector{
connectFunc: func(context.Context) (driver.Conn, error) {
connectCalls += 1
return nil, fmt.Errorf("some other error")
},
}
c := NewConnector(dsn1, mct1)
gotConn, err := c.Connect(context.Background())
if err == nil {
t.Error("Connect returned nil error, expected mock error")
}
if gotConn != nil {
t.Errorf("Connect returned non-nil driver.Conn, expected nil on error")
}
if connectCalls != 1 {
t.Errorf("Connect called %d times, expected 1", connectCalls)
}
if swapperCalls != 0 {
t.Errorf("Hotswap func called %d times, expected 0", swapperCalls)
}
}
func TestHotswapNoBlockers(t *testing.T) {
setup()
connectCalls := 0
dsn1 := "user:pass@tcp(127.0.0.1)/"
mct1 := &mockConnector{
connectFunc: func(context.Context) (driver.Conn, error) {
connectCalls += 1
return nil, accessDeniedErr
},
}
newDSN = "user2:pass2@tcp(127.0.0.1)/"
c := NewConnector(dsn1, mct1)
v, err := c.Connect(context.Background())
if err != nil {
t.Errorf("Connect returned error '%s', expected nil", err)
}
if v == nil {
t.Errorf("Connect returned nil driver.Conn, expected non-nil value")
}
if connectCalls != 1 {
t.Errorf("Connect called %d times, expected 1", connectCalls)
}
if swapperCalls != 1 {
t.Errorf("Hotswap func called %d times, expected 0", swapperCalls)
}
if gotDSN != dsn1 {
t.Errorf("got DSN '%s', expected '%s'", gotDSN, dsn1)
}
conn, ok := v.(*mockConn)
if !ok {
t.Errorf("Connect did not return a mockConn, returned %#v", v)
} else if conn.cfg.User != "user2" {
t.Errorf("new DSN user = '%s', expected 'user2'", conn.cfg.User)
}
// Using new Connector, not original, so when we connect again, the orginal
// should not increment the vars it enclosed. We can verify that new conn
// is not from this test's connector because default conns have default=true.
v, err = c.Connect(context.Background())
if err != nil {
t.Errorf("Connect returned error '%s', expected nil", err)
}
if v == nil {
t.Errorf("Connect returned nil driver.Conn, expected non-nil value")
}
if connectCalls != 1 {
t.Errorf("Connect called %d times, expected 1", connectCalls)
}
if swapperCalls != 1 {
t.Errorf("Hotswap func called %d times, expected 0", swapperCalls)
}
conn, ok = v.(*mockConn)
if !ok {
t.Errorf("Connect did not return a mockConn, returned %#v", v)
} else if conn.def != true {
t.Errorf("new Conn made by old Collector, expected it to be made by new Connector (with mockConn.def=true)")
}
}
func TestHotswapWithBlocker(t *testing.T) {
setup()
// To make other conns block during the hot swap, we make the hot swap func block
swappingChan := make(chan struct{})
blockChan := make(chan struct{})
SetHotswapFunc(func(ctx context.Context, currentDSN string) string {
close(swappingChan)
<-blockChan
return "user3:pass3@tcp(localhost)/"
})
connsChan := make(chan driver.Conn, 2)
// This Connector is called once and causes the hot swap that blocks.
// It's never called again; a new Connector is created.
mct1 := &mockConnector{
connectFunc: func(context.Context) (driver.Conn, error) {
return nil, accessDeniedErr
},
}
dsn1 := "user:pass@tcp(127.0.0.1)/"
c := NewConnector(dsn1, mct1)
// This goroutine becomes the hot swapper. To ensure it runs first and calls
// the hot swap func, we wait for it to close swappingChan...
conn1Chan := make(chan struct{})
go func() {
defer close(conn1Chan)
conn, _ := c.Connect(context.Background())
connsChan <- conn
}()
<-swappingChan
time.Sleep(20 * time.Millisecond)
// Once swappingChan is closed, we know that ^ goroutine is running and blocked
// on blockChan. So start another conn attempt which should block on the first.
conn2Chan := make(chan struct{})
go func() {
defer close(conn2Chan)
conn, _ := c.Connect(context.Background())
// Wait for conn1 to send and return. This only orders gotConns below,
// not any func calls because we're waiting here in the test, not anywhere
// inside the connector.
<-conn1Chan
connsChan <- conn
}()
// After we close blockChan, the first conn/groutine creates a new Connector and
// calls its Connect method, getting a new new (mock) Conn. When it returns,
// it closes an internal chan which unblocks the second conn/goroutine which
// should call Connect on the new Connector the first one just created. We test
// this below with mockConnector.calls == 2.
close(blockChan)
<-conn1Chan
<-conn2Chan
if mct1.calls != 2 {
t.Errorf("Connect called %d times, expected 2", mct1.calls)
}
gotConns := []driver.Conn{}
CONNS:
for {
select {
case c := <-connsChan:
gotConns = append(gotConns, c)
default:
break CONNS
}
}
if len(gotConns) != 2 {
t.Errorf("got %d Conn, expected 2", len(gotConns))
}
for i, v := range gotConns {
conn, ok := v.(*mockConn)
if !ok {
t.Errorf("Connect did not return a mockConn, returned %#v", v)
continue
}
if conn.def != true {
t.Errorf("new Conn made by old Collector, expected it to be made by new Connector (with mockConn.def=true)")
}
if conn.connNo != i+1 {
t.Errorf("Conn number %d, expected %d", conn.connNo, i+1)
}
}
}