diff --git a/libs/core/cache/test/redis-cache.test.ts b/libs/core/cache/test/redis-cache.test.ts index ba28ba89..19c6fcff 100644 --- a/libs/core/cache/test/redis-cache.test.ts +++ b/libs/core/cache/test/redis-cache.test.ts @@ -27,11 +27,13 @@ if (!testRedisUrl) { describe.skipIf(!testRedisUrl)('RedisCache', () => { let cache: RedisCache; let redisConfig: RedisConfig; + let testPrefix: string; beforeEach(() => { redisConfig = parseTestRedisConfig(testRedisUrl!); + testPrefix = `test:${crypto.randomUUID()}:`; const options: CacheOptions = { - keyPrefix: 'test:', + keyPrefix: testPrefix, ttl: 3600, shared: false, redisConfig, @@ -40,7 +42,14 @@ describe.skipIf(!testRedisUrl)('RedisCache', () => { }); afterEach(async () => { - await cache?.disconnect().catch(() => {}); + if (!cache) return; + try { + await cache.clear(); + } catch { + // Ignore cleanup errors if Redis is unreachable or already disconnected + } finally { + await cache.disconnect().catch(() => {}); + } }); describe('Core functionality', () => { @@ -174,7 +183,7 @@ describe.skipIf(!testRedisUrl)('RedisCache', () => { it('should handle errors gracefully in get', async () => { // Force an error by using invalid JSON const badCache = new RedisCache({ - keyPrefix: 'bad:', + keyPrefix: `bad:${crypto.randomUUID()}:`, shared: false, redisConfig, }); @@ -188,6 +197,7 @@ describe.skipIf(!testRedisUrl)('RedisCache', () => { const stats = badCache.getStats(); expect(stats.misses).toBe(1); } finally { + await badCache.clear().catch(() => {}); await badCache.disconnect().catch(() => {}); } }); diff --git a/libs/core/shutdown/test/shutdown.test.ts b/libs/core/shutdown/test/shutdown.test.ts index 0faeb0bf..55e5af60 100644 --- a/libs/core/shutdown/test/shutdown.test.ts +++ b/libs/core/shutdown/test/shutdown.test.ts @@ -1,4 +1,4 @@ -import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test'; +import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test'; import { Shutdown } from '../src/shutdown'; describe('Shutdown', () => { @@ -126,28 +126,45 @@ describe('Shutdown', () => { }); it('should respect timeout', async () => { - let slowTimer: ReturnType | undefined; - const slowHandler = mock(async () => { - await new Promise(resolve => { - slowTimer = setTimeout(resolve, 2000); - }); - }); + let releaseHandler!: () => void; + const slowHandler = mock( + () => + new Promise(resolve => { + releaseHandler = resolve; + }) + ); - Shutdown.reset(); - shutdown = Shutdown.getInstance({ timeout: 100, autoRegister: false }); - shutdown.onShutdown(slowHandler, 10, 'Slow Handler'); + let scheduledCallback: (() => void) | undefined; + let scheduledDelay: number | undefined; + const fakeTimer = {} as ReturnType; + const setTimeoutSpy = spyOn(globalThis, 'setTimeout').mockImplementation((( + cb: (...args: unknown[]) => void, + delay?: number + ) => { + scheduledCallback = () => cb(); + scheduledDelay = delay; + return fakeTimer; + }) as unknown as typeof setTimeout); + const clearTimeoutSpy = spyOn(globalThis, 'clearTimeout').mockImplementation(() => {}); - const start = Date.now(); try { - await expect(shutdown.shutdown()).rejects.toThrow('Shutdown timeout'); - } finally { - clearTimeout(slowTimer); - } - const duration = Date.now() - start; + Shutdown.reset(); + shutdown = Shutdown.getInstance({ timeout: 100, autoRegister: false }); + shutdown.onShutdown(slowHandler, 10, 'Slow Handler'); - // Shutdown waits for timeout (100ms) plus some processing time - expect(duration).toBeGreaterThan(90); - expect(duration).toBeLessThan(500); + const shutdownPromise = shutdown.shutdown(); + expect(scheduledDelay).toBe(100); + expect(scheduledCallback).toBeDefined(); + + scheduledCallback!(); + + await expect(shutdownPromise).rejects.toThrow('Shutdown timeout'); + expect(clearTimeoutSpy).toHaveBeenCalledWith(fakeTimer); + } finally { + releaseHandler?.(); + setTimeoutSpy.mockRestore(); + clearTimeoutSpy.mockRestore(); + } }); });