2021-08-31 16:31:16 +02:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-21 21:05:00 +02:00
|
|
|
ctx = context.TODO()
|
|
|
|
key string = "test"
|
|
|
|
val interface{} = "hello go-micro"
|
2021-08-31 16:31:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestMemCache tests the in-memory cache implementation.
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
|
t.Run("CacheGetMiss", func(t *testing.T) {
|
2022-06-21 21:05:00 +02:00
|
|
|
if _, _, err := NewCache().Get(ctx, key); err == nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error("expected to get no value from cache")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CacheGetHit", func(t *testing.T) {
|
|
|
|
c := NewCache()
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if err := c.Put(ctx, key, val, 0); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if a, _, err := c.Get(ctx, key); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Errorf("Expected a value, got err: %s", err)
|
|
|
|
} else if a != val {
|
|
|
|
t.Errorf("Expected '%v', got '%v'", val, a)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CacheGetExpired", func(t *testing.T) {
|
|
|
|
c := NewCache()
|
|
|
|
e := 20 * time.Millisecond
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if err := c.Put(ctx, key, val, e); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(25 * time.Millisecond)
|
2022-06-21 21:05:00 +02:00
|
|
|
if _, _, err := c.Get(ctx, key); err == nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error("expected to get no value from cache")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CacheGetValid", func(t *testing.T) {
|
|
|
|
c := NewCache()
|
|
|
|
e := 25 * time.Millisecond
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if err := c.Put(ctx, key, val, e); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(20 * time.Millisecond)
|
2022-06-21 21:05:00 +02:00
|
|
|
if _, _, err := c.Get(ctx, key); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Errorf("expected a value, got err: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CacheDeleteMiss", func(t *testing.T) {
|
2022-06-21 21:05:00 +02:00
|
|
|
if err := NewCache().Delete(ctx, key); err == nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error("expected to delete no value from cache")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CacheDeleteHit", func(t *testing.T) {
|
|
|
|
c := NewCache()
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if err := c.Put(ctx, key, val, 0); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if err := c.Delete(ctx, key); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Errorf("Expected to delete an item, got err: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if _, _, err := c.Get(ctx, key); err == nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheWithOptions(t *testing.T) {
|
|
|
|
t.Run("CacheWithExpiration", func(t *testing.T) {
|
|
|
|
c := NewCache(Expiration(20 * time.Millisecond))
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if err := c.Put(ctx, key, val, 0); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(25 * time.Millisecond)
|
2022-06-21 21:05:00 +02:00
|
|
|
if _, _, err := c.Get(ctx, key); err == nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Error("expected to get no value from cache")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CacheWithItems", func(t *testing.T) {
|
|
|
|
c := NewCache(Items(map[string]Item{key: {val, 0}}))
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
if a, _, err := c.Get(ctx, key); err != nil {
|
2021-08-31 16:31:16 +02:00
|
|
|
t.Errorf("Expected a value, got err: %s", err)
|
|
|
|
} else if a != val {
|
|
|
|
t.Errorf("Expected '%v', got '%v'", val, a)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|