minor #22121 [Lock] Simplify BlockingTest (jderusse)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Lock] Simplify BlockingTest

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

7130dd8296 Simplify BlockingTest
This commit is contained in:
Fabien Potencier 2017-04-02 19:33:59 +02:00
commit ae7bcaeb96
2 changed files with 20 additions and 34 deletions

View File

@ -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);
}
}
} else {
usleep(1 * $clockDelay);
try {
// This call should be block by the child #1
$store->waitAndSave($key);
$this->assertTrue($store->exists($key));
$store->delete($key);
exit(0);
} catch (\Exception $e) {
exit(2);
}
try {
// This call should failed given the lock should already by acquired by the child #1
$store->save($key);
$this->fail('The store saves a locked key.');
} catch (LockConflictedException $e) {
}
// This call should be blocked by the child #1
$store->waitAndSave($key);
$this->assertTrue($store->exists($key));
$store->delete($key);
// 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);

View File

@ -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));
}
}