question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Comments:32 (21 by maintainers)

github_iconTop GitHub Comments

4reactions
dubzzzcommented, Mar 15, 2022

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.

2reactions
dubzzzcommented, Mar 13, 2022

You probably want to try with:

    it('works', async () => {
+++        await fc.assert(
---        fc.assert(
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found