PHP code example of race-lab / laravel-race-lab
1. Go to this page and download the library: Download race-lab/laravel-race-lab library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
race-lab / laravel-race-lab example snippets
use RaceLab\LaravelRaceLab\Scenario\QueryPoint;
it('does not lose concurrent wallet increments', function () {
$walletId = Wallet::factory()->create(['balance' => 100])->id;
$result = race('wallet increment is atomic')
->worker('A', fn () => app(WalletService::class)->add($walletId, 50))
->worker('B', fn () => app(WalletService::class)->add($walletId, 100))
->barrier(
'wallet-read',
QueryPoint::after()->select()->table('wallets')->oncePerWorker()
)
->run();
$result->assertAllSucceeded();
expect(Wallet::find($walletId)->balance)->toBe('250.00');
});
$result = race('withdrawal cannot be approved twice')
->workers(2)
->barrier(
'pending-read',
QueryPoint::after()->select()->table('withdrawals')
)
->run(function () use ($withdrawalId) {
app(WithdrawalService::class)->approve($withdrawalId);
});
$result
->assertExactlyOneSucceeded()
->assertNoTimeouts();
$result->assertDatabaseCount('wallet_transactions', 1);
$result = race('controlled write order')
->worker('A', fn () => app(Service::class)->work($id, 'A'))
->worker('B', fn () => app(Service::class)->work($id, 'B'))
->gate('before-write', QueryPoint::before()->update()->table('wallets'))
->barrier(
'a-write-finished',
QueryPoint::after()->update()->table('wallets'),
workers: ['A']
)
->schedule(function ($schedule) {
$schedule
->waitFor('before-write', ['A', 'B'])
->release('before-write', 'A')
->waitFor('a-write-finished', ['A']) // proves A actually wrote
->release('before-write', 'B');
})
->run();
$run = race('scenario')
->worker('A', $a)
->worker('B', $b)
->gate('read', QueryPoint::after()->select()->table('wallets'))
->runAsync();
$run->waitFor('read', ['A', 'B']);
$run->release('read', ['A']);
$run->release('read', ['B']);
$result = $run->join();
use RaceLab\LaravelRaceLab\Support\RaceLab;
// production code: no-op unless a Race Lab worker is active
RaceLab::checkpoint('eligibility-checked');
race('check then act')
->workers(2)
->manualBarrier('eligibility-checked')
->run(fn () => app(Service::class)->execute($id));
use RaceLab\LaravelRaceLab\Scenario\TransactionPoint;
->barrier('tx-open', TransactionPoint::begin())
->gate('before-commit', TransactionPoint::committing())
QueryPoint::before()
->connection('mysql')
->update()
->table('wallets')
->contains('balance')
->hit(2)
->oncePerWorker();
QueryPoint::after()->sqlRegex('/select .* for update/i');
$result->assertAllSucceeded();
$result->assertExactlyOneSucceeded();
$result->assertExactlyOneFailed();
$result->assertWorkerSucceeded('A');
$result->assertWorkerFailed('B');
$result->assertNoTimeouts();
$result->assertNoDeadlocks();
$result->assertExceptionCount(DuplicatePaymentException::class, 1);
$result->assertDatabaseCount('payment_attempts', 1);
$result->assertDatabaseHas('withdrawals', ['id' => $id, 'status' => 'approved']);
$result->assertInvariant(fn () => Wallet::find($id)->balance === '250.00');
echo $result->report();
bash
php artisan race-lab:replay storage/framework/race-lab/<run-id>
bash
php artisan race-lab:doctor
php artisan race-lab:replay <run-directory-or-plan.json>