|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Tests\Stream; |
| 4 | + |
| 5 | +use React\Stream\Buffer; |
| 6 | +use React\Tests\Socket\TestCase; |
| 7 | + |
| 8 | +class BufferTest extends TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @covers React\Stream\Buffer::__construct |
| 12 | + */ |
| 13 | + public function testConstructor() |
| 14 | + { |
| 15 | + $stream = fopen('php://temp', 'r+'); |
| 16 | + $loop = $this->createLoopMock(); |
| 17 | + |
| 18 | + $buffer = new Buffer($stream, $loop); |
| 19 | + $buffer->on('error', $this->expectCallableNever()); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @covers React\Stream\Buffer::write |
| 24 | + * @covers React\Stream\Buffer::handleWrite |
| 25 | + */ |
| 26 | + public function testWrite() |
| 27 | + { |
| 28 | + $stream = fopen('php://temp', 'r+'); |
| 29 | + $loop = $this->createWriteableLoopMock(); |
| 30 | + |
| 31 | + $buffer = new Buffer($stream, $loop); |
| 32 | + $buffer->on('error', $this->expectCallableNever()); |
| 33 | + |
| 34 | + $buffer->write("foobar\n"); |
| 35 | + rewind($stream); |
| 36 | + $this->assertSame("foobar\n", fread($stream, 1024)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @covers React\Stream\Buffer::write |
| 41 | + * @covers React\Stream\Buffer::handleWrite |
| 42 | + */ |
| 43 | + public function testWriteReturnsFalseWhenBufferIsFull() |
| 44 | + { |
| 45 | + $stream = fopen('php://temp', 'r+'); |
| 46 | + $loop = $this->createWriteableLoopMock(); |
| 47 | + $loop->preventWrites = true; |
| 48 | + |
| 49 | + $buffer = new Buffer($stream, $loop); |
| 50 | + $buffer->softLimit = 4; |
| 51 | + $buffer->on('error', $this->expectCallableNever()); |
| 52 | + |
| 53 | + $this->assertTrue($buffer->write("foo")); |
| 54 | + $loop->preventWrites = false; |
| 55 | + $this->assertFalse($buffer->write("bar\n")); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @covers React\Stream\Buffer::write |
| 60 | + * @covers React\Stream\Buffer::handleWrite |
| 61 | + */ |
| 62 | + public function testWriteDetectsWhenOtherSideIsClosed() |
| 63 | + { |
| 64 | + list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); |
| 65 | + |
| 66 | + $loop = $this->createWriteableLoopMock(); |
| 67 | + |
| 68 | + $buffer = new Buffer($a, $loop); |
| 69 | + $buffer->softLimit = 4; |
| 70 | + $buffer->on('error', $this->expectCallableOnce()); |
| 71 | + |
| 72 | + fclose($b); |
| 73 | + |
| 74 | + $buffer->write("foo"); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @covers React\Stream\Buffer::write |
| 79 | + * @covers React\Stream\Buffer::handleWrite |
| 80 | + */ |
| 81 | + public function testDrain() |
| 82 | + { |
| 83 | + $stream = fopen('php://temp', 'r+'); |
| 84 | + $loop = $this->createWriteableLoopMock(); |
| 85 | + $loop->preventWrites = true; |
| 86 | + |
| 87 | + $buffer = new Buffer($stream, $loop); |
| 88 | + $buffer->softLimit = 4; |
| 89 | + $buffer->on('error', $this->expectCallableNever()); |
| 90 | + $buffer->on('drain', $this->expectCallableOnce()); |
| 91 | + |
| 92 | + $buffer->write("foo"); |
| 93 | + $loop->preventWrites = false; |
| 94 | + $buffer->listening = false; |
| 95 | + $buffer->write("bar\n"); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @covers React\Stream\Buffer::end |
| 100 | + */ |
| 101 | + public function testEnd() |
| 102 | + { |
| 103 | + $stream = fopen('php://temp', 'r+'); |
| 104 | + $loop = $this->createLoopMock(); |
| 105 | + |
| 106 | + $buffer = new Buffer($stream, $loop); |
| 107 | + $buffer->on('error', $this->expectCallableNever()); |
| 108 | + $buffer->on('close', $this->expectCallableOnce()); |
| 109 | + |
| 110 | + $this->assertTrue($buffer->isWritable()); |
| 111 | + $buffer->end(); |
| 112 | + $this->assertFalse($buffer->isWritable()); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @covers React\Stream\Buffer::end |
| 117 | + */ |
| 118 | + public function testEndWithData() |
| 119 | + { |
| 120 | + $stream = fopen('php://temp', 'r+'); |
| 121 | + $loop = $this->createWriteableLoopMock(); |
| 122 | + |
| 123 | + $buffer = new Buffer($stream, $loop); |
| 124 | + $buffer->on('error', $this->expectCallableNever()); |
| 125 | + $buffer->on('close', $this->expectCallableOnce()); |
| 126 | + |
| 127 | + $buffer->end('final words'); |
| 128 | + |
| 129 | + rewind($stream); |
| 130 | + $this->assertSame('final words', stream_get_contents($stream)); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @covers React\Stream\Buffer::isWritable |
| 135 | + * @covers React\Stream\Buffer::close |
| 136 | + */ |
| 137 | + public function testClose() |
| 138 | + { |
| 139 | + $stream = fopen('php://temp', 'r+'); |
| 140 | + $loop = $this->createLoopMock(); |
| 141 | + |
| 142 | + $buffer = new Buffer($stream, $loop); |
| 143 | + $buffer->on('error', $this->expectCallableNever()); |
| 144 | + $buffer->on('close', $this->expectCallableOnce()); |
| 145 | + |
| 146 | + $this->assertTrue($buffer->isWritable()); |
| 147 | + $buffer->close(); |
| 148 | + $this->assertFalse($buffer->isWritable()); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @covers React\Stream\Buffer::write |
| 153 | + * @covers React\Stream\Buffer::close |
| 154 | + */ |
| 155 | + public function testWritingToClosedBufferShouldNotWriteToStream() |
| 156 | + { |
| 157 | + $stream = fopen('php://temp', 'r+'); |
| 158 | + $loop = $this->createWriteableLoopMock(); |
| 159 | + |
| 160 | + $buffer = new Buffer($stream, $loop); |
| 161 | + $buffer->close(); |
| 162 | + |
| 163 | + $buffer->write('foo'); |
| 164 | + |
| 165 | + rewind($stream); |
| 166 | + $this->assertSame('', stream_get_contents($stream)); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @covers React\Stream\Buffer::handleWrite |
| 171 | + * @covers React\Stream\Buffer::errorHandler |
| 172 | + */ |
| 173 | + public function testError() |
| 174 | + { |
| 175 | + $stream = null; |
| 176 | + $loop = $this->createWriteableLoopMock(); |
| 177 | + |
| 178 | + $error = null; |
| 179 | + |
| 180 | + $buffer = new Buffer($stream, $loop); |
| 181 | + $buffer->on('error', function ($message) use (&$error) { |
| 182 | + $error = $message; |
| 183 | + }); |
| 184 | + |
| 185 | + $buffer->write('Attempting to write to bad stream'); |
| 186 | + $this->assertInstanceOf('Exception', $error); |
| 187 | + $this->assertSame('Tried to write to invalid stream.', $error->getMessage()); |
| 188 | + } |
| 189 | + |
| 190 | + public function testWritingToClosedStream() |
| 191 | + { |
| 192 | + if ('Darwin' === PHP_OS) { |
| 193 | + $this->markTestSkipped('OS X issue with shutting down pair for writing'); |
| 194 | + } |
| 195 | + |
| 196 | + list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); |
| 197 | + $loop = $this->createWriteableLoopMock(); |
| 198 | + |
| 199 | + $error = null; |
| 200 | + |
| 201 | + $buffer = new Buffer($a, $loop); |
| 202 | + $buffer->on('error', function($message) use (&$error) { |
| 203 | + $error = $message; |
| 204 | + }); |
| 205 | + |
| 206 | + $buffer->write('foo'); |
| 207 | + stream_socket_shutdown($b, STREAM_SHUT_RD); |
| 208 | + stream_socket_shutdown($a, STREAM_SHUT_RD); |
| 209 | + $buffer->write('bar'); |
| 210 | + |
| 211 | + $this->assertInstanceOf('Exception', $error); |
| 212 | + $this->assertSame('Tried to write to closed stream.', $error->getMessage()); |
| 213 | + } |
| 214 | + |
| 215 | + private function createWriteableLoopMock() |
| 216 | + { |
| 217 | + $loop = $this->createLoopMock(); |
| 218 | + $loop->preventWrites = false; |
| 219 | + $loop |
| 220 | + ->expects($this->any()) |
| 221 | + ->method('addWriteStream') |
| 222 | + ->will($this->returnCallback(function ($stream, $listener) use ($loop) { |
| 223 | + if (!$loop->preventWrites) { |
| 224 | + call_user_func($listener, $stream); |
| 225 | + } |
| 226 | + })); |
| 227 | + |
| 228 | + return $loop; |
| 229 | + } |
| 230 | + |
| 231 | + private function createLoopMock() |
| 232 | + { |
| 233 | + return $this->getMock('React\EventLoop\LoopInterface'); |
| 234 | + } |
| 235 | +} |
0 commit comments