|
8 | 8 | use NeedleProject\LaravelRabbitMq\AMQPConnection;
|
9 | 9 | use PhpAmqpLib\Channel\AMQPChannel;
|
10 | 10 | use Tests\NeedleProject\LaravelRabbitMq\Stubs\ExchangeEntityDetailsStub;
|
| 11 | +use PhpAmqpLib\Exception\AMQPChannelClosedException; |
11 | 12 |
|
12 | 13 | class ExchangeEntityTest extends TestCase
|
13 | 14 | {
|
@@ -354,4 +355,82 @@ public function testPublishWithAutoCreate()
|
354 | 355 | );
|
355 | 356 | $exchange->publish('a');
|
356 | 357 | }
|
| 358 | + |
| 359 | + |
| 360 | + public function testPublishRetry() |
| 361 | + { |
| 362 | + $amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
| 363 | + ->disableOriginalConstructor() |
| 364 | + ->getMock(); |
| 365 | + |
| 366 | + $channelMock = $this->getMockBuilder(AMQPChannel::class) |
| 367 | + ->disableOriginalConstructor() |
| 368 | + ->getMock(); |
| 369 | + |
| 370 | + $amqpConnection->expects($this->atLeastOnce()) |
| 371 | + ->method('getChannel') |
| 372 | + ->willReturn($channelMock); |
| 373 | + |
| 374 | + $amqpConnection->expects($this->atLeastOnce()) |
| 375 | + ->method('reconnect') |
| 376 | + ->willReturn($channelMock); |
| 377 | + |
| 378 | + $retries = 0; |
| 379 | + $channelMock->expects($this->exactly(2)) |
| 380 | + ->method('basic_publish') |
| 381 | + ->will($this->returnCallback(function ($args) use (&$retries) { |
| 382 | + if (0 === $retries) { |
| 383 | + $retries++; |
| 384 | + throw new AMQPChannelClosedException("Channel is Closed"); |
| 385 | + } |
| 386 | + return null; |
| 387 | + })); |
| 388 | + |
| 389 | + $queue = ExchangeEntity::createExchange( |
| 390 | + $amqpConnection, |
| 391 | + 'foo', |
| 392 | + [ |
| 393 | + 'name' => 'exchange.name.on.rabbit', |
| 394 | + 'auto_create' => true, |
| 395 | + 'bind' => [['queue' => 'foo', 'routing_key' => '*']] |
| 396 | + ] |
| 397 | + ); |
| 398 | + $queue->publish('a'); |
| 399 | + $this->assertEquals(1, $retries); |
| 400 | + } |
| 401 | + |
| 402 | + public function testPublishMaxRetry() |
| 403 | + { |
| 404 | + $amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
| 405 | + ->disableOriginalConstructor() |
| 406 | + ->getMock(); |
| 407 | + |
| 408 | + $channelMock = $this->getMockBuilder(AMQPChannel::class) |
| 409 | + ->disableOriginalConstructor() |
| 410 | + ->getMock(); |
| 411 | + |
| 412 | + $amqpConnection->expects($this->atLeastOnce()) |
| 413 | + ->method('getChannel') |
| 414 | + ->willReturn($channelMock); |
| 415 | + |
| 416 | + $amqpConnection->expects($this->atLeastOnce()) |
| 417 | + ->method('reconnect') |
| 418 | + ->willReturn($channelMock); |
| 419 | + |
| 420 | + $channelMock->expects($this->exactly(3)) |
| 421 | + ->method('basic_publish') |
| 422 | + ->will($this->throwException(new AMQPChannelClosedException("Channel is Closed"))); |
| 423 | + |
| 424 | + $queue = ExchangeEntity::createExchange( |
| 425 | + $amqpConnection, |
| 426 | + 'foo', |
| 427 | + [ |
| 428 | + 'name' => 'exchange.name.on.rabbit', |
| 429 | + 'auto_create' => true, |
| 430 | + 'bind' => [['queue' => 'foo', 'routing_key' => '*']] |
| 431 | + ] |
| 432 | + ); |
| 433 | + $this->expectException(AMQPChannelClosedException::class); |
| 434 | + $queue->publish('a'); |
| 435 | + } |
357 | 436 | }
|
0 commit comments