From 6f6fa909468b282478e34b633c78f4ae3d36b064 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 22 Nov 2018 14:29:05 -0800 Subject: [PATCH] add channels to tests using Listen in a thread to sync; add some cleanup to other tests; fix a potential NPE --- peer/connections/peerpeerconnection.go | 4 +++- peer/connections/peerpeerconnection_test.go | 7 +++++-- peer/connections/peerserverconnection_test.go | 7 +++++-- peer/cwtch_peer_test.go | 2 ++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/peer/connections/peerpeerconnection.go b/peer/connections/peerpeerconnection.go index 7db1cea..cd5a674 100644 --- a/peer/connections/peerpeerconnection.go +++ b/peer/connections/peerpeerconnection.go @@ -150,5 +150,7 @@ func (ppc *PeerPeerConnection) Run() error { // Close closes the connection func (ppc *PeerPeerConnection) Close() { ppc.state = KILLED - ppc.connection.Conn.Close() + if ppc.connection != nil { + ppc.connection.Conn.Close() + } } diff --git a/peer/connections/peerpeerconnection_test.go b/peer/connections/peerpeerconnection_test.go index 4901a5e..ec15a1f 100644 --- a/peer/connections/peerpeerconnection_test.go +++ b/peer/connections/peerpeerconnection_test.go @@ -22,8 +22,9 @@ func PeerAuthValid(hostname string, key ed25519.PublicKey) (allowed, known bool) return true, true } -func runtestpeer(t *testing.T, tp *TestPeer, identity identity.Identity) { +func runtestpeer(t *testing.T, tp *TestPeer, identity identity.Identity, listenChan chan bool) { ln, _ := net.Listen("tcp", "127.0.0.1:5452") + listenChan <- true conn, _ := ln.Accept() defer conn.Close() @@ -65,7 +66,9 @@ func TestPeerPeerConnection(t *testing.T) { tp := new(TestPeer) tp.Init() - go runtestpeer(t, tp, identity) + listenChan := make(chan bool) + go runtestpeer(t, tp, identity, listenChan) + <-listenChan state := ppc.GetState() if state != DISCONNECTED { fmt.Println("ERROR state should be disconnected") diff --git a/peer/connections/peerserverconnection_test.go b/peer/connections/peerserverconnection_test.go index 5015025..33f01b2 100644 --- a/peer/connections/peerserverconnection_test.go +++ b/peer/connections/peerserverconnection_test.go @@ -33,8 +33,9 @@ func (ts *TestServer) HandleFetchRequest() []*protocol.GroupMessage { return []*protocol.GroupMessage{{Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}, {Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}} } -func runtestserver(t *testing.T, ts *TestServer, identity identity.Identity) { +func runtestserver(t *testing.T, ts *TestServer, identity identity.Identity, listenChan chan bool) { ln, _ := net.Listen("tcp", "127.0.0.1:5451") + listenChan <- true conn, _ := ln.Accept() defer conn.Close() @@ -70,7 +71,9 @@ func TestPeerServerConnection(t *testing.T) { ts := new(TestServer) ts.Init() - go runtestserver(t, ts, identity) + listenChan := make(chan bool) + go runtestserver(t, ts, identity, listenChan) + <-listenChan onionAddr := identity.Hostname() psc := NewPeerServerConnection(connectivity.LocalProvider(), "127.0.0.1:5451|"+onionAddr) diff --git a/peer/cwtch_peer_test.go b/peer/cwtch_peer_test.go index b71a9bd..6f605c4 100644 --- a/peer/cwtch_peer_test.go +++ b/peer/cwtch_peer_test.go @@ -23,8 +23,10 @@ func TestTrustPeer(t *testing.T) { groupName := "2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd" alice := NewCwtchPeer("alice") alice.Init(connectivity.LocalProvider()) + defer alice.Shutdown() bob := NewCwtchPeer("bob") bob.Init(connectivity.LocalProvider()) + defer bob.Shutdown() bobOnion := bob.GetProfile().Onion aliceOnion := alice.GetProfile().Onion