2019-10-03 00:17:34 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-10-05 20:47:03 +00:00
|
|
|
func TestNew_MemoryURI(t *testing.T) {
|
2019-10-03 00:17:34 +00:00
|
|
|
cache, err := New("memory://")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error creating cache:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := cache.(*MemoryCache); !ok {
|
|
|
|
t.Fatal("Cache is not instance of MemoryCache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 20:47:03 +00:00
|
|
|
func TestMemoryCache_Get(t *testing.T) {
|
2019-10-03 00:17:34 +00:00
|
|
|
cache, err := New("memory://")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error creating cache:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := "test"
|
|
|
|
|
|
|
|
cache.Set("test", obj, time.Minute)
|
|
|
|
|
|
|
|
var new string
|
|
|
|
|
|
|
|
cache.Get("test", &new)
|
|
|
|
|
|
|
|
if obj != new {
|
|
|
|
t.Fatal("Expected", obj, "got", new)
|
|
|
|
}
|
|
|
|
}
|
2019-10-05 20:47:03 +00:00
|
|
|
|
|
|
|
func TestMemoryCache_GetRaw(t *testing.T) {
|
|
|
|
cache, err := New("memory://")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error creating cache:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := "test"
|
|
|
|
|
|
|
|
cache.Set("test", obj, time.Minute)
|
|
|
|
|
|
|
|
v, err := cache.Get("test")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to get value:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
new := string(v)
|
|
|
|
|
|
|
|
if obj != new {
|
|
|
|
t.Fatal("Expected", obj, "got", new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryCache_Has(t *testing.T) {
|
|
|
|
cache, err := New("memory://")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error creating cache:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.Set("test", "test", time.Minute)
|
|
|
|
|
|
|
|
if !cache.Has("test") {
|
|
|
|
t.Fatal("Expected cache to have object 'test'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryCache_Del(t *testing.T) {
|
|
|
|
cache, err := New("memory://")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error creating cache:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.Set("test", "test", time.Minute)
|
|
|
|
|
|
|
|
if !cache.Has("test") {
|
|
|
|
t.Fatal("Expected cache to have object 'test'")
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.Del("test")
|
|
|
|
|
|
|
|
if cache.Has("test") {
|
|
|
|
t.Fatal("Cache did not properly delete item")
|
|
|
|
}
|
|
|
|
}
|