Completed
Push — parallel_test_fork ( b1fcc3...0461b5 )
by
unknown
33:11 queued 13:37
created

BaseParallelTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 43
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 15 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 (!in_array($dbms, ['mysql', 'postgresql'])) {
24
            $this->markTestSkipped('Parallel test require mysql or postgresql db');
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
        $connection->close();
45
46
        foreach ($list as $process) {
47
            $process->start();
48
        }
49
50
        foreach ($list as $process) {
51
            $process->wait();
52
        }
53
54
        $connection->connect();
55
    }
56
}
57