This commit is contained in:
2025-05-29 14:06:47 +00:00
parent af13d3af10
commit e6b3ae395c
9 changed files with 246 additions and 196 deletions

View File

@ -20,12 +20,29 @@ const TEST_DATA = 'Hello through dynamic port mapper!';
// Cleanup function to close all servers and proxies
function cleanup() {
return Promise.all([
...testServers.map(({ server }) => new Promise<void>(resolve => {
server.close(() => resolve());
})),
smartProxy ? smartProxy.stop() : Promise.resolve()
]);
console.log('Starting cleanup...');
const promises = [];
// Close test servers
for (const { server, port } of testServers) {
promises.push(new Promise<void>(resolve => {
console.log(`Closing test server on port ${port}`);
server.close(() => {
console.log(`Test server on port ${port} closed`);
resolve();
});
}));
}
// Stop SmartProxy
if (smartProxy) {
console.log('Stopping SmartProxy...');
promises.push(smartProxy.stop().then(() => {
console.log('SmartProxy stopped');
}));
}
return Promise.all(promises);
}
// Helper: Creates a test TCP server that listens on a given port
@ -223,7 +240,20 @@ tap.test('should handle errors in port mapping functions', async () => {
// Cleanup
tap.test('cleanup port mapping test environment', async () => {
await cleanup();
// Add timeout to prevent hanging if SmartProxy shutdown has issues
const cleanupPromise = cleanup();
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Cleanup timeout after 5 seconds')), 5000)
);
try {
await Promise.race([cleanupPromise, timeoutPromise]);
} catch (error) {
console.error('Cleanup error:', error);
// Force cleanup even if there's an error
testServers = [];
smartProxy = null as any;
}
});
export default tap.start();