Skip to content

Commit 2b0c59c

Browse files
authored
Merge pull request #23 from aymenn/utils-tests
Utils tests
2 parents d1b3ff3 + af8fa92 commit 2b0c59c

19 files changed

+638
-53
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
.env.local
33
.env
4-
launch.json
4+
launch.json
5+
coverage/

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
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import client from "../twilioClient"
22

33
import retry from 'async-retry'
4+
import { retryConfig } from "../config/retry.config";
45

5-
export const deleteConversation = async (conversationSid: string) : Promise<boolean> => {
6+
export const deleteConversation = async (conversationSid: string, retryOptions = retryConfig) : Promise<boolean> => {
67
return retry(async(quit) => {
78
try {
9+
console.log(client['conversations']['conversations']['remove'])
810
await client.conversations.conversations(conversationSid).remove()
911
return true
1012
} catch (err) {
1113
if (err.status !== 429) {
12-
quit(new Error(err));
14+
console.log('Quit without retry')
15+
console.log(err)
16+
quit(new Error('Quit without retry'));
1317
return;
1418
}
1519

1620
console.log('Re-trying on 429 error');
1721
throw new Error(err);
1822
}
19-
})
23+
}, retryOptions)
2024
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation';
22
import retry from 'async-retry'
33
import client from '../twilioClient'
4+
import { retryConfig } from '../config/retry.config';
45

5-
export const getConversationByAddressPair = async (address: string, proxyAddress: string) : Promise<ParticipantConversationInstance> => {
6+
export const getConversationByAddressPair = async (address: string, proxyAddress: string, retryOptions = retryConfig) : Promise<ParticipantConversationInstance> => {
67
return retry(async(quit) => {
78
try {
8-
if (address === undefined) {
9-
throw "getOpenConversationsForAddressPair: address is missing";
10-
}
11-
129
const participantConversations = await client.conversations.v1
1310
.participantConversations
1411
.list({ address: address });
15-
12+
1613
const conversation = participantConversations.find(p => {
1714
if (p.conversationState !== 'closed' && p.participantMessagingBinding.proxy_address === proxyAddress) {
1815
console.log(`Found a non-closed conversation ${p.conversationSid} with proxy address ${p.participantMessagingBinding.proxy_address} for address ${address}`);
1916
return p;
2017
}
2118
});
22-
2319
return conversation;
2420
} catch(err) {
2521
if (err.status !== 429) {
@@ -30,5 +26,5 @@ export const getConversationByAddressPair = async (address: string, proxyAddress
3026
console.log('Re-trying on 429 error');
3127
throw new Error(err);
3228
}
33-
})
29+
}, retryOptions)
3430
}

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export * from "./retryAddParticipant.util"
66
export * from "./listConversationParticipants.util"
77
export * from "./participantsToDial.util"
88
export * from "./getConversationByAddressPair.util"
9-
export * from "./generateConferenceName.util"
9+
export * from "./generateConferenceName.util"

src/utils/listConversationParticipants.util.ts

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

5-
export const listConversationParticipants = async (conversation: string) : Promise<ParticipantInstance[]> => {
6+
export const listConversationParticipants = async (conversation: string, retryOptions = retryConfig) : Promise<ParticipantInstance[]> => {
67
return retry(async (quit) => {
78
try {
89
const participants = await client.conversations
@@ -20,6 +21,6 @@ export const listConversationParticipants = async (conversation: string) : Promi
2021
console.log('Re-trying on 429 error');
2122
throw new Error(err);
2223
}
23-
})
24+
}, retryOptions)
2425

2526
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import retry from 'async-retry'
22
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation'
33
import client from '../twilioClient'
4+
import { retryConfig } from "../config/retry.config";
45

5-
export const listParticipantConversations = async (phoneNumber: string) : Promise<ParticipantConversationInstance[]> => {
6+
export const listParticipantConversations = async (phoneNumber: string, retryOptions = retryConfig) : Promise<ParticipantConversationInstance[]> => {
67
return retry(async (quit) => {
78
try {
89
const activeConversations = await client.conversations.participantConversations.list({address: phoneNumber})
910
return activeConversations
1011
} catch (err) {
1112
if (err.status !== 429) {
12-
quit(new Error(err));
13+
console.log('Quit without retry')
14+
console.log(err)
15+
quit(new Error('Quit without retry'));
1316
return;
1417
}
1518

1619
console.log('Re-trying on 429 error');
1720
throw new Error(err);
1821
}
19-
})
22+
}, retryOptions)
2023

2124
}

src/utils/participantsToDial.util.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ export const participantsToDial = (participants: Array<ParticipantInstance>, fro
55

66
const output = participants.reduce((result, p) => {
77
if (p.messagingBinding.type === "sms" && p.messagingBinding.address != from) {
8-
console.log(`Adding ${p.messagingBinding.address} to list of numbers to dial.\n`)
9-
10-
result.push({
11-
address: p.messagingBinding.address,
12-
proxyAddress: p.messagingBinding.proxy_address
13-
})
8+
console.log(`Adding ${p.messagingBinding.address} to list of numbers to dial.\n`)
9+
10+
result.push({
11+
address: p.messagingBinding.address,
12+
proxyAddress: p.messagingBinding.proxy_address
13+
})
1414
}
1515

1616
return result;
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
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(err)
22+
throw new Error(err)
23+
}
2824
}
2925
}

tests/loadtest.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ async function makeRequest(contacts) {
1616

1717
return new Promise((resolve, reject) => {
1818

19+
const stringBuffer = new Buffer.from(`${process.env.AUTH_USERNAME}:${process.env.AUTH_PASSWORD}`, "utf-8")
20+
const basicAuthToken = stringBuffer.toString('base64')
21+
1922
fetch(`http://localhost:${port}/sessions`, {
2023
method: 'post',
2124
body: JSON.stringify(body),
22-
headers: {'Content-Type': 'application/json'}
25+
headers: {
26+
'Content-Type': 'application/json',
27+
'Authorization': `Basic ${basicAuthToken}`
28+
}
2329
})
2430
.then(res => {
2531
const data = res.json()
@@ -39,13 +45,13 @@ async function runTest() {
3945
let results = [];
4046
try {
4147
const requests = [];
42-
for (let i = 0; i < 300; ++i) {
48+
for (let i = 0; i < 1000; ++i) {
4349
const contacts = ['+1925215'+ ('0000'+i).slice(-4), '+1925635'+ ('0000'+i).slice(-4)];
4450
console.log(contacts)
4551

4652
const promise = makeRequest(contacts)
4753
requests.push(promise);
48-
await sleep(2)
54+
await sleep(100)
4955
}
5056

5157
results = await Promise.allSettled(requests);

0 commit comments

Comments
 (0)