|
| 1 | +<?php |
| 2 | + |
| 3 | +use ConnectionManager\Extra\Multiple\ConnectionManagerConcurrent; |
| 4 | +use React\Promise; |
| 5 | + |
| 6 | +class ConnectionManagerConcurrentTest extends TestCase |
| 7 | +{ |
| 8 | + public function testEmptyRejects() |
| 9 | + { |
| 10 | + $connector = new ConnectionManagerConcurrent(); |
| 11 | + |
| 12 | + $promise = $connector->create('google.com', 80); |
| 13 | + |
| 14 | + $this->assertPromiseReject($promise); |
| 15 | + } |
| 16 | + |
| 17 | + public function testWillForwardToInnerConnector() |
| 18 | + { |
| 19 | + $pending = new Promise\Promise(function() { }); |
| 20 | + |
| 21 | + $only = $this->getMock('React\SocketClient\ConnectorInterface'); |
| 22 | + $only->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); |
| 23 | + |
| 24 | + $connector = new ConnectionManagerConcurrent(); |
| 25 | + $connector->addConnectionManager($only); |
| 26 | + |
| 27 | + $promise = $connector->create('google.com', 80); |
| 28 | + |
| 29 | + $promise->then($this->expectCallableNever(), $this->expectCallableNever()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testWillCancelOtherIfOneResolves() |
| 33 | + { |
| 34 | + $resolved = Promise\resolve($this->getMock('React\Stream\DuplexStreamInterface')); |
| 35 | + $first = $this->getMock('React\SocketClient\ConnectorInterface'); |
| 36 | + $first->expects($this->once())->method('create')->with('google.com', 80)->willReturn($resolved); |
| 37 | + |
| 38 | + $pending = new Promise\Promise(function() { }, $this->expectCallableOnce()); |
| 39 | + $second = $this->getMock('React\SocketClient\ConnectorInterface'); |
| 40 | + $second->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); |
| 41 | + |
| 42 | + $connector = new ConnectionManagerConcurrent(); |
| 43 | + $connector->addConnectionManager($first); |
| 44 | + $connector->addConnectionManager($second); |
| 45 | + |
| 46 | + $promise = $connector->create('google.com', 80); |
| 47 | + |
| 48 | + $this->assertPromiseResolve($promise); |
| 49 | + } |
| 50 | + |
| 51 | + public function testWillCloseOtherIfOneResolves() |
| 52 | + { |
| 53 | + $resolved = Promise\resolve($this->getMock('React\Stream\DuplexStreamInterface')); |
| 54 | + $first = $this->getMock('React\SocketClient\ConnectorInterface'); |
| 55 | + $first->expects($this->once())->method('create')->with('google.com', 80)->willReturn($resolved); |
| 56 | + |
| 57 | + $slower = $this->getMock('React\Stream\DuplexStreamInterface'); |
| 58 | + $slower->expects($this->once())->method('close'); |
| 59 | + $second = $this->getMock('React\SocketClient\ConnectorInterface'); |
| 60 | + $second->expects($this->once())->method('create')->with('google.com', 80)->willReturn(Promise\resolve($slower)); |
| 61 | + |
| 62 | + $connector = new ConnectionManagerConcurrent(); |
| 63 | + $connector->addConnectionManager($first); |
| 64 | + $connector->addConnectionManager($second); |
| 65 | + |
| 66 | + $promise = $connector->create('google.com', 80); |
| 67 | + |
| 68 | + $this->assertPromiseResolve($promise); |
| 69 | + } |
| 70 | +} |
0 commit comments