Пример #1
0
 public function testFlushDeferred()
 {
     $amqpChannel = $this->getMockBuilder('AMQPChannel')->disableOriginalConstructor()->getMock();
     $amqpChannel->expects($this->once())->method('getPrefetchCount')->willReturn(3);
     $message = $this->getMockBuilder('AMQPEnvelope')->disableOriginalConstructor()->getMock();
     $message->expects($this->any())->method('getDeliveryTag')->willReturnCallback(function () {
         return uniqid();
     });
     $amqpQueue = $this->getMockBuilder('AMQPQueue')->disableOriginalConstructor()->getMock();
     $eventManager = m::mock('Zend\\EventManager\\EventManager');
     $eventManager->shouldReceive('setIdentifiers');
     $amqpQueue->expects($this->once())->method('getChannel')->willReturn($amqpChannel);
     $amqpQueue->expects($this->any())->method('get')->willReturn($message);
     $consumer = new Consumer(array($amqpQueue), 1, 1 * 1000 * 500);
     $consumer->setEventManager($eventManager);
     // Create a callback function with a return value set by the data provider.
     $callbackFunction = function () {
         static $i = 0;
         $i++;
         switch ($i) {
             case 1:
                 return null;
             case 2:
                 return null;
             case 3:
                 return null;
             case 4:
                 return ConsumerInterface::MSG_ACK;
             case 5:
                 return false;
             case 6:
                 return ConsumerInterface::MSG_REJECT;
             case 7:
                 return ConsumerInterface::MSG_REJECT_REQUEUE;
             case 8:
                 return true;
         }
         return null;
     };
     $flushCallbackFunction = function () {
         static $i = 0;
         $i++;
         if ($i == 1) {
             return true;
         }
         return false;
     };
     $eventFlushResponse = $this->getMock('Zend\\EventManager\\ResponseCollection');
     $eventFlushResponse->expects($this->atLeastOnce())->method('last')->willReturnCallback($flushCallbackFunction);
     $eventResponse = $this->getMock('Zend\\EventManager\\ResponseCollection');
     $eventResponse->expects($this->atLeastOnce())->method('last')->willReturnCallback($callbackFunction);
     $eventManager->shouldReceive('trigger')->atLeast(1)->with('delivery', $consumer, ['message' => $message, 'queue' => $amqpQueue])->andReturn($eventResponse);
     $eventManager->shouldReceive('trigger')->atLeast(1)->with('flushDeferred', $consumer)->andReturn($eventFlushResponse);
     $eventManager->shouldReceive('trigger')->times(3)->with('ack', $consumer, m::any());
     $amqpQueue->expects($this->exactly(3))->method('ack');
     $amqpQueue->expects($this->exactly(3))->method('reject');
     $consumer->consume(5);
 }
Пример #2
0
 /**
  * Constructor
  *
  * @param AMQPQueue $queue
  * @param float $idleTimeout in seconds
  * @param int $waitTimeout in microseconds
  */
 public function __construct(AMQPQueue $queue, $idleTimeout, $waitTimeout)
 {
     $queues = array($queue);
     parent::__construct($queues, $idleTimeout, $waitTimeout);
 }
 /**
  * Create service with name
  *
  * @param ServiceLocatorInterface $serviceLocator
  * @param string $name
  * @param string $requestedName
  * @return Consumer
  * @throws Exception\InvalidArgumentException
  */
 public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
 {
     // get global service locator, if we are in a plugin manager
     if ($serviceLocator instanceof AbstractPluginManager) {
         $serviceLocator = $serviceLocator->getServiceLocator();
     }
     $spec = $this->getSpec($serviceLocator, $name, $requestedName);
     $this->validateSpec($serviceLocator, $spec, $requestedName);
     $connection = $this->getConnection($serviceLocator, $spec);
     $channel = $this->createChannel($connection, $spec);
     $config = $this->getConfig($serviceLocator);
     $queues = array();
     foreach ($spec['queues'] as $queue) {
         if ($this->useAutoSetupFabric($spec)) {
             // will create the exchange to declare it on the channel
             // the created exchange will not be used afterwards
             $exchangeName = $config['queues'][$queue]['exchange'];
             $this->getExchange($serviceLocator, $channel, $exchangeName, $this->useAutoSetupFabric($spec));
         }
         $queueSpec = $this->getQueueSpec($serviceLocator, $queue);
         $queues[] = $this->getQueue($queueSpec, $channel, $this->useAutoSetupFabric($spec));
     }
     $idleTimeout = isset($spec['idle_timeout']) ? $spec['idle_timeout'] : 5.0;
     $waitTimeout = isset($spec['wait_timeout']) ? $spec['wait_timeout'] : 100000;
     $consumer = new Consumer($queues, $idleTimeout, $waitTimeout);
     if (isset($spec['logger'])) {
         if (!$serviceLocator->has($spec['logger'])) {
             throw new Exception\InvalidArgumentException('The logger ' . $spec['logger'] . ' is not configured');
         }
         /** @var \Zend\Log\LoggerInterface $logger */
         $logger = $serviceLocator->get($spec['logger']);
         $loggerListener = new LoggerListener($logger);
         $consumer->getEventManager()->attachAggregate($loggerListener);
     }
     $callbackManager = $this->getCallbackManager($serviceLocator);
     if (isset($spec['callback'])) {
         if (!$callbackManager->has($spec['callback'])) {
             throw new Exception\InvalidArgumentException('The required callback ' . $spec['callback'] . ' can not be found');
         }
         /** @var callable $callback */
         $callback = $callbackManager->get($spec['callback']);
         if ($callback) {
             $consumer->getEventManager()->attach('delivery', $callback);
         }
     }
     if (isset($spec['flush_callback'])) {
         if (!$callbackManager->has($spec['flush_callback'])) {
             throw new Exception\InvalidArgumentException('The required callback ' . $spec['flush_callback'] . ' can not be found');
         }
         /** @var callable $callback */
         $flushCallback = $callbackManager->get($spec['flush_callback']);
         if ($flushCallback) {
             $consumer->getEventManager()->attach('flush', $flushCallback);
         }
     }
     if (isset($spec['error_callback'])) {
         if (!$callbackManager->has($spec['error_callback'])) {
             throw new Exception\InvalidArgumentException('The required callback ' . $spec['error_callback'] . ' can not be found');
         }
         /** @var callable $callback */
         $errorCallback = $callbackManager->get($spec['error_callback']);
         if ($errorCallback) {
             $consumer->getEventManager()->attach('deliveryException', $errorCallback);
             $consumer->getEventManager()->attach('flushDeferredException', $errorCallback);
         }
     }
     if (isset($spec['listeners']) and is_array($spec['listeners'])) {
         foreach ($spec['listeners'] as $listener) {
             if (is_string($listener)) {
                 /** @var \Zend\EventManager\ListenerAggregateInterface $listener */
                 $listener = $serviceLocator->get($listener);
             }
             $consumer->getEventManager()->attachAggregate($listener);
         }
     }
     return $consumer;
 }