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

fix fatal's outside of test routines for golang16 #140

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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