Skip to content

Commit

Permalink
test MRU reset
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacd committed Nov 3, 2023
1 parent 1db4c89 commit bdbebee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions collector/compression/zstd/mru.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ func (mru *mru[T]) Reset() Gen {
mru.mu.Lock()
defer mru.mu.Unlock()
mru.reset = Gen(time.Now())
mru.freelist = nil
mru.putTimes = nil
return mru.reset
}
37 changes: 26 additions & 11 deletions collector/compression/zstd/mru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package zstd

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -16,11 +15,11 @@ type gint struct {
}

func TestMRUGet(t *testing.T) {
defer resetTest()

var m mru[*gint]
const cnt = 5

TTL = time.Minute

v, g := m.Get()
require.Nil(t, v)

Expand All @@ -42,9 +41,12 @@ func TestMRUGet(t *testing.T) {
}

func TestMRUPut(t *testing.T) {
defer resetTest()

var m mru[*gint]
const cnt = 5

// Use zero TTL => no freelist
TTL = 0

g := m.Reset()
Expand All @@ -59,11 +61,24 @@ func TestMRUPut(t *testing.T) {
require.Equal(t, 0, m.Size())
}

// func TestMRUReset(t *testing.T) {
// var m mru[*gint]
// TTL = time.Minute
// m.Put(new(gint))
// require.Equal(t, 1, m.Size())
// m.Reset()
// require.Equal(t, 1, m.Size())
// }
func TestMRUReset(t *testing.T) {
defer resetTest()

var m mru[*gint]

g := m.Reset()

m.Put(&gint{
Gen: g,
})
require.Equal(t, 1, m.Size())

m.Reset()
require.Equal(t, 0, m.Size())

// This doesn't take because its generation is before the reset.
m.Put(&gint{
Gen: g,
})
require.Equal(t, 0, m.Size())
}

0 comments on commit bdbebee

Please sign in to comment.