dcrouter/test/suite/smtpclient_performance/test.cperf-07.queue-management.ts

168 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-05-26 10:35:50 +00:00
import { tap, expect } from '@git.zone/tstest/tapbundle';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
import { createSmtpClient, createPooledSmtpClient } from '../../../ts/mail/delivery/smtpclient/index.js';
2025-05-24 18:12:08 +00:00
import { Email } from '../../../ts/mail/core/classes.email.js';
2025-05-26 10:35:50 +00:00
tap.test('setup - start SMTP server for queue management tests', async () => {
// Just a placeholder to ensure server starts properly
});
2025-05-24 18:12:08 +00:00
2025-05-26 10:35:50 +00:00
tap.test('CPERF-07: queue management - basic queue processing', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
2025-05-24 18:12:08 +00:00
});
2025-05-26 10:35:50 +00:00
console.log('Testing basic queue processing...');
const poolClient = createPooledSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
maxConnections: 2,
2025-05-24 18:12:08 +00:00
});
2025-05-26 10:35:50 +00:00
// Queue up 10 emails
const emailCount = 10;
const emails = Array(emailCount).fill(null).map((_, i) =>
new Email({
from: 'sender@example.com',
to: [`queue${i}@example.com`],
subject: `Queue test ${i}`,
text: `Testing queue management - message ${i}`,
})
);
console.log(`Queueing ${emailCount} emails...`);
const queueStart = Date.now();
// Send all emails (they will be queued and processed)
const sendPromises = emails.map((email, index) =>
poolClient.sendMail(email).then(result => {
console.log(` Email ${index} sent`);
return result;
})
);
const results = await Promise.all(sendPromises);
const queueTime = Date.now() - queueStart;
// Verify all succeeded
results.forEach(result => expect(result.success).toBeTrue());
console.log(`All ${emailCount} emails processed in ${queueTime}ms`);
console.log(`Average time per email: ${(queueTime / emailCount).toFixed(1)}ms`);
// Should process queue efficiently
expect(queueTime).toBeLessThan(20000); // Less than 20 seconds for 10 emails
await poolClient.close();
await stopTestServer(testServer);
});
tap.test('CPERF-07: queue management - queue with rate limiting', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
});
2025-05-24 18:12:08 +00:00
2025-05-26 10:35:50 +00:00
console.log('Testing queue with rate limiting...');
const client = createSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
2025-05-24 18:12:08 +00:00
});
2025-05-26 10:35:50 +00:00
// Send 5 emails sequentially (simulating rate limiting)
const emailCount = 5;
const rateLimitDelay = 200; // 200ms between emails
console.log(`Sending ${emailCount} emails with ${rateLimitDelay}ms rate limit...`);
const rateStart = Date.now();
for (let i = 0; i < emailCount; i++) {
const email = new Email({
from: 'sender@example.com',
to: [`ratelimit${i}@example.com`],
subject: `Rate limit test ${i}`,
text: `Testing rate limited queue - message ${i}`,
});
2025-05-24 18:12:08 +00:00
2025-05-26 10:35:50 +00:00
const result = await client.sendMail(email);
expect(result.success).toBeTrue();
2025-05-24 18:12:08 +00:00
2025-05-26 10:35:50 +00:00
console.log(` Email ${i} sent`);
// Simulate rate limiting delay
if (i < emailCount - 1) {
await new Promise(resolve => setTimeout(resolve, rateLimitDelay));
2025-05-24 18:12:08 +00:00
}
2025-05-26 10:35:50 +00:00
}
const rateTime = Date.now() - rateStart;
const expectedMinTime = (emailCount - 1) * rateLimitDelay;
console.log(`Rate limited emails sent in ${rateTime}ms`);
console.log(`Expected minimum time: ${expectedMinTime}ms`);
// Should respect rate limiting
expect(rateTime).toBeGreaterThanOrEqual(expectedMinTime);
await client.close();
await stopTestServer(testServer);
});
tap.test('CPERF-07: queue management - queue overflow handling', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
2025-05-24 18:12:08 +00:00
});
2025-05-26 10:35:50 +00:00
console.log('Testing queue overflow handling...');
// Create pool with small connection limit
const poolClient = createPooledSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
maxConnections: 1, // Only 1 connection to force queueing
2025-05-24 18:12:08 +00:00
});
2025-05-26 10:35:50 +00:00
// Send multiple emails at once to test queueing
const emails = Array(5).fill(null).map((_, i) =>
new Email({
from: 'sender@example.com',
to: [`overflow${i}@example.com`],
subject: `Overflow test ${i}`,
text: `Testing queue overflow - message ${i}`,
})
);
console.log('Sending 5 emails through 1 connection...');
const overflowStart = Date.now();
const results = await Promise.all(
emails.map(email => poolClient.sendMail(email))
);
const overflowTime = Date.now() - overflowStart;
// All should succeed despite limited connections
results.forEach(result => expect(result.success).toBeTrue());
console.log(`Queue overflow handled in ${overflowTime}ms`);
console.log(`All emails successfully queued and sent through single connection`);
await poolClient.close();
await stopTestServer(testServer);
});
tap.test('cleanup - stop SMTP server', async () => {
// Cleanup is handled in individual tests
});
tap.start();