@@ -251,7 +251,10 @@ func TestTCPProxy(t *testing.T) {
251
251
time .Sleep (100 * time .Millisecond ) // give server time to start
252
252
253
253
// Test self, see if we can do TCP connections
254
- received := tcpDummyClient (serverIP , serverPort , send , t )
254
+ received , err := tcpDummyClient (serverIP , serverPort , send , t )
255
+ if err != nil {
256
+ t .Errorf ("Failed tcp dummy: %s" , err )
257
+ }
255
258
if received != send {
256
259
t .Errorf ("Failed to receive data from tcpDummyServer, send:[%+v] recieved:[%+v]" , []byte (send ), []byte (received ))
257
260
}
@@ -267,7 +270,10 @@ func TestTCPProxy(t *testing.T) {
267
270
time .Sleep (100 * time .Millisecond ) // give server time to start
268
271
269
272
// Test self, see if we can do TCP connections
270
- received = tcpDummyClient (proxyIP , proxyPort , send , t )
273
+ received , err = tcpDummyClient (proxyIP , proxyPort , send , t )
274
+ if err != nil {
275
+ t .Errorf ("Failed tcp dummy: %s" , err )
276
+ }
271
277
if received != send {
272
278
t .Errorf ("Failed to receive data from tcpProxy, send:[%+v] recieved:[%+v]" , []byte (send ), []byte (received ))
273
279
}
@@ -283,11 +289,11 @@ func getKey(m map[string]string) string {
283
289
return ""
284
290
}
285
291
286
- func tcpDummyClient (ip string , port int , send string , t * testing.T ) string {
292
+ func tcpDummyClient (ip string , port int , send string , t * testing.T ) ( string , error ) {
287
293
// connect to server
288
294
conn , err := net .Dial ("tcp" , fmt .Sprintf ("%s:%d" , ip , port ))
289
295
if err != nil {
290
- t . Fatal ( err )
296
+ return "" , err
291
297
}
292
298
293
299
defer conn .Close ()
@@ -296,18 +302,18 @@ func tcpDummyClient(ip string, port int, send string, t *testing.T) string {
296
302
buf := make ([]byte , 256 )
297
303
_ , err = conn .Read (buf )
298
304
if err != nil {
299
- t . Fatal ( err )
305
+ return "" , err
300
306
}
301
307
302
308
buf = bytes .Trim (buf , "\x00 " ) // remove nul
303
- return string (buf )
309
+ return string (buf ), nil
304
310
}
305
311
306
- func tcpDummyServer (ip string , port int , exit chan bool , t * testing.T ) {
312
+ func tcpDummyServer (ip string , port int , exit chan bool , t * testing.T ) error {
307
313
// start listener
308
314
l , err := net .Listen ("tcp" , fmt .Sprintf ("%s:%d" , ip , port ))
309
315
if err != nil {
310
- t . Fatal ( err )
316
+ return err
311
317
}
312
318
313
319
buf := make ([]byte , 256 )
@@ -320,7 +326,7 @@ func tcpDummyServer(ip string, port int, exit chan bool, t *testing.T) {
320
326
321
327
_ , err = conn .Read (buf )
322
328
if err != nil {
323
- t . Fatal ( err )
329
+ return
324
330
}
325
331
326
332
fmt .Fprintf (conn , string (buf ))
@@ -333,7 +339,7 @@ func tcpDummyServer(ip string, port int, exit chan bool, t *testing.T) {
333
339
select {
334
340
case _ = <- exit :
335
341
l .Close ()
336
- return
342
+ return err
337
343
}
338
344
}
339
345
}
0 commit comments