fix: resolve test hang issues in CI

- Fixed MSW event listener memory leaks
- Added proper database connection cleanup
- Fixed MSW server lifecycle management
- Reduced global test timeout to 30s for faster failure detection
- Added resource cleanup in all integration tests

This should resolve the GitHub Actions test hanging issue
This commit is contained in:
czlonkowski
2025-07-29 13:07:51 +02:00
parent c824fb5ebf
commit 115bb6f36c
5 changed files with 88 additions and 25 deletions

Binary file not shown.

View File

@@ -138,9 +138,13 @@ describe('Database Connection Management', () => {
// Test concurrent reads
const promises = connections.map((conn, index) => {
return new Promise((resolve) => {
const result = conn.prepare('SELECT ? as id').get(index);
resolve(result);
return new Promise((resolve, reject) => {
try {
const result = conn.prepare('SELECT ? as id').get(index);
resolve(result);
} catch (error) {
reject(error);
}
});
});
@@ -148,12 +152,32 @@ describe('Database Connection Management', () => {
expect(results).toHaveLength(connectionCount);
} finally {
// Cleanup connections
connections.forEach(conn => conn.close());
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
fs.unlinkSync(`${dbPath}-wal`);
fs.unlinkSync(`${dbPath}-shm`);
// Cleanup connections - ensure all are closed even if some fail
await Promise.all(
connections.map(async (conn) => {
try {
if (conn.open) {
conn.close();
}
} catch (error) {
// Ignore close errors
}
})
);
// Clean up files with error handling
try {
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
if (fs.existsSync(`${dbPath}-wal`)) {
fs.unlinkSync(`${dbPath}-wal`);
}
if (fs.existsSync(`${dbPath}-shm`)) {
fs.unlinkSync(`${dbPath}-shm`);
}
} catch (error) {
// Ignore cleanup errors
}
}
});
@@ -285,7 +309,7 @@ describe('Database Connection Management', () => {
db.exec('ROLLBACK');
conn2.close();
}
});
}, { timeout: 5000 }); // Add explicit timeout
});
describe('Database Configuration', () => {

View File

@@ -50,13 +50,21 @@ describe('MSW Setup Verification', () => {
});
describe('Integration Test Server', () => {
let serverStarted = false;
beforeAll(() => {
// Start a separate MSW instance for more control
mswTestServer.start({ onUnhandledRequest: 'error' });
// Only start if not already running
if (!serverStarted) {
mswTestServer.start({ onUnhandledRequest: 'error' });
serverStarted = true;
}
});
afterAll(() => {
mswTestServer.stop();
if (serverStarted) {
mswTestServer.stop();
serverStarted = false;
}
});
it('should handle workflow creation with custom response', async () => {
@@ -163,7 +171,7 @@ describe('MSW Setup Verification', () => {
expect(requests).toHaveLength(2);
expect(requests[0].url).toContain('/api/v1/workflows');
expect(requests[1].url).toContain('/api/v1/executions');
});
}, { timeout: 10000 }); // Increase timeout for this specific test
it('should work with scoped handlers', async () => {
const result = await mswTestServer.withScope(

View File

@@ -66,17 +66,34 @@ export const mswTestServer = {
waitForRequests: (count: number, timeout = 5000): Promise<Request[]> => {
return new Promise((resolve, reject) => {
const requests: Request[] = [];
const timeoutId = setTimeout(() => {
let timeoutId: NodeJS.Timeout | null = null;
// Event handler function to allow cleanup
const handleRequest = ({ request }: { request: Request }) => {
requests.push(request);
if (requests.length === count) {
cleanup();
resolve(requests);
}
};
// Cleanup function to remove listener and clear timeout
const cleanup = () => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
integrationTestServer.events.removeListener('request:match', handleRequest);
};
// Set timeout
timeoutId = setTimeout(() => {
cleanup();
reject(new Error(`Timeout waiting for ${count} requests. Got ${requests.length}`));
}, timeout);
integrationTestServer.events.on('request:match', ({ request }) => {
requests.push(request);
if (requests.length === count) {
clearTimeout(timeoutId);
resolve(requests);
}
});
// Add event listener
integrationTestServer.events.on('request:match', handleRequest);
});
},
@@ -86,14 +103,28 @@ export const mswTestServer = {
verifyNoUnhandledRequests: (): Promise<void> => {
return new Promise((resolve, reject) => {
let hasUnhandled = false;
let timeoutId: NodeJS.Timeout | null = null;
integrationTestServer.events.on('request:unhandled', ({ request }) => {
const handleUnhandled = ({ request }: { request: Request }) => {
hasUnhandled = true;
cleanup();
reject(new Error(`Unhandled request: ${request.method} ${request.url}`));
});
};
const cleanup = () => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
integrationTestServer.events.removeListener('request:unhandled', handleUnhandled);
};
// Add event listener
integrationTestServer.events.on('request:unhandled', handleUnhandled);
// Give a small delay to allow any pending requests
setTimeout(() => {
timeoutId = setTimeout(() => {
cleanup();
if (!hasUnhandled) {
resolve();
}

View File

@@ -77,7 +77,7 @@ function setTestDefaults(): void {
TEST_TIMEOUT_UNIT: '5000',
TEST_TIMEOUT_INTEGRATION: '15000',
TEST_TIMEOUT_E2E: '30000',
TEST_TIMEOUT_GLOBAL: '60000',
TEST_TIMEOUT_GLOBAL: '30000', // Reduced from 60s to 30s to catch hangs faster
// Test execution
TEST_RETRY_ATTEMPTS: '2',