Skip to content

Commit

Permalink
getter is optional; test eviction of least recently used
Browse files Browse the repository at this point in the history
  • Loading branch information
janishorsts committed Oct 11, 2023
1 parent 8f7b057 commit bbf2f24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, error) { //nolint:iret

func (c *Cache[K, V]) populateByGetter(ctx context.Context, key K) (V, error) { //nolint:ireturn
if c.getter == nil {
return zeroValue[V](), errors.New("getter is nil") //nolint:goerr113
return zeroValue[V](), fmt.Errorf("value not found for key: %v: %w", key, ErrNotFound)
}

c.mu.Lock() // TODO: not sure about position of this lock
Expand Down
42 changes: 33 additions & 9 deletions lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"golang.org/x/sync/errgroup"
)

func Test_Cache_WithSize(t *testing.T) {
func Test_WithSize(t *testing.T) {
t.Parallel()

c := New(WithSize[int, int](10))
Expand All @@ -24,7 +24,7 @@ func Test_Cache_WithSize(t *testing.T) {
assert.Zero(t, c.Len())
}

func Test_Cache_WithTTL(t *testing.T) {
func Test_WithTTL(t *testing.T) {
t.Parallel()

ctx := context.Background()
Expand All @@ -43,11 +43,11 @@ func Test_Cache_WithTTL(t *testing.T) {

v, err = c.Get(ctx, 1)

require.ErrorContains(t, err, "getter is nil")
require.ErrorIs(t, err, ErrNotFound)
assert.Empty(t, v)
}

func Test_Cache_WithGetter(t *testing.T) {
func Test_WithGetter(t *testing.T) {
t.Parallel()

err := quick.Check(func(key int, value string) bool {
Expand All @@ -74,7 +74,7 @@ func Test_Cache_WithGetter(t *testing.T) {
assert.NoError(t, err)
}

func Test_Cache_WithGetter_Parallel(t *testing.T) {
func Test_WithGetterParallel(t *testing.T) {
t.Parallel()

key, value := 1, "OK"
Expand Down Expand Up @@ -114,7 +114,7 @@ func Test_Cache_WithGetter_Parallel(t *testing.T) {
assert.EqualValues(t, 1, count)
}

func Test_Cache_Getter_Panics(t *testing.T) {
func Test_GetterPanics(t *testing.T) {
t.Parallel()

c := New(
Expand Down Expand Up @@ -143,7 +143,7 @@ func Test_Cache_Getter_Panics(t *testing.T) {
wg.Wait()
}

func Test_Cache_WithAfterEvict(t *testing.T) {
func Test_WithAfterEvict(t *testing.T) {
t.Parallel()

c := New(
Expand All @@ -166,7 +166,7 @@ func Test_Cache_WithAfterEvict(t *testing.T) {
assert.Empty(t, v)
}

func Test_Cache_Get_One_Key_Multiple_Times(t *testing.T) {
func Test_GetOneKeyMultipleTimes(t *testing.T) {
t.Parallel()

var getterExecCount int
Expand All @@ -191,7 +191,7 @@ func Test_Cache_Get_One_Key_Multiple_Times(t *testing.T) {
assert.Equal(t, 1, getterExecCount)
}

func Test_Cache_Update_Key(t *testing.T) {
func Test_UpdateKey(t *testing.T) {
t.Parallel()

c := New[int, string]()
Expand All @@ -214,3 +214,27 @@ func Test_Cache_Update_Key(t *testing.T) {
require.Equal(t, 1, c.Len())
require.Equal(t, 1, len(c.lookup))
}

func Test_EvictLeastRecent(t *testing.T) {
t.Parallel()

c := New[int, string](WithSize[int, string](2))

ctx := context.Background()

err := c.Set(ctx, 1, "one")
require.NoError(t, err)

err = c.Set(ctx, 2, "two")
require.NoError(t, err)

_, err = c.Get(ctx, 1)
require.NoError(t, err)

err = c.Set(ctx, 3, "three")
require.NoError(t, err)

v, err := c.Get(ctx, 2)
require.ErrorIs(t, err, ErrNotFound)
require.Empty(t, v)
}

0 comments on commit bbf2f24

Please sign in to comment.