@@ -35,7 +35,7 @@ abstract protected function getStore();
35
35
public function testBlockingLocks ()
36
36
{
37
37
// Amount a microsecond used to order async actions
38
- $ clockDelay = 200000 ;
38
+ $ clockDelay = 50000 ;
39
39
40
40
if (\PHP_VERSION_ID < 50600 || defined ('HHVM_VERSION_ID ' )) {
41
41
$ this ->markTestSkipped ('The PHP engine does not keep resource in child forks ' );
@@ -46,31 +46,46 @@ public function testBlockingLocks()
46
46
/** @var StoreInterface $store */
47
47
$ store = $ this ->getStore ();
48
48
$ key = new Key (uniqid (__METHOD__ , true ));
49
+ $ parentPID = posix_getpid ();
49
50
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 );
53
57
54
58
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
56
60
$ store ->save ($ key );
57
61
$ this ->fail ('The store saves a locked key. ' );
58
62
} catch (LockConflictedException $ e ) {
59
63
}
60
64
65
+ // send the ready signal to the child
66
+ posix_kill ($ childPID , SIGHUP );
67
+
61
68
// This call should be blocked by the child #1
62
69
$ store ->waitAndSave ($ key );
63
70
$ this ->assertTrue ($ store ->exists ($ key ));
64
71
$ store ->delete ($ key );
65
72
66
73
// Now, assert the child process worked well
67
- pcntl_waitpid ($ childPID1 , $ status1 );
74
+ pcntl_waitpid ($ childPID , $ status1 );
68
75
$ this ->assertSame (0 , pcntl_wexitstatus ($ status1 ), 'The child process couldn \'t lock the resource ' );
69
76
} else {
77
+ // Block SIGHUP signal
78
+ pcntl_sigprocmask (SIG_BLOCK , array (SIGHUP ));
70
79
try {
71
80
$ 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 );
74
89
$ store ->delete ($ key );
75
90
exit (0 );
76
91
} catch (\Exception $ e ) {
0 commit comments