Skip to content

Commit 497c51f

Browse files
committed
fix: race in session test
This commit was moved from ipfs/go-bitswap@02942c3
1 parent 6ac6ced commit 497c51f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

bitswap/internal/session/session_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package session
22

33
import (
44
"context"
5+
"sync"
56
"testing"
67
"time"
78

@@ -59,8 +60,9 @@ type wantReq struct {
5960
}
6061

6162
type fakePeerManager struct {
62-
cancels []cid.Cid
6363
wantReqs chan wantReq
64+
lk sync.Mutex
65+
cancels []cid.Cid
6466
}
6567

6668
func newFakePeerManager() *fakePeerManager {
@@ -81,8 +83,15 @@ func (pm *fakePeerManager) BroadcastWantHaves(ctx context.Context, cids []cid.Ci
8183
}
8284
}
8385
func (pm *fakePeerManager) SendCancels(ctx context.Context, cancels []cid.Cid) {
86+
pm.lk.Lock()
87+
defer pm.lk.Unlock()
8488
pm.cancels = append(pm.cancels, cancels...)
8589
}
90+
func (pm *fakePeerManager) allCancels() []cid.Cid {
91+
pm.lk.Lock()
92+
defer pm.lk.Unlock()
93+
return append([]cid.Cid{}, pm.cancels...)
94+
}
8695

8796
func TestSessionGetBlocks(t *testing.T) {
8897
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
@@ -173,7 +182,7 @@ func TestSessionGetBlocks(t *testing.T) {
173182
time.Sleep(10 * time.Millisecond)
174183

175184
// Verify wants were cancelled
176-
if len(fpm.cancels) != len(blks) {
185+
if len(fpm.allCancels()) != len(blks) {
177186
t.Fatal("expected cancels to be sent for all wants")
178187
}
179188
}

0 commit comments

Comments
 (0)