Testing concurrency with Typescript/JS

I’m working on a TS application and I need to test some concurrent code and how it interacts with a database (the code deals with idepotency). I want to verify that multiple requests that modify the same resource would apply the changes correctly or fail, rather than experience lost updates or processing them twice.

I quite a lot of experience testing this in Java (as it has a ton of concurrency primitives like semaphores, barriers, and other things to control concurrency and parallelism), but I’m struggling to figure out how to do this in TS.

For example, in Java I would start 2 threads passing a CyclicBarrier with 2 parties. Both threads would execute up to the barrier, wait for eachother, and then continue. In this way I could often ensure that the code worked as expected. If I couldn’t ensure 100% that the code would be run in parallel, I would run the same time multiple times or with more threads to increase the chances of parallel execution.

I cannot find any concurrency primitives in JS that would allow me to do something similar. The best I’ve found is to do a busy wait on a shared value. Some blogs suggest to use worker threads.

Any suggestions how this is usually done? This is a difficult topic, so links to blogs, articles & books are appreciated!