github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/cachestore_test.go (about) 1 // Copyright 2023 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package storer_test 6 7 import ( 8 "context" 9 "testing" 10 "time" 11 12 "github.com/ethersphere/bee/v2/pkg/spinlock" 13 "github.com/ethersphere/bee/v2/pkg/storage/storagetest" 14 chunktesting "github.com/ethersphere/bee/v2/pkg/storage/testing" 15 storer "github.com/ethersphere/bee/v2/pkg/storer" 16 "github.com/ethersphere/bee/v2/pkg/swarm" 17 ) 18 19 func testCacheStore(t *testing.T, newStorer func() (*storer.DB, error)) { 20 t.Helper() 21 22 chunks := chunktesting.GenerateTestRandomChunks(9) 23 24 lstore, err := newStorer() 25 if err != nil { 26 t.Fatal(err) 27 } 28 29 t.Run("cache chunks", func(t *testing.T) { 30 t.Run("commit", func(t *testing.T) { 31 putter := lstore.Cache() 32 for _, ch := range chunks { 33 err := putter.Put(context.TODO(), ch) 34 if err != nil { 35 t.Fatalf("Cache.Put(...): unexpected error: %v", err) 36 } 37 } 38 }) 39 }) 40 t.Run("lookup", func(t *testing.T) { 41 t.Run("commit", func(t *testing.T) { 42 getter := lstore.Lookup() 43 for _, ch := range chunks { 44 have, err := getter.Get(context.TODO(), ch.Address()) 45 if err != nil { 46 t.Fatalf("Cache.Get(...): unexpected error: %v", err) 47 } 48 if !have.Equal(ch) { 49 t.Fatalf("chunk %s does not match", ch.Address()) 50 } 51 } 52 }) 53 }) 54 t.Run("cache chunks beyond capacity", func(t *testing.T) { 55 newChunks := chunktesting.GenerateTestRandomChunks(5) 56 putter := lstore.Cache() 57 for _, ch := range newChunks { 58 err := putter.Put(context.TODO(), ch) 59 if err != nil { 60 t.Fatalf("Cache.Put(...): unexpected error: %v", err) 61 } 62 } 63 64 info, err := lstore.DebugInfo(context.Background()) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 info, err = lstore.DebugInfo(context.Background()) 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 err = spinlock.WaitWithInterval(time.Second*5, time.Second, func() bool { 75 info, err = lstore.DebugInfo(context.Background()) 76 if err != nil { 77 t.Fatal(err) 78 } 79 if info.Cache.Size == 10 { 80 return true 81 } 82 return false 83 }) 84 if err != nil { 85 t.Fatal(err) 86 } 87 }) 88 } 89 90 func TestCacheStore(t *testing.T) { 91 t.Parallel() 92 93 t.Run("inmem", func(t *testing.T) { 94 t.Parallel() 95 96 testCacheStore(t, func() (*storer.DB, error) { 97 98 opts := dbTestOps(swarm.RandAddress(t), 100, nil, nil, time.Second) 99 opts.CacheCapacity = 10 100 101 return storer.New(context.Background(), "", opts) 102 }) 103 }) 104 t.Run("disk", func(t *testing.T) { 105 t.Parallel() 106 107 opts := dbTestOps(swarm.RandAddress(t), 100, nil, nil, time.Second) 108 opts.CacheCapacity = 10 109 110 testCacheStore(t, diskStorer(t, opts)) 111 }) 112 } 113 114 func BenchmarkCachePutter(b *testing.B) { 115 baseAddr := swarm.RandAddress(b) 116 opts := dbTestOps(baseAddr, 10000, nil, nil, time.Second) 117 opts.CacheCapacity = 10 118 storer, err := diskStorer(b, opts)() 119 if err != nil { 120 b.Fatal(err) 121 } 122 123 b.ResetTimer() 124 b.ReportAllocs() 125 storagetest.BenchmarkChunkStoreWriteSequential(b, storer.Cache()) 126 }