Ejemplo n.º 1
0
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")
	}
}
Ejemplo n.º 2
0
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)
	}
}
Ejemplo n.º 3
0
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)
	}
}
Ejemplo n.º 4
0
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)
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
0
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")
	}
}
Ejemplo n.º 7
0
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()
	}
}
Ejemplo n.º 8
0
func (bg *BlockGenerator) Next() *blocks.Block {
	bg.seq++
	return blocks.NewBlock([]byte(string(bg.seq)))
}
Ejemplo n.º 9
0
func blockFromInt(i int) *blocks.Block {
	return blocks.NewBlock([]byte(string(i)))
}