Skip to content

Commit 96a52b7

Browse files
author
Jamie Hannaford
committed
Merge pull request #448 from ycombinator/fix-pagination-for-queues
Fix iteration of queue messages
2 parents fd8f91f + 2b34bb7 commit 96a52b7

File tree

7 files changed

+64
-19
lines changed

7 files changed

+64
-19
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Test Instructions
2727

2828
### To run unit tests:
2929
```bash
30-
phpunit
30+
vendor/bin/phpunit
3131
```
3232

3333
### To run the full suite of acceptance tests:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"psr/log": "~1.0"
2727
},
2828
"require-dev" : {
29+
"phpunit/phpunit": "4.3.*",
2930
"guzzle/guzzle": "~3.8",
3031
"satooshi/php-coveralls": "0.6.*@dev",
3132
"jakub-onderka/php-parallel-lint": "0.*",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright 2012-2014 Rackspace US, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCloud\Queues\Collection;
19+
20+
use OpenCloud\Common\Collection\PaginatedIterator;
21+
22+
class MessageIterator extends PaginatedIterator
23+
{
24+
protected function shouldAppend()
25+
{
26+
return $this->nextUrl ||
27+
($this->position % $this->getOption('limit.page') == 0);
28+
}
29+
}

lib/OpenCloud/Queues/Resource/Queue.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OpenCloud\Common\Http\Message\Formatter;
2323
use OpenCloud\Common\Resource\PersistentResource;
2424
use OpenCloud\Queues\Exception;
25+
use OpenCloud\Queues\Collection\MessageIterator;
2526
use OpenCloud\Common\Metadata;
2627

2728
/**
@@ -250,7 +251,7 @@ public function createMessages(array $messages)
250251
* messages as well as unclaimed messages. If not specified, defaults
251252
* to FALSE (i.e. only unclaimed messages are returned).
252253
*
253-
* @return Collection
254+
* @return \OpenCloud\Queues\Collection\MessageIterator
254255
*/
255256
public function listMessages(array $options = array())
256257
{
@@ -276,7 +277,7 @@ public function listMessages(array $options = array())
276277
'limit.page' => 10
277278
);
278279

279-
return PaginatedIterator::factory($this, $options);
280+
return MessageIterator::factory($this, $options);
280281
}
281282

282283
/**

tests/OpenCloud/Smoke/Unit/AbstractUnit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @link
3030
*/
31-
abstract class AbstractUnit
31+
abstract class AbstractUnit extends \PHPUnit_Framework_TestCase
3232
{
3333
/**
3434
* The credentials cache filename.

tests/OpenCloud/Smoke/Unit/Queues.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace OpenCloud\Smoke\Unit;
1919

20+
use OpenCloud\Queues\Resource\Queue;
21+
2022
class Queues extends AbstractUnit implements UnitInterface
2123
{
2224
const QUEUE_NAME = 'test_queue';
@@ -71,33 +73,45 @@ public function doMessageBlock()
7173
$this->step('Messages');
7274

7375
// post
76+
$numMessagesCreated = 0;
7477
$this->stepInfo('Create messages for queue %s', $this->queue->getName());
7578
$this->queue->createMessage(array(
7679
'body' => (object) array(
7780
'instructions' => 'Do it now!'
7881
),
7982
'ttl' => 300
8083
));
84+
++$numMessagesCreated;
85+
86+
for ($creationBatch = 0; $creationBatch < 3; ++$creationBatch) {
87+
$messages = array();
88+
for ($messageIndex = 0; $messageIndex < Queue::MAX_POST_MESSAGES; ++$messageIndex) {
89+
$messages[] = array(
90+
'body' => (object) array('message_number' => ($creationBatch * Queue::MAX_POST_MESSAGES) + $messageIndex + 1),
91+
'ttl' => mt_rand(300, 600)
92+
);
93+
++$numMessagesCreated;
94+
}
95+
$this->queue->createMessages($messages);
96+
}
8197

82-
$this->queue->createMessages(array(
83-
array(
84-
'body' => (object) array('foo' => 'bar'),
85-
'ttl' => 700
86-
),
87-
array(
88-
'body' => (object) array('baz' => 'lol'),
89-
'ttl' => 600
90-
)
91-
));
92-
93-
// list
98+
// list ( 'echo' => true is needed to list client's own messages)
9499
$step = $this->stepInfo('List messages for queue %s', $this->queue->getName());
95-
$messages = $this->queue->listMessages();
100+
$messages = $this->queue->listMessages(array(
101+
'echo' => true
102+
));
96103
$ids = array();
104+
$numMessagesListed = 0;
105+
$step->stepInfo("%-30s | %-40s", "Message ID", "Message body");
106+
$step->stepInfo("%-30s | %-40s", str_repeat("-", 30), str_repeat("-", 40));
97107
foreach ($messages as $message) {
98-
$step->stepInfo($message->getId());
108+
$step->stepInfo("%-30s | %-40s", $message->getId(), json_encode($message->getBody()));
99109
$ids[] = $message->getId();
110+
++$numMessagesListed;
100111
}
112+
$this->stepInfo("Number of messages to be listed: " . $numMessagesCreated);
113+
$this->stepInfo("Number of messages actually listed: " . $numMessagesListed);
114+
$this->assertEquals($numMessagesCreated, $numMessagesListed);
101115

102116
array_pop($ids);
103117
}

tests/OpenCloud/Tests/Queues/Resource/QueueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function test_Get_Message()
122122
public function test_List_Message()
123123
{
124124
$this->assertInstanceOf(
125-
self::COLLECTION_CLASS,
125+
'OpenCloud\Queues\Collection\MessageIterator',
126126
$this->queue->setName('foo')->listMessages(array(
127127
'ids' => array(100, 901, 58)
128128
))

0 commit comments

Comments
 (0)