How do I test concurrency for an API?
See original GitHub issue💬 Question and Help
I am developing a booking app and I want to use the fast-check
library to simulate race conditions in my API. Basically, I want to simulate the use case where many users would call the same API to book the same table at the same time :
Suppose this is my API : POST /book
with a request body like :
{
"table": 1,
"time": "20:00"
}
My koa
API looks something like this:
router
.post('/book', (ctx) => {
const { table, time } = ctx.request.body;
const { isBooked } = await checkIfAvailable(table,time).body; // calls MongoDB
if(!isBooked){
ctx.body = {
table,
time,
booked: true
};
} else {
ctx.throw("This table is already booked", 404)
}
})
My test looks like this :
mockedCheckIfAvailable.mockImplementation(
s.scheduleFunction(async (table,time) => {
return booking;
}),
);
const res = await request
.post('/book')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(booking);
expect(res).toBe(true);
I read the Race Conditions docs but I can’t get around how to apply it.
Issue Analytics
- State:
- Created 2 years ago
- Comments:32 (21 by maintainers)
Top Results From Across the Web
Test concurrent requests to a REST based web server
This is a great tool for testing REST APIs. In this example: "-c 100" means 100 concurrent requests and. "-n 100" means 100...
Read more >How can I simulate 100 concurrent API calls to test my REST ...
1.Use Jmeter or Soap UI. Load Testing Overview | Load Testing.
Read more >Concurrent testing of REST APIs with ScalaTest - LinkedIn
Concurrent testing made it easy. Your concurrent test, as in ConcurrentEntitiesTest looks like · Indefinite test data from finite configured test ...
Read more >What is concurrency testing? - Educative.io
Concurrency testing is a type of software testing that checks the performance of software when multiple users are logged in and perform actions ......
Read more >REST API Testing Strategy: What Exactly Should You Test?
Here's a technical look at how we test our APIs. ... Small concurrency tests – concurrent API calls that write to the same...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Now that I have a clearer idea of what caused the issue, I will certainly add another wait* onto the instance of scheduler. Something like:
waitFor
that would be able to wait for a promise and unlock potential dependencies it can rely on as they appear. In your case, it would be use to wrap the request.You probably want to try with: