func TestPutThenGetBlocks(t *testing.T) { checkErr := func(err error) { if err != nil { t.Fatal(err) } } bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore())) block1 := blocks.NewBlock([]byte("some data 1")) block2 := blocks.NewBlock([]byte("some data 2")) block3 := blocks.NewBlock([]byte("some data 3")) block4 := blocks.NewBlock([]byte("some data 4")) checkErr(bs.Put(block1)) checkErr(bs.Put(block2)) checkErr(bs.Put(block3)) out := bs.GetChan([]key.Key{block1.Key(), block2.Key(), block3.Key(), block4.Key()}) checkGets := func(expect *blocks.Block, out <-chan *blocks.Block) { actual := <-out if !bytes.Equal(expect.Data, actual.Data) { t.Fatal("blocks differ") } } checkGets(block1, out) checkGets(block2, out) checkGets(block3, out) _, more := <-out if more { t.Fatal("should be closed") } }
func TestValueTypeMismatch(t *testing.T) { block := blocks.NewBlock([]byte("some data")) datastore := ds.NewMapDatastore() k := BlockPrefix.Child(block.Key().DsKey()) datastore.Put(k, "data that isn't a block!") blockstore := NewBlockstore(ds_sync.MutexWrap(datastore)) _, err := blockstore.Get(block.Key()) if err != ValueTypeMismatch { t.Fatal(err) } }
func TestHasBlockReturnsNil(t *testing.T) { store := bstore() ex := Exchange(store) block := blocks.NewBlock([]byte("data")) err := ex.HasBlock(context.Background(), block) if err != nil { t.Fail() } if _, err := store.Get(block.Key()); err != nil { t.Fatal(err) } }
func TestElideDuplicateWrite(t *testing.T) { cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()} bs := NewBlockstore(syncds.MutexWrap(cd)) cachedbs, err := WriteCached(bs, 1) if err != nil { t.Fatal(err) } b1 := blocks.NewBlock([]byte("foo")) cachedbs.Put(b1) cd.SetFunc(func() { t.Fatal("write hit the datastore") }) cachedbs.Put(b1) }
func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []key.Key) { if d == nil { d = ds.NewMapDatastore() } bs := NewBlockstore(ds_sync.MutexWrap(d)) keys := make([]key.Key, N) for i := 0; i < N; i++ { block := blocks.NewBlock([]byte(fmt.Sprintf("some data %d", i))) err := bs.Put(block) if err != nil { t.Fatal(err) } keys[i] = block.Key() } return bs, keys }
func TestPutThenGetBlock(t *testing.T) { bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore())) block := blocks.NewBlock([]byte("some data")) err := bs.Put(block) if err != nil { t.Fatal(err) } blockFromBlockstore, err := bs.Get(block.Key()) if err != nil { t.Fatal(err) } if !bytes.Equal(block.Data, blockFromBlockstore.Data) { t.Fatal("blocks differ") } }
func TestRemoveCacheEntryOnDelete(t *testing.T) { b := blocks.NewBlock([]byte("foo")) cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()} bs := NewBlockstore(syncds.MutexWrap(cd)) cachedbs, err := WriteCached(bs, 1) if err != nil { t.Fatal(err) } cachedbs.Put(b) writeHitTheDatastore := false cd.SetFunc(func() { writeHitTheDatastore = true }) cachedbs.DeleteBlock(b.Key()) cachedbs.Put(b) if !writeHitTheDatastore { t.Fail() } }
func (bg *BlockGenerator) Next() *blocks.Block { bg.seq++ return blocks.NewBlock([]byte(string(bg.seq))) }
func blockFromInt(i int) *blocks.Block { return blocks.NewBlock([]byte(string(i))) }