From 7475315479d31e0ae4e75f1dcf9e331cd717ff22 Mon Sep 17 00:00:00 2001 From: frrist Date: Tue, 4 May 2021 14:04:35 -0700 Subject: [PATCH] test: add PutMany with duplicates test --- arc_cache.go | 11 +++++------ arc_cache_test.go | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/arc_cache.go b/arc_cache.go index 582b00c..8a35ccf 100644 --- a/arc_cache.go +++ b/arc_cache.go @@ -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{} } @@ -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 } diff --git a/arc_cache_test.go b/arc_cache_test.go index dcd9c6e..a15ff2d 100644 --- a/arc_cache_test.go +++ b/arc_cache_test.go @@ -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}) }