Skip to content

Commit 67e192b

Browse files
committed
Add coverage to retryAddParticipant
1 parent f11242a commit 67e192b

File tree

2 files changed

+84
-22
lines changed

2 files changed

+84
-22
lines changed
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
import { addParticipant } from "./addParticipant.util"
22

3-
export const retryAddParticipant = async (conversationSid: string, participantAddress: string, proxyAddresses: Array<string>) => {
4-
try {
5-
while(proxyAddresses.length > 0) {
6-
try {
7-
const participant = {
8-
'messagingBinding.address': participantAddress,
9-
'messagingBinding.proxyAddress': proxyAddresses[0]
10-
} as any
11-
12-
return addParticipant(conversationSid, participant)
3+
export async function retryAddParticipant(conversationSid: string, participantAddress: string, proxyAddresses: Array<string>) {
4+
if(proxyAddresses.length < 1) {
5+
throw new Error(`No available proxy addresses for ${participantAddress}`)
6+
}
137

14-
} catch(err) {
15-
if (err.code === 50416) {
16-
const remainingProxyAddresses = proxyAddresses.shift()
17-
retryAddParticipant(conversationSid, participantAddress, remainingProxyAddresses as any)
18-
}
19-
console.log(err)
20-
throw new Error(err)
21-
}
22-
}
8+
try {
9+
const participant = {
10+
'messagingBinding.address': participantAddress,
11+
'messagingBinding.proxyAddress': proxyAddresses[0]
12+
} as any
2313

24-
throw new Error(`No proxy addresses available for ${participantAddress}`)
14+
const result = await addParticipant(conversationSid, participant)
15+
return result
2516
} catch(err) {
26-
console.log(err)
27-
throw new Error(err)
17+
if (err.code === 50416) {
18+
proxyAddresses.shift()
19+
return retryAddParticipant(conversationSid, participantAddress, proxyAddresses)
20+
} else {
21+
console.log('hereherh')
22+
console.log(err)
23+
throw new Error(err)
24+
}
2825
}
2926
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { addParticipant } from '../../src/utils/addParticipant.util'
2+
import { mockParticpantInstance } from '../support/testSupport'
3+
import * as retry from '../../src/utils/retryAddParticipant.util'
4+
5+
jest.mock('../../src/utils/addParticipant.util')
6+
7+
8+
describe('retryParticipantAdd', () => {
9+
it('throws if no proxy addresses are left', async () => {
10+
await expect(retry.retryAddParticipant('CH1234', '+111', []))
11+
.rejects
12+
.toThrowError('No available proxy addresses for +111')
13+
})
14+
15+
it('calls itself again when 50416 is thrown to try another address', async () => {
16+
const mockAddParticipant = jest.mocked(addParticipant, true)
17+
18+
interface TwilioError extends Error {
19+
code: number
20+
}
21+
22+
class TwilioError extends Error {
23+
constructor(message) {
24+
super(message);
25+
this.name = "ConcurrencyLimit";
26+
this.code = 50416
27+
}
28+
}
29+
30+
const retrySpy = jest.spyOn(retry, 'retryAddParticipant')
31+
32+
const rejectedValue = new TwilioError('Number already in use')
33+
34+
mockAddParticipant
35+
.mockRejectedValueOnce(rejectedValue)
36+
.mockResolvedValueOnce(mockParticpantInstance as any)
37+
// .mockResolvedValueOnce(mockParticpantInstance as any)
38+
39+
const result = await retry.retryAddParticipant('CH1234', '+111', ['+222', '+333'])
40+
expect(retrySpy).toBeCalledTimes(1)
41+
expect(mockAddParticipant).nthCalledWith(1, 'CH1234', {
42+
'messagingBinding.address': '+111',
43+
'messagingBinding.proxyAddress': '+222'
44+
})
45+
expect(mockAddParticipant).nthCalledWith(2, 'CH1234', {
46+
'messagingBinding.address': '+111',
47+
'messagingBinding.proxyAddress': '+333'
48+
})
49+
expect(mockAddParticipant).toBeCalledTimes(2)
50+
expect(result).toEqual(mockParticpantInstance)
51+
})
52+
53+
it('throws an error if client err code is not 50416', async () => {
54+
const mockAddParticipant = jest.mocked(addParticipant, true)
55+
56+
const rejectedValue = new Error('Twilio Problem')
57+
mockAddParticipant
58+
.mockRejectedValue(rejectedValue)
59+
60+
debugger
61+
await expect(retry.retryAddParticipant('CH1234', '+111', ['+222', '+333']))
62+
.rejects
63+
.toThrow('Twilio Problem')
64+
})
65+
})

0 commit comments

Comments
 (0)