ssh/test: fix test goroutine error handling
Adds an error channel to the test helper function
testPortForward() to collect errors that happen inside
a goroutine.
Run-TryBot: Han-Wen Nienhuys
TryBot-Result: Gobot Gobot
@@ -8,6 +8,7 @@ package test
import (
"bytes"
+ "fmt"
"io"
"io/ioutil"
"math/rand"
@@ -31,17 +32,21 @@ func testPortForward(t *testing.T, n, listenAddr string) {
t.Fatal(err)
}
+ errCh := make(chan error, 1)
+
go func() {
+ defer close(errCh)
sshConn, err := sshListener.Accept()
if err != nil {
- t.Fatalf("listen.Accept failed: %v", err)
+ errCh <- fmt.Errorf("listen.Accept failed: %v", err)
+ return
}
+ defer sshConn.Close()
_, err = io.Copy(sshConn, sshConn)
if err != nil && err != io.EOF {
- t.Fatalf("ssh client copy: %v", err)
+ errCh <- fmt.Errorf("ssh client copy: %v", err)
}
- sshConn.Close()
}()
forwardedAddr := sshListener.Addr().String()
@@ -76,6 +81,12 @@ func testPortForward(t *testing.T, n, listenAddr string) {
t.Errorf("netConn.CloseWrite: %v", err)
}
+ // Check for errors on server goroutine
+ err = <-errCh
+ if err != nil {
+ t.Fatalf("server: %v", err)
+ }
+
read := <-readChan
if len(sent) != len(read) {
