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

Commit b305189

Browse files
committed
test: add PutMany with duplicates test
1 parent ab78397 commit b305189

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

arc_cache.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,6 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
198198
// call put on block if result is inconclusive or we are sure that
199199
// the block isn't in storage
200200
if has, _, ok := b.queryCache(block.Cid()); !ok || (ok && !has) {
201-
if !block.Cid().Defined() {
202-
log.Error("undefined cid in arc cache")
203-
continue
204-
}
205-
206201
good = append(good, block)
207202
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]] = &sync.Mutex{}
208203
}
@@ -220,7 +215,11 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
220215
}
221216
for _, block := range good {
222217
b.cacheSize(block.Cid(), len(block.RawData()))
223-
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]].Unlock()
218+
if mx := mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]]; mx != nil {
219+
mx.Unlock()
220+
// set nil to avoid double unlocking
221+
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]] = nil
222+
}
224223
}
225224
return nil
226225
}

arc_cache_test.go

+29-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
)
1212

1313
var exampleBlock = blocks.NewBlock([]byte("foo"))
14+
var exampleBlock2 = blocks.NewBlock([]byte("bar"))
1415

1516
func testArcCached(ctx context.Context, bs Blockstore) (*arccache, error) {
1617
if ctx == nil {
@@ -246,16 +247,34 @@ func TestDifferentKeyObjectsWork(t *testing.T) {
246247
}
247248

248249
func TestPutManyCaches(t *testing.T) {
249-
arc, _, cd := createStores(t)
250-
arc.PutMany([]blocks.Block{exampleBlock})
250+
t.Run("happy path PutMany", func(t *testing.T) {
251+
arc, _, cd := createStores(t)
252+
arc.PutMany([]blocks.Block{exampleBlock})
253+
254+
trap("has hit datastore", cd, t)
255+
arc.Has(exampleBlock.Cid())
256+
arc.GetSize(exampleBlock.Cid())
257+
untrap(cd)
258+
arc.DeleteBlock(exampleBlock.Cid())
259+
260+
arc.Put(exampleBlock)
261+
trap("PunMany has hit datastore", cd, t)
262+
arc.PutMany([]blocks.Block{exampleBlock})
263+
})
251264

252-
trap("has hit datastore", cd, t)
253-
arc.Has(exampleBlock.Cid())
254-
arc.GetSize(exampleBlock.Cid())
255-
untrap(cd)
256-
arc.DeleteBlock(exampleBlock.Cid())
265+
t.Run("PutMany with duplicates", func(t *testing.T) {
266+
arc, _, cd := createStores(t)
267+
arc.PutMany([]blocks.Block{exampleBlock, exampleBlock2, exampleBlock})
268+
269+
trap("has hit datastore", cd, t)
270+
arc.Has(exampleBlock.Cid())
271+
arc.GetSize(exampleBlock.Cid())
272+
untrap(cd)
273+
arc.DeleteBlock(exampleBlock.Cid())
274+
275+
arc.Put(exampleBlock)
276+
trap("PunMany has hit datastore", cd, t)
277+
arc.PutMany([]blocks.Block{exampleBlock})
278+
})
257279

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

0 commit comments

Comments
 (0)