Пример #1
0
 public function testPeekMode()
 {
     $queue = $this->createQueue(__FUNCTION__);
     $adapter = $queue->getAdapter();
     $this->checkAdapterSupport('deleteQueue');
     if (!$queue->isReceiveParamSupported(ReceiveParameters::PEEK_MODE)) {
         $this->markTestSkipped($this->getAdapterName() . ' does not support peek mode');
     }
     $body = 'test peek mode';
     $queue->send($body);
     $params = new ReceiveParameters();
     $params->setPeekMode(true);
     $messages = $queue->receive(1, $params);
     $this->assertCount(1, $messages);
     $message = $messages->current();
     $this->assertInstanceOf($queue->getOptions()->getMessageClass(), $message);
     $this->assertSame($body, $message->getContent());
     //Test message is still visibile
     $messages = $queue->receive(1, $params);
     $this->assertCount(1, $messages);
     $message = $messages->current();
     $this->assertInstanceOf($queue->getOptions()->getMessageClass(), $message);
     $this->assertSame($body, $message->getContent());
     // delete the queue we created
     $adapter->deleteQueue($queue->getName());
 }
Пример #2
0
 public function testSetPeekInvalidArgument()
 {
     $this->setExpectedException('Stakhanovist\\Queue\\Exception\\InvalidArgumentException');
     $this->receiveParameter->setPeekMode([]);
 }
 /**
  * Execute the request
  *
  * @param  MvcEvent $e
  * @return mixed
  * @throws Exception\DomainException
  */
 public function onDispatch(MvcEvent $e)
 {
     $routeMatch = $e->getRouteMatch();
     if (!$routeMatch) {
         /**
          * @todo Determine requirements for when route match is missing.
          *       Potentially allow pulling directly from request metadata?
          */
         throw new \Zend\Mvc\Exception\DomainException('Missing route matches; unsure how to retrieve action');
     }
     $action = $routeMatch->getParam('action', null);
     if ($action) {
         $action = strtolower($action);
     }
     switch ($action) {
         case 'process':
             $message = $routeMatch->getParam('message', null);
             if ($message instanceof MessageInterface) {
                 $result = $this->process($message);
             } else {
                 throw new \InvalidArgumentException('Missing or invalid message type: must be an instace of ' . MessageInterface::class);
             }
             break;
         case 'send':
         case 'receive':
         case 'await':
             $queue = $routeMatch->getParam('queue', null);
             if (is_string($queue)) {
                 $queue = $this->getServiceLocator()->get($queue);
             }
             if (!$queue instanceof QueueClientInterface) {
                 throw new \InvalidArgumentException('Invalid queue param type: must be a string or an instace of ' . QueueClientInterface::class);
             }
             if ($action === 'send') {
                 $message = $routeMatch->getParam('message', null);
                 $parameters = $routeMatch->getParam('sendParameters', null);
                 if ($parameters === null) {
                     $params = null;
                 } else {
                     if ($parameters instanceof SendParametersInterface) {
                         $params = $parameters;
                     } else {
                         if (is_string($parameters)) {
                             $params = new SendParameters();
                             $params->fromString($parameters);
                         } else {
                             if (is_array($parameters)) {
                                 $params = new SendParameters();
                                 $params->fromArray($parameters);
                             } else {
                                 throw new \InvalidArgumentException('Invalid sendParameters param type: must be null, an array, a string or an instace of ' . SendParametersInterface::class);
                             }
                         }
                     }
                 }
                 $result = $this->send($queue, $message, $params);
             } else {
                 $parameters = $routeMatch->getParam('receiveParameters', null);
                 if ($parameters === null) {
                     $params = null;
                 } else {
                     if ($parameters instanceof ReceiveParametersInterface) {
                         $params = $parameters;
                     } else {
                         if (is_string($parameters)) {
                             $params = new ReceiveParameters();
                             $params->fromString($parameters);
                         } else {
                             if (is_array($parameters)) {
                                 $params = new ReceiveParameters();
                                 $params->fromArray($parameters);
                             } else {
                                 throw new \InvalidArgumentException('Invalid receiveParameters param type: must be null, an array, a string or an instace of ' . QueueClientInterface::class);
                             }
                         }
                     }
                 }
                 if ($action === 'receive') {
                     $maxMessages = (int) $routeMatch->getParam('maxMessages', 1);
                     $result = $this->receive($queue, $maxMessages, $params);
                 } else {
                     $result = $this->await($queue, $params);
                 }
             }
             break;
         default:
             throw new \InvalidArgumentException(sprintf('Invalid action "%s". Only "process", "send", "receive", "await" are allowed', $action));
     }
     $e->setResult($result);
     return $result;
 }