Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Oct 27, 2023
1 parent 43d5258 commit f663678
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/storer/cachestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func (db *DB) cacheWorker(ctx context.Context) {
continue
}

newSize := size - uint64((float64(capc) * 0.90)) // evict until cache size is 90% of capacity
evict := size - uint64((float64(capc) * 0.90)) // evict until cache size is 90% of capacity

err := db.Execute(ctx, func(s internal.Storage) error {
return db.cacheObj.RemoveOldest(ctx, s, s.ChunkStore(), newSize)
return db.cacheObj.RemoveOldest(ctx, s, s.ChunkStore(), evict)
})
if err != nil {
db.logger.Warning("cache eviction failure", "error", err)
Expand Down
21 changes: 19 additions & 2 deletions pkg/storer/cachestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/ethersphere/bee/pkg/spinlock"
storage "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/storage/storagetest"
chunktesting "github.com/ethersphere/bee/pkg/storage/testing"
Expand Down Expand Up @@ -99,6 +100,7 @@ func testCacheStore(t *testing.T, newStorer func() (*storer.DB, error)) {
lstore.SetRepoStorePutHook(nil)
// add chunks beyond capacity and verify the correct chunks are removed
// from the cache based on last access order

newChunks := chunktesting.GenerateTestRandomChunks(5)
putter := lstore.Cache()
for _, ch := range newChunks {
Expand All @@ -113,8 +115,23 @@ func testCacheStore(t *testing.T, newStorer func() (*storer.DB, error)) {
t.Fatal(err)
}

if info.Cache.Size != 10 {
t.Fatalf("unexpected cache size: want 10 have %d", info.Cache.Size)
info, err = lstore.DebugInfo(context.Background())
if err != nil {
t.Fatal(err)
}

err = spinlock.WaitWithInterval(time.Second*5, time.Second, func() bool {
info, err = lstore.DebugInfo(context.Background())
if err != nil {
t.Fatal(err)
}
if info.Cache.Size == 10 {
return true
}
return false
})
if err != nil {
t.Fatal(err)
}
})
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/storer/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,14 @@ func (c *Cache) RemoveOldest(
}
}

return batch.Commit()
err = batch.Commit()
if err != nil {
return err
}

c.size.Add(-int64(len(evictItems)))

return nil
}

type cacheEntry struct {
Expand Down

0 comments on commit f663678

Please sign in to comment.