src/Symfony/Component/Lock/Tests/Store/BlockingStoreTestTrait.php
/**
     * Tests blocking locks thanks to pcntl.
     *
     * This test is time sensible: the $clockDelay could be adjust.
     *
     * @requires extension pcntl
     */
Tests blocking locks thanks to pcntl.
This test is time sensible: the $clockDelay could be adjust.

@requires extension pcntl
Tests blocking locks thanks to pcntl . This test is time sensible : the $clockDelay could be adjust . @requires extension pcntl
public function testBlockingLocks()
    {
        $clockDelay = 50000;
        $store = $this->getStore();
        $key = new Key(uniqid(__METHOD__, true));
        if ($childPID1 = pcntl_fork()) {
            usleep(2 * $clockDelay);
            try {
                $store->save($key);
                $this->fail('The store saves a locked key.');
            } catch (LockConflictedException $e) {
            }
            $store->waitAndSave($key);
            $this->assertTrue($store->exists($key));
            $store->delete($key);
            pcntl_waitpid($childPID1, $status1);
            $this->assertSame(0, pcntl_wexitstatus($status1), 'The child process couldn\'t lock the resource');
        } else {
            try {
                $store->save($key);
                usleep(3 * $clockDelay);
                $store->delete($key);
                exit(0);
            } catch (\Exception $e) {
                exit(1);
            }
        }
    }
public function testBlockingLocks ( ) { $ clockDelay = 50000 ; $ store = $ this -> getStore ( ) ; $ key = new Key ( uniqid ( __METHOD__ , true ) ) ; if ( $ childPID1 = pcntl_fork ( ) ) { usleep ( 2 * $ clockDelay ) ; try { $ store -> save ( $ key ) ; $ this -> fail ( 'The store saves a locked key.' ) ; } catch ( LockConflictedException $ e ) { } $ store -> waitAndSave ( $ key ) ; $ this -> assertTrue ( $ store -> exists ( $ key ) ) ; $ store -> delete ( $ key ) ; pcntl_waitpid ( $ childPID1 , $ status1 ) ; $ this -> assertSame ( 0 , pcntl_wexitstatus ( $ status1 ) , 'The child process couldn\'t lock the resource' ) ; } else { try { $ store -> save ( $ key ) ; usleep ( 3 * $ clockDelay ) ; $ store -> delete ( $ key ) ; exit ( 0 ) ; } catch ( \ Exception $ e ) { exit ( 1 ) ; } } }
Tests blocking locks thanks to pcntl.
Tests blocking locks thanks to pcntl .
// Amount a microsecond used to order async actions /** @var StoreInterface $store */ // give time to fork to start // This call should failed given the lock should already by acquired by the child #1 // This call should be blocked by the child #1 // Now, assert the child process worked well // Wait 3 ClockDelay to let parent process to finish