@@ -65,7 +65,7 @@ type errorDialer struct {
65
65
fakeDialer
66
66
}
67
67
68
- func (errorDialer ) Close () error {
68
+ func (* errorDialer ) Close () error {
69
69
return errors .New ("errorDialer returns error on Close" )
70
70
}
71
71
@@ -143,15 +143,15 @@ func TestClientInitialization(t *testing.T) {
143
143
desc : "with incrementing automatic port selection" ,
144
144
in : & proxy.Config {
145
145
Addr : "127.0.0.1" ,
146
- Port : 5432 , // default port
146
+ Port : 6000 ,
147
147
Instances : []proxy.InstanceConnConfig {
148
148
{Name : inst1 },
149
149
{Name : inst2 },
150
150
},
151
151
},
152
152
wantTCPAddrs : []string {
153
- "127.0.0.1:5432 " ,
154
- "127.0.0.1:5433 " ,
153
+ "127.0.0.1:6000 " ,
154
+ "127.0.0.1:6001 " ,
155
155
},
156
156
},
157
157
{
@@ -238,25 +238,6 @@ func TestClientInitialization(t *testing.T) {
238
238
}
239
239
}
240
240
241
- func tryTCPDial (t * testing.T , addr string ) net.Conn {
242
- attempts := 10
243
- var (
244
- conn net.Conn
245
- err error
246
- )
247
- for i := 0 ; i < attempts ; i ++ {
248
- conn , err = net .Dial ("tcp" , addr )
249
- if err != nil {
250
- time .Sleep (100 * time .Millisecond )
251
- continue
252
- }
253
- return conn
254
- }
255
-
256
- t .Fatalf ("failed to dial in %v attempts: %v" , attempts , err )
257
- return nil
258
- }
259
-
260
241
func TestClientLimitsMaxConnections (t * testing.T ) {
261
242
d := & fakeDialer {}
262
243
in := & proxy.Config {
@@ -291,17 +272,92 @@ func TestClientLimitsMaxConnections(t *testing.T) {
291
272
// wait only a second for the result (since nothing is writing to the
292
273
// socket)
293
274
conn2 .SetReadDeadline (time .Now ().Add (time .Second ))
294
- _ , rErr := conn2 .Read (make ([]byte , 1 ))
295
- if rErr != io .EOF {
296
- t .Fatalf ("conn.Read should return io.EOF, got = %v" , rErr )
275
+
276
+ wantEOF := func (t * testing.T , c net.Conn ) {
277
+ var got error
278
+ for i := 0 ; i < 10 ; i ++ {
279
+ _ , got = c .Read (make ([]byte , 1 ))
280
+ if got == io .EOF {
281
+ return
282
+ }
283
+ time .Sleep (100 * time .Millisecond )
284
+ }
285
+ t .Fatalf ("conn.Read should return io.EOF, got = %v" , got )
297
286
}
298
287
288
+ wantEOF (t , conn2 )
289
+
299
290
want := 1
300
291
if got := d .dialAttempts (); got != want {
301
292
t .Fatalf ("dial attempts did not match expected, want = %v, got = %v" , want , got )
302
293
}
303
294
}
304
295
296
+ func tryTCPDial (t * testing.T , addr string ) net.Conn {
297
+ attempts := 10
298
+ var (
299
+ conn net.Conn
300
+ err error
301
+ )
302
+ for i := 0 ; i < attempts ; i ++ {
303
+ conn , err = net .Dial ("tcp" , addr )
304
+ if err != nil {
305
+ time .Sleep (100 * time .Millisecond )
306
+ continue
307
+ }
308
+ return conn
309
+ }
310
+
311
+ t .Fatalf ("failed to dial in %v attempts: %v" , attempts , err )
312
+ return nil
313
+ }
314
+
315
+ func TestClientCloseWaitsForActiveConnections (t * testing.T ) {
316
+ in := & proxy.Config {
317
+ Addr : "127.0.0.1" ,
318
+ Port : 5000 ,
319
+ Instances : []proxy.InstanceConnConfig {
320
+ {Name : "proj:region:pg" },
321
+ },
322
+ Dialer : & fakeDialer {},
323
+ }
324
+ c , err := proxy .NewClient (context .Background (), & cobra.Command {}, in )
325
+ if err != nil {
326
+ t .Fatalf ("proxy.NewClient error: %v" , err )
327
+ }
328
+ go c .Serve (context .Background ())
329
+
330
+ conn := tryTCPDial (t , "127.0.0.1:5000" )
331
+ _ = conn .Close ()
332
+
333
+ if err := c .Close (); err != nil {
334
+ t .Fatalf ("c.Close error: %v" , err )
335
+ }
336
+
337
+ in .WaitOnClose = time .Second
338
+ in .Port = 5001
339
+ c , err = proxy .NewClient (context .Background (), & cobra.Command {}, in )
340
+ if err != nil {
341
+ t .Fatalf ("proxy.NewClient error: %v" , err )
342
+ }
343
+ go c .Serve (context .Background ())
344
+
345
+ var open []net.Conn
346
+ for i := 0 ; i < 5 ; i ++ {
347
+ conn = tryTCPDial (t , "127.0.0.1:5001" )
348
+ open = append (open , conn )
349
+ }
350
+ defer func () {
351
+ for _ , o := range open {
352
+ o .Close ()
353
+ }
354
+ }()
355
+
356
+ if err := c .Close (); err == nil {
357
+ t .Fatal ("c.Close should error, got = nil" )
358
+ }
359
+ }
360
+
305
361
func TestClientClosesCleanly (t * testing.T ) {
306
362
in := & proxy.Config {
307
363
Addr : "127.0.0.1" ,
@@ -316,12 +372,8 @@ func TestClientClosesCleanly(t *testing.T) {
316
372
t .Fatalf ("proxy.NewClient error want = nil, got = %v" , err )
317
373
}
318
374
go c .Serve (context .Background ())
319
- time .Sleep (time .Second ) // allow the socket to start listening
320
375
321
- conn , dErr := net .Dial ("tcp" , "127.0.0.1:5000" )
322
- if dErr != nil {
323
- t .Fatalf ("net.Dial error = %v" , dErr )
324
- }
376
+ conn := tryTCPDial (t , "127.0.0.1:5000" )
325
377
_ = conn .Close ()
326
378
327
379
if err := c .Close (); err != nil {
@@ -343,7 +395,9 @@ func TestClosesWithError(t *testing.T) {
343
395
t .Fatalf ("proxy.NewClient error want = nil, got = %v" , err )
344
396
}
345
397
go c .Serve (context .Background ())
346
- time .Sleep (time .Second ) // allow the socket to start listening
398
+
399
+ conn := tryTCPDial (t , "127.0.0.1:5000" )
400
+ defer conn .Close ()
347
401
348
402
if err = c .Close (); err == nil {
349
403
t .Fatal ("c.Close() should error, got nil" )
0 commit comments