github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/blockservice/test/blocks_test.go (about)

     1  package bstest
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
     9  	dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
    10  	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
    11  	blocks "github.com/ipfs/go-ipfs/blocks"
    12  	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
    13  	blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
    14  	key "github.com/ipfs/go-ipfs/blocks/key"
    15  	. "github.com/ipfs/go-ipfs/blockservice"
    16  	offline "github.com/ipfs/go-ipfs/exchange/offline"
    17  	u "github.com/ipfs/go-ipfs/util"
    18  )
    19  
    20  func TestBlocks(t *testing.T) {
    21  	bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
    22  	bs := New(bstore, offline.Exchange(bstore))
    23  	defer bs.Close()
    24  
    25  	b := blocks.NewBlock([]byte("beep boop"))
    26  	h := u.Hash([]byte("beep boop"))
    27  	if !bytes.Equal(b.Multihash, h) {
    28  		t.Error("Block Multihash and data multihash not equal")
    29  	}
    30  
    31  	if b.Key() != key.Key(h) {
    32  		t.Error("Block key and data multihash key not equal")
    33  	}
    34  
    35  	k, err := bs.AddBlock(b)
    36  	if err != nil {
    37  		t.Error("failed to add block to BlockService", err)
    38  		return
    39  	}
    40  
    41  	if k != b.Key() {
    42  		t.Error("returned key is not equal to block key", err)
    43  	}
    44  
    45  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    46  	defer cancel()
    47  	b2, err := bs.GetBlock(ctx, b.Key())
    48  	if err != nil {
    49  		t.Error("failed to retrieve block from BlockService", err)
    50  		return
    51  	}
    52  
    53  	if b.Key() != b2.Key() {
    54  		t.Error("Block keys not equal.")
    55  	}
    56  
    57  	if !bytes.Equal(b.Data, b2.Data) {
    58  		t.Error("Block data is not equal.")
    59  	}
    60  }
    61  
    62  func TestGetBlocksSequential(t *testing.T) {
    63  	var servs = Mocks(4)
    64  	for _, s := range servs {
    65  		defer s.Close()
    66  	}
    67  	bg := blocksutil.NewBlockGenerator()
    68  	blks := bg.Blocks(50)
    69  
    70  	var keys []key.Key
    71  	for _, blk := range blks {
    72  		keys = append(keys, blk.Key())
    73  		servs[0].AddBlock(blk)
    74  	}
    75  
    76  	t.Log("one instance at a time, get blocks concurrently")
    77  
    78  	for i := 1; i < len(servs); i++ {
    79  		ctx, cancel := context.WithTimeout(context.Background(), time.Second*50)
    80  		defer cancel()
    81  		out := servs[i].GetBlocks(ctx, keys)
    82  		gotten := make(map[key.Key]*blocks.Block)
    83  		for blk := range out {
    84  			if _, ok := gotten[blk.Key()]; ok {
    85  				t.Fatal("Got duplicate block!")
    86  			}
    87  			gotten[blk.Key()] = blk
    88  		}
    89  		if len(gotten) != len(blks) {
    90  			t.Fatalf("Didnt get enough blocks back: %d/%d", len(gotten), len(blks))
    91  		}
    92  	}
    93  }