packer/rpc: Fix tests that swallowed errors in goroutines

This commit is contained in:
Lars Lehtonen 2019-09-11 05:22:01 -07:00
parent 142dc846d4
commit f62bc25f18
2 changed files with 18 additions and 9 deletions

View File

@ -12,14 +12,15 @@ func testConn(t *testing.T) (net.Conn, net.Conn) {
} }
var serverConn net.Conn var serverConn net.Conn
doneCh := make(chan struct{}) errChan := make(chan error)
go func() { go func() {
defer close(doneCh) defer close(errChan)
defer l.Close() defer l.Close()
var err error var err error
serverConn, err = l.Accept() serverConn, err = l.Accept()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) errChan <- err
return
} }
}() }()
@ -27,7 +28,11 @@ func testConn(t *testing.T) (net.Conn, net.Conn) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
<-doneCh
err = <-errChan
if err != nil {
t.Fatalf("err: %s", err)
}
return clientConn, serverConn return clientConn, serverConn
} }

View File

@ -60,18 +60,19 @@ func testYamux(t *testing.T) (client *yamux.Session, server *yamux.Session) {
} }
// Server side // Server side
doneCh := make(chan struct{}) errChan := make(chan error)
go func() { go func() {
defer close(doneCh) defer close(errChan)
conn, err := l.Accept() conn, err := l.Accept()
l.Close() l.Close()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) errChan <- err
return
} }
server, err = yamux.Server(conn, nil) server, err = yamux.Server(conn, nil)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) errChan <- err
} }
}() }()
@ -86,7 +87,10 @@ func testYamux(t *testing.T) (client *yamux.Session, server *yamux.Session) {
} }
// Wait for the server // Wait for the server
<-doneCh err = <-errChan
if err != nil {
t.Fatalf("err: %s", err)
}
return return
} }