コード例 #1
0
 public function testQueueConsumer()
 {
     $c = new AMQPPecl(AMQPCredentials::createDefault());
     $channel = $c->createChannel(1);
     $channel->exchangeDeclare(self::EXCHANGE_NAME, AMQPExchangeConfig::create()->setType(new AMQPExchangeType(AMQPExchangeType::DIRECT))->setDurable(true));
     $inQueueCount = $channel->queueDeclare(self::QUEUE_NAME, AMQPQueueConfig::create()->setDurable(true));
     $this->assertSame(0, $inQueueCount);
     $channel->queueBind(self::QUEUE_NAME, self::EXCHANGE_NAME, self::ROUTING_KEY);
     //cleanup
     $channel->queuePurge(self::QUEUE_NAME);
     for ($i = 1; $i <= self::COUNT_OF_PUBLISH; $i++) {
         $channel->basicPublish(self::EXCHANGE_NAME, self::ROUTING_KEY, AMQPOutgoingMessage::create()->setBody("message {$i}")->setTimestamp(Timestamp::makeNow())->setAppId(__CLASS__)->setMessageId($i)->setContentEncoding('utf-8'));
     }
     $consumer = new AMQPTestCaseQueueConsumer($channel);
     $channel->basicConsume(self::QUEUE_NAME, false, $consumer);
     $i = 0;
     while (true) {
         $message = $consumer->getNextDelivery();
         //send acknowledge to RabbitMQ
         $channel->basicAck($message->getDeliveryTag(), false);
         $i++;
         $this->assertSame($consumer->getConsumerTag(), $message->getConsumerTag());
         $this->assertEquals($i, $message->getMessageId());
         $this->assertEquals(__CLASS__, $message->getAppId());
         $this->assertEquals("message {$i}", $message->getBody());
         if ($i == self::COUNT_OF_PUBLISH) {
             break;
         }
     }
     $channel->basicCancel($consumer->getConsumerTag());
     //observer logic test
     $this->assertSame('ABBBBBC', $consumer->getCheckString());
     //drop channels and close connection
     $c->disconnect();
     $c = new AMQPPecl(AMQPCredentials::createDefault());
     $channel = $c->createChannel(1);
     $channel->exchangeDeclare(self::EXCHANGE_NAME, AMQPExchangeConfig::create()->setType(new AMQPExchangeType(AMQPExchangeType::DIRECT))->setDurable(true));
     //queue must be empty, because we sent acknowledge
     $inQueueCount = $channel->queueDeclare(self::QUEUE_NAME, AMQPQueueConfig::create()->setDurable(true));
     $this->assertSame(0, $inQueueCount);
     //cleanup
     $channel->queuePurge(self::QUEUE_NAME);
 }
コード例 #2
0
 protected function exchangeToExchangeCleanup()
 {
     $c = new AMQPPecl(AMQPCredentials::createDefault());
     $channel = $c->createChannel(1);
     $this->exchangeDeclare($channel, 'basic');
     $this->exchangeDeclare($channel, 'exchangeBinded');
     $this->queueDeclare($channel, 'exchangeBinded');
     $this->queueDeclare($channel, 'basic');
     $this->queueBind($channel, 'basic');
     $this->queueBind($channel, 'exchangeBinded');
     $channel->queueBind(self::$queueList['basic']['name'], self::$queueList['exchangeBinded']['exchange'], self::$queueList['basic']['key']);
     $this->queueUnbind($channel, 'basic');
     $this->queueUnbind($channel, 'exchangeBinded');
     $channelInterface = $channel->queueUnbind(self::$queueList['basic']['name'], self::$queueList['exchangeBinded']['exchange'], self::$queueList['basic']['key']);
     $this->assertInstanceOf('AMQPChannelInterface', $channelInterface);
     $this->queueDelete($channel, 'exchangeBinded');
     $this->queueDelete($channel, 'basic');
     $this->exchangeDelete($channel, 'basic');
     $this->exchangeDelete($channel, 'exchangeBinded');
 }