mirror of https://github.com/eleith/emailjs.git
test/queue: refactor to differentiate between successful and failed task attempts
This commit is contained in:
parent
f481adbe80
commit
1073f165ce
113
test/queue.ts
113
test/queue.ts
|
@ -5,6 +5,12 @@ import { SMTPClient, Message } from '../email.js';
|
|||
const port = 7777;
|
||||
|
||||
test('synchronous queue failures are handled gracefully by client', async (t) => {
|
||||
const tlsClient = new SMTPClient({ port, timeout: 100, tls: true });
|
||||
const secureServer = new SMTPServer({ secure: true });
|
||||
|
||||
let attemptCount = 0;
|
||||
let failureCount = 0;
|
||||
|
||||
const mailQueue: (() => Promise<void>)[] = [];
|
||||
function* mailQueueGenerator() {
|
||||
while (mailQueue.length > 0) {
|
||||
|
@ -12,60 +18,69 @@ test('synchronous queue failures are handled gracefully by client', async (t) =>
|
|||
}
|
||||
}
|
||||
|
||||
const tlsClient = new SMTPClient({ port, timeout: 100, tls: true });
|
||||
const secureServer = new SMTPServer({ secure: true });
|
||||
|
||||
let tryCount = 0;
|
||||
secureServer.on('error', (err) =>
|
||||
t.log(`Try #${tryCount} failed: ${err.message}`)
|
||||
);
|
||||
|
||||
await t.throwsAsync(
|
||||
new Promise<void>((resolve, reject) => {
|
||||
secureServer.listen(port, async () => {
|
||||
const mailTask = async () => {
|
||||
try {
|
||||
await tlsClient.sendAsync(
|
||||
new Message({
|
||||
from: 'piglet@gmail.com',
|
||||
to: 'pooh@gmail.com',
|
||||
subject: 'this is a test TEXT message from emailjs',
|
||||
text: 'hello friend, i hope this message finds you well.',
|
||||
})
|
||||
);
|
||||
resolve();
|
||||
} catch (err) {
|
||||
if (tryCount++ < 5) {
|
||||
void mailQueue.push(mailTask);
|
||||
} else {
|
||||
t.log(
|
||||
`SMTPClient ${JSON.stringify(
|
||||
{
|
||||
tries: --tryCount,
|
||||
// @ts-expect-error need to check protected prop
|
||||
ready: tlsClient.ready,
|
||||
// @ts-expect-error need to check protected prop
|
||||
sending: tlsClient.sending,
|
||||
state: tlsClient.smtp.state(),
|
||||
},
|
||||
null,
|
||||
'\t'
|
||||
).replace(/"/g, '')}`
|
||||
secureServer
|
||||
.on('error', () => {
|
||||
/** intentionally swallow errors */
|
||||
})
|
||||
.listen(port, async () => {
|
||||
const mailTask = async () => {
|
||||
try {
|
||||
await tlsClient.sendAsync(
|
||||
new Message({
|
||||
from: 'piglet@gmail.com',
|
||||
to: 'pooh@gmail.com',
|
||||
subject: 'this is a test TEXT message from emailjs',
|
||||
text: 'hello friend, i hope this message finds you well.',
|
||||
})
|
||||
);
|
||||
// @ts-expect-error need to check protected prop
|
||||
t.false(tlsClient.ready);
|
||||
// @ts-expect-error need to check protected prop
|
||||
t.false(tlsClient.sending);
|
||||
t.is(tlsClient.smtp.state(), 0);
|
||||
reject(err);
|
||||
resolve();
|
||||
} catch (err) {
|
||||
if (attemptCount < 5) {
|
||||
void mailQueue.push(mailTask);
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
void mailQueue.push(mailTask);
|
||||
for (const task of mailQueueGenerator()) {
|
||||
try {
|
||||
attemptCount++;
|
||||
t.log(`Attempting to execute task #${attemptCount}...`);
|
||||
await task?.();
|
||||
t.log(`Attempt #${attemptCount} succeeded.`);
|
||||
} catch (err) {
|
||||
failureCount++;
|
||||
t.log(`Attempt #${attemptCount} failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
void mailQueue.push(mailTask);
|
||||
for (const task of mailQueueGenerator()) {
|
||||
await task?.();
|
||||
}
|
||||
});
|
||||
t.log(
|
||||
`Finished after ${attemptCount} attempts with ${failureCount} failures.`
|
||||
);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
t.log(
|
||||
`SMTPClient ${JSON.stringify(
|
||||
{
|
||||
// @ts-expect-error need to check protected prop
|
||||
ready: tlsClient.ready,
|
||||
// @ts-expect-error need to check protected prop
|
||||
sending: tlsClient.sending,
|
||||
state: tlsClient.smtp.state(),
|
||||
},
|
||||
null,
|
||||
'\t'
|
||||
).replace(/"/g, '')}`
|
||||
);
|
||||
|
||||
// @ts-expect-error need to check protected prop
|
||||
t.false(tlsClient.ready);
|
||||
// @ts-expect-error need to check protected prop
|
||||
t.false(tlsClient.sending);
|
||||
t.is(tlsClient.smtp.state(), 0);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue