Spaces:
Runtime error
Runtime error
File size: 5,877 Bytes
4782147 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | import { describe, it, expect, beforeEach, vi } from "vitest";
import { SafeRedisCache } from "./safe-redis-cache";
import { MemoryCache } from "./memory-cache";
import { RedisCache } from "./redis-cache";
vi.mock("./redis-cache");
vi.mock("logger", () => ({
default: {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
describe("SafeRedisCache", () => {
let cache: SafeRedisCache;
let mockRedisCache: any;
let mockMemoryCache: any;
beforeEach(() => {
vi.clearAllMocks();
mockRedisCache = {
get: vi.fn(),
set: vi.fn(),
has: vi.fn(),
delete: vi.fn(),
clear: vi.fn(),
getAll: vi.fn(),
disconnect: vi.fn(),
};
mockMemoryCache = new MemoryCache();
});
it("should use Redis when available", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
mockRedisCache.get.mockResolvedValue("value");
const result = await cache.get("key");
expect(result).toBe("value");
expect(mockRedisCache.get).toHaveBeenCalledWith("key");
});
it("should fallback to memory cache when Redis fails", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
mockRedisCache.get.mockRejectedValue(new Error("Redis connection failed"));
await mockMemoryCache.set("key", "memoryValue");
const result = await cache.get("key");
expect(result).toBe("memoryValue");
});
it("should handle rate limit errors gracefully", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
mockRedisCache.set.mockRejectedValue(new Error("rate limit exceeded"));
await cache.set("key", "value");
const result = await mockMemoryCache.get("key");
expect(result).toBe("value");
});
it("should retry Redis connection after failure", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({
serverCache: mockMemoryCache,
retryDelay: 100, // Short delay for testing
});
// First call fails
mockRedisCache.get.mockRejectedValueOnce(new Error("Connection failed"));
await cache.get("key1");
// Wait for retry delay
await new Promise((resolve) => setTimeout(resolve, 150));
// Next call should attempt reconnection
mockRedisCache.has.mockResolvedValueOnce(true); // Connection test succeeds
mockRedisCache.get.mockResolvedValueOnce("value2");
const result = await cache.get("key2");
expect(mockRedisCache.has).toHaveBeenCalledWith("__test__");
expect(result).toBe("value2");
});
it("should set values in both caches when using Redis", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
mockRedisCache.set.mockResolvedValue(undefined);
await cache.set("key", "value", 1000);
expect(mockRedisCache.set).toHaveBeenCalledWith("key", "value", 1000);
expect(await mockMemoryCache.get("key")).toBe("value");
});
it("should delete from both caches", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
await mockMemoryCache.set("key", "value");
mockRedisCache.delete.mockResolvedValue(undefined);
await cache.delete("key");
expect(mockRedisCache.delete).toHaveBeenCalledWith("key");
expect(await mockMemoryCache.has("key")).toBe(false);
});
it("should clear both caches", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
await mockMemoryCache.set("key1", "value1");
await mockMemoryCache.set("key2", "value2");
mockRedisCache.clear.mockResolvedValue(undefined);
await cache.clear();
expect(mockRedisCache.clear).toHaveBeenCalled();
expect((await mockMemoryCache.getAll()).size).toBe(0);
});
it("should report cache status correctly", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
expect(cache.isUsingRedis()).toBe(true);
expect(cache.getCacheStatus()).toEqual({
redis: true,
retries: 0,
});
// Simulate Redis failure
mockRedisCache.get.mockRejectedValue(new Error("Connection failed"));
await cache.get("key");
expect(cache.isUsingRedis()).toBe(false);
expect(cache.getCacheStatus().redis).toBe(false);
});
it("should handle OOM errors", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({ serverCache: mockMemoryCache });
mockRedisCache.set.mockRejectedValue(new Error("OOM command not allowed"));
await cache.set("key", "value");
const result = await mockMemoryCache.get("key");
expect(result).toBe("value");
expect(cache.isUsingRedis()).toBe(false);
});
it("should respect max retries limit", async () => {
vi.mocked(RedisCache).mockImplementation(() => mockRedisCache);
cache = new SafeRedisCache({
serverCache: mockMemoryCache,
maxRetries: 2,
retryDelay: 50,
});
// Make Redis fail
mockRedisCache.get.mockRejectedValue(new Error("Connection failed"));
await cache.get("key");
// Attempt retries
for (let i = 0; i < 5; i++) {
await new Promise((resolve) => setTimeout(resolve, 60));
mockRedisCache.has.mockRejectedValue(new Error("Still failing"));
await cache.get(`key${i}`);
}
const status = cache.getCacheStatus();
expect(status.retries).toBeLessThanOrEqual(2);
});
});
|