Issue
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.
Solution
The answer to this can be found in this PR: https://github.com/dubzzz/fast-check/issues/2780
Answered By - devio
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.