Quellcode durchsuchen

extract tests into seperate files

bergquist vor 6 Jahren
Ursprung
Commit
a60bb83a70

+ 8 - 2
pkg/infra/distcache/distcache.go

@@ -8,6 +8,7 @@ import (
 
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/services/sqlstore"
+	redis "gopkg.in/redis.v2"
 
 	"github.com/grafana/grafana/pkg/registry"
 )
@@ -35,11 +36,16 @@ type CacheOpts struct {
 
 func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
 	if opts.name == "redis" {
-		return newRedisStorage(nil)
+		opt := &redis.Options{
+			Network: "tcp",
+			Addr:    "localhost:6379",
+		}
+
+		return newRedisStorage(redis.NewClient(opt))
 	}
 
 	if opts.name == "memcache" {
-		return newMemcacheStorage("localhost:9090")
+		return newMemcacheStorage("localhost:11211")
 	}
 
 	// if opts.name == "memory" {

+ 8 - 7
pkg/infra/distcache/distcache_test.go

@@ -27,18 +27,19 @@ func createTestClient(t *testing.T, name string) cacheStorage {
 }
 
 func TestAllCacheClients(t *testing.T) {
-	clients := []string{"database", "redis"} // add redis, memcache, memory
+	//clients := []string{"database", "redis", "memcache"} // add redis, memcache, memory
+	clients := []string{} // add redis, memcache, memory
 
 	for _, v := range clients {
 		client := createTestClient(t, v)
 
-		CanPutGetAndDeleteCachedObjects(t, v, client)
-		CanNotFetchExpiredItems(t, v, client)
-		CanSetInfiniteCacheExpiration(t, v, client)
+		CanPutGetAndDeleteCachedObjects(t, client)
+		CanNotFetchExpiredItems(t, client)
+		CanSetInfiniteCacheExpiration(t, client)
 	}
 }
 
-func CanPutGetAndDeleteCachedObjects(t *testing.T, name string, client cacheStorage) {
+func CanPutGetAndDeleteCachedObjects(t *testing.T, client cacheStorage) {
 	cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
 
 	err := client.Put("key", cacheableStruct, 0)
@@ -58,7 +59,7 @@ func CanPutGetAndDeleteCachedObjects(t *testing.T, name string, client cacheStor
 	assert.Equal(t, err, ErrCacheItemNotFound)
 }
 
-func CanNotFetchExpiredItems(t *testing.T, name string, client cacheStorage) {
+func CanNotFetchExpiredItems(t *testing.T, client cacheStorage) {
 	cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
 
 	err := client.Put("key", cacheableStruct, time.Second)
@@ -72,7 +73,7 @@ func CanNotFetchExpiredItems(t *testing.T, name string, client cacheStorage) {
 	assert.Equal(t, err, ErrCacheItemNotFound)
 }
 
-func CanSetInfiniteCacheExpiration(t *testing.T, name string, client cacheStorage) {
+func CanSetInfiniteCacheExpiration(t *testing.T, client cacheStorage) {
 	cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
 
 	// insert cache item one day back

+ 7 - 3
pkg/infra/distcache/memcached_storage.go

@@ -24,7 +24,7 @@ func newItem(sid string, data []byte, expire int32) *memcache.Item {
 	}
 }
 
-// Set sets value to given key in the cache.
+// Put sets value to given key in the cache.
 func (s *memcacheStorage) Put(key string, val interface{}, expires time.Duration) error {
 	item := &cachedItem{Val: val}
 
@@ -35,13 +35,17 @@ func (s *memcacheStorage) Put(key string, val interface{}, expires time.Duration
 
 	memcacheItem := newItem(key, bytes, int32(expires))
 
-	s.c.Add(memcacheItem)
-	return nil
+	return s.c.Add(memcacheItem)
 }
 
 // Get gets value by given key in the cache.
 func (s *memcacheStorage) Get(key string) (interface{}, error) {
 	i, err := s.c.Get(key)
+
+	if err != nil && err.Error() == "memcache: cache miss" {
+		return nil, ErrCacheItemNotFound
+	}
+
 	if err != nil {
 		return nil, err
 	}

+ 1 - 7
pkg/infra/distcache/redis_storage.go

@@ -11,13 +11,7 @@ type redisStorage struct {
 }
 
 func newRedisStorage(c *redis.Client) *redisStorage {
-	opt := &redis.Options{
-		Network: "tcp",
-		Addr:    "localhost:6379",
-	}
-	return &redisStorage{
-		c: redis.NewClient(opt),
-	}
+	return &redisStorage{c: c}
 }
 
 // Set sets value to given key in session.

+ 11 - 0
pkg/infra/distcache/redis_storage_test.go

@@ -1 +1,12 @@
 package distcache
+
+import "testing"
+
+func TestRedisCacheStorage(t *testing.T) {
+
+	client := createTestClient(t, "redis")
+
+	CanPutGetAndDeleteCachedObjects(t, client)
+	CanNotFetchExpiredItems(t, client)
+	CanSetInfiniteCacheExpiration(t, client)
+}