|
| 1 | +<?php |
| 2 | + |
| 3 | +use Datagram\Socket; |
| 4 | +use React\Promise\When; |
| 5 | +use React\Promise\PromiseInterface; |
| 6 | + |
| 7 | +class SocketTest extends TestCase |
| 8 | +{ |
| 9 | + private $factory; |
| 10 | + |
| 11 | + public function setUp() |
| 12 | + { |
| 13 | + $this->loop = React\EventLoop\Factory::create(); |
| 14 | + $this->factory = new Datagram\Factory($this->loop, $this->createResolverMock()); |
| 15 | + } |
| 16 | + |
| 17 | + public function testCreateClientCloseWillNotBlock() |
| 18 | + { |
| 19 | + $promise = $this->factory->createClient('127.0.0.1', 12345); |
| 20 | + $client = $this->getValueFromResolvedPromise($promise); |
| 21 | + |
| 22 | + $client->send('test'); |
| 23 | + $client->close(); |
| 24 | + |
| 25 | + $this->loop->run(); |
| 26 | + |
| 27 | + return $client; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * |
| 32 | + * @param Socket $client |
| 33 | + * @depends testCreateClientCloseWillNotBlock |
| 34 | + */ |
| 35 | + public function testClientCloseAgainWillNotBlock(Socket $client) |
| 36 | + { |
| 37 | + $client->close(); |
| 38 | + $this->loop->run(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testCreateClientEndWillNotBlock() |
| 42 | + { |
| 43 | + $promise = $this->factory->createClient('127.0.0.1', 12345); |
| 44 | + $client = $this->getValueFromResolvedPromise($promise); |
| 45 | + |
| 46 | + $client->send('test'); |
| 47 | + $client->end(); |
| 48 | + |
| 49 | + $this->loop->run(); |
| 50 | + |
| 51 | + return $client; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * |
| 56 | + * @param Socket $client |
| 57 | + * @depends testCreateClientEndWillNotBlock |
| 58 | + */ |
| 59 | + public function testClientEndAgainWillNotBlock(Socket $client) |
| 60 | + { |
| 61 | + $client->end(); |
| 62 | + $this->loop->run(); |
| 63 | + } |
| 64 | + |
| 65 | + public function testCreatePair() |
| 66 | + { |
| 67 | + $promise = $this->factory->createServer(0, '127.0.0.1'); |
| 68 | + $server = $this->getValueFromResolvedPromise($promise); |
| 69 | + |
| 70 | + $promise = $this->factory->createClient('127.0.0.1', $server->getPort()); |
| 71 | + $client = $this->getValueFromResolvedPromise($promise); |
| 72 | + |
| 73 | + $that = $this; |
| 74 | + $server->on('message', function ($message, $remote, $server) use ($that) { |
| 75 | + $that->assertEquals('test', $message); |
| 76 | + |
| 77 | + // once the server receives a message, send it pack to client and stop server |
| 78 | + $server->send('response:' . $message, $remote); |
| 79 | + $server->end(); |
| 80 | + }); |
| 81 | + |
| 82 | + $client->on('message', function ($message, $remote, $client) use ($that) { |
| 83 | + $that->assertEquals('response:test', $message); |
| 84 | + |
| 85 | + // once the client receives a message, stop client |
| 86 | + $client->end(); |
| 87 | + }); |
| 88 | + |
| 89 | + $client->send('test'); |
| 90 | + |
| 91 | + $this->loop->run(); |
| 92 | + } |
| 93 | +} |
0 commit comments