2024-01-21 22:54:26 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2024-03-03 12:20:05 +03:00
|
|
|
"context"
|
2024-01-21 22:54:26 +03:00
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Testredis.NewCache tests the creation of a new cache instance
|
|
|
|
func TestNewCache(t *testing.T) {
|
|
|
|
_, err := redis.NewCache(1000, 1<<20) // 1 MB size
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create cache: %v", err)
|
|
|
|
}
|
|
|
|
t.Log("Cache created successfully")
|
|
|
|
}
|
|
|
|
|
2024-03-03 12:20:05 +03:00
|
|
|
// TestCacheSetKeyAndGetKey tests SetKeyting and GetKeyting values in the cache
|
|
|
|
func TestCacheSetKeyAndGetKey(t *testing.T) {
|
2024-01-21 22:54:26 +03:00
|
|
|
cache, err := redis.NewCache(1000, 1<<20)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create cache: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value := "key1", "value1"
|
2024-03-03 12:20:05 +03:00
|
|
|
SetKeySuccess := cache.SetKey(key, value, 1)
|
|
|
|
if !SetKeySuccess {
|
|
|
|
t.Fatalf("Failed to SetKey key: %s", key)
|
2024-01-21 22:54:26 +03:00
|
|
|
}
|
2024-03-03 12:20:05 +03:00
|
|
|
log.Printf("SetKey operation successful for key: %s", key)
|
2024-01-21 22:54:26 +03:00
|
|
|
|
|
|
|
// Allow value to pass through buffers
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
2024-03-03 12:20:05 +03:00
|
|
|
GetKeyValue, found := cache.GetKey(key)
|
|
|
|
if !found || GetKeyValue != value {
|
|
|
|
t.Errorf("Failed to GetKey key: %s, expected value: %s, got: %v", key, value, GetKeyValue)
|
2024-01-21 22:54:26 +03:00
|
|
|
} else {
|
2024-03-03 12:20:05 +03:00
|
|
|
log.Printf("GetKey operation successful for key: %s", key)
|
2024-01-21 22:54:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCacheClearKey tests the clearing of a specific key from the cache
|
|
|
|
func TestCacheClearKey(t *testing.T) {
|
|
|
|
cache, err := redis.NewCache(1000, 1<<20)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create cache: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := "key1"
|
2024-03-03 12:20:05 +03:00
|
|
|
cache.SetKey(key, "value1", 1)
|
|
|
|
log.Printf("Key %s SetKey in cache", key)
|
2024-01-21 22:54:26 +03:00
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
cache.ClearKey(key)
|
|
|
|
log.Printf("Key %s cleared from cache", key)
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
2024-03-03 12:20:05 +03:00
|
|
|
_, found := cache.GetKey(key)
|
2024-01-21 22:54:26 +03:00
|
|
|
if found {
|
|
|
|
t.Errorf("Expected key %s to be cleared", key)
|
|
|
|
} else {
|
|
|
|
log.Printf("ClearKey operation successful for key: %s", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCacheClear tests clearing all keys from the cache
|
|
|
|
func TestCacheClear(t *testing.T) {
|
|
|
|
cache, err := redis.NewCache(1000, 1<<20)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create cache: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := "key1"
|
2024-03-03 12:20:05 +03:00
|
|
|
cache.SetKey(key, "value1", 1)
|
|
|
|
log.Printf("Key %s SetKey in cache", key)
|
2024-01-21 22:54:26 +03:00
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
cache.Clear()
|
|
|
|
t.Log("All keys cleared from cache")
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
2024-03-03 12:20:05 +03:00
|
|
|
_, found := cache.GetKey(key)
|
2024-01-21 22:54:26 +03:00
|
|
|
if found {
|
|
|
|
t.Errorf("Expected cache to be cleared, but key %s was found", key)
|
|
|
|
} else {
|
|
|
|
t.Log("Clear operation successful, cache is empty")
|
|
|
|
}
|
|
|
|
}
|
2024-03-03 12:20:05 +03:00
|
|
|
|
|
|
|
func TestSetCache(t *testing.T) {
|
2024-03-04 23:31:15 +03:00
|
|
|
cache, err := redis.NewCache(1000, 1<<20)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create cache: %v", err)
|
|
|
|
}
|
|
|
|
client := redis.NewClient(&redis.Options{Addr: ":6379", CacheObject: cache})
|
2024-03-03 12:20:05 +03:00
|
|
|
defer client.Close()
|
|
|
|
ctx := context.Background()
|
|
|
|
client.Ping(ctx)
|
2024-03-04 23:31:15 +03:00
|
|
|
// TODO: fix this
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
val, found := client.Options().CacheObject.GetKey("ping")
|
|
|
|
if found {
|
|
|
|
t.Log(val)
|
|
|
|
} else {
|
|
|
|
t.Error("Key not found")
|
|
|
|
}
|
|
|
|
ping := client.Ping(ctx)
|
|
|
|
if ping.Val() == "PONG" {
|
|
|
|
t.Log(ping.Val())
|
|
|
|
} else {
|
|
|
|
t.Error("Ping from cache failed")
|
|
|
|
}
|
2024-03-03 12:20:05 +03:00
|
|
|
}
|