Unverified Commit 6486d677 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #47 from shazow/blockstore-put-tst

Add test to maintain Put contract of calling Has first
parents 717588ff 9a3ae2aa
......@@ -104,6 +104,38 @@ func TestPutThenGetSizeBlock(t *testing.T) {
}
}
type countHasDS struct {
ds.Datastore
hasCount int
}
func (ds *countHasDS) Has(key ds.Key) (exists bool, err error) {
ds.hasCount += 1
return ds.Datastore.Has(key)
}
func TestPutUsesHas(t *testing.T) {
// Some datastores rely on the implementation detail that Put checks Has
// first, to avoid overriding existing objects' metadata. This test ensures
// that Blockstore continues to behave this way.
// Please ping https://github.com/ipfs/go-ipfs-blockstore/pull/47 if this
// behavior is being removed.
ds := &countHasDS{
Datastore: ds.NewMapDatastore(),
}
bs := NewBlockstore(ds_sync.MutexWrap(ds))
bl := blocks.NewBlock([]byte("some data"))
if err := bs.Put(bl); err != nil {
t.Fatal(err)
}
if err := bs.Put(bl); err != nil {
t.Fatal(err)
}
if ds.hasCount != 2 {
t.Errorf("Blockstore did not call Has before attempting Put, this breaks compatibility")
}
}
func TestHashOnRead(t *testing.T) {
orginalDebug := u.Debug
defer (func() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment