Skip to content

Commit ff95169

Browse files
committed
Fix lock failling test
1 parent 01d1563 commit ff95169

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract protected function getStore();
3535
public function testBlockingLocks()
3636
{
3737
// Amount a microsecond used to order async actions
38-
$clockDelay = 200000;
38+
$clockDelay = 50000;
3939

4040
if (\PHP_VERSION_ID < 50600 || defined('HHVM_VERSION_ID')) {
4141
$this->markTestSkipped('The PHP engine does not keep resource in child forks');
@@ -46,31 +46,46 @@ public function testBlockingLocks()
4646
/** @var StoreInterface $store */
4747
$store = $this->getStore();
4848
$key = new Key(uniqid(__METHOD__, true));
49+
$parentPID = posix_getpid();
4950

50-
if ($childPID1 = pcntl_fork()) {
51-
// give time to fork to start
52-
usleep(1 * $clockDelay);
51+
// Block SIGHUP signal
52+
pcntl_sigprocmask(SIG_BLOCK, array(SIGHUP));
53+
54+
if ($childPID = pcntl_fork()) {
55+
// Wait the start of the child
56+
pcntl_sigwaitinfo(array(SIGHUP), $info);
5357

5458
try {
55-
// This call should failed given the lock should already by acquired by the child #1
59+
// This call should failed given the lock should already by acquired by the child
5660
$store->save($key);
5761
$this->fail('The store saves a locked key.');
5862
} catch (LockConflictedException $e) {
5963
}
6064

65+
// send the ready signal to the child
66+
posix_kill($childPID, SIGHUP);
67+
6168
// This call should be blocked by the child #1
6269
$store->waitAndSave($key);
6370
$this->assertTrue($store->exists($key));
6471
$store->delete($key);
6572

6673
// Now, assert the child process worked well
67-
pcntl_waitpid($childPID1, $status1);
74+
pcntl_waitpid($childPID, $status1);
6875
$this->assertSame(0, pcntl_wexitstatus($status1), 'The child process couldn\'t lock the resource');
6976
} else {
77+
// Block SIGHUP signal
78+
pcntl_sigprocmask(SIG_BLOCK, array(SIGHUP));
7079
try {
7180
$store->save($key);
72-
// Wait 2 ClockDelay to let parent process to finish
73-
usleep(2 * $clockDelay);
81+
// send the ready signal to the parent
82+
posix_kill($parentPID, SIGHUP);
83+
84+
// Wait for the parent to be ready
85+
pcntl_sigwaitinfo(array(SIGHUP), $info);
86+
87+
// Wait ClockDelay to let parent assert to finish
88+
usleep($clockDelay);
7489
$store->delete($key);
7590
exit(0);
7691
} catch (\Exception $e) {

0 commit comments

Comments
 (0)