@cryptotaxi247 / kubo / commits / 7d07347ed

Provider tests

License: MIT Signed-off-by: Erik Ingenito <erik@carbonfive.com>

Erik Ingenito committed Mar 14, 2019 at 16:49 UTC 7d07347ed718ff1759b6210e3113946b3dcb2d6e
2 files changed +80
go.mod
+1
@@ -30,6 +30,7 @@ require (
30 github.com/ipfs/go-fs-lock v0.0.1
31 github.com/ipfs/go-ipfs-addr v0.0.1
32 github.com/ipfs/go-ipfs-blockstore v0.0.1
33 + github.com/ipfs/go-ipfs-blocksutil v0.0.1
34 github.com/ipfs/go-ipfs-chunker v0.0.1
35 github.com/ipfs/go-ipfs-cmdkit v0.0.1
36 github.com/ipfs/go-ipfs-cmds v0.0.1
provider/provider_test.go new
+79
@@ -0,0 +1,79 @@
1 +package provider
2 +
3 +import (
4 + "context"
5 + "github.com/ipfs/go-cid"
6 + "github.com/ipfs/go-datastore"
7 + "github.com/ipfs/go-ipfs-blocksutil"
8 + pstore "github.com/libp2p/go-libp2p-peerstore"
9 + "math/rand"
10 + "testing"
11 + "time"
12 +)
13 +
14 +var blockGenerator = blocksutil.NewBlockGenerator()
15 +
16 +type mockRouting struct {
17 + provided chan cid.Cid
18 +}
19 +
20 +func mockContentRouting() *mockRouting {
21 + r := mockRouting{}
22 + r.provided = make(chan cid.Cid)
23 + return &r
24 +}
25 +
26 +func TestAnnouncement(t *testing.T) {
27 + ctx := context.Background()
28 + defer func() {
29 + ctx.Done()
30 + }()
31 +
32 + queue, err := NewQueue(ctx, "test", datastore.NewMapDatastore())
33 + if err != nil {
34 + t.Fatal(err)
35 + }
36 +
37 + r := mockContentRouting()
38 +
39 + provider := NewProvider(ctx, queue, r)
40 + provider.Run()
41 +
42 + cids := cid.NewSet()
43 +
44 + for i := 0; i < 100; i++ {
45 + c := blockGenerator.Next().Cid()
46 + cids.Add(c)
47 + }
48 +
49 + go func() {
50 + for _, c := range cids.Keys() {
51 + err = provider.Provide(c)
52 + // A little goroutine stirring to exercise some different states
53 + r := rand.Intn(10)
54 + time.Sleep(time.Microsecond * time.Duration(r))
55 + }
56 + }()
57 +
58 + for cids.Len() > 0 {
59 + select {
60 + case cp := <-r.provided:
61 + if !cids.Has(cp) {
62 + t.Fatal("Wrong CID provided")
63 + }
64 + cids.Remove(cp)
65 + case <-time.After(time.Second * 1):
66 + t.Fatal("Timeout waiting for cids to be provided.")
67 + }
68 + }
69 +}
70 +
71 +func (r *mockRouting) Provide(ctx context.Context, cid cid.Cid, recursive bool) error {
72 + r.provided <- cid
73 + return nil
74 +}
75 +
76 +// Search for peers who are able to provide a given key
77 +func (r *mockRouting) FindProvidersAsync(ctx context.Context, cid cid.Cid, timeout int) <-chan pstore.PeerInfo {
78 + return nil
79 +}
\ No newline at end of file