Skip to content

Commit 3fa6294

Browse files
committed
Finish addParticipant tests
1 parent 9ee221d commit 3fa6294

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

src/utils/addParticipant.util.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import retry from 'async-retry';
22
import client from "../twilioClient";
33
import { ParticipantInstance, ParticipantListInstanceCreateOptions } from "twilio/lib/rest/conversations/v1/conversation/participant";
4+
import { retryConfig } from '../config/retry.config';
45

56
export const addParticipant = async (
67
conversationSid: string,
7-
participant: ParticipantListInstanceCreateOptions
8+
participant: ParticipantListInstanceCreateOptions,
9+
retryOptions = retryConfig
810
) : Promise<ParticipantInstance> => {
911
return retry(async (quit) => {
1012
try {
@@ -16,12 +18,13 @@ export const addParticipant = async (
1618
return createdParticipant
1719
} catch (err) {
1820
if (err.status !== 429) {
21+
console.log('Quit without retry')
1922
quit(new Error(err));
2023
return;
2124
}
2225

2326
console.log('Re-trying on 429 error');
2427
throw new Error(err);
2528
}
26-
})
29+
}, retryOptions)
2730
}

tests/utils/addParticipant.test.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ describe('addParticipant util', () => {
1919

2020
it('it adds participant to conversation', async () => {
2121
let createSpy = jest.fn((options) => { return mockParticipant })
22-
23-
let participantsSpy = { participants: { create: createSpy } } ;
24-
25-
const conversationsSpy = jest.fn((options) => { return participantsSpy });
22+
const conversationsSpy = jest.fn((options) => {
23+
return {
24+
participants: { create: createSpy }
25+
}
26+
});
2627

2728
mockedClient['conversations'] = {
2829
conversations: conversationsSpy
@@ -35,5 +36,58 @@ describe('addParticipant util', () => {
3536
expect(result).not.toBeNull();
3637
})
3738

39+
it('calls quit if error is not a 429 retry', async () => {
40+
let createSpy = jest.fn((options) => { throw new Error('Twilio Problem') })
41+
const conversationsSpy = jest.fn((options) => {
42+
return {
43+
participants: { create: createSpy }
44+
}
45+
});
46+
47+
mockedClient['conversations'] = {
48+
conversations: conversationsSpy
49+
} as any
50+
51+
const consoleSpy = jest.spyOn(console, 'log');
52+
53+
try {
54+
await addParticipant("myConversationSid", mockParticipant );
55+
} catch (e) {
56+
expect(consoleSpy).toHaveBeenCalledWith('Quit without retry');
57+
}
58+
})
59+
60+
it('throws error to retry on 429 status code', async () => {
3861

62+
interface TwilioError extends Error {
63+
status: number
64+
}
65+
66+
class TwilioError extends Error {
67+
constructor(message) {
68+
super(message);
69+
this.name = "ConcurrencyLimit";
70+
this.status = 429
71+
}
72+
}
73+
74+
let createSpy = jest.fn((options) => { throw new TwilioError('Concurrency Limit') })
75+
const conversationsSpy = jest.fn((options) => {
76+
return {
77+
participants: { create: createSpy }
78+
}
79+
});
80+
81+
mockedClient['conversations'] = {
82+
conversations: conversationsSpy
83+
} as any
84+
85+
const consoleSpy = jest.spyOn(console, 'log');
86+
87+
try {
88+
await addParticipant("myConversationSid", mockParticipant, { retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
89+
} catch (e) {
90+
expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
91+
}
92+
})
3993
})

0 commit comments

Comments
 (0)