Skip to content

Commit 777fab8

Browse files
committed
Working tests for createConversation, improvements to loadtest
1 parent 6e348fc commit 777fab8

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/config/retry.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const retryConfig = {
2+
retries: 5,
3+
factor: 2,
4+
minTimeout: 1000,
5+
maxTimeout: 360000
6+
}

src/utils/createConversation.util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { SessionPostBody } from "../@types/types";
33
import client from "../twilioClient";
44

55
import retry from 'async-retry';
6+
import { retryConfig } from "../config/retry.config";
67

7-
export const createConversation = async (options: SessionPostBody) : Promise<ConversationInstance> => {
88

9+
export const createConversation = async (options: SessionPostBody, retryOptions = retryConfig) : Promise<ConversationInstance> => {
910
return retry(async (quit) => {
1011
try {
1112
return client.conversations.conversations.create(options);
@@ -20,5 +21,5 @@ export const createConversation = async (options: SessionPostBody) : Promise<Con
2021
console.log('Re-trying on 429 error');
2122
throw new Error(err);
2223
}
23-
})
24+
}, retryOptions)
2425
}

tests/loadtest.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const client = twilio(
99
process.env.TWILIO_AUTH_TOKEN
1010
);
1111

12+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
13+
1214
async function makeRequest(contacts) {
1315
const body = { addresses: contacts }
1416

@@ -43,13 +45,17 @@ async function runTest() {
4345

4446
const promise = makeRequest(contacts)
4547
requests.push(promise);
48+
await sleep(2)
4649
}
4750

4851
results = await Promise.allSettled(requests);
4952
console.timeEnd('testCreate');
5053
} catch(e) {
5154
console.error(`failed ${JSON.stringify(e)}`);
5255
} finally {
56+
57+
const deletePromises = []
58+
5359
for (let i = 0; i < results.length; ++i) {
5460
const response = results[i];
5561

@@ -63,13 +69,23 @@ async function runTest() {
6369
if (response.value.sid) {
6470
console.log(`Removing conversation ${response.value.sid}`);
6571
}
66-
await client.conversations.conversations(response.value.sid).remove()
67-
72+
const deletePromise = client.conversations.conversations(response.value.sid).remove()
73+
deletePromises.push(deletePromise)
6874
}
6975
} catch(e){
7076
console.error(`Couldnt delete convo ${results[i]}: ${JSON.stringify(e)}`)
7177
}
78+
await sleep(50)
7279
}
80+
81+
try {
82+
await Promise.all(deletePromises)
83+
} catch (err) {
84+
console.log(err);
85+
}
86+
87+
console.log('Done cleaning up conversations')
88+
7389
}
7490
}
7591

tests/utils/createConversation.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,37 @@ describe('createConversation util', () => {
4040
expect(consoleSpy).toHaveBeenCalledWith('Quit without retry');
4141
}
4242
})
43+
44+
it('throws error to retry on 429 status code', async () => {
45+
46+
interface TwilioError extends Error {
47+
status: number
48+
}
49+
50+
class TwilioError extends Error {
51+
constructor(message) {
52+
super(message);
53+
this.name = "ConcurrencyLimit";
54+
this.status = 429
55+
}
56+
}
57+
58+
mockedClient['conversations'] = {
59+
conversations: {
60+
create: (options) => {
61+
throw new TwilioError('Too many requests')
62+
}
63+
}
64+
} as any
65+
66+
const consoleSpy = jest.spyOn(console, 'log');
67+
68+
try {
69+
await createConversation(
70+
{ friendlyName: "my conversation", addresses: ['1', '2']},
71+
{ retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
72+
} catch (e) {
73+
expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
74+
}
75+
})
4376
})

0 commit comments

Comments
 (0)