public function testPerf()
 {
     for ($i = 0; $i < 100; ++$i) {
         // Queue list command
         $command = ['command' => 'list'];
         $this->queue->push($command);
         $this->queue->receive(10);
     }
 }
 public function testPushAndReceive()
 {
     $queue1 = $this->em->getRepository('Heri\\Bundle\\JobQueueBundle\\Entity\\Queue')->findOneByName($this->queueName . '1');
     $this->assertNotNull($queue1, 'Queue created');
     // Queue 1 list command
     $command1 = ['command' => 'list'];
     $this->queue->push($command1);
     $messages = $this->getMessages($queue1);
     $this->assertEquals(1, count($messages), 'Count number of messages');
     $message1 = $this->em->getRepository('Heri\\Bundle\\JobQueueBundle\\Entity\\Message')->find(1);
     $this->assertEquals($this->queueName . '1', $message1->getQueue()->getName(), 'Message1 queue relation check');
     $this->assertEquals($command1, json_decode($message1->getBody(), true), 'Verify encoded message in table');
     $this->assertEquals(0, $message1->getPriority(), 'Message1 priority');
     $this->assertEquals(false, $message1->getEnded(), 'Message1 no ended');
     $this->assertEquals(false, $message1->getFailed(), 'Message1 no failed');
     $queue1 = $this->em->getRepository('Heri\\Bundle\\JobQueueBundle\\Entity\\Queue')->findOneByName($this->queueName . '1');
     $this->assertNotNull($queue1, 'Queue created');
     // Queue 1 demo:great command
     $command2 = ['command' => 'demo:great', 'argument' => ['name' => 'Alexandre', '--yell' => true]];
     $this->queue->highPriority()->push($command2);
     $messages = $this->getMessages($queue1);
     $this->assertEquals(2, count($messages), 'Count number of messages');
     $message2 = $this->em->getRepository('Heri\\Bundle\\JobQueueBundle\\Entity\\Message')->find(2);
     $this->assertEquals($this->queueName . '1', $message2->getQueue()->getName(), 'Message2 queue relation check');
     $this->assertEquals($command2, json_decode($message2->getBody(), true), 'Verify encoded message in table');
     $this->assertEquals(1, $message2->getPriority(), 'Message2 priority');
     $this->assertEquals(false, $message2->getEnded(), 'Message2 no ended');
     $this->assertEquals(false, $message2->getFailed(), 'Message2 no failed');
     // Run demo:great command using directly receive method (priority 1)
     $this->queue->receive($this->maxMessages);
     $exceptions = $this->getMessageLogs();
     $this->assertEquals(1, count($exceptions), 'Exception logged from demo:great');
     $messages = $this->getMessages($queue1);
     $this->assertEquals(2, count($messages), '2 pending message not yet handled');
     // Run list command using listen method  (priority 0)
     $this->queue->listen($this->queueName . '1');
     $exceptions = $this->getMessageLogs();
     $this->assertEquals(1, count($exceptions), 'No more exception logged');
     $messages = $this->getMessages($queue1);
     $this->assertEquals(1, count($messages), '1 pending message left');
     $message2 = $this->em->getRepository('Heri\\Bundle\\JobQueueBundle\\Entity\\Message')->find(2);
     $this->assertEquals(false, $message2->getEnded(), 'Message2 no ended');
     $this->assertEquals(true, $message2->getFailed(), 'Message2 failed');
     $exceptions = $this->getMessageLogs();
     $this->assertEquals(1, count($exceptions), '1 exception logged');
     $exception = $this->em->getRepository('Heri\\Bundle\\JobQueueBundle\\Entity\\MessageLog')->find(1);
     $this->assertRegExp('/There are no commands defined in the "demo" namespace/', $exception->getLog(), 'Logged exception in database');
 }
 public function testZeroRetry()
 {
     $queue1 = $this->em->getRepository('Heri\\Bundle\\JobQueueBundle\\Entity\\Queue')->findOneByName($this->queueName . '1');
     $queue1->setTimeout(0);
     $queue1->setMaxRetries(0);
     $this->assertNotNull($queue1, 'Queue created');
     $this->em->persist($queue1);
     $this->em->flush();
     $messages = $this->getMessages($queue1);
     $this->assertEquals(0, count($messages), 'Count number of messages');
     // Queue 1 demo:great command
     $command2 = ['command' => 'demo:great', 'argument' => ['name' => 'Alexandre', '--yell' => true]];
     $this->queue->highPriority()->push($command2);
     $messages = $this->getMessages($queue1);
     $this->assertEquals(1, count($messages), 'Count number of messages');
     $this->assertEquals(false, $messages[0]->getFailed());
     $this->assertEquals(0, $messages[0]->getNumRetries());
     $this->assertNull($messages[0]->getTimeout());
     $exceptions = $this->getMessageLogs();
     $this->assertEquals(0, count($exceptions), 'Exception logged from demo:great');
     // 1st try
     $this->queue->receive($this->maxMessages);
     $messages = $this->getMessages($queue1);
     $this->assertEquals(true, $messages[0]->getFailed());
     $this->assertEquals(0, $messages[0]->getNumRetries());
     $this->assertNotNull($messages[0]->getTimeout());
     $previousTimeout = $messages[0]->getTimeout();
     $exceptions = $this->getMessageLogs();
     $this->assertEquals(1, count($exceptions), 'Exception logged from demo:great');
     // No more retries for this message
     usleep(1000000);
     // bypass timeout
     $this->queue->receive($this->maxMessages, 0);
     $messages = $this->getMessages($queue1);
     $this->assertEquals(true, $messages[0]->getFailed());
     $this->assertEquals(0, $messages[0]->getNumRetries());
     $this->assertEquals($previousTimeout, $messages[0]->getTimeout());
     $exceptions = $this->getMessageLogs();
     $this->assertEquals(1, count($exceptions), 'Exception logged from demo:great');
 }