Files
prysm/beacon-chain/sync/slot_aware_cache_test.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
2025-11-14 01:27:22 +00:00

122 lines
3.4 KiB
Go

package sync
import (
"fmt"
"testing"
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
"github.com/OffchainLabs/prysm/v7/testing/require"
)
func TestSlotAwareCache(t *testing.T) {
cache := newSlotAwareCache(100)
t.Run("basic operations", func(t *testing.T) {
// Add entries for different slots
cache.Add(primitives.Slot(10), "key1", "value1")
cache.Add(primitives.Slot(20), "key2", "value2")
cache.Add(primitives.Slot(30), "key3", "value3")
// Check they exist
val, exists := cache.Get("key1")
require.Equal(t, true, exists)
require.Equal(t, "value1", val)
val, exists = cache.Get("key2")
require.Equal(t, true, exists)
require.Equal(t, "value2", val)
val, exists = cache.Get("key3")
require.Equal(t, true, exists)
require.Equal(t, "value3", val)
// Check cache stats
totalEntries, slotsTracked := cache.cache.Len(), len(cache.slotToKeys)
require.Equal(t, 3, totalEntries)
require.Equal(t, 3, slotsTracked)
})
// Test slot-based pruning
t.Run("slot-based pruning", func(t *testing.T) {
cache := newSlotAwareCache(100)
// Add entries for different slots
cache.Add(primitives.Slot(10), "key10", "value10")
cache.Add(primitives.Slot(20), "key20", "value20")
cache.Add(primitives.Slot(30), "key30", "value30")
cache.Add(primitives.Slot(40), "key40", "value40")
pruned := cache.pruneSlotsBefore(primitives.Slot(25))
require.Equal(t, 2, pruned) // Should prune entries from slots 10 and 20
// Check that entries from slots 10 and 20 are gone
_, exists := cache.Get("key10")
require.Equal(t, false, exists)
_, exists = cache.Get("key20")
require.Equal(t, false, exists)
// Check that entries from slots 30 and 40 still exist
val, exists := cache.Get("key30")
require.Equal(t, true, exists)
require.Equal(t, "value30", val)
val, exists = cache.Get("key40")
require.Equal(t, true, exists)
require.Equal(t, "value40", val)
// Check cache stats
totalEntries, slotsTracked := cache.cache.Len(), len(cache.slotToKeys)
require.Equal(t, 2, totalEntries)
require.Equal(t, 2, slotsTracked)
})
t.Run("multiple keys per slot", func(t *testing.T) {
cache := newSlotAwareCache(100)
// Add multiple entries for the same slot
cache.Add(primitives.Slot(10), "key1", "value1")
cache.Add(primitives.Slot(10), "key2", "value2")
cache.Add(primitives.Slot(20), "key3", "value3")
// Check they exist
val, exists := cache.Get("key1")
require.Equal(t, true, exists)
require.Equal(t, "value1", val)
val, exists = cache.Get("key2")
require.Equal(t, true, exists)
require.Equal(t, "value2", val)
// Prune slot 10
pruned := cache.pruneSlotsBefore(primitives.Slot(15))
require.Equal(t, 2, pruned) // Should prune both keys from slot 10
// Check that both keys from slot 10 are gone
_, exists = cache.Get("key1")
require.Equal(t, false, exists)
_, exists = cache.Get("key2")
require.Equal(t, false, exists)
// Check that key from slot 20 still exists
val, exists = cache.Get("key3")
require.Equal(t, true, exists)
require.Equal(t, "value3", val)
})
t.Run("bounded slot tracking", func(t *testing.T) {
cache := newSlotAwareCache(200000) // Large cache to avoid LRU eviction
// Add entries for 1005 slots, each with one key
for i := range 1005 {
slot := primitives.Slot(i)
key := fmt.Sprintf("key%d", i)
cache.Add(slot, key, fmt.Sprintf("value%d", i))
}
// Should only track 1000 slots (oldest 5 slots pruned)
require.Equal(t, 1000, len(cache.slotToKeys))
})
}