Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 4b4fab8

Browse files
committed
add simple FindProvs support
1 parent 7853ed6 commit 4b4fab8

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

shell.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,22 @@ func (s *Shell) FindProvs(ctx context.Context, cid string) (<-chan pstore.PeerIn
355355
outchan := make(chan pstore.PeerInfo, 4)
356356

357357
go func() {
358+
defer cancel()
359+
358360
var n notif.QueryEvent
359361
scanner := bufio.NewScanner(resp.Output)
360362
for scanner.Scan() {
361363
json.Unmarshal(scanner.Bytes(), &n)
362364
if n.Type == notif.Provider {
363365
for _, p := range n.Responses {
364-
outchan <- *p
366+
select {
367+
case outchan <- *p:
368+
case <-ctx.Done():
369+
return
370+
}
365371
}
366372
}
367373
}
368-
cancel()
369374
}()
370375

371376
go func() {

shell_test.go

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

33
import (
44
"bytes"
5+
"context"
56
"crypto/md5"
67
"fmt"
78
"io"
@@ -167,3 +168,18 @@ func TestPubSub(t *testing.T) {
167168

168169
is.Nil(sub.Cancel())
169170
}
171+
172+
func TestFindProvs(t *testing.T) {
173+
is := is.New(t)
174+
s := NewShell(shellUrl)
175+
ctx, cancel := context.WithCancel(context.Background())
176+
defer cancel()
177+
178+
c, err := s.FindProvs(ctx, "Qme1g4e3m2SmdiSGGU3vSWmUStwUjc5oECnEriaK9Xa1HU")
179+
is.Nil(err)
180+
181+
p := <-c
182+
t.Logf("prov: %s", p)
183+
is.NotNil(p)
184+
is.NotNil(p.ID)
185+
}

0 commit comments

Comments
 (0)