fix(tests): fix tests

This commit is contained in:
Juergen Kunz
2025-06-22 23:15:30 +00:00
parent 6e16f9423a
commit 1aead55296

View File

@ -1,7 +1,7 @@
import { expect, tap } from '@git.zone/tstest/tapbundle'; import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SmartProxy } from '../ts/index.js'; import { SmartProxy } from '../ts/index.js';
tap.test('cleanup queue bug - verify queue processing handles more than batch size', async (tools) => { tap.test('cleanup queue bug - verify queue processing handles more than batch size', async () => {
console.log('\n=== Cleanup Queue Bug Test ==='); console.log('\n=== Cleanup Queue Bug Test ===');
console.log('Purpose: Verify that the cleanup queue correctly processes all connections'); console.log('Purpose: Verify that the cleanup queue correctly processes all connections');
console.log('even when there are more than the batch size (100)'); console.log('even when there are more than the batch size (100)');
@ -37,14 +37,23 @@ tap.test('cleanup queue bug - verify queue processing handles more than batch si
remoteAddress: '127.0.0.1', remoteAddress: '127.0.0.1',
removeAllListeners: () => {}, removeAllListeners: () => {},
destroy: () => {}, destroy: () => {},
end: () => {} end: () => {},
on: () => {},
once: () => {},
emit: () => {},
pause: () => {},
resume: () => {}
}; };
const mockOutgoing = { const mockOutgoing = {
destroyed: true, destroyed: true,
writable: false, writable: false,
removeAllListeners: () => {},
destroy: () => {}, destroy: () => {},
end: () => {} end: () => {},
on: () => {},
once: () => {},
emit: () => {}
}; };
const mockRecord = { const mockRecord = {
@ -73,38 +82,56 @@ tap.test('cleanup queue bug - verify queue processing handles more than batch si
// Queue all connections for cleanup // Queue all connections for cleanup
console.log('\n--- Queueing all connections for cleanup ---'); console.log('\n--- Queueing all connections for cleanup ---');
// The cleanup queue processes immediately when it reaches batch size (100)
// So after queueing 150, the first 100 will be processed immediately
for (const conn of mockConnections) { for (const conn of mockConnections) {
cm.initiateCleanupOnce(conn, 'test_cleanup'); cm.initiateCleanupOnce(conn, 'test_cleanup');
} }
console.log(`Cleanup queue size: ${cm.cleanupQueue.size}`); // After queueing 150, the first 100 should have been processed immediately
expect(cm.cleanupQueue.size).toEqual(150); // leaving 50 in the queue
console.log(`Cleanup queue size after queueing: ${cm.cleanupQueue.size}`);
console.log(`Active connections after initial batch: ${cm.getConnectionCount()}`);
// Wait for cleanup to complete // The first 100 should have been cleaned up immediately
console.log('\n--- Waiting for cleanup batches to process ---'); expect(cm.cleanupQueue.size).toEqual(50);
expect(cm.getConnectionCount()).toEqual(50);
// The cleanup happens in batches, wait for all to complete // Wait for remaining cleanup to complete
console.log('\n--- Waiting for remaining cleanup batches to process ---');
// The remaining 50 connections should be cleaned up in the next batch
let waitTime = 0; let waitTime = 0;
let lastCount = cm.getConnectionCount();
while (cm.getConnectionCount() > 0 || cm.cleanupQueue.size > 0) { while (cm.getConnectionCount() > 0 || cm.cleanupQueue.size > 0) {
await new Promise(resolve => setTimeout(resolve, 100)); await new Promise(resolve => setTimeout(resolve, 100));
waitTime += 100; waitTime += 100;
const currentCount = cm.getConnectionCount();
if (currentCount !== lastCount) {
console.log(`Active connections: ${currentCount}, Queue size: ${cm.cleanupQueue.size}`);
lastCount = currentCount;
}
if (waitTime > 5000) { if (waitTime > 5000) {
console.log('Timeout waiting for cleanup to complete'); console.log('Timeout waiting for cleanup to complete');
break; break;
} }
} }
console.log(`Cleanup completed in ${waitTime}ms`); console.log(`All cleanup completed in ${waitTime}ms`);
// Check final state // Check final state
const finalCount = cm.getConnectionCount(); const finalCount = cm.getConnectionCount();
console.log(`\nFinal connection count: ${finalCount}`); console.log(`\nFinal connection count: ${finalCount}`);
console.log(`Cleanup queue size: ${cm.cleanupQueue.size}`); console.log(`Final cleanup queue size: ${cm.cleanupQueue.size}`);
// All connections should be cleaned up // All connections should be cleaned up
expect(finalCount).toEqual(0); expect(finalCount).toEqual(0);
expect(cm.cleanupQueue.size).toEqual(0); expect(cm.cleanupQueue.size).toEqual(0);
// Verify termination stats // Verify termination stats - all 150 should have been terminated
const stats = cm.getTerminationStats(); const stats = cm.getTerminationStats();
console.log('Termination stats:', stats); console.log('Termination stats:', stats);
expect(stats.incoming.test_cleanup).toEqual(150); expect(stats.incoming.test_cleanup).toEqual(150);