From 18998478238e7b50303f7c0b2d06f3c86639e9a7 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 1 Nov 2017 13:28:18 -0700 Subject: [PATCH] brontide: make establishTestConnection full async MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- brontide/noise_test.go | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/brontide/noise_test.go b/brontide/noise_test.go index fa4a1924..87bc72b4 100644 --- a/brontide/noise_test.go +++ b/brontide/noise_test.go @@ -14,8 +14,8 @@ import ( ) func establishTestConnection() (net.Conn, net.Conn, error) { - // First, generate the long-term private keys both ends of the connection - // within our test. + // First, generate the long-term private keys both ends of the + // connection within our test. localPriv, err := btcec.NewPrivateKey(btcec.S256()) if err != nil { 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 - // main one. If both errors are nil, then encryption+auth was succesful. - errChan := make(chan error) - connChan := make(chan net.Conn) + // main one. If both errors are nil, then encryption+auth was + // successful. + conErrChan := make(chan error, 1) + connChan := make(chan net.Conn, 1) go func() { conn, err := Dial(remotePriv, netAddr) - errChan <- err + conErrChan <- err connChan <- conn }() - localConn, listenErr := listener.Accept() - if listenErr != nil { - return nil, nil, listenErr + lisErrChan := make(chan error, 1) + lisChan := make(chan net.Conn, 1) + 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 { - return nil, nil, dialErr - } + localConn := <-lisChan remoteConn := <-connChan return localConn, remoteConn, nil