Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from ipfs/fix/handle-queue-close
Browse files Browse the repository at this point in the history
fix: handle closed queue
  • Loading branch information
Stebalien committed Apr 6, 2020
2 parents 3a98ef9 + 596dc49 commit 4ff379e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion simple/provider.go
Expand Up @@ -85,7 +85,12 @@ func (p *Provider) handleAnnouncements() {
select {
case <-p.ctx.Done():
return
case c := <-p.queue.Dequeue():
case c, ok := <-p.queue.Dequeue():
if !ok {
// queue closed.
return
}

p.doProvide(c)
}
}
Expand Down
31 changes: 31 additions & 0 deletions simple/provider_test.go
Expand Up @@ -84,6 +84,37 @@ func TestAnnouncement(t *testing.T) {
t.Fatal("Timeout waiting for cids to be provided.")
}
}
prov.Close()

select {
case cp := <-r.provided:
t.Fatal("did not expect to provide CID: ", cp)
case <-time.After(time.Second * 1):
}
}

func TestClose(t *testing.T) {
ctx := context.Background()
defer ctx.Done()

ds := sync.MutexWrap(datastore.NewMapDatastore())
queue, err := q.NewQueue(ctx, "test", ds)
if err != nil {
t.Fatal(err)
}

r := mockContentRouting()

prov := NewProvider(ctx, queue, r)
prov.Run()

prov.Close()

select {
case cp := <-r.provided:
t.Fatal("did not expect to provide anything, provided: ", cp)
case <-time.After(time.Second * 1):
}
}

func TestAnnouncementTimeout(t *testing.T) {
Expand Down

0 comments on commit 4ff379e

Please sign in to comment.