Files
objecttothis 29a9b1a7e7 Bugfix: Resolve Race Condition in Rewards and Gift Card Spending (#4640)
* Implement atomic updates for gift card and reward point decrements, enhance error handling for insufficient balances, and add regression tests for concurrency safety.

* Add translations for insufficient gift card balance and reward points error messages across all supported languages.

* Reorder `clear_suspended_sale_detail` call to ensure transactional consistency.

* Reorder `clear_all` call to align with success and error handling logic.

* Ensure soft-deleted gift cards are excluded in balance updates.

* Refactor change_quantity logic with atomic upserts, improve error handling for insufficient stock, and update related tests and constants.

* Added check for NEW_ENTRY

* Added unit tests to test changes.

* Fix class name casing in ItemQuantityTest for consistency.

* Fix Bulgarian translations for insufficient balance error messages in Sales module.

* Fix Greek translations for insufficient balance error messages in Sales module.

* Fix Armenian translations for insufficient balance error messages in Sales module.

* Fix Tamil translations for insufficient balance error messages in Sales module.

* Implement race condition testing for database methods with concurrent process support.

* Fix class name casing in ItemTest for consistency.

* Improve concurrent process handling in race condition tests; add readiness and synchronization barriers.

* Improve handling of process I/O streams and timeout management in race condition tests.

* Add test for decrementing gift card value when marked as deleted

* Add `finally` block to ensure proper cleanup in async database race condition tests

* Improve error handling and timeout management in async database race condition tests.

* Refactor test utilities to use shared `EmployeeFixtureTrait` and `ItemFixtureTrait`.

* Track process exit codes explicitly in race condition tests for improved error detection and debugging.

* Improve error handling in `ConcurrentDbRaceTrait` by adding exceptions for `mysqli_poll` and `mysqli_reap_async_query`.

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>

---------

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-08-20 02:24:23 +04:00

39 lines
1.2 KiB
PHP

<?php
/**
* Standalone CLI worker used by ConcurrentDbRaceTrait::raceTwoProcesses().
* Bootstraps CodeIgniter in its own process so the model method call below
* runs on a genuinely separate DB connection from any sibling worker,
* exercising real concurrent access instead of simulating it sequentially.
*
* Usage: php RaceWorker.php <method> <arg> [<arg> ...]
*/
chdir(dirname(__DIR__, 2));
require __DIR__ . '/../../vendor/codeigniter4/framework/system/Test/bootstrap.php';
$method = $argv[1];
$args = array_slice($argv, 2);
$readyFile = getenv('RACE_READY_FILE');
$goFile = getenv('RACE_GO_FILE');
file_put_contents($readyFile, '1');
while (!file_exists($goFile)) {
usleep(1000);
}
$result = match ($method) {
'giftcard' => model(App\Models\Giftcard::class)
->decrementGiftcardValue((string) $args[0], (float) $args[1]),
'rewardPoints' => model(App\Models\Customer::class)
->adjustRewardPoints((int) $args[0], (float) $args[1]),
'itemQuantity' => model(App\Models\Item_quantity::class)
->changeQuantity((int) $args[0], (int) $args[1], (float) $args[2]),
default => throw new InvalidArgumentException("Unknown race method: {$method}"),
};
echo $result ? '1' : '0';