Skip to content

Commit 334b62a

Browse files
committed
minor #23847 [Lock] Fix/lock test (jderusse)
This PR was merged into the 3.4 branch. Discussion ---------- [Lock] Fix/lock test | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A Test on the Lock component often fail, the purpose of this PR is to fix it. The goal of this failling test is to assert that blocking locks works. To test the blocking mode, I had to create 2 concurent process and I used pcntl_fork for that. Sadly, travis takes more time to fork the process than configured TTL. This PR replace the previous "sleep" to let child and parent starts, by a pcntl communication between child and parent. | Process 1 | Process 2 | ----------------------- | ---------- | Wait N°2 | | | Acquire Lock | | Send signal to N°1 | Wake up | Wait N°1 | Try lock(non block) | | => assert failure | | Send signal to N°2 | | lock(block) | Wake up | .. | sleep 50 000ms | .. | .. | .. | Release lock | => assert Acquired | exit | Release lock | | => assert child exit 0 | Commits ------- ff95169 Fix lock failling test
2 parents a1faaa1 + ff95169 commit 334b62a

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

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

+23-8
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)