|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Lock\Tests\Store;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Lock\Exception\LockConflictedException; |
14 | 15 | use Symfony\Component\Lock\Key;
|
15 | 16 | use Symfony\Component\Lock\StoreInterface;
|
16 | 17 |
|
@@ -47,43 +48,28 @@ public function testBlockingLocks()
|
47 | 48 | $key = new Key(uniqid(__METHOD__, true));
|
48 | 49 |
|
49 | 50 | 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); |
61 | 53 |
|
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) { |
82 | 59 | }
|
| 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'); |
83 | 69 | } else {
|
84 | 70 | try {
|
85 | 71 | $store->save($key);
|
86 |
| - // Wait 3 ClockDelay to let other child to be initialized |
| 72 | + // Wait 3 ClockDelay to let parent process to finish |
87 | 73 | usleep(3 * $clockDelay);
|
88 | 74 | $store->delete($key);
|
89 | 75 | exit(0);
|
|
0 commit comments