2025-05-23 19:03:44 +00:00
|
|
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
2025-05-23 21:20:39 +00:00
|
|
|
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
|
2025-05-23 19:49:25 +00:00
|
|
|
import type { SmtpServer } from '../../../ts/mail/delivery/smtpserver/index.js';
|
2025-05-23 21:20:39 +00:00
|
|
|
import { createConcurrentConnections, performSmtpHandshake, closeSmtpConnection } from '../../helpers/utils.js';
|
2025-05-23 19:03:44 +00:00
|
|
|
|
2025-05-23 19:49:25 +00:00
|
|
|
let testServer: SmtpServer;
|
2025-05-23 19:03:44 +00:00
|
|
|
const CONCURRENT_COUNT = 10;
|
|
|
|
|
|
|
|
tap.test('setup - start SMTP server', async () => {
|
2025-05-23 21:20:39 +00:00
|
|
|
testServer = await startTestServer({
|
|
|
|
port: 2526
|
|
|
|
});
|
2025-05-23 19:49:25 +00:00
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
2025-05-23 19:03:44 +00:00
|
|
|
expect(testServer.port).toEqual(2526);
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('CM-02: Multiple Simultaneous Connections - server handles concurrent connections', async () => {
|
|
|
|
const startTime = Date.now();
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Create multiple concurrent connections
|
|
|
|
console.log(`🔄 Creating ${CONCURRENT_COUNT} concurrent connections...`);
|
|
|
|
const sockets = await createConcurrentConnections(
|
|
|
|
testServer.hostname,
|
|
|
|
testServer.port,
|
|
|
|
CONCURRENT_COUNT
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(sockets).toBeArray();
|
|
|
|
expect(sockets.length).toEqual(CONCURRENT_COUNT);
|
|
|
|
|
|
|
|
// Verify all connections are active
|
|
|
|
let activeCount = 0;
|
|
|
|
for (const socket of sockets) {
|
|
|
|
if (socket && !socket.destroyed) {
|
|
|
|
activeCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(activeCount).toEqual(CONCURRENT_COUNT);
|
|
|
|
|
|
|
|
// Perform handshake on all connections
|
|
|
|
console.log('🤝 Performing handshake on all connections...');
|
|
|
|
const handshakePromises = sockets.map(socket =>
|
|
|
|
performSmtpHandshake(socket).catch(err => ({ error: err.message }))
|
|
|
|
);
|
|
|
|
|
|
|
|
const results = await Promise.all(handshakePromises);
|
|
|
|
const successCount = results.filter(r => Array.isArray(r)).length;
|
|
|
|
|
|
|
|
expect(successCount).toBeGreaterThan(0);
|
|
|
|
console.log(`✅ ${successCount}/${CONCURRENT_COUNT} connections completed handshake`);
|
|
|
|
|
|
|
|
// Close all connections
|
|
|
|
console.log('🔚 Closing all connections...');
|
|
|
|
await Promise.all(
|
|
|
|
sockets.map(socket => closeSmtpConnection(socket).catch(() => {}))
|
|
|
|
);
|
|
|
|
|
|
|
|
const duration = Date.now() - startTime;
|
|
|
|
console.log(`✅ Multiple connection test completed in ${duration}ms`);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.error('❌ Multiple connection test failed:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('CM-02: Connection limit enforcement - verify max connections', async () => {
|
|
|
|
const maxConnections = 5;
|
|
|
|
|
|
|
|
// Start a new server with lower connection limit
|
2025-05-23 19:49:25 +00:00
|
|
|
const limitedServer = await startTestServer();
|
2025-05-23 19:03:44 +00:00
|
|
|
|
2025-05-23 19:49:25 +00:00
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));try {
|
2025-05-23 19:03:44 +00:00
|
|
|
// Try to create more connections than allowed
|
|
|
|
const attemptCount = maxConnections + 5;
|
|
|
|
console.log(`🔄 Attempting ${attemptCount} connections (limit: ${maxConnections})...`);
|
|
|
|
|
|
|
|
const connectionPromises = [];
|
|
|
|
for (let i = 0; i < attemptCount; i++) {
|
|
|
|
connectionPromises.push(
|
|
|
|
createConcurrentConnections(limitedServer.hostname, limitedServer.port, 1)
|
|
|
|
.then(() => ({ success: true, index: i }))
|
|
|
|
.catch(err => ({ success: false, index: i, error: err.message }))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = await Promise.all(connectionPromises);
|
|
|
|
const successfulConnections = results.filter(r => r.success).length;
|
|
|
|
const failedConnections = results.filter(r => !r.success).length;
|
|
|
|
|
|
|
|
console.log(`✅ Successful connections: ${successfulConnections}`);
|
|
|
|
console.log(`❌ Failed connections: ${failedConnections}`);
|
|
|
|
|
|
|
|
// Some connections should fail due to limit
|
|
|
|
expect(failedConnections).toBeGreaterThan(0);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
await stopTestServer(limitedServer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('cleanup - stop SMTP server', async () => {
|
2025-05-23 21:20:39 +00:00
|
|
|
await stopTestServer(testServer);
|
2025-05-23 19:03:44 +00:00
|
|
|
console.log('✅ Test server stopped');
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.start();
|