From b6348f59921db887462598109901a1e31934e7f1 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Tue, 26 Oct 2021 16:35:14 +0800 Subject: [PATCH] feat(test): update lru test --- pkg/lru/lru_test.go | 11 ++++++----- pkg/lru/ttl_test.go | 4 ++-- pkg/lru/woker_test.go | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/lru/lru_test.go b/pkg/lru/lru_test.go index 1a1e3c8..369e42a 100644 --- a/pkg/lru/lru_test.go +++ b/pkg/lru/lru_test.go @@ -61,7 +61,7 @@ func TestNewLRUCache2(t *testing.T) { func TestLruProcess(t *testing.T) { lru := NewLRUCache() - lru.clearSize = 1000 + lru.clearSize = 3600 for i := 100; i < 200; i++ { lru.Add(&proto.BaseKey{ @@ -78,16 +78,17 @@ func TestLruProcess(t *testing.T) { }, stringx.NewStringSingle()) } - require.Equal(t, lru.nowSize, int64(200*8)) + require.Equal(t, lru.nowSize, int64(200*24)) // 自动清理测试 + fmt.Println(lru.clearSize) + require.Equal(t, lru.li.Len(), 200) time.Sleep(3 * time.Second) - fmt.Println(lru.nowSize) require.Less(t, lru.nowSize, lru.clearSize+1) - // TTL 测试 + // TTL 测试, 100-200 key 发生自动清理 留下 50-100 共 100(0-100) + 20个 key,5s 后,前 0-100的 key 过期,剩下 time.Sleep(2 * time.Second) - require.Equal(t, lru.li.Len(), 25) + require.Equal(t, lru.li.Len(), 50) // 过期全部的 Key for i := 100; i < 200; i++ { diff --git a/pkg/lru/ttl_test.go b/pkg/lru/ttl_test.go index 76b8d15..2f67610 100644 --- a/pkg/lru/ttl_test.go +++ b/pkg/lru/ttl_test.go @@ -28,9 +28,9 @@ func Test_LruTTl(t *testing.T) { Key: "990", Ttl: 10, }, s) - require.Equal(t, lru.nowSize, int64(16)) + require.Equal(t, lru.nowSize, int64(48)) time.Sleep(4 * time.Second) - require.Equal(t, lru.nowSize, int64(8)) + require.Equal(t, lru.nowSize, int64(24)) } diff --git a/pkg/lru/woker_test.go b/pkg/lru/woker_test.go index 6009258..887c63c 100644 --- a/pkg/lru/woker_test.go +++ b/pkg/lru/woker_test.go @@ -16,7 +16,7 @@ func TestWorker(t *testing.T) { ctx := context.Background() lru := NewLRUCache() produce := event.NewProduce(lru.GetDriver()) - workEvent := event.NewEvent(OptionEventName) + workEvent := produce.NewEvent(OptionEventName) workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) { v1 := stringx.NewStringSingle() key := proto.BaseKey{ @@ -39,7 +39,7 @@ func TestSingleCache_DelToClearSize(t *testing.T) { produce := event.NewProduce(lru.GetDriver()) for i := int32(20000); i >= 1; i-- { - workEvent := event.NewEvent(OptionEventName) + workEvent := produce.NewEvent(OptionEventName) workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) { v1 := stringx.NewStringSingle() key := proto.BaseKey{ @@ -52,6 +52,7 @@ func TestSingleCache_DelToClearSize(t *testing.T) { workEvent.InitWaitEvent() produce.Call(ctx, workEvent) workEvent.StartWaitEvent(2 * time.Second) + produce.Recovery(workEvent) } time.Sleep(5 * time.Second)