From bdbebee7ef49765f636b11d329f74cbf1dc8e1ea Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Fri, 3 Nov 2023 14:41:32 -0700 Subject: [PATCH] test MRU reset --- collector/compression/zstd/mru.go | 2 ++ collector/compression/zstd/mru_test.go | 37 ++++++++++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/collector/compression/zstd/mru.go b/collector/compression/zstd/mru.go index b6970740..913852f8 100644 --- a/collector/compression/zstd/mru.go +++ b/collector/compression/zstd/mru.go @@ -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 } diff --git a/collector/compression/zstd/mru_test.go b/collector/compression/zstd/mru_test.go index 518c8673..c68875c4 100644 --- a/collector/compression/zstd/mru_test.go +++ b/collector/compression/zstd/mru_test.go @@ -5,7 +5,6 @@ package zstd import ( "testing" - "time" "github.com/stretchr/testify/require" ) @@ -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) @@ -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() @@ -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()) +}