Commit 02942c30 authored by Dirk McCormick's avatar Dirk McCormick

fix: race in session test

parent 932e2d60
...@@ -2,6 +2,7 @@ package session ...@@ -2,6 +2,7 @@ package session
import ( import (
"context" "context"
"sync"
"testing" "testing"
"time" "time"
...@@ -59,8 +60,9 @@ type wantReq struct { ...@@ -59,8 +60,9 @@ type wantReq struct {
} }
type fakePeerManager struct { type fakePeerManager struct {
cancels []cid.Cid
wantReqs chan wantReq wantReqs chan wantReq
lk sync.Mutex
cancels []cid.Cid
} }
func newFakePeerManager() *fakePeerManager { func newFakePeerManager() *fakePeerManager {
...@@ -81,8 +83,15 @@ func (pm *fakePeerManager) BroadcastWantHaves(ctx context.Context, cids []cid.Ci ...@@ -81,8 +83,15 @@ func (pm *fakePeerManager) BroadcastWantHaves(ctx context.Context, cids []cid.Ci
} }
} }
func (pm *fakePeerManager) SendCancels(ctx context.Context, cancels []cid.Cid) { func (pm *fakePeerManager) SendCancels(ctx context.Context, cancels []cid.Cid) {
pm.lk.Lock()
defer pm.lk.Unlock()
pm.cancels = append(pm.cancels, cancels...) 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) { func TestSessionGetBlocks(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
...@@ -173,7 +182,7 @@ func TestSessionGetBlocks(t *testing.T) { ...@@ -173,7 +182,7 @@ func TestSessionGetBlocks(t *testing.T) {
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
// Verify wants were cancelled // 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") t.Fatal("expected cancels to be sent for all wants")
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment