This commit is contained in:
2025-05-24 00:23:35 +00:00
parent 0907949f8a
commit cb52446f65
76 changed files with 1401 additions and 867 deletions

View File

@ -3,16 +3,19 @@ import * as net from 'net';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
const TEST_PORT = 2525;
let testServer: ITestServer;
const TEST_TIMEOUT = 60000; // Longer timeout for keepalive tests
tap.test('Keepalive - should maintain TCP keepalive', async (tools) => {
const done = tools.defer();
// Start test server
const testServer = await startTestServer();
testServer = await startTestServer({ port: TEST_PORT });
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 1000));try {
try {
const socket = net.createConnection({
host: 'localhost',
port: TEST_PORT,
@ -25,7 +28,7 @@ tap.test('Keepalive - should maintain TCP keepalive', async (tools) => {
});
// Enable TCP keepalive
const keepAliveDelay = 5000; // 5 seconds
const keepAliveDelay = 1000; // 1 second
socket.setKeepAlive(true, keepAliveDelay);
console.log(`TCP keepalive enabled with ${keepAliveDelay}ms delay`);
@ -55,7 +58,7 @@ tap.test('Keepalive - should maintain TCP keepalive', async (tools) => {
// Wait for keepalive duration + buffer
console.log('Waiting for keepalive period...');
await new Promise(resolve => setTimeout(resolve, keepAliveDelay + 2000));
await new Promise(resolve => setTimeout(resolve, keepAliveDelay + 500));
// Verify connection is still alive by sending NOOP
socket.write('NOOP\r\n');
@ -72,7 +75,7 @@ tap.test('Keepalive - should maintain TCP keepalive', async (tools) => {
socket.end();
} finally {
await stopTestServer();
await stopTestServer(testServer);
done.resolve();
}
});
@ -81,10 +84,11 @@ tap.test('Keepalive - should maintain idle connection for extended period', asyn
const done = tools.defer();
// Start test server
const testServer = await startTestServer();
testServer = await startTestServer({ port: TEST_PORT });
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 1000));try {
try {
const socket = net.createConnection({
host: 'localhost',
port: TEST_PORT,
@ -120,7 +124,7 @@ tap.test('Keepalive - should maintain idle connection for extended period', asyn
// Test multiple keepalive periods
const periods = 3;
const periodDuration = 5000; // 5 seconds each
const periodDuration = 1000; // 1 second each
for (let i = 0; i < periods; i++) {
console.log(`Keepalive period ${i + 1}/${periods}...`);
@ -144,7 +148,7 @@ tap.test('Keepalive - should maintain idle connection for extended period', asyn
socket.end();
} finally {
await stopTestServer();
await stopTestServer(testServer);
done.resolve();
}
});
@ -153,10 +157,11 @@ tap.test('Keepalive - should detect connection loss', async (tools) => {
const done = tools.defer();
// Start test server
const testServer = await startTestServer();
testServer = await startTestServer({ port: TEST_PORT });
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 1000));try {
try {
const socket = net.createConnection({
host: 'localhost',
port: TEST_PORT,
@ -205,10 +210,10 @@ tap.test('Keepalive - should detect connection loss', async (tools) => {
console.log('Connection established, now simulating server shutdown...');
// Shutdown server to simulate connection loss
await stopTestServer();
await stopTestServer(testServer);
// Wait for keepalive to detect connection loss
await new Promise(resolve => setTimeout(resolve, 10000));
await new Promise(resolve => setTimeout(resolve, 3000));
// Connection should be detected as lost
expect(connectionLost).toEqual(true);
@ -224,10 +229,11 @@ tap.test('Keepalive - should handle long-running SMTP session', async (tools) =>
const done = tools.defer();
// Start test server
const testServer = await startTestServer();
testServer = await startTestServer({ port: TEST_PORT });
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 1000));try {
try {
const socket = net.createConnection({
host: 'localhost',
port: TEST_PORT,
@ -265,10 +271,10 @@ tap.test('Keepalive - should handle long-running SMTP session', async (tools) =>
// Simulate a long-running session with periodic activity
const activities = [
{ command: 'MAIL FROM:<sender1@example.com>', delay: 3000 },
{ command: 'RSET', delay: 4000 },
{ command: 'MAIL FROM:<sender2@example.com>', delay: 3000 },
{ command: 'RSET', delay: 2000 }
{ command: 'MAIL FROM:<sender1@example.com>', delay: 500 },
{ command: 'RSET', delay: 500 },
{ command: 'MAIL FROM:<sender2@example.com>', delay: 500 },
{ command: 'RSET', delay: 500 }
];
for (const activity of activities) {
@ -298,7 +304,7 @@ tap.test('Keepalive - should handle long-running SMTP session', async (tools) =>
socket.end();
} finally {
await stopTestServer();
await stopTestServer(testServer);
done.resolve();
}
});
@ -307,10 +313,11 @@ tap.test('Keepalive - should handle NOOP as keepalive mechanism', async (tools)
const done = tools.defer();
// Start test server
const testServer = await startTestServer();
testServer = await startTestServer({ port: TEST_PORT });
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 1000));try {
try {
const socket = net.createConnection({
host: 'localhost',
port: TEST_PORT,
@ -342,7 +349,7 @@ tap.test('Keepalive - should handle NOOP as keepalive mechanism', async (tools)
});
// Use NOOP as application-level keepalive
const noopInterval = 5000; // 5 seconds
const noopInterval = 1000; // 1 second
const noopCount = 3;
console.log(`Sending ${noopCount} NOOP commands as keepalive...`);
@ -367,7 +374,7 @@ tap.test('Keepalive - should handle NOOP as keepalive mechanism', async (tools)
socket.end();
} finally {
await stopTestServer();
await stopTestServer(testServer);
done.resolve();
}
});