mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package distcache
|
|
|
|
import (
|
|
"encoding/gob"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/bmizerany/assert"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
)
|
|
|
|
type CacheableStruct struct {
|
|
String string
|
|
Int64 int64
|
|
}
|
|
|
|
func init() {
|
|
gob.Register(CacheableStruct{})
|
|
}
|
|
|
|
func createTestClient(t *testing.T, name string) cacheStorage {
|
|
t.Helper()
|
|
|
|
sqlstore := sqlstore.InitTestDB(t)
|
|
return createClient(CacheOpts{name: name}, sqlstore)
|
|
}
|
|
|
|
func TestAllCacheClients(t *testing.T) {
|
|
clients := []string{"database", "redis"} // add redis, memcache, memory
|
|
|
|
for _, v := range clients {
|
|
client := createTestClient(t, v)
|
|
|
|
CanPutGetAndDeleteCachedObjects(t, v, client)
|
|
CanNotFetchExpiredItems(t, v, client)
|
|
CanSetInfiniteCacheExpiration(t, v, client)
|
|
}
|
|
}
|
|
|
|
func CanPutGetAndDeleteCachedObjects(t *testing.T, name string, client cacheStorage) {
|
|
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
|
|
|
|
err := client.Put("key", cacheableStruct, 0)
|
|
assert.Equal(t, err, nil)
|
|
|
|
data, err := client.Get("key")
|
|
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("key")
|
|
assert.Equal(t, err, nil)
|
|
|
|
_, err = client.Get("key")
|
|
assert.Equal(t, err, ErrCacheItemNotFound)
|
|
}
|
|
|
|
func CanNotFetchExpiredItems(t *testing.T, name string, client cacheStorage) {
|
|
if name == "redis" {
|
|
t.Skip() //this test does not work with redis since it uses its own getTime fn
|
|
}
|
|
|
|
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
|
|
|
|
// insert cache item one day back
|
|
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
|
|
err := client.Put("key", cacheableStruct, 10000*time.Second)
|
|
assert.Equal(t, err, nil)
|
|
|
|
// should not be able to read that value since its expired
|
|
getTime = time.Now
|
|
_, err = client.Get("key")
|
|
assert.Equal(t, err, ErrCacheItemNotFound)
|
|
}
|
|
|
|
func CanSetInfiniteCacheExpiration(t *testing.T, name string, client cacheStorage) {
|
|
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
|
|
|
|
// insert cache item one day back
|
|
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
|
|
err := client.Put("key", cacheableStruct, 0)
|
|
assert.Equal(t, err, nil)
|
|
|
|
// should not be able to read that value since its expired
|
|
getTime = time.Now
|
|
data, err := client.Get("key")
|
|
s, ok := data.(CacheableStruct)
|
|
|
|
assert.Equal(t, ok, true)
|
|
assert.Equal(t, s.String, "hej")
|
|
assert.Equal(t, s.Int64, int64(2000))
|
|
}
|