github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/exchange/reprovide/reprovide_test.go (about)

     1  package reprovide_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
     7  	dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
     8  	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
     9  	blocks "github.com/ipfs/go-ipfs/blocks"
    10  	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
    11  	mock "github.com/ipfs/go-ipfs/routing/mock"
    12  	testutil "github.com/ipfs/go-ipfs/util/testutil"
    13  
    14  	. "github.com/ipfs/go-ipfs/exchange/reprovide"
    15  )
    16  
    17  func TestReprovide(t *testing.T) {
    18  	ctx, cancel := context.WithCancel(context.Background())
    19  	defer cancel()
    20  
    21  	mrserv := mock.NewServer()
    22  
    23  	idA := testutil.RandIdentityOrFatal(t)
    24  	idB := testutil.RandIdentityOrFatal(t)
    25  
    26  	clA := mrserv.Client(idA)
    27  	clB := mrserv.Client(idB)
    28  
    29  	bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
    30  
    31  	blk := blocks.NewBlock([]byte("this is a test"))
    32  	bstore.Put(blk)
    33  
    34  	reprov := NewReprovider(clA, bstore)
    35  	err := reprov.Reprovide(ctx)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	provs, err := clB.FindProviders(ctx, blk.Key())
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	if len(provs) == 0 {
    46  		t.Fatal("Should have gotten a provider")
    47  	}
    48  
    49  	if provs[0].ID != idA.ID() {
    50  		t.Fatal("Somehow got the wrong peer back as a provider.")
    51  	}
    52  }