Skip to content

Commit

Permalink
fix fatal's outside of test routines for golang16 (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdoorn committed Mar 15, 2021
1 parent 1302b76 commit d6da0f3
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ func TestTCPProxy(t *testing.T) {
time.Sleep(100 * time.Millisecond) // give server time to start

// Test self, see if we can do TCP connections
received := tcpDummyClient(serverIP, serverPort, send, t)
received, err := tcpDummyClient(serverIP, serverPort, send, t)
if err != nil {
t.Errorf("Failed tcp dummy: %s", err)
}
if received != send {
t.Errorf("Failed to receive data from tcpDummyServer, send:[%+v] recieved:[%+v]", []byte(send), []byte(received))
}
Expand All @@ -267,7 +270,10 @@ func TestTCPProxy(t *testing.T) {
time.Sleep(100 * time.Millisecond) // give server time to start

// Test self, see if we can do TCP connections
received = tcpDummyClient(proxyIP, proxyPort, send, t)
received, err = tcpDummyClient(proxyIP, proxyPort, send, t)
if err != nil {
t.Errorf("Failed tcp dummy: %s", err)
}
if received != send {
t.Errorf("Failed to receive data from tcpProxy, send:[%+v] recieved:[%+v]", []byte(send), []byte(received))
}
Expand All @@ -283,11 +289,11 @@ func getKey(m map[string]string) string {
return ""
}

func tcpDummyClient(ip string, port int, send string, t *testing.T) string {
func tcpDummyClient(ip string, port int, send string, t *testing.T) (string, error) {
// connect to server
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", ip, port))
if err != nil {
t.Fatal(err)
return "", err
}

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

buf = bytes.Trim(buf, "\x00") // remove nul
return string(buf)
return string(buf), nil
}

func tcpDummyServer(ip string, port int, exit chan bool, t *testing.T) {
func tcpDummyServer(ip string, port int, exit chan bool, t *testing.T) error {
// start listener
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ip, port))
if err != nil {
t.Fatal(err)
return err
}

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

_, err = conn.Read(buf)
if err != nil {
t.Fatal(err)
return
}

fmt.Fprintf(conn, string(buf))
Expand All @@ -333,7 +339,7 @@ func tcpDummyServer(ip string, port int, exit chan bool, t *testing.T) {
select {
case _ = <-exit:
l.Close()
return
return err
}
}
}
Expand Down

0 comments on commit d6da0f3

Please sign in to comment.