grafana/pkg/infra/remotecache/remotecache_test.go

93 lines
2.3 KiB
Go
Raw Normal View History

2019-03-08 13:49:16 -06:00
package remotecache
2019-02-14 16:13:46 -06:00
import (
"testing"
"time"
"github.com/grafana/grafana/pkg/services/sqlstore"
2019-03-03 14:48:00 -06:00
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-02-14 16:13:46 -06:00
)
type CacheableStruct struct {
String string
Int64 int64
}
func init() {
2019-03-05 08:34:51 -06:00
Register(CacheableStruct{})
2019-02-14 16:13:46 -06:00
}
func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore *sqlstore.SQLStore) CacheStorage {
2019-02-14 16:13:46 -06:00
t.Helper()
2019-03-08 13:49:16 -06:00
dc := &RemoteCache{
2019-03-03 14:48:00 -06:00
SQLStore: sqlstore,
Cfg: &setting.Cfg{
2019-03-08 13:49:16 -06:00
RemoteCacheOptions: opts,
2019-03-03 14:48:00 -06:00
},
}
err := dc.Init()
require.Nil(t, err, "Failed to init client for test")
2019-03-03 14:48:00 -06:00
2019-03-14 03:22:03 -05:00
return dc
2019-03-03 14:48:00 -06:00
}
func TestCachedBasedOnConfig(t *testing.T) {
cfg := setting.NewCfg()
err := cfg.Load(&setting.CommandLineArgs{
2019-03-03 14:48:00 -06:00
HomePath: "../../../",
})
require.Nil(t, err, "Failed to load config")
2019-03-03 14:48:00 -06:00
2019-03-08 13:49:16 -06:00
client := createTestClient(t, cfg.RemoteCacheOptions, sqlstore.InitTestDB(t))
2019-03-03 14:48:00 -06:00
runTestsForClient(t, client)
2019-02-14 16:13:46 -06:00
}
2019-03-14 02:57:38 -05:00
func TestInvalidCacheTypeReturnsError(t *testing.T) {
_, err := createClient(&setting.RemoteCacheOptions{Name: "invalid"}, nil)
assert.Equal(t, err, ErrInvalidCacheType)
}
2019-03-05 08:15:05 -06:00
func runTestsForClient(t *testing.T, client CacheStorage) {
2019-03-03 05:41:38 -06:00
canPutGetAndDeleteCachedObjects(t, client)
canNotFetchExpiredItems(t, client)
2019-02-15 02:48:32 -06:00
}
2019-03-05 08:15:05 -06:00
func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
2019-02-14 16:13:46 -06:00
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
err := client.Set("key1", cacheableStruct, 0)
assert.Equal(t, err, nil, "expected nil. got: ", err)
2019-02-14 16:13:46 -06:00
data, err := client.Get("key1")
assert.Equal(t, err, nil)
2019-02-14 16:13:46 -06:00
s, ok := data.(CacheableStruct)
assert.Equal(t, ok, true)
assert.Equal(t, s.String, "hej")
assert.Equal(t, s.Int64, int64(2000))
err = client.Delete("key1")
2019-02-14 16:13:46 -06:00
assert.Equal(t, err, nil)
_, err = client.Get("key1")
2019-02-14 16:13:46 -06:00
assert.Equal(t, err, ErrCacheItemNotFound)
}
2019-03-05 08:15:05 -06:00
func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
2019-02-14 16:13:46 -06:00
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
err := client.Set("key1", cacheableStruct, time.Second)
2019-02-14 16:13:46 -06:00
assert.Equal(t, err, nil)
// not sure how this can be avoided when testing redis/memcached :/
<-time.After(time.Second + time.Millisecond)
2019-02-14 16:13:46 -06:00
// should not be able to read that value since its expired
_, err = client.Get("key1")
2019-02-14 16:13:46 -06:00
assert.Equal(t, err, ErrCacheItemNotFound)
}