mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
- Created resolveWaiting helper function to centralize the logic for resolving and cleaning up waiting entries - Simplified test error message to avoid redundancy - This improves code maintainability by avoiding duplication Co-authored-by: arthurfiorette <47537704+arthurfiorette@users.noreply.github.com>
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
import assert from 'node:assert';
|
|
import { describe, it } from 'node:test';
|
|
import { buildMemoryStorage } from '../../src/storage/memory.js';
|
|
import { mockAxios } from '../mocks/axios.js';
|
|
|
|
describe('Waiting Memory Leak', () => {
|
|
it('should clean up waiting map when entry is evicted from storage due to maxEntries', async () => {
|
|
// Create storage with maxEntries=2 to force eviction
|
|
const storage = buildMemoryStorage(false, false, 2);
|
|
const axios = mockAxios({ storage });
|
|
|
|
// Make 3 concurrent requests to different URLs
|
|
// The first request should be evicted when the third one starts
|
|
const promise1 = axios.get('url1');
|
|
const promise2 = axios.get('url2');
|
|
const promise3 = axios.get('url3');
|
|
|
|
// Wait for all requests to complete
|
|
await Promise.all([promise1, promise2, promise3]);
|
|
|
|
// The waiting map should be empty after all requests complete
|
|
assert.equal(axios.waiting.size, 0, 'waiting map should be empty after all requests complete');
|
|
});
|
|
|
|
it('should clean up waiting map when loading entry is evicted during concurrent requests', async () => {
|
|
// Create storage with maxEntries=1 to force aggressive eviction
|
|
const storage = buildMemoryStorage(false, false, 1);
|
|
const axios = mockAxios({ storage });
|
|
|
|
// Start two concurrent requests
|
|
const promise1 = axios.get('url1');
|
|
const promise2 = axios.get('url2');
|
|
|
|
// Wait for both to complete
|
|
const [result1, result2] = await Promise.all([promise1, promise2]);
|
|
|
|
// Verify responses are valid
|
|
assert.ok(result1.data);
|
|
assert.ok(result2.data);
|
|
|
|
// The waiting map should be empty
|
|
assert.equal(axios.waiting.size, 0, 'waiting map should be empty but has entries');
|
|
});
|
|
|
|
it('should handle multiple waves of concurrent requests with maxEntries', async () => {
|
|
const storage = buildMemoryStorage(false, false, 2);
|
|
const axios = mockAxios({ storage });
|
|
|
|
// First wave of requests
|
|
await Promise.all([axios.get('url1'), axios.get('url2'), axios.get('url3')]);
|
|
|
|
assert.equal(axios.waiting.size, 0, 'waiting map should be empty after first wave');
|
|
|
|
// Second wave of requests
|
|
await Promise.all([axios.get('url4'), axios.get('url5'), axios.get('url6')]);
|
|
|
|
assert.equal(axios.waiting.size, 0, 'waiting map should be empty after second wave');
|
|
});
|
|
});
|