Renamed retries to attempts

This commit is contained in:
Mikunj 2020-06-10 15:55:32 +10:00
parent 88f87c0a70
commit 1c84fa6c02
2 changed files with 9 additions and 10 deletions

View file

@ -20,17 +20,19 @@ export function canSendToSnode(): boolean {
/**
* Send a message via service nodes.
*
* @param message The message to send.
* @param retries The amount of times to retry sending.
* @param attempts The amount of times to attempt sending. Minimum value is 1.
*/
export async function send(
{ device, plainTextBuffer, encryption, timestamp, ttl }: RawMessage,
retries: number = 3
message: RawMessage,
attempts: number = 3
): Promise<void> {
if (!canSendToSnode()) {
throw new Error('lokiMessageAPI is not initialized.');
}
const { device, plainTextBuffer, encryption, timestamp, ttl } = message;
const { envelopeType, cipherText } = await MessageEncrypter.encrypt(
device,
plainTextBuffer,
@ -39,13 +41,10 @@ export async function send(
const envelope = await buildEnvelope(envelopeType, timestamp, cipherText);
const data = wrapEnvelope(envelope);
// pRetry counts retries after making the first call.
// So a retry count of 3 means you make a request then if it fails you make another request 3 times until it succeeds.
// This means a total of 4 requests are being sent, where as for us when we want 3 retries we only want 3 requests sent.
return pRetry(
async () => lokiMessageAPI.sendMessage(device, data, timestamp, ttl),
{
retries: retries - 1,
retries: Math.max(attempts - 1, 0),
factor: 1,
}
);

View file

@ -80,10 +80,10 @@ describe('MessageSender', () => {
it('should only retry the specified amount of times before throwing', async () => {
lokiMessageAPIStub.sendMessage.throws(new Error('API error'));
const retries = 2;
const promise = MessageSender.send(rawMessage, retries);
const attempts = 2;
const promise = MessageSender.send(rawMessage, attempts);
await expect(promise).is.rejectedWith('API error');
expect(lokiMessageAPIStub.sendMessage.callCount).to.equal(retries);
expect(lokiMessageAPIStub.sendMessage.callCount).to.equal(attempts);
});
it('should not throw error if successful send occurs within the retry limit', async () => {