From 8551b64b354e8d9e30f833b8227f94ae4b7d16d8 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 11 Nov 2019 01:26:06 -0600 Subject: [PATCH] enforce keys sorting in test --- expirable_cache_test.go | 6 +++++- lru_cache_test.go | 5 ++++- redis_cache_test.go | 6 +++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/expirable_cache_test.go b/expirable_cache_test.go index 9445334..6af1002 100644 --- a/expirable_cache_test.go +++ b/expirable_cache_test.go @@ -2,6 +2,7 @@ package lcw import ( "fmt" + "sort" "sync/atomic" "testing" "time" @@ -23,7 +24,10 @@ func TestExpirableCache(t *testing.T) { assert.Equal(t, 5, lc.Stat().Keys) assert.Equal(t, int64(5), lc.Stat().Misses) - assert.Equal(t, []string{"key-0", "key-1", "key-2", "key-3", "key-4"}, lc.Keys()) + + keys := lc.Keys()[:] + sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) + assert.EqualValues(t, []string{"key-0", "key-1", "key-2", "key-3", "key-4"}, keys) _, e := lc.Get("key-xx", func() (Value, error) { return "result-xx", nil diff --git a/lru_cache_test.go b/lru_cache_test.go index eaf2f6d..a93da65 100644 --- a/lru_cache_test.go +++ b/lru_cache_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "log" "net/http" + "sort" "sync/atomic" "testing" @@ -28,7 +29,9 @@ func TestLruCache_MaxKeys(t *testing.T) { assert.Equal(t, int32(i+1), atomic.LoadInt32(&coldCalls)) } - assert.Equal(t, []string{"key-0", "key-1", "key-2", "key-3", "key-4"}, lc.Keys()) + keys := lc.Keys()[:] + sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) + assert.EqualValues(t, []string{"key-0", "key-1", "key-2", "key-3", "key-4"}, keys) // check if really cached res, err := lc.Get("key-3", func() (Value, error) { diff --git a/redis_cache_test.go b/redis_cache_test.go index 60bfad2..c054d5a 100644 --- a/redis_cache_test.go +++ b/redis_cache_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "log" + "sort" "sync/atomic" "testing" "time" @@ -45,7 +46,10 @@ func TestExpirableRedisCache(t *testing.T) { assert.Equal(t, 5, lc.Stat().Keys) assert.Equal(t, int64(5), lc.Stat().Misses) - assert.Equal(t, []string{"key-0", "key-1", "key-2", "key-3", "key-4"}, lc.Keys()) + + keys := lc.Keys()[:] + sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) + assert.EqualValues(t, []string{"key-0", "key-1", "key-2", "key-3", "key-4"}, keys) _, e := lc.Get("key-xx", func() (Value, error) { return "result-xx", nil