/** * @covers MicrosoftAzure\Storage\Queue\Models\PeekMessagesOptions::setNumberOfMessages */ public function testSetNumberOfMessages() { // Setup $peekMessagesOptions = new PeekMessagesOptions(); $expected = 10; // Test $peekMessagesOptions->setNumberOfMessages($expected); // Assert $actual = $peekMessagesOptions->getNumberOfMessages(); $this->assertEquals($expected, $actual); }
public function testCheckPeekMessagesOptions() { $options = new PeekMessagesOptions(); $this->assertNull($options->getTimeout(), 'Default PeekMessagesOptions->getTimeout'); $this->assertNull($options->getNumberOfMessages(), 'Default PeekMessagesOptions->getNumberOfMessages'); $options->setTimeout(self::INT_MAX_VALUE); $options->setNumberOfMessages(0); $this->assertEquals(self::INT_MAX_VALUE, $options->getTimeout(), 'Set PeekMessagesOptions->getTimeout'); $this->assertEquals(0, $options->getNumberOfMessages(), 'Set PeekMessagesOptions->getNumberOfMessages'); }
/** * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::clearMessages * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::createMessage * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::listMessages * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::peekMessages */ private function peekMessagesWorker($options) { self::println('Trying $options: ' . self::tmptostring($options)); $queue = QueueServiceFunctionalTestData::$testQueueNames; $queue = $queue[0]; $this->restProxy->clearMessages($queue); // Put three messages into the queue. $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText()); $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText()); $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText()); // Default is 1 message $effectiveNumOfMessages = is_null($options) || is_null($options->getNumberOfMessages()) ? 1 : $options->getNumberOfMessages(); $effectiveNumOfMessages = $effectiveNumOfMessages < 0 ? 0 : $effectiveNumOfMessages; $expectedNumMessagesFirst = $effectiveNumOfMessages > 3 ? 3 : $effectiveNumOfMessages; try { $res = is_null($options) ? $this->restProxy->peekMessages($queue) : $this->restProxy->peekMessages($queue, $options); if (is_null($options)) { $options = new PeekMessagesOptions(); } if (!is_null($options->getNumberOfMessages()) && ($options->getNumberOfMessages() < 1 || $options->getNumberOfMessages() > 32)) { $this->assertTrue(false, 'Expect getNumberOfMessages < 1 or 32 < numMessages in $options to throw'); } else { if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) { $this->assertTrue(false, 'Expect negative timeouts in $options to throw'); } } $this->assertEquals($expectedNumMessagesFirst, count($res->getQueueMessages()), 'getQueueMessages() count'); $opts = new PeekMessagesOptions(); $opts->setNumberOfMessages(32); $res2 = $this->restProxy->peekMessages($queue, $opts); $this->assertEquals(3, count($res2->getQueueMessages()), 'getQueueMessages() count'); $this->restProxy->listMessages($queue); $opts = new PeekMessagesOptions(); $opts->setNumberOfMessages(32); $res3 = $this->restProxy->peekMessages($queue, $opts); $this->assertEquals(2, count($res3->getQueueMessages()), 'getQueueMessages() count'); } catch (ServiceException $e) { if (is_null($options)) { $options = new PeekMessagesOptions(); } if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) { $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode'); } else { if (!is_null($options->getNumberOfMessages()) && ($options->getNumberOfMessages() < 1 || $options->getNumberOfMessages() > 32)) { $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode'); } else { throw $e; } } } $this->restProxy->clearMessages($queue); }
/** * Retrieves a message from the front of the queue, without changing * the message visibility. * * @param string $queueName The queue name. * @param PeekMessagesOptions $options The optional parameters. * * @return MicrosoftAzure\Storage\Common\Models\PeekMessagesResult */ public function peekMessages($queueName, $options = null) { Validate::isString($queueName, 'queueName'); Validate::notNullOrEmpty($queueName, 'queueName'); $method = Resources::HTTP_GET; $headers = array(); $queryParams = array(); $postParams = array(); $path = $queueName . '/messages'; $statusCode = Resources::STATUS_OK; if (is_null($options)) { $options = new PeekMessagesOptions(); } $messagesCount = $options->getNumberOfMessages(); $timeout = $options->getTimeout(); $this->addOptionalQueryParam($queryParams, Resources::QP_PEEK_ONLY, 'true'); $this->addOptionalQueryParam($queryParams, Resources::QP_TIMEOUT, $timeout); $this->addOptionalQueryParam($queryParams, Resources::QP_NUM_OF_MESSAGES, $messagesCount); $response = $this->send($method, $headers, $queryParams, $postParams, $path, $statusCode); $parsed = $this->dataSerializer->unserialize($response->getBody()); return PeekMessagesResult::create($parsed); }