dimanche 7 mars 2021

Laravel - how to run the same PHP unit test n times to all start concurrently (testing for race conditions)

I am using a transaction accompanied by updateOrCreate(). As you can infer, things could go awry if multiple connections access and update the same id, a race condition in writing into the DB (horrible!).

I am also locking that specific row that is being dealt with in my model. My abbreviated code looks as follows in the file writing to the DB:

DB::beginTransaction();

DB::table('myTable')->where('my_id', $cells[0])->lockForUpdate()->get();

$outcome = Outcome::updateOrCreate([
    'my_id' => $cells[0]
    ],
    [
        'etc',$cells[1],
        'etc-etc',$cells[2],
    ]);

DB::commit();

Please let me know if this is true: As far as I understood, I am getting a lock for that specific row so transaction n is on a queue awaiting transaction n - 1 to end, sequentially.

I would like to know how to test this. Do I run multiples of the same TestCase? If so, how? Or maybe there is another way to achieve this kind of concurrent test?

Aucun commentaire:

Enregistrer un commentaire