Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit d6da0f3

Browse files
authored
fix fatal's outside of test routines for golang16 (#140)
1 parent 1302b76 commit d6da0f3

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

pkg/proxy/proxy_test.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,10 @@ func TestTCPProxy(t *testing.T) {
251251
time.Sleep(100 * time.Millisecond) // give server time to start
252252

253253
// 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+
}
255258
if received != send {
256259
t.Errorf("Failed to receive data from tcpDummyServer, send:[%+v] recieved:[%+v]", []byte(send), []byte(received))
257260
}
@@ -267,7 +270,10 @@ func TestTCPProxy(t *testing.T) {
267270
time.Sleep(100 * time.Millisecond) // give server time to start
268271

269272
// 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+
}
271277
if received != send {
272278
t.Errorf("Failed to receive data from tcpProxy, send:[%+v] recieved:[%+v]", []byte(send), []byte(received))
273279
}
@@ -283,11 +289,11 @@ func getKey(m map[string]string) string {
283289
return ""
284290
}
285291

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) {
287293
// connect to server
288294
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", ip, port))
289295
if err != nil {
290-
t.Fatal(err)
296+
return "", err
291297
}
292298

293299
defer conn.Close()
@@ -296,18 +302,18 @@ func tcpDummyClient(ip string, port int, send string, t *testing.T) string {
296302
buf := make([]byte, 256)
297303
_, err = conn.Read(buf)
298304
if err != nil {
299-
t.Fatal(err)
305+
return "", err
300306
}
301307

302308
buf = bytes.Trim(buf, "\x00") // remove nul
303-
return string(buf)
309+
return string(buf), nil
304310
}
305311

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 {
307313
// start listener
308314
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ip, port))
309315
if err != nil {
310-
t.Fatal(err)
316+
return err
311317
}
312318

313319
buf := make([]byte, 256)
@@ -320,7 +326,7 @@ func tcpDummyServer(ip string, port int, exit chan bool, t *testing.T) {
320326

321327
_, err = conn.Read(buf)
322328
if err != nil {
323-
t.Fatal(err)
329+
return
324330
}
325331

326332
fmt.Fprintf(conn, string(buf))
@@ -333,7 +339,7 @@ func tcpDummyServer(ip string, port int, exit chan bool, t *testing.T) {
333339
select {
334340
case _ = <-exit:
335341
l.Close()
336-
return
342+
return err
337343
}
338344
}
339345
}

0 commit comments

Comments
 (0)