Concurrency   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testConcurrency() 0 49 8
A getConcurrencyQueueSize() 0 4 1
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.');
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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.');
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
49
50
        $workload = serialize(self::getHandler());
51
        for ($i = 1; $i <= $queueSize; $i++) {
52
            $this->queue->push($i);
0 ignored issues
show
Bug introduced by
The property queue does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class GearmanException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
60
            $result = false;
61
        }
62
63
        if (!$result) {
64
            $this->markTestSkipped('Unable to run gearman tasks. Check if gearman server is running.');
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
65
        }
66
67
        $this->assertEquals($queueSize, count($poppedItems));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
68
        $this->assertGreaterThan(1, count($workerIds), 'Not enough workers to test concurrency.');
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
69
    }
70
71
    protected function getConcurrencyQueueSize()
72
    {
73
        return (int) getenv('PHIVE_CONCUR_QUEUE_SIZE');
74
    }
75
}
76