Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Add test to maintain Put contract of calling Has first
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Feb 24, 2020
1 parent 717588f commit 9a3ae2a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 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 9a3ae2a

Please sign in to comment.