|
1
|
|
|
<?php |
|
2
|
|
|
namespace Slince\Process\Tests\Pipe; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use Slince\Process\Pipe\DuplexFifo; |
|
6
|
|
|
use Slince\Process\Pipe\ReadableFifo; |
|
7
|
|
|
use Slince\Process\Pipe\WritableFifo; |
|
8
|
|
|
use Slince\Process\Tests\Utils; |
|
9
|
|
|
|
|
10
|
|
|
class DuplexFifoTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
protected $lastPd; |
|
13
|
|
|
|
|
14
|
|
|
public function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
file_exists('/tmp/test1.pipe') && unlink('/tmp/test1.pipe'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function testRead() |
|
20
|
|
|
{ |
|
21
|
|
|
$pathname = '/tmp/test1.pipe'; |
|
22
|
|
|
$nativeFifo = Utils::makeNativeWriteFifo($pathname); |
|
23
|
|
|
fwrite($nativeFifo, 'hello'); |
|
24
|
|
|
|
|
25
|
|
|
$fifo = new DuplexFifo($pathname); |
|
26
|
|
|
$this->assertEquals('hello', $fifo->read()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testNonBlockingRead() |
|
30
|
|
|
{ |
|
31
|
|
|
$pathname = '/tmp/test1.pipe'; |
|
32
|
|
|
$this->syncExecute(sprintf("php %s %s %d", __DIR__ . '/WriteFifo.php', 'hello', 1)); |
|
33
|
|
|
$fifo = new DuplexFifo($pathname, false); |
|
34
|
|
|
$this->assertEmpty($fifo->read()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testBlockingRead() |
|
38
|
|
|
{ |
|
39
|
|
|
$pathname = '/tmp/test1.pipe'; |
|
40
|
|
|
$this->syncExecute(sprintf("php %s %s %d", __DIR__ . '/WriteFifo.php', 'hello', 1)); |
|
41
|
|
|
$fifo = new DuplexFifo($pathname, true); |
|
42
|
|
|
$this->assertEquals('hello', $fifo->read()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testNonBlockingWrite() |
|
46
|
|
|
{ |
|
47
|
|
|
$fifo = new DuplexFifo('/tmp/test2.pipe', true, false); |
|
48
|
|
|
$bytes = $fifo->write(str_repeat('a', 65500)); |
|
49
|
|
|
$this->assertEquals(65500, $bytes); |
|
50
|
|
|
|
|
51
|
|
|
$bytes = $fifo->write(str_repeat('a', 65500)); |
|
52
|
|
|
$this->assertEquals(0, $bytes); |
|
53
|
|
|
|
|
54
|
|
|
$bytes = $fifo->write(str_repeat('a', 35)); |
|
55
|
|
|
$this->assertEquals(35, $bytes); |
|
56
|
|
|
|
|
57
|
|
|
$fifo->close(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testBlockingWrite() |
|
61
|
|
|
{ |
|
62
|
|
|
$fifo = new DuplexFifo('/tmp/test2.pipe', true, true); |
|
63
|
|
|
$bytes = $fifo->write(str_repeat('a', 65500)); |
|
64
|
|
|
$this->assertEquals(65500, $bytes); |
|
65
|
|
|
|
|
66
|
|
|
$this->syncExecute(sprintf("php %s %d %d", __DIR__ . '/ReadFifo.php', 65500, 1)); |
|
67
|
|
|
|
|
68
|
|
|
$bytes = $fifo->write(str_repeat('a', 65500)); |
|
69
|
|
|
$this->assertEquals(65500, $bytes); |
|
70
|
|
|
|
|
71
|
|
|
$fifo->close(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGetFifo() |
|
75
|
|
|
{ |
|
76
|
|
|
$fifo = new DuplexFifo('/tmp/test2.pipe'); |
|
77
|
|
|
$this->assertInstanceOf(ReadableFifo::class, $fifo->getReadFifo()); |
|
78
|
|
|
$this->assertInstanceOf(WritableFifo::class, $fifo->getWriteFifo()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testClose() |
|
82
|
|
|
{ |
|
83
|
|
|
$fifo = new DuplexFifo('/tmp/test2.pipe'); |
|
84
|
|
|
$fifo->close(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function syncExecute($command) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->lastPd = Utils::asyncExecute($command); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function tearDown() |
|
93
|
|
|
{ |
|
94
|
|
|
is_resource($this->lastPd) && pclose($this->lastPd); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|