HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/tests/Unit/LockTest.php
<?php

use App\Library\Lockable;

// To test concurrency
// Step 1: Make sure sleep(x) with "x" is higher than value "v" of $waitTimeout(v)
// Step 2: Execute two tests in two console c1 (first) and c2 (later): ./vendor/bin/pest tests/Unit/LockTest.php
// Expected result: c1 shall take x seconds to finish
//                  c2 shall take v seconds to time out (with the "No exception" message;
test('Exclusive lock just works', function () {
    $file = '/tmp/gaugau';
    $task = function () {
        sleep(1);
        // Just do nothing here
    };
    $waitTimeout = 2;
    $waitTimeoutCallback = function () {
        // Do not throw an excpetion if a waitTimeoutCallback is present
        echo "No exception\n";
    };

    Lockable::withExclusiveLock($file, $task, $waitTimeout, $waitTimeoutCallback);

    expect(true)->toBe(true);
});