'How do I test race conditions and concurrency using fast-check?

💬 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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source