|
8 | 8 | use PhpAmqpLib\Exception\AMQPHeartbeatMissedException; |
9 | 9 | use PhpAmqpLib\Message\AMQPMessage; |
10 | 10 | use PhpAmqpLib\Tests\Functional\AbstractConnectionTest; |
| 11 | +use PhpAmqpLib\Tests\Functional\ToxiProxy; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * @group connection |
@@ -278,4 +279,82 @@ public function must_throw_exception_after_connection_was_restored($type) |
278 | 279 | $this->assertChannelClosed($channel); |
279 | 280 | $this->assertConnectionClosed($connection); |
280 | 281 | } |
| 282 | + |
| 283 | + /** |
| 284 | + * Try to close and reopen connection with two channels. |
| 285 | + * |
| 286 | + * @test |
| 287 | + * @small |
| 288 | + * @group connection |
| 289 | + * @group proxy |
| 290 | + * @testWith ["stream"] |
| 291 | + * ["socket"] |
| 292 | + * @covers \PhpAmqpLib\Wire\IO\StreamIO::write() |
| 293 | + * @covers \PhpAmqpLib\Wire\IO\SocketIO::write() |
| 294 | + * |
| 295 | + * @param string $type |
| 296 | + */ |
| 297 | + public function must_throw_exception_after_connection_was_restored_with_two_channels($type) |
| 298 | + { |
| 299 | + $timeout = 1; |
| 300 | + |
| 301 | + // Create proxy part |
| 302 | + $host = trim(getenv('TOXIPROXY_AMQP_TARGET')); |
| 303 | + if (empty($host)) { |
| 304 | + $host = HOST; |
| 305 | + } |
| 306 | + $proxy = new ToxiProxy('amqp_connection', $this->get_toxiproxy_host()); |
| 307 | + $proxy->open($host, PORT, $this->get_toxiproxy_amqp_port()); |
| 308 | + |
| 309 | + /** @var AbstractConnection $connection */ |
| 310 | + $connection = $this->connection_create( |
| 311 | + $type, |
| 312 | + $proxy->getHost(), |
| 313 | + $proxy->getPort(), |
| 314 | + array('timeout' => $timeout) |
| 315 | + ); |
| 316 | + |
| 317 | + $channel = $connection->channel(); |
| 318 | + $anotherChannel = $connection->channel(); |
| 319 | + |
| 320 | + $this->assertTrue($channel->is_open()); |
| 321 | + $this->assertTrue($anotherChannel->is_open()); |
| 322 | + |
| 323 | + $this->queue_bind($channel, $exchange_name = 'test_exchange_broken', $queue_name); |
| 324 | + $message = new AMQPMessage( |
| 325 | + 'test', |
| 326 | + ['delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT] |
| 327 | + ); |
| 328 | + $channel->basic_publish($message, $exchange_name, $queue_name); |
| 329 | + |
| 330 | + // drop proxy connection and wait longer than timeout |
| 331 | + $proxy->close(); |
| 332 | + sleep($timeout); |
| 333 | + usleep(100000); |
| 334 | + // Reopen proxy |
| 335 | + $proxy->open($host, PORT, $this->get_toxiproxy_amqp_port()); |
| 336 | + |
| 337 | + $retry = 0; |
| 338 | + $exception = null; |
| 339 | + do { |
| 340 | + try { |
| 341 | + $channel->basic_publish($message, $exchange_name, $queue_name); |
| 342 | + } catch (\PHPUnit_Exception $exception) { |
| 343 | + throw $exception; |
| 344 | + } catch (\Exception $exception) { |
| 345 | + break; |
| 346 | + } |
| 347 | + } while (!$exception && ++$retry < 100); |
| 348 | + |
| 349 | + $this->assertInstanceOf(AMQPConnectionClosedException::class, $exception); |
| 350 | + $this->assertGreaterThan(0, $exception->getCode()); |
| 351 | + $this->assertChannelClosed($channel); |
| 352 | + |
| 353 | + // Now lets reconnect |
| 354 | + $connection->reconnect(); |
| 355 | + |
| 356 | + // Both old channels must be closed |
| 357 | + $this->assertChannelClosed($channel); |
| 358 | + $this->assertChannelClosed($anotherChannel); |
| 359 | + } |
281 | 360 | } |
0 commit comments