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

Commit

Permalink
test: add PutMany with duplicates test
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist committed May 4, 2021
1 parent ab78397 commit 7475315
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
11 changes: 5 additions & 6 deletions arc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
// call put on block if result is inconclusive or we are sure that
// the block isn't in storage
if has, _, ok := b.queryCache(block.Cid()); !ok || (ok && !has) {
if !block.Cid().Defined() {
log.Error("undefined cid in arc cache")
continue
}

good = append(good, block)
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]] = &sync.Mutex{}
}
Expand All @@ -220,7 +215,11 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
}
for _, block := range good {
b.cacheSize(block.Cid(), len(block.RawData()))
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]].Unlock()
if mx := mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]]; mx != nil {
mx.Unlock()
// set nil to avoid double unlocking
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]] = nil
}
}
return nil
}
Expand Down
38 changes: 28 additions & 10 deletions arc_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,34 @@ func TestDifferentKeyObjectsWork(t *testing.T) {
}

func TestPutManyCaches(t *testing.T) {
arc, _, cd := createStores(t)
arc.PutMany([]blocks.Block{exampleBlock})
t.Run("happy path PutMany", func(t *testing.T) {
arc, _, cd := createStores(t)
arc.PutMany([]blocks.Block{exampleBlock})

trap("has hit datastore", cd, t)
arc.Has(exampleBlock.Cid())
arc.GetSize(exampleBlock.Cid())
untrap(cd)
arc.DeleteBlock(exampleBlock.Cid())

arc.Put(exampleBlock)
trap("PunMany has hit datastore", cd, t)
arc.PutMany([]blocks.Block{exampleBlock})
})

trap("has hit datastore", cd, t)
arc.Has(exampleBlock.Cid())
arc.GetSize(exampleBlock.Cid())
untrap(cd)
arc.DeleteBlock(exampleBlock.Cid())
t.Run("PutMany with duplicates", func(t *testing.T) {
arc, _, cd := createStores(t)
arc.PutMany([]blocks.Block{exampleBlock, exampleBlock})

trap("has hit datastore", cd, t)
arc.Has(exampleBlock.Cid())
arc.GetSize(exampleBlock.Cid())
untrap(cd)
arc.DeleteBlock(exampleBlock.Cid())

arc.Put(exampleBlock)
trap("PunMany has hit datastore", cd, t)
arc.PutMany([]blocks.Block{exampleBlock})
})

arc.Put(exampleBlock)
trap("PunMany has hit datastore", cd, t)
arc.PutMany([]blocks.Block{exampleBlock})
}

0 comments on commit 7475315

Please sign in to comment.