|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Phive Queue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Phive\Queue\Tests\Queue; |
|
13
|
|
|
|
|
14
|
|
|
trait Concurrency |
|
15
|
|
|
{ |
|
16
|
|
|
use Persistence; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @group concurrency |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testConcurrency() |
|
22
|
|
|
{ |
|
23
|
|
|
if (!class_exists('GearmanClient', false)) { |
|
24
|
|
|
$this->markTestSkipped('pecl/gearman is required for this test to run.'); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$client = new \GearmanClient(); |
|
28
|
|
|
$client->addServer(); |
|
29
|
|
|
|
|
30
|
|
|
$workerIds = []; |
|
31
|
|
|
$poppedItems = []; |
|
32
|
|
|
$client->setCompleteCallback(function (\GearmanTask $task) use (&$workerIds, &$poppedItems) { |
|
33
|
|
|
$data = explode(':', $task->data(), 2); |
|
34
|
|
|
if (!is_array($data) || 2 !== count($data)) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
list($workerId, $item) = $data; |
|
39
|
|
|
|
|
40
|
|
|
$workerIds[$workerId] = true; |
|
41
|
|
|
|
|
42
|
|
|
if (!isset($poppedItems[$item])) { |
|
43
|
|
|
$poppedItems[$item] = true; |
|
44
|
|
|
} |
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
$queueSize = $this->getConcurrencyQueueSize(); |
|
48
|
|
|
$this->assertGreaterThan(10, $queueSize, 'Queue size is too small to test concurrency.'); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$workload = serialize(self::getHandler()); |
|
51
|
|
|
for ($i = 1; $i <= $queueSize; $i++) { |
|
52
|
|
|
$this->queue->push($i); |
|
|
|
|
|
|
53
|
|
|
$client->addTask('pop', $workload); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
|
|
// run the tasks in parallel (assuming multiple workers) |
|
58
|
|
|
$result = $client->runTasks(); |
|
59
|
|
|
} catch (\GearmanException $e) { |
|
|
|
|
|
|
60
|
|
|
$result = false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!$result) { |
|
64
|
|
|
$this->markTestSkipped('Unable to run gearman tasks. Check if gearman server is running.'); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals($queueSize, count($poppedItems)); |
|
|
|
|
|
|
68
|
|
|
$this->assertGreaterThan(1, count($workerIds), 'Not enough workers to test concurrency.'); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function getConcurrencyQueueSize() |
|
72
|
|
|
{ |
|
73
|
|
|
return (int) getenv('PHIVE_CONCUR_QUEUE_SIZE'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.