From b7577d9fe30e705574e98f7f061b5ef48e42bb39 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 10 Jul 2019 13:59:58 -0700 Subject: [PATCH] Removing non-deterministic sleeps, replace with channel wait --- .../connections/peerserverconnection_test.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/protocol/connections/peerserverconnection_test.go b/protocol/connections/peerserverconnection_test.go index cb0fb6c..cc8946b 100644 --- a/protocol/connections/peerserverconnection_test.go +++ b/protocol/connections/peerserverconnection_test.go @@ -23,11 +23,11 @@ func ServerAuthValid(hostname string, key ed25519.PublicKey) (allowed, known boo type TestServer struct { connection.AutoConnectionHandler - Received bool + Received chan bool } func (ts *TestServer) HandleGroupMessage(gm *protocol.GroupMessage) { - ts.Received = true + ts.Received<-true } func (ts *TestServer) HandleFetchRequest() []*protocol.GroupMessage { @@ -69,8 +69,10 @@ func TestPeerServerConnection(t *testing.T) { identity := identity.InitializeV3("", &priv, &pub) + t.Logf("Launching Server....\n") ts := new(TestServer) ts.Init() + ts.Received = make(chan bool) listenChan := make(chan bool) go runtestserver(t, ts, identity, listenChan) <-listenChan @@ -94,18 +96,13 @@ func TestPeerServerConnection(t *testing.T) { } time.Sleep(time.Second * 1) go psc.Run() - time.Sleep(time.Second * 2) - state = psc.GetState() - if state != AUTHENTICATED { - t.Errorf("connection should now be authed(%v), instead was %v", AUTHENTICATED, state) - } + psc.WaitTilAuthenticated() gm := &protocol.GroupMessage{Ciphertext: []byte("hello"), Signature: []byte{}} psc.SendGroupMessage(gm) - time.Sleep(time.Second * 2) - if ts.Received == false { - t.Errorf("Should have received a group message in test server") - } + + // Wait until message is received + <-ts.Received if numcalls != 2 { t.Errorf("Should have received 2 calls from fetch request, instead received %v", numcalls)