Skip to content

Commit

Permalink
Provider tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Erik Ingenito <[email protected]>
  • Loading branch information
Erik Ingenito committed Mar 14, 2019
1 parent 8337035 commit 7d07347
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/ipfs/go-fs-lock v0.0.1
github.com/ipfs/go-ipfs-addr v0.0.1
github.com/ipfs/go-ipfs-blockstore v0.0.1
github.com/ipfs/go-ipfs-blocksutil v0.0.1
github.com/ipfs/go-ipfs-chunker v0.0.1
github.com/ipfs/go-ipfs-cmdkit v0.0.1
github.com/ipfs/go-ipfs-cmds v0.0.1
Expand Down
79 changes: 79 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package provider

import (
"context"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-ipfs-blocksutil"
pstore "github.com/libp2p/go-libp2p-peerstore"
"math/rand"
"testing"
"time"
)

var blockGenerator = blocksutil.NewBlockGenerator()

type mockRouting struct {
provided chan cid.Cid
}

func mockContentRouting() *mockRouting {
r := mockRouting{}
r.provided = make(chan cid.Cid)
return &r
}

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

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

r := mockContentRouting()

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

cids := cid.NewSet()

for i := 0; i < 100; i++ {
c := blockGenerator.Next().Cid()
cids.Add(c)
}

go func() {
for _, c := range cids.Keys() {
err = provider.Provide(c)
// A little goroutine stirring to exercise some different states
r := rand.Intn(10)
time.Sleep(time.Microsecond * time.Duration(r))
}
}()

for cids.Len() > 0 {
select {
case cp := <-r.provided:
if !cids.Has(cp) {
t.Fatal("Wrong CID provided")
}
cids.Remove(cp)
case <-time.After(time.Second * 1):
t.Fatal("Timeout waiting for cids to be provided.")
}
}
}

func (r *mockRouting) Provide(ctx context.Context, cid cid.Cid, recursive bool) error {
r.provided <- cid
return nil
}

// Search for peers who are able to provide a given key
func (r *mockRouting) FindProvidersAsync(ctx context.Context, cid cid.Cid, timeout int) <-chan pstore.PeerInfo {
return nil
}

0 comments on commit 7d07347

Please sign in to comment.