Skip to content

Commit ae7bcae

Browse files
committed
minor symfony#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 ------- 7130dd8 Simplify BlockingTest
2 parents 365716d + 7130dd8 commit ae7bcae

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

src/Symfony/Component/Lock/Tests/Store/BlockingStoreTestTrait.php

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Symfony\Component\Lock\Exception\LockConflictedException;
1415
use Symfony\Component\Lock\Key;
1516
use Symfony\Component\Lock\StoreInterface;
1617

@@ -47,43 +48,28 @@ public function testBlockingLocks()
4748
$key = new Key(uniqid(__METHOD__, true));
4849

4950
if ($childPID1 = pcntl_fork()) {
50-
if ($childPID2 = pcntl_fork()) {
51-
if ($childPID3 = pcntl_fork()) {
52-
// This is the parent, wait for the end of child process to assert their results
53-
pcntl_waitpid($childPID1, $status1);
54-
pcntl_waitpid($childPID2, $status2);
55-
pcntl_waitpid($childPID3, $status3);
56-
$this->assertSame(0, pcntl_wexitstatus($status1));
57-
$this->assertSame(0, pcntl_wexitstatus($status2));
58-
$this->assertSame(3, pcntl_wexitstatus($status3));
59-
} else {
60-
usleep(1 * $clockDelay);
51+
// give time to fork to start
52+
usleep(2 * $clockDelay);
6153

62-
try {
63-
// This call should failed given the lock should already by acquired by the child #1
64-
$store->save($key);
65-
exit(0);
66-
} catch (\Exception $e) {
67-
exit(3);
68-
}
69-
}
70-
} else {
71-
usleep(1 * $clockDelay);
72-
73-
try {
74-
// This call should be block by the child #1
75-
$store->waitAndSave($key);
76-
$this->assertTrue($store->exists($key));
77-
$store->delete($key);
78-
exit(0);
79-
} catch (\Exception $e) {
80-
exit(2);
81-
}
54+
try {
55+
// This call should failed given the lock should already by acquired by the child #1
56+
$store->save($key);
57+
$this->fail('The store saves a locked key.');
58+
} catch (LockConflictedException $e) {
8259
}
60+
61+
// This call should be blocked by the child #1
62+
$store->waitAndSave($key);
63+
$this->assertTrue($store->exists($key));
64+
$store->delete($key);
65+
66+
// Now, assert the child process worked well
67+
pcntl_waitpid($childPID1, $status1);
68+
$this->assertSame(0, pcntl_wexitstatus($status1), 'The child process couldn\'t lock the resource');
8369
} else {
8470
try {
8571
$store->save($key);
86-
// Wait 3 ClockDelay to let other child to be initialized
72+
// Wait 3 ClockDelay to let parent process to finish
8773
usleep(3 * $clockDelay);
8874
$store->delete($key);
8975
exit(0);

src/Symfony/Component/Lock/Tests/Store/ExpiringStoreTestTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
trait ExpiringStoreTestTrait
2121
{
2222
/**
23-
* Amount a microsecond used to order async actions
23+
* Amount a microsecond used to order async actions.
2424
*
2525
* @return int
2626
*/
@@ -72,7 +72,7 @@ public function testRefreshLock()
7272
$store->putOffExpiration($key, 1.0 * $clockDelay / 1000000);
7373
$this->assertTrue($store->exists($key));
7474

75-
usleep(1.5 * $clockDelay);
75+
usleep(2.1 * $clockDelay);
7676
$this->assertFalse($store->exists($key));
7777
}
7878
}

0 commit comments

Comments
 (0)