Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera committed Jul 30, 2024
1 parent aa4b919 commit ad3f9d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions mod/node-api/backend/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func (c *QueryCache) GetQueryContext(slot math.Slot) (context.Context, bool) {
}

// AddQueryContext adds a query context to the cache for the given slot.
//
//nolint:revive // ctx is the value in cache.
func (c *QueryCache) AddQueryContext(slot math.Slot, ctx context.Context) {
c.queryCtxCache.Add(slot, ctx)
}
16 changes: 9 additions & 7 deletions mod/node-api/backend/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ func TestQueryCache(t *testing.T) {
}

cacheUnderTest := cache.NewQueryCache(cacheConfig)
ctx := context.WithValue(context.Background(), "test", "test")
//nolint:revive,staticcheck // ok for test.
ctx := context.WithValue(context.Background(), "testK", "testV")

t.Run("Get from empty cache", func(t *testing.T) {
ctx, ok := cacheUnderTest.GetQueryContext(0)
emtpyCtx, ok := cacheUnderTest.GetQueryContext(0)
require.False(t, ok)
require.Nil(t, ctx)
require.Nil(t, emtpyCtx)
})

t.Run("Set and Get", func(t *testing.T) {
Expand All @@ -51,7 +52,7 @@ func TestQueryCache(t *testing.T) {

ctx2, ok := cacheUnderTest.GetQueryContext(slot)
require.True(t, ok)
require.Equal(t, ctx.Value("test"), ctx2.Value("test"))
require.Equal(t, ctx.Value("testK"), ctx2.Value("testK"))
})

t.Run("Overwrite existing", func(t *testing.T) {
Expand All @@ -60,14 +61,15 @@ func TestQueryCache(t *testing.T) {

ctx2, ok := cacheUnderTest.GetQueryContext(slot)
require.True(t, ok)
require.Equal(t, ctx.Value("test"), ctx2.Value("test"))
require.Equal(t, ctx.Value("testK"), ctx2.Value("testK"))

newCtx := context.WithValue(context.Background(), "test", "new_test")
//nolint:revive,staticcheck // ok for test.
newCtx := context.WithValue(context.Background(), "testK", "testV_new")
cacheUnderTest.AddQueryContext(slot, newCtx)

ctx3, ok := cacheUnderTest.GetQueryContext(slot)
require.True(t, ok)
require.Equal(t, newCtx.Value("test"), ctx3.Value("test"))
require.Equal(t, newCtx.Value("testK"), ctx3.Value("testK"))
})

t.Run("Prune and verify deletion", func(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion mod/node-api/backend/cache/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ package cache
import "time"

const (
// defaultQueryContextSize is the default size of the query context cache.
defaultQueryContextSize = 20
defaultQueryContextTTL = 0
// defaultQueryContextTTL is 0, which means the query context does not have
// an expiry.
defaultQueryContextTTL = 0
)

// Config is the configuration for an EngineCache.
Expand Down

0 comments on commit ad3f9d4

Please sign in to comment.