|
| 1 | +package redis_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"log" |
| 5 | +"testing" |
| 6 | +"time" |
| 7 | + |
| 8 | +"github.com/redis/go-redis/v9" |
| 9 | +) |
| 10 | + |
| 11 | +// Testredis.NewCache tests the creation of a new cache instance |
| 12 | +func TestNewCache(t *testing.T) { |
| 13 | +_, err := redis.NewCache(1000, 1<<20) // 1 MB size |
| 14 | +if err != nil { |
| 15 | +t.Fatalf("Failed to create cache: %v", err) |
| 16 | +} |
| 17 | +t.Log("Cache created successfully") |
| 18 | +} |
| 19 | + |
| 20 | +// TestCacheSetAndGet tests setting and getting values in the cache |
| 21 | +func TestCacheSetAndGet(t *testing.T) { |
| 22 | +cache, err := redis.NewCache(1000, 1<<20) |
| 23 | +if err != nil { |
| 24 | +t.Fatalf("Failed to create cache: %v", err) |
| 25 | +} |
| 26 | + |
| 27 | +key, value := "key1", "value1" |
| 28 | +setSuccess := cache.Set(key, value, 1) |
| 29 | +if !setSuccess { |
| 30 | +t.Fatalf("Failed to set key: %s", key) |
| 31 | +} |
| 32 | +log.Printf("Set operation successful for key: %s", key) |
| 33 | + |
| 34 | +// Allow value to pass through buffers |
| 35 | +time.Sleep(10 * time.Millisecond) |
| 36 | + |
| 37 | +getValue, found := cache.Get(key) |
| 38 | +if !found || getValue != value { |
| 39 | +t.Errorf("Failed to get key: %s, expected value: %s, got: %v", key, value, getValue) |
| 40 | +} else { |
| 41 | +log.Printf("Get operation successful for key: %s", key) |
| 42 | +} |
| 43 | +} |
| 44 | + |
| 45 | +// TestCacheClearKey tests the clearing of a specific key from the cache |
| 46 | +func TestCacheClearKey(t *testing.T) { |
| 47 | +cache, err := redis.NewCache(1000, 1<<20) |
| 48 | +if err != nil { |
| 49 | +t.Fatalf("Failed to create cache: %v", err) |
| 50 | +} |
| 51 | + |
| 52 | +key := "key1" |
| 53 | +cache.Set(key, "value1", 1) |
| 54 | +log.Printf("Key %s set in cache", key) |
| 55 | + |
| 56 | +time.Sleep(10 * time.Millisecond) |
| 57 | + |
| 58 | +cache.ClearKey(key) |
| 59 | +log.Printf("Key %s cleared from cache", key) |
| 60 | + |
| 61 | +time.Sleep(10 * time.Millisecond) |
| 62 | + |
| 63 | +_, found := cache.Get(key) |
| 64 | +if found { |
| 65 | +t.Errorf("Expected key %s to be cleared", key) |
| 66 | +} else { |
| 67 | +log.Printf("ClearKey operation successful for key: %s", key) |
| 68 | +} |
| 69 | +} |
| 70 | + |
| 71 | +// TestCacheClear tests clearing all keys from the cache |
| 72 | +func TestCacheClear(t *testing.T) { |
| 73 | +cache, err := redis.NewCache(1000, 1<<20) |
| 74 | +if err != nil { |
| 75 | +t.Fatalf("Failed to create cache: %v", err) |
| 76 | +} |
| 77 | + |
| 78 | +key := "key1" |
| 79 | +cache.Set(key, "value1", 1) |
| 80 | +log.Printf("Key %s set in cache", key) |
| 81 | + |
| 82 | +time.Sleep(10 * time.Millisecond) |
| 83 | + |
| 84 | +cache.Clear() |
| 85 | +t.Log("All keys cleared from cache") |
| 86 | + |
| 87 | +time.Sleep(10 * time.Millisecond) |
| 88 | + |
| 89 | +_, found := cache.Get(key) |
| 90 | +if found { |
| 91 | +t.Errorf("Expected cache to be cleared, but key %s was found", key) |
| 92 | +} else { |
| 93 | +t.Log("Clear operation successful, cache is empty") |
| 94 | +} |
| 95 | +} |
0 commit comments