brontide: make establishTestConnection full async

In this commit we modify the establishTestConnection() function that
each of the brontide unit tests utilize. Before this commit, we would
fully block on the Accept method of the listener. Since then it has
been observed, that at times if Accept blocks indefinitely, then the
entire test will fail after 10 minutes. To allow the test to return
early with a pertinent error, we’ll now make the entire test async, so
we can immediately return with an error if detected.
This commit is contained in:
Olaoluwa Osuntokun 2017-11-01 13:28:18 -07:00
parent b28d613b07
commit 1899847823
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21

@ -14,8 +14,8 @@ import (
) )
func establishTestConnection() (net.Conn, net.Conn, error) { func establishTestConnection() (net.Conn, net.Conn, error) {
// First, generate the long-term private keys both ends of the connection // First, generate the long-term private keys both ends of the
// within our test. // connection within our test.
localPriv, err := btcec.NewPrivateKey(btcec.S256()) localPriv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -42,24 +42,38 @@ func establishTestConnection() (net.Conn, net.Conn, error) {
} }
// Initiate a connection with a separate goroutine, and listen with our // Initiate a connection with a separate goroutine, and listen with our
// main one. If both errors are nil, then encryption+auth was succesful. // main one. If both errors are nil, then encryption+auth was
errChan := make(chan error) // successful.
connChan := make(chan net.Conn) conErrChan := make(chan error, 1)
connChan := make(chan net.Conn, 1)
go func() { go func() {
conn, err := Dial(remotePriv, netAddr) conn, err := Dial(remotePriv, netAddr)
errChan <- err conErrChan <- err
connChan <- conn connChan <- conn
}() }()
localConn, listenErr := listener.Accept() lisErrChan := make(chan error, 1)
if listenErr != nil { lisChan := make(chan net.Conn, 1)
return nil, nil, listenErr go func() {
localConn, listenErr := listener.Accept()
lisErrChan <- listenErr
lisChan <- localConn
}()
select {
case err := <-conErrChan:
if err != nil {
return nil, nil, err
}
case err := <-lisErrChan:
if err != nil {
return nil, nil, err
}
} }
if dialErr := <-errChan; dialErr != nil { localConn := <-lisChan
return nil, nil, dialErr
}
remoteConn := <-connChan remoteConn := <-connChan
return localConn, remoteConn, nil return localConn, remoteConn, nil