-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSleepMulti.php
96 lines (79 loc) · 2.74 KB
/
TestSleepMulti.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* JBZoo Toolbox - Cli.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Cli
*/
declare(strict_types=1);
namespace JBZoo\PHPUnit\TestApp\Commands;
use JBZoo\Cli\CliCommandMultiProc;
use JBZoo\Cli\Exception;
use JBZoo\Cli\OutLvl;
use JBZoo\Utils\Env;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use function JBZoo\Data\json;
class TestSleepMulti extends CliCommandMultiProc
{
/**
* {@inheritDoc}
*/
protected function configure(): void
{
$this
->setName('test:sleep-multi')
->addOption('sleep', null, InputOption::VALUE_REQUIRED, 'Timeout in seconds to sleep the PHP script', 5)
->addOption('random-sleep', null, InputOption::VALUE_NONE, 'Random sleep timer (1-5)')
->addArgument('arg-1', InputArgument::OPTIONAL, 'Custom Agrument #1', 'QWERTY-1')
->addArgument('arg-2', InputArgument::OPTIONAL, 'Custom Agrument #2', 'QWERTY-2')
->addArgument('arg-3', InputArgument::OPTIONAL, 'Custom Agrument #3', 'QWERTY-3');
parent::configure();
}
protected function executeOneProcess(string $pmThreadId): int
{
$sleep = $this->getOptInt('sleep');
if ($this->getOptBool('random-sleep')) {
$sleep = \random_int(1, 5);
}
if ($sleep === 2 && $pmThreadId === '2') {
throw new Exception('Exception messsage');
}
$this->_([
"Started: {$pmThreadId}",
'Sleep : ' . $sleep,
'Arg #1: ' . $this->outputMode->getInput()->getArgument('arg-1'),
'Arg #2: ' . $this->outputMode->getInput()->getArgument('arg-2'),
'Arg #3: ' . $this->outputMode->getInput()->getArgument('arg-3'),
'Env Var: ' . Env::string('JBZOO_TEST_VAR'),
]);
\sleep($sleep);
$this->_("Finished: {$pmThreadId}", OutLvl::Q);
return 0;
}
/**
* @return string[]
*/
protected function getListOfChildIds(): array
{
return ['1', '2', '3', '4', '5'];
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @phan-suppress PhanPluginPossiblyStaticProtectedMethod
* @phan-suppress PhanUnusedProtectedNoOverrideMethodParameter
*/
protected function afterFinishAllProcesses(array $procPool): void
{
$result = [];
foreach ($procPool as $procInfo) {
$result[] = $procInfo['std_out'];
}
$this->_((string)json($result));
}
}