Completed
Push — 7.5 ( 13f815...467086 )
by André
19:24
created

BaseParallelTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 2
A addParallelProcess() 0 12 1
A runParallelProcesses() 0 16 3
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\API\Repository\Tests\Parallel;
10
11
use eZ\Publish\API\Repository\Tests\BaseTest;
12
use Jenner\SimpleFork\Process;
13
14
abstract class BaseParallelTestCase extends BaseTest
15
{
16
    protected function setUp(): void
17
    {
18
        parent::setUp();
19
20
        $connection = $this->getRawDatabaseConnection();
21
        $dbms = $connection->getDatabasePlatform()->getName();
22
23
        if ($dbms == 'sqlite') {
24
            $this->markTestSkipped('Can not run parallel test on sqlite');
25
        }
26
    }
27
28
    protected function addParallelProcess(ParallelProcessList $list, callable $callback): void
29
    {
30
        $connection = $this->getRawDatabaseConnection();
31
32
        $process = new Process(function () use ($callback, $connection) {
33
            $connection->connect();
34
            $callback();
35
            $connection->close();
36
        });
37
38
        $list->addProcess($process);
39
    }
40
41
    protected function runParallelProcesses(ParallelProcessList $list): void
42
    {
43
        $connection = $this->getRawDatabaseConnection();
44
        // @see https://www.php.net/manual/en/function.pcntl-fork.php#70721
45
        $connection->close();
46
47
        foreach ($list as $process) {
48
            $process->start();
49
        }
50
51
        foreach ($list as $process) {
52
            $process->wait();
53
        }
54
55
        $connection->connect();
56
    }
57
}
58