Simplify BlockingTest
This commit is contained in:
parent
e3d99649aa
commit
7130dd8296
@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\Component\Lock\Tests\Store;
|
||||
|
||||
use Symfony\Component\Lock\Exception\LockConflictedException;
|
||||
use Symfony\Component\Lock\Key;
|
||||
use Symfony\Component\Lock\StoreInterface;
|
||||
|
||||
@ -47,43 +48,28 @@ trait BlockingStoreTestTrait
|
||||
$key = new Key(uniqid(__METHOD__, true));
|
||||
|
||||
if ($childPID1 = pcntl_fork()) {
|
||||
if ($childPID2 = pcntl_fork()) {
|
||||
if ($childPID3 = pcntl_fork()) {
|
||||
// This is the parent, wait for the end of child process to assert their results
|
||||
pcntl_waitpid($childPID1, $status1);
|
||||
pcntl_waitpid($childPID2, $status2);
|
||||
pcntl_waitpid($childPID3, $status3);
|
||||
$this->assertSame(0, pcntl_wexitstatus($status1));
|
||||
$this->assertSame(0, pcntl_wexitstatus($status2));
|
||||
$this->assertSame(3, pcntl_wexitstatus($status3));
|
||||
} else {
|
||||
usleep(1 * $clockDelay);
|
||||
// give time to fork to start
|
||||
usleep(2 * $clockDelay);
|
||||
|
||||
try {
|
||||
// This call should failed given the lock should already by acquired by the child #1
|
||||
$store->save($key);
|
||||
exit(0);
|
||||
} catch (\Exception $e) {
|
||||
exit(3);
|
||||
$this->fail('The store saves a locked key.');
|
||||
} catch (LockConflictedException $e) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
usleep(1 * $clockDelay);
|
||||
|
||||
try {
|
||||
// This call should be block by the child #1
|
||||
// This call should be blocked by the child #1
|
||||
$store->waitAndSave($key);
|
||||
$this->assertTrue($store->exists($key));
|
||||
$store->delete($key);
|
||||
exit(0);
|
||||
} catch (\Exception $e) {
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
// Now, assert the child process worked well
|
||||
pcntl_waitpid($childPID1, $status1);
|
||||
$this->assertSame(0, pcntl_wexitstatus($status1), 'The child process couldn\'t lock the resource');
|
||||
} else {
|
||||
try {
|
||||
$store->save($key);
|
||||
// Wait 3 ClockDelay to let other child to be initialized
|
||||
// Wait 3 ClockDelay to let parent process to finish
|
||||
usleep(3 * $clockDelay);
|
||||
$store->delete($key);
|
||||
exit(0);
|
||||
|
@ -20,7 +20,7 @@ use Symfony\Component\Lock\StoreInterface;
|
||||
trait ExpiringStoreTestTrait
|
||||
{
|
||||
/**
|
||||
* Amount a microsecond used to order async actions
|
||||
* Amount a microsecond used to order async actions.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@ -72,7 +72,7 @@ trait ExpiringStoreTestTrait
|
||||
$store->putOffExpiration($key, 1.0 * $clockDelay / 1000000);
|
||||
$this->assertTrue($store->exists($key));
|
||||
|
||||
usleep(1.5 * $clockDelay);
|
||||
usleep(2.1 * $clockDelay);
|
||||
$this->assertFalse($store->exists($key));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user