-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6837b66
commit 4d2a9da
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { getTestSetup } from './init'; | ||
|
||
describe('Rate limiting', () => { | ||
const { baseUrl, projectId, httpClient } = getTestSetup(); | ||
|
||
it('Simulate flood and check is rate limited', async () => { | ||
// Using default max tokens of 100 | ||
const max_tokens = 100; | ||
|
||
// Flooding requests twice then max tokens | ||
const requests_to_send = max_tokens * 2; | ||
|
||
// Sending flood requests to the generators endpoint since it's not dependent on the third parties | ||
const payload = { | ||
partnerUserId: 'someUserID', | ||
destinationWallets:[{ address: '0x1234567890123456789012345678901234567890' }], | ||
}; | ||
const promises = []; | ||
for (let i = 0; i < requests_to_send; i++) { | ||
promises.push( | ||
httpClient.post(`${baseUrl}/v1/generators/onrampurl?projectId=${projectId}`, payload) | ||
); | ||
} | ||
const results = await Promise.allSettled(promises); | ||
|
||
let ok_statuses_counter = 0; | ||
let rate_limited_statuses_counter = 0; | ||
results.forEach((result) => { | ||
if (result.status === 'fulfilled' && result.value.status === 429) { | ||
rate_limited_statuses_counter++; | ||
}else if (result.status === 'fulfilled' && result.value.status === 200) { | ||
ok_statuses_counter++; | ||
} | ||
}); | ||
|
||
console.log(`➜ Rate limited statuses: ${rate_limited_statuses_counter} out of ${requests_to_send} total requests.`); | ||
|
||
// Check if there are any successful and rate limited statuses | ||
expect(ok_statuses_counter).toBeGreaterThan(0); | ||
expect(rate_limited_statuses_counter).toBeGreaterThan(0); | ||
}) | ||
|
||
it('Flood below max tokens and check is NOT rate limited', async () => { | ||
// Using default max tokens of 100 | ||
const max_tokens = 100; | ||
|
||
// Sending flood requests to the endpoint other then in first test case | ||
// since the key is composite with the matched endpoint | ||
const promises = []; | ||
for (let i = 0; i < max_tokens; i++) { | ||
promises.push( | ||
httpClient.get(`${baseUrl}/health`) | ||
); | ||
} | ||
const results = await Promise.allSettled(promises); | ||
|
||
let ok_statuses_counter = 0; | ||
let rate_limited_statuses_counter = 0; | ||
results.forEach((result) => { | ||
if (result.status === 'fulfilled' && result.value.status === 429) { | ||
rate_limited_statuses_counter++; | ||
}else if (result.status === 'fulfilled' && result.value.status === 200) { | ||
ok_statuses_counter++; | ||
} | ||
}); | ||
|
||
console.log(`➜ Rate limited statuses: ${rate_limited_statuses_counter} out of ${max_tokens} total requests.`); | ||
|
||
// Check if there are no any rate limited statuses | ||
expect(ok_statuses_counter).toBe(max_tokens); | ||
expect(rate_limited_statuses_counter).toBe(0); | ||
}) | ||
}) |