|
5 | 5 | use OldSound\RabbitMqBundle\Event\AfterProcessingMessageEvent; |
6 | 6 | use OldSound\RabbitMqBundle\Event\BeforeProcessingMessageEvent; |
7 | 7 | use OldSound\RabbitMqBundle\Event\OnConsumeEvent; |
| 8 | +use OldSound\RabbitMqBundle\Event\OnIdleEvent; |
8 | 9 | use OldSound\RabbitMqBundle\RabbitMq\Consumer; |
9 | 10 | use PhpAmqpLib\Exception\AMQPTimeoutException; |
10 | 11 | use PhpAmqpLib\Message\AMQPMessage; |
@@ -144,7 +145,7 @@ public function testConsume($data) |
144 | 145 | $amqpChannel->callbacks = $consumerCallBacks; |
145 | 146 |
|
146 | 147 | /** |
147 | | - * Mock ait method and use a callback to remove one element each time from callbacks |
| 148 | + * Mock wait method and use a callback to remove one element each time from callbacks |
148 | 149 | * This will simulate a basic consumer consume with provided messages count |
149 | 150 | */ |
150 | 151 | $amqpChannel->expects($this->exactly(count($consumerCallBacks))) |
@@ -202,4 +203,56 @@ public function testIdleTimeoutExitCode() |
202 | 203 |
|
203 | 204 | $this->assertTrue(2 == $consumer->consume(1)); |
204 | 205 | } |
| 206 | + |
| 207 | + public function testShouldAllowContinueConsumptionAfterIdleTimeout() |
| 208 | + { |
| 209 | + // set up amqp connection |
| 210 | + $amqpConnection = $this->prepareAMQPConnection(); |
| 211 | + // set up amqp channel |
| 212 | + $amqpChannel = $this->prepareAMQPChannel(); |
| 213 | + $amqpChannel->expects($this->atLeastOnce()) |
| 214 | + ->method('getChannelId') |
| 215 | + ->with() |
| 216 | + ->willReturn(true); |
| 217 | + $amqpChannel->expects($this->once()) |
| 218 | + ->method('basic_consume') |
| 219 | + ->withAnyParameters() |
| 220 | + ->willReturn(true); |
| 221 | + |
| 222 | + // set up consumer |
| 223 | + $consumer = $this->getConsumer($amqpConnection, $amqpChannel); |
| 224 | + // disable autosetup fabric so we do not mock more objects |
| 225 | + $consumer->disableAutoSetupFabric(); |
| 226 | + $consumer->setChannel($amqpChannel); |
| 227 | + $consumer->setIdleTimeout(2); |
| 228 | + $amqpChannel->callbacks = array('idle_timeout_exit_code'); |
| 229 | + |
| 230 | + $amqpChannel->expects($this->exactly(2)) |
| 231 | + ->method('wait') |
| 232 | + ->with(null, false, $consumer->getIdleTimeout()) |
| 233 | + ->willThrowException(new AMQPTimeoutException()); |
| 234 | + |
| 235 | + // set up event dispatcher |
| 236 | + $eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') |
| 237 | + ->disableOriginalConstructor() |
| 238 | + ->getMock(); |
| 239 | + |
| 240 | + $eventDispatcher->expects($this->at(1)) |
| 241 | + ->method('dispatch') |
| 242 | + ->with(OnIdleEvent::NAME, $this->isInstanceOf('OldSound\RabbitMqBundle\Event\OnIdleEvent')) |
| 243 | + ->willReturnCallback(function($eventName, OnIdleEvent $event) { |
| 244 | + $event->setForceStop(false); |
| 245 | + }); |
| 246 | + $eventDispatcher->expects($this->at(3)) |
| 247 | + ->method('dispatch') |
| 248 | + ->with(OnIdleEvent::NAME, $this->isInstanceOf('OldSound\RabbitMqBundle\Event\OnIdleEvent')) |
| 249 | + ->willReturn(function($eventName, OnIdleEvent $event) { |
| 250 | + $event->setForceStop(true); |
| 251 | + }); |
| 252 | + |
| 253 | + $consumer->setEventDispatcher($eventDispatcher); |
| 254 | + |
| 255 | + $this->setExpectedException('PhpAmqpLib\Exception\AMQPTimeoutException'); |
| 256 | + $consumer->consume(10); |
| 257 | + } |
205 | 258 | } |
0 commit comments