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

BaseParallelTestCase::addParallelProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
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