From 02942c3041f092d6a91ac5d17017a49eb5233afa Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Thu, 23 Apr 2020 16:46:28 -0400 Subject: [PATCH] fix: race in session test --- internal/session/session_test.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/session/session_test.go b/internal/session/session_test.go index a8773f1..194a1ec 100644 --- a/internal/session/session_test.go +++ b/internal/session/session_test.go @@ -2,6 +2,7 @@ package session import ( "context" + "sync" "testing" "time" @@ -59,8 +60,9 @@ type wantReq struct { } type fakePeerManager struct { - cancels []cid.Cid wantReqs chan wantReq + lk sync.Mutex + cancels []cid.Cid } func newFakePeerManager() *fakePeerManager { @@ -81,8 +83,15 @@ func (pm *fakePeerManager) BroadcastWantHaves(ctx context.Context, cids []cid.Ci } } func (pm *fakePeerManager) SendCancels(ctx context.Context, cancels []cid.Cid) { + pm.lk.Lock() + defer pm.lk.Unlock() pm.cancels = append(pm.cancels, cancels...) } +func (pm *fakePeerManager) allCancels() []cid.Cid { + pm.lk.Lock() + defer pm.lk.Unlock() + return append([]cid.Cid{}, pm.cancels...) +} func TestSessionGetBlocks(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) @@ -173,7 +182,7 @@ func TestSessionGetBlocks(t *testing.T) { time.Sleep(10 * time.Millisecond) // Verify wants were cancelled - if len(fpm.cancels) != len(blks) { + if len(fpm.allCancels()) != len(blks) { t.Fatal("expected cancels to be sent for all wants") } } -- GitLab