Skip to content

Commit

Permalink
Add test to maintain Put contract of calling Has first
Browse files Browse the repository at this point in the history
This commit was moved from ipfs/go-ipfs-blockstore@9a3ae2a
  • Loading branch information
shazow committed Feb 24, 2020
1 parent f821f8a commit fdc5711
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions blockstore/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit fdc5711

Please sign in to comment.