コード例 #1
0
 public function testsetVisibilityTimeoutInvalidArgument()
 {
     $this->setExpectedException(InvalidArgumentException::class);
     $this->receiveParameter->setVisibilityTimeout([]);
 }
コード例 #2
0
ファイル: AdapterTest.php プロジェクト: stakhanovist/queue
 /**
  * This test checks if a message is invisibile the proper amount of time
  *
  * Usually adapters that support deleteMessage() by nature will support visibility.
  */
 public function testVisibilityTimeout()
 {
     $debug = false;
     $default_timeout = 3;
     // how long we tell the queue to keep the message invisible
     $extra_delay = 2;
     // how long we are willing to wait for the test to finish before failing
     // keep in mind that some queue services are on forigen machines and need network time.
     $queue = $this->createQueue(__FUNCTION__);
     $adapter = $queue->getAdapter();
     $this->checkAdapterSupport('deleteQueue');
     if (!$queue->isReceiveParamSupported(ReceiveParameters::VISIBILITY_TIMEOUT)) {
         $adapter->deleteQueue($queue->getName());
         $this->markTestSkipped($this->getAdapterName() . ' does not support visibility of messages');
         return;
     }
     $body = 'hello world';
     $queue->send($body);
     $receiveParams = new ReceiveParameters();
     $receiveParams->setVisibilityTimeout($default_timeout);
     $messages = $queue->receive(1, $receiveParams);
     // messages are deleted at the bottom.
     if ($adapter instanceof CountMessagesCapableInterface) {
         $this->assertEquals(1, $queue->count());
     }
     $start = microtime(true);
     $this->assertTrue($messages instanceof MessageIterator);
     $timeout = $start + $default_timeout + $extra_delay;
     $found = false;
     $check = microtime(true);
     $end = false;
     do {
         $search = $queue->receive(1);
         if (microtime(true) - $check > 0.1) {
             $check = microtime(true);
             if ($debug) {
                 echo "Checking - found ", count($search), " messages at : ", $check, "\n";
             }
         }
         if (count($search) > 0) {
             if ($search->current()->getContent() == $body) {
                 $found = true;
                 $end = microtime(true);
             } else {
                 $this->fail('sent message is not the message received');
             }
         }
     } while ($found === false && microtime(true) < $timeout);
     // record end time
     if ($end === false) {
         $end = microtime(true);
     }
     $duration = sprintf("%5.2f seconds", $end - $start);
     /*
     There has to be some fuzzyness regarding comparisons because while
     the timeout may be honored, the actual code time, database querying
     and so on, may take more than the timeout time.
     */
     if ($found) {
         if (abs($end - $start - $default_timeout) < $extra_delay) {
             // stupid Db Adapter responds in a fraction less than a second.
             $this->assertTrue(true, 'Message was invisible for the required amount of time');
         } else {
             if ($debug) {
                 echo 'Duration: ', $duration, "\n";
             }
             $this->fail('Message was NOT invisible for the required amount of time');
         }
     } else {
         $this->fail('Message never became visibile duration:' . $duration);
     }
     if ($debug) {
         echo "Duration {$duration}\n";
     }
     // now we delete the messages
     if ($adapter instanceof DeleteMessageCapableInterface) {
         foreach ($messages as $msg) {
             $adapter->deleteMessage($queue, $msg);
         }
     }
     // delete the queue we created
     $adapter->deleteQueue($queue->getName());
 }