Skip to content

Commit

Permalink
test(prq): test canceling FindProviders context after completion
Browse files Browse the repository at this point in the history
This commit was moved from ipfs/go-bitswap@04e4766
  • Loading branch information
Stebalien committed Feb 27, 2019
1 parent 5f6b72c commit 247573a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions bitswap/providerquerymanager/providerquerymanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,35 @@ func TestFindProviderPreCanceled(t *testing.T) {
t.Fatal("shouldn't have blocked waiting on a closed context")
}
}

func TestCancelFindProvidersAfterCompletion(t *testing.T) {
peers := testutil.GeneratePeers(2)
fpn := &fakeProviderNetwork{
peersFound: peers,
delay: 1 * time.Millisecond,
}
ctx := context.Background()
providerQueryManager := New(ctx, fpn)
providerQueryManager.Startup()
providerQueryManager.SetFindProviderTimeout(100 * time.Millisecond)
keys := testutil.GenerateCids(1)

sessionCtx, cancel := context.WithCancel(ctx)
firstRequestChan := providerQueryManager.FindProvidersAsync(sessionCtx, keys[0])
<-firstRequestChan // wait for everything to start.
time.Sleep(10 * time.Millisecond) // wait for the incoming providres to stop.
cancel() // cancel the context.

timer := time.NewTimer(10 * time.Millisecond)
defer timer.Stop()
for {
select {
case _, ok := <-firstRequestChan:
if !ok {
return
}
case <-timer.C:
t.Fatal("should have finished receiving responses within timeout")
}
}
}

0 comments on commit 247573a

Please # to comment.