From 5c01c498756c5c30054a97d84b9dd88cd488c43f Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 1 Jun 2022 11:57:46 -0700 Subject: [PATCH 1/2] Wait for event to be sent before we close --- p2p/host/pstoremanager/pstoremanager_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/p2p/host/pstoremanager/pstoremanager_test.go b/p2p/host/pstoremanager/pstoremanager_test.go index a535d1bde6..ed3289b1e3 100644 --- a/p2p/host/pstoremanager/pstoremanager_test.go +++ b/p2p/host/pstoremanager/pstoremanager_test.go @@ -86,11 +86,18 @@ func TestClose(t *testing.T) { emitter, err := eventBus.Emitter(new(event.EvtPeerConnectednessChanged)) require.NoError(t, err) + + sub, err := eventBus.Subscribe(&event.EvtPeerConnectednessChanged{}) + require.NoError(t, err) + require.NoError(t, emitter.Emit(event.EvtPeerConnectednessChanged{ Peer: "foobar", Connectedness: network.NotConnected, })) - time.Sleep(10 * time.Millisecond) // make sure the event is sent before we close + + // make sure the event is sent before we close + <-sub.Out() + done := make(chan struct{}) pstore.EXPECT().RemovePeer(peer.ID("foobar")).Do(func(peer.ID) { close(done) }) require.NoError(t, man.Close()) From bc1ee4f3c7ad5f95a49fb064b16b38221cf53c0d Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 1 Jun 2022 12:39:13 -0700 Subject: [PATCH 2/2] Add timeout on channel --- p2p/host/pstoremanager/pstoremanager_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/p2p/host/pstoremanager/pstoremanager_test.go b/p2p/host/pstoremanager/pstoremanager_test.go index ed3289b1e3..8a2a8f1ed5 100644 --- a/p2p/host/pstoremanager/pstoremanager_test.go +++ b/p2p/host/pstoremanager/pstoremanager_test.go @@ -96,10 +96,18 @@ func TestClose(t *testing.T) { })) // make sure the event is sent before we close - <-sub.Out() + select { + case <-sub.Out(): + case <-time.After(5 * time.Second): + t.Fatalf("Hit timeout") + } done := make(chan struct{}) pstore.EXPECT().RemovePeer(peer.ID("foobar")).Do(func(peer.ID) { close(done) }) require.NoError(t, man.Close()) - <-done + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatalf("Hit timeout") + } }