|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Tests\Socket; |
| 4 | + |
| 5 | +use React\Socket\AccountingServer; |
| 6 | +use React\Socket\Server; |
| 7 | +use React\EventLoop\Factory; |
| 8 | +use Clue\React\Block; |
| 9 | + |
| 10 | +class AccountingServerTest extends TestCase |
| 11 | +{ |
| 12 | + public function testA() |
| 13 | + { |
| 14 | + $server = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); |
| 15 | + $server = new AccountingServer($server); |
| 16 | + } |
| 17 | + |
| 18 | + public function testGetAddressWillBePassedThroughToTcpServer() |
| 19 | + { |
| 20 | + $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); |
| 21 | + $tcp->expects($this->once())->method('getAddress')->willReturn('127.0.0.1:1234'); |
| 22 | + |
| 23 | + $server = new AccountingServer($tcp); |
| 24 | + |
| 25 | + $this->assertEquals('127.0.0.1:1234', $server->getAddress()); |
| 26 | + } |
| 27 | + |
| 28 | + public function testPauseWillBePassedThroughToTcpServer() |
| 29 | + { |
| 30 | + $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); |
| 31 | + $tcp->expects($this->once())->method('pause'); |
| 32 | + |
| 33 | + $server = new AccountingServer($tcp); |
| 34 | + |
| 35 | + $server->pause(); |
| 36 | + } |
| 37 | + |
| 38 | + public function testResumeWillBePassedThroughToTcpServer() |
| 39 | + { |
| 40 | + $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); |
| 41 | + $tcp->expects($this->once())->method('resume'); |
| 42 | + |
| 43 | + $server = new AccountingServer($tcp); |
| 44 | + |
| 45 | + $server->resume(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testCloseWillBePassedThroughToTcpServer() |
| 49 | + { |
| 50 | + $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); |
| 51 | + $tcp->expects($this->once())->method('close'); |
| 52 | + |
| 53 | + $server = new AccountingServer($tcp); |
| 54 | + |
| 55 | + $server->close(); |
| 56 | + } |
| 57 | + |
| 58 | + public function testSocketErrorWillBeForwarded() |
| 59 | + { |
| 60 | + $loop = $this->getMock('React\EventLoop\LoopInterface'); |
| 61 | + |
| 62 | + $tcp = new Server(0, $loop); |
| 63 | + |
| 64 | + $server = new AccountingServer($tcp); |
| 65 | + |
| 66 | + $server->on('error', $this->expectCallableOnce()); |
| 67 | + |
| 68 | + $tcp->emit('error', array(new \RuntimeException('test'))); |
| 69 | + } |
| 70 | + |
| 71 | + public function testSocketConnectionWillBeForwarded() |
| 72 | + { |
| 73 | + $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 74 | + |
| 75 | + $loop = $this->getMock('React\EventLoop\LoopInterface'); |
| 76 | + |
| 77 | + $tcp = new Server(0, $loop); |
| 78 | + |
| 79 | + $server = new AccountingServer($tcp); |
| 80 | + $server->on('connection', $this->expectCallableOnceWith($connection)); |
| 81 | + $server->on('error', $this->expectCallableNever()); |
| 82 | + |
| 83 | + $tcp->emit('connection', array($connection)); |
| 84 | + |
| 85 | + $this->assertEquals(array($connection), $server->getConnections()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testSocketConnectionWillBeClosedOnceLimitIsReached() |
| 89 | + { |
| 90 | + $first = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 91 | + $first->expects($this->never())->method('close'); |
| 92 | + $second = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 93 | + $second->expects($this->once())->method('close'); |
| 94 | + |
| 95 | + $loop = $this->getMock('React\EventLoop\LoopInterface'); |
| 96 | + |
| 97 | + $tcp = new Server(0, $loop); |
| 98 | + |
| 99 | + $server = new AccountingServer($tcp, 1); |
| 100 | + $server->on('connection', $this->expectCallableOnceWith($first)); |
| 101 | + $server->on('error', $this->expectCallableOnce()); |
| 102 | + |
| 103 | + $tcp->emit('connection', array($first)); |
| 104 | + $tcp->emit('connection', array($second)); |
| 105 | + } |
| 106 | + |
| 107 | + public function testSocketDisconnectionWillRemoveFromList() |
| 108 | + { |
| 109 | + $loop = Factory::create(); |
| 110 | + |
| 111 | + $tcp = new Server(0, $loop); |
| 112 | + |
| 113 | + $socket = stream_socket_client('tcp://' . $tcp->getAddress()); |
| 114 | + fclose($socket); |
| 115 | + |
| 116 | + $server = new AccountingServer($tcp); |
| 117 | + $server->on('connection', $this->expectCallableOnce()); |
| 118 | + $server->on('error', $this->expectCallableNever()); |
| 119 | + |
| 120 | + Block\sleep(0.1, $loop); |
| 121 | + |
| 122 | + $this->assertEquals(array(), $server->getConnections()); |
| 123 | + } |
| 124 | +} |
0 commit comments