diff --git a/db/cache/cache_test.go b/db/cache/cache_test.go new file mode 100644 index 0000000..d227dba --- /dev/null +++ b/db/cache/cache_test.go @@ -0,0 +1,38 @@ +package cache + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCache(t *testing.T) { + cl := []Cache{NewFastCache(100000), NewRedis("127.0.0.1:6379", "")} + + for i, c := range cl { + key := []byte("key") + value := []byte("value") + + require.Nil(t, c.Put(key, value, time.Now().Add(1*time.Hour)), i) + + v, err := c.Get(key) + require.Nil(t, err, i) + + assert.Equal(t, v, value, i) + + require.Nil(t, c.Del(key), i) + + v, err = c.Get(key) + require.Nil(t, err, i) + require.Nil(t, v, i) + + require.Nil(t, c.Put(key, value, time.Now().Add(2*time.Second)), i) + time.Sleep(3 * time.Second) + + v, err = c.Get(key) + require.Nil(t, err, i) + require.Nil(t, v, i) + } +} diff --git a/db/cache/fastcache_test.go b/db/cache/fastcache_test.go deleted file mode 100644 index ff0069f..0000000 --- a/db/cache/fastcache_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package cache - -import ( - "testing" - "time" - - "github.com/samber/lo" -) - -func TestFastCache(t *testing.T) { - f := NewFastCache(100000) - c := CacheHelp[string]{ - Cache: f, - } - c.Put([]byte("123"), "123", time.Now().Add(10*time.Second)) - - if lo.Must(c.Get([]byte("123"))) != "123" { - t.FailNow() - } -} diff --git a/db/cache/redis_test.go b/db/cache/redis_test.go deleted file mode 100644 index 57d4ce6..0000000 --- a/db/cache/redis_test.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build redis - -package cache - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestRedis(t *testing.T) { - c := NewRedis("127.0.0.1:6379", "") - - key := []byte("key") - value := []byte("value") - - require.Nil(t, c.Put(key, value, time.Now().Add(1*time.Hour))) - - v, err := c.Get(key) - require.Nil(t, err) - - assert.Equal(t, v, value) - - require.Nil(t, c.Del(key)) - - v, err = c.Get(key) - require.Nil(t, err) - require.Nil(t, v) - - require.Nil(t, c.Put(key, value, time.Now().Add(2*time.Second))) - time.Sleep(3 * time.Second) - - v, err = c.Get(key) - require.Nil(t, err) - require.Nil(t, v) -}