1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aimeos\Base\Process; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class PcntlTest extends \PHPUnit\Framework\TestCase |
7
|
|
|
{ |
8
|
|
|
protected function setUp() : void |
9
|
|
|
{ |
10
|
|
|
if( function_exists( 'pcntl_fork' ) === false ) { |
11
|
|
|
$this->markTestSkipped( 'PCNTL extension not available' ); |
12
|
|
|
} |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public function testClone() |
17
|
|
|
{ |
18
|
|
|
$object = new \Aimeos\Base\Process\Pcntl(); |
19
|
|
|
$this->assertNotSame( $object, clone $object ); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
public function testExec() |
24
|
|
|
{ |
25
|
|
|
$fcn = function() {}; |
26
|
|
|
$result = $this->access( 'exec' )->invokeArgs( new \Aimeos\Base\Process\Pcntl(), [$fcn, []] ); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function testExecException() |
31
|
|
|
{ |
32
|
|
|
$fcn = function() { |
33
|
|
|
throw new \RuntimeException(); |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
$result = $this->access( 'exec' )->invokeArgs( new \Aimeos\Base\Process\Pcntl(), [$fcn, []] ); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public function testIsAvailable() |
41
|
|
|
{ |
42
|
|
|
$object = new \Aimeos\Base\Process\Pcntl(); |
43
|
|
|
$this->assertTrue( $object->isAvailable() ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function testRun() |
48
|
|
|
{ |
49
|
|
|
$object = new \Aimeos\Base\Process\Pcntl( 1 ); |
50
|
|
|
$fcn = function() { sleep( 1 ); }; |
51
|
|
|
|
52
|
|
|
$start = microtime( true ); |
53
|
|
|
$return = $object->start( $fcn, [\TestHelper::getConfig()] )->start( $fcn, [] )->wait(); |
54
|
|
|
$msec = ( microtime( true ) - $start ); |
55
|
|
|
|
56
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Process\Iface::class, $return ); |
57
|
|
|
$this->assertGreaterThan( 1, $msec ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
public function testRunError() |
62
|
|
|
{ |
63
|
|
|
$fcn = function() { throw new \Exception(); }; |
64
|
|
|
|
65
|
|
|
stream_filter_register( "redirect", "\Aimeos\Base\Process\DiscardFilter" ); |
66
|
|
|
$filter = stream_filter_prepend( STDERR, "redirect", STREAM_FILTER_WRITE ); |
67
|
|
|
|
68
|
|
|
$object = new \Aimeos\Base\Process\Pcntl(); |
69
|
|
|
$result = $object->start( $fcn, [], true )->wait(); |
70
|
|
|
|
71
|
|
|
stream_filter_remove( $filter ); |
72
|
|
|
|
73
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Process\Iface::class, $result ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
protected function access( $name ) |
78
|
|
|
{ |
79
|
|
|
$class = new \ReflectionClass( \Aimeos\Base\Process\Pcntl::class ); |
80
|
|
|
$method = $class->getMethod( $name ); |
81
|
|
|
$method->setAccessible( true ); |
82
|
|
|
|
83
|
|
|
return $method; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
class DiscardFilter extends \php_user_filter |
90
|
|
|
{ |
91
|
|
|
public function filter( $in, $out, &$consumed, $closing ) : int |
92
|
|
|
{ |
93
|
|
|
while( $bucket = stream_bucket_make_writeable( $in ) ) |
94
|
|
|
{ |
95
|
|
|
$bucket->data = ''; |
96
|
|
|
$consumed += $bucket->datalen; |
97
|
|
|
stream_bucket_append( $out, $bucket ); |
98
|
|
|
} |
99
|
|
|
return PSFS_PASS_ON; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|