|
| 1 | +/** |
| 2 | + * Memory Stability Stress Test for Oracle Service |
| 3 | + * |
| 4 | + * Uses fake timers to fast-forward through extended operation and verifies: |
| 5 | + * - Cache Map size stays bounded (no unbounded growth) |
| 6 | + * - setInterval is properly cleared on stop() |
| 7 | + * - No event listener accumulation on the process object |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 11 | +import { OracleService } from '../src/index.js'; |
| 12 | +import type { OracleServiceConfig } from '../src/config.js'; |
| 13 | + |
| 14 | +vi.mock('../src/services/contract-updater.js', () => ({ |
| 15 | + createContractUpdater: vi.fn(() => ({ |
| 16 | + updatePrices: vi.fn().mockResolvedValue([ |
| 17 | + { success: true, asset: 'XLM', price: 150000n, timestamp: Date.now() }, |
| 18 | + ]), |
| 19 | + healthCheck: vi.fn().mockResolvedValue(true), |
| 20 | + getAdminPublicKey: vi.fn().mockReturnValue('GTEST123'), |
| 21 | + })), |
| 22 | + ContractUpdater: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../src/providers/coingecko.js', () => ({ |
| 26 | + createCoinGeckoProvider: vi.fn(() => ({ |
| 27 | + name: 'coingecko', |
| 28 | + isEnabled: true, |
| 29 | + priority: 1, |
| 30 | + weight: 0.6, |
| 31 | + getSupportedAssets: () => ['XLM', 'BTC', 'ETH'], |
| 32 | + fetchPrice: vi.fn().mockResolvedValue({ |
| 33 | + asset: 'XLM', |
| 34 | + price: 0.15, |
| 35 | + timestamp: Math.floor(Date.now() / 1000), |
| 36 | + source: 'coingecko', |
| 37 | + }), |
| 38 | + })), |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock('../src/providers/binance.js', () => ({ |
| 42 | + createBinanceProvider: vi.fn(() => ({ |
| 43 | + name: 'binance', |
| 44 | + isEnabled: true, |
| 45 | + priority: 2, |
| 46 | + weight: 0.4, |
| 47 | + getSupportedAssets: () => ['XLM', 'BTC', 'ETH'], |
| 48 | + fetchPrice: vi.fn().mockResolvedValue({ |
| 49 | + asset: 'XLM', |
| 50 | + price: 0.152, |
| 51 | + timestamp: Math.floor(Date.now() / 1000), |
| 52 | + source: 'binance', |
| 53 | + }), |
| 54 | + })), |
| 55 | +})); |
| 56 | + |
| 57 | +const BASE_CONFIG: OracleServiceConfig = { |
| 58 | + stellarNetwork: 'testnet', |
| 59 | + stellarRpcUrl: 'https://soroban-testnet.stellar.org', |
| 60 | + contractId: 'CTEST123', |
| 61 | + adminSecretKey: 'STEST123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456', |
| 62 | + updateIntervalMs: 1000, |
| 63 | + maxPriceDeviationPercent: 10, |
| 64 | + priceStaleThresholdSeconds: 300, |
| 65 | + cacheTtlSeconds: 30, |
| 66 | + logLevel: 'error', |
| 67 | + providers: [ |
| 68 | + { |
| 69 | + name: 'coingecko', |
| 70 | + enabled: true, |
| 71 | + priority: 1, |
| 72 | + weight: 0.6, |
| 73 | + baseUrl: 'https://api.coingecko.com/api/v3', |
| 74 | + rateLimit: { maxRequests: 10, windowMs: 60000 }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: 'binance', |
| 78 | + enabled: true, |
| 79 | + priority: 2, |
| 80 | + weight: 0.4, |
| 81 | + baseUrl: 'https://api.binance.com/api/v3', |
| 82 | + rateLimit: { maxRequests: 1200, windowMs: 60000 }, |
| 83 | + }, |
| 84 | + ], |
| 85 | +}; |
| 86 | + |
| 87 | +describe('OracleService Memory Stability', () => { |
| 88 | + let service: OracleService; |
| 89 | + |
| 90 | + beforeEach(() => { |
| 91 | + vi.useFakeTimers(); |
| 92 | + service = new OracleService({ ...BASE_CONFIG }); |
| 93 | + }); |
| 94 | + |
| 95 | + afterEach(() => { |
| 96 | + service.stop(); |
| 97 | + vi.useRealTimers(); |
| 98 | + vi.clearAllMocks(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('cache size stays bounded after many update cycles', async () => { |
| 102 | + const assets = ['XLM', 'BTC', 'ETH']; |
| 103 | + |
| 104 | + // Run 500 update cycles directly (no real time needed) |
| 105 | + for (let i = 0; i < 500; i++) { |
| 106 | + await service.updatePrices(assets); |
| 107 | + } |
| 108 | + |
| 109 | + const stats = service.getStatus().aggregatorStats; |
| 110 | + // Cache has maxEntries=100 by default; it must never exceed that |
| 111 | + expect(stats.cacheStats.size).toBeLessThanOrEqual(100); |
| 112 | + // With only 3 assets the cache should hold at most 3 entries |
| 113 | + expect(stats.cacheStats.size).toBeLessThanOrEqual(assets.length); |
| 114 | + }); |
| 115 | + |
| 116 | + it('interval is cleared after stop()', async () => { |
| 117 | + const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval'); |
| 118 | + |
| 119 | + await service.start(['XLM']); |
| 120 | + expect(service.getStatus().isRunning).toBe(true); |
| 121 | + |
| 122 | + service.stop(); |
| 123 | + |
| 124 | + expect(clearIntervalSpy).toHaveBeenCalledTimes(1); |
| 125 | + expect(service.getStatus().isRunning).toBe(false); |
| 126 | + |
| 127 | + clearIntervalSpy.mockRestore(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('no new process event listeners accumulate across start/stop cycles', async () => { |
| 131 | + const listenersBefore = process.listenerCount('uncaughtException'); |
| 132 | + |
| 133 | + for (let i = 0; i < 10; i++) { |
| 134 | + await service.start(['XLM']); |
| 135 | + service.stop(); |
| 136 | + // Re-create service to simulate repeated instantiation |
| 137 | + service = new OracleService({ ...BASE_CONFIG }); |
| 138 | + } |
| 139 | + |
| 140 | + const listenersAfter = process.listenerCount('uncaughtException'); |
| 141 | + expect(listenersAfter).toBeLessThanOrEqual(listenersBefore + 1); |
| 142 | + }); |
| 143 | + |
| 144 | + it('fast-forwarded timer triggers updates without growing circuit breaker Maps', async () => { |
| 145 | + await service.start(['XLM', 'BTC']); |
| 146 | + |
| 147 | + const tickCount = 50; |
| 148 | + for (let i = 0; i < tickCount; i++) { |
| 149 | + await vi.advanceTimersByTimeAsync(BASE_CONFIG.updateIntervalMs); |
| 150 | + } |
| 151 | + |
| 152 | + const metrics = service.getStatus().circuitBreakers; |
| 153 | + // Circuit breaker Map is fixed at provider count — must not grow |
| 154 | + expect(metrics.length).toBe(2); // coingecko + binance |
| 155 | + }); |
| 156 | + |
| 157 | + it('service is fully stopped and interval does not fire after stop()', async () => { |
| 158 | + const updateSpy = vi.spyOn(service, 'updatePrices'); |
| 159 | + |
| 160 | + await service.start(['XLM']); |
| 161 | + const callsAfterStart = updateSpy.mock.calls.length; |
| 162 | + |
| 163 | + service.stop(); |
| 164 | + |
| 165 | + // Advance time well past the interval — no additional calls expected |
| 166 | + await vi.advanceTimersByTimeAsync(BASE_CONFIG.updateIntervalMs * 10); |
| 167 | + |
| 168 | + expect(updateSpy.mock.calls.length).toBe(callsAfterStart); |
| 169 | + }); |
| 170 | +}); |
0 commit comments