Remove incorrect concurrent uses of testing.T.Fatal

This commit is contained in:
Andy Wang 2020-12-23 23:12:51 +00:00
parent 70a9723377
commit 3b24c33e78
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
2 changed files with 23 additions and 16 deletions

View File

@ -10,7 +10,6 @@ import (
"net" "net"
"sync" "sync"
"testing" "testing"
"time"
) )
func serveEcho(l net.Listener) { func serveEcho(l net.Listener) {
@ -72,22 +71,26 @@ func runEchoTest(t *testing.T, conns []net.Conn, msgLen int) {
for _, conn := range conns { for _, conn := range conns {
wg.Add(1) wg.Add(1)
go func(conn net.Conn) { go func(conn net.Conn) {
defer wg.Done()
// we cannot call t.Fatalf in concurrent contexts
n, err := conn.Write(testData) n, err := conn.Write(testData)
if n != msgLen { if n != msgLen {
t.Fatalf("written only %v, err %v", n, err) t.Errorf("written only %v, err %v", n, err)
return
} }
recvBuf := make([]byte, msgLen) recvBuf := make([]byte, msgLen)
conn.SetReadDeadline(time.Now().Add(time.Second))
_, err = io.ReadFull(conn, recvBuf) _, err = io.ReadFull(conn, recvBuf)
if err != nil { if err != nil {
t.Fatalf("failed to read back: %v", err) t.Errorf("failed to read back: %v", err)
return
} }
if !bytes.Equal(testData, recvBuf) { if !bytes.Equal(testData, recvBuf) {
t.Fatalf("echoed data not correct") t.Errorf("echoed data not correct")
return
} }
wg.Done()
}(conn) }(conn)
} }
wg.Wait() wg.Wait()

View File

@ -220,30 +220,34 @@ func establishSession(lcc client.LocalConnConfig, rcc client.RemoteConnConfig, a
return proxyToCkClientD, proxyFromCkServerL, netToCkServerD, redirFromCkServerL, nil return proxyToCkClientD, proxyFromCkServerL, netToCkServerD, redirFromCkServerL, nil
} }
func runEchoTest(t *testing.T, conns []net.Conn, maxMsgLen int) { func runEchoTest(t *testing.T, conns []net.Conn, msgLen int) {
var wg sync.WaitGroup var wg sync.WaitGroup
testData := make([]byte, msgLen)
rand.Read(testData)
for _, conn := range conns { for _, conn := range conns {
wg.Add(1) wg.Add(1)
go func(conn net.Conn) { go func(conn net.Conn) {
testDataLen := rand.Intn(maxMsgLen) defer wg.Done()
testData := make([]byte, testDataLen)
rand.Read(testData)
// we cannot call t.Fatalf in concurrent contexts
n, err := conn.Write(testData) n, err := conn.Write(testData)
if n != testDataLen { if n != msgLen {
t.Fatalf("written only %v, err %v", n, err) t.Errorf("written only %v, err %v", n, err)
return
} }
recvBuf := make([]byte, testDataLen) recvBuf := make([]byte, msgLen)
_, err = io.ReadFull(conn, recvBuf) _, err = io.ReadFull(conn, recvBuf)
if err != nil { if err != nil {
t.Fatalf("failed to read back: %v", err) t.Errorf("failed to read back: %v", err)
return
} }
if !bytes.Equal(testData, recvBuf) { if !bytes.Equal(testData, recvBuf) {
t.Fatalf("echoed data not correct") t.Errorf("echoed data not correct")
return
} }
wg.Done()
}(conn) }(conn)
} }
wg.Wait() wg.Wait()