Skip to content

Commit 4226d07

Browse files
committed
Merge pull request #16 from graze/feature/purge-queue
Add purge to ProducerInterface
2 parents 150f49d + 0da424b commit 4226d07

File tree

10 files changed

+103
-6
lines changed

10 files changed

+103
-6
lines changed

src/Adapter/AdapterInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Graze\Queue\Adapter\Exception\FailedAcknowledgementException;
1818
use Graze\Queue\Adapter\Exception\FailedEnqueueException;
19+
use Graze\Queue\Adapter\Exception\UnsupportedOperationException;
1920
use Graze\Queue\Message\MessageFactoryInterface;
2021
use Graze\Queue\Message\MessageInterface;
2122
use Iterator;
@@ -43,4 +44,6 @@ public function dequeue(MessageFactoryInterface $factory, $limit);
4344
* @throws FailedEnqueueException
4445
*/
4546
public function enqueue(array $messages);
47+
48+
public function purge();
4649
}

src/Adapter/ArrayAdapter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
final class ArrayAdapter implements AdapterInterface
2323
{
2424
/**
25-
* @param MessageInterface[]
25+
* @param array
2626
*/
2727
protected $queue = [];
2828

@@ -70,6 +70,14 @@ public function enqueue(array $messages)
7070
}
7171
}
7272

73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function purge()
77+
{
78+
$this->queue = [];
79+
}
80+
7381
/**
7482
* @param MessageInterface $message
7583
*/

src/Adapter/SqsAdapter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ public function enqueue(array $messages)
190190
}
191191
}
192192

193+
/**
194+
* {@inheritdoc}
195+
*/
196+
public function purge()
197+
{
198+
$this->client->purgeQueue(['QueueUrl' => $this->getQueueUrl()]);
199+
}
200+
193201
/**
194202
* @param MessageInterface[] $messages
195203
* @return array
@@ -254,7 +262,7 @@ protected function getOption($name, $default = null)
254262
*/
255263
protected function getQueueUrl()
256264
{
257-
if (!$this->url) {
265+
if (! $this->url) {
258266
$result = $this->client->createQueue([
259267
'QueueName' => $this->name,
260268
'Attributes' => $this->options,

src/Client.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public function send(array $messages)
8484
return $this->adapter->enqueue($messages);
8585
}
8686

87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function purge()
91+
{
92+
return $this->adapter->purge();
93+
}
94+
8795
/**
8896
* @return callable
8997
*/

src/ConsumerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
interface ConsumerInterface
2020
{
2121
/**
22-
* @param callable $worker
23-
* @param integer|null $limit Integer limit or Null no limit
22+
* @param callable $worker
23+
* @param integer $limit
2424
*/
2525
public function receive(callable $worker, $limit = 1);
2626
}

src/ProducerInterface.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@
1414

1515
namespace Graze\Queue;
1616

17+
use Graze\Queue\Adapter\Exception\UnsupportedOperationException;
1718
use Graze\Queue\Message\MessageInterface;
1819

1920
interface ProducerInterface
2021
{
2122
/**
22-
* @return MessageInterface
23+
* @param string $body
24+
* @param array $options
2325
*/
2426
public function create($body, array $options = []);
2527

2628
/**
27-
* @param MessageInterface[] $message
29+
* @param array $message
2830
*/
2931
public function send(array $messages);
32+
33+
public function purge();
3034
}

tests/integration/ArrayIntegrationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,16 @@ public function testSend()
8282
{
8383
$this->client->send([$this->client->create('foo')]);
8484
}
85+
86+
public function testPurge()
87+
{
88+
$this->client->purge();
89+
90+
$msgs = [];
91+
$this->client->receive(function ($msg) use (&$msgs) {
92+
$msgs[] = $msg;
93+
}, null);
94+
95+
assertThat($msgs, is(emptyArray()));
96+
}
8597
}

tests/integration/SqsIntegrationTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,33 @@ public function testSend()
202202

203203
$this->client->send([$this->client->create('foo')]);
204204
}
205+
206+
public function testPurge()
207+
{
208+
$url = $this->stubCreateQueue();
209+
$timeout = $this->stubQueueVisibilityTimeout($url);
210+
211+
$receiveModel = m::mock('Aws\ResultInterface');
212+
$receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([]);
213+
$this->sqsClient->shouldReceive('receiveMessage')->once()->with([
214+
'QueueUrl' => $url,
215+
'AttributeNames' => ['All'],
216+
'MaxNumberOfMessages' => 1,
217+
'VisibilityTimeout' => $timeout
218+
])->andReturn($receiveModel);
219+
220+
$purgeModel = m::mock('Aws\ResultInterface');
221+
$this->sqsClient->shouldReceive('purgeQueue')->once()->with([
222+
'QueueUrl' => $url,
223+
])->andReturn($purgeModel);
224+
225+
$this->client->purge();
226+
227+
$msgs = [];
228+
$this->client->receive(function ($msg) use (&$msgs) {
229+
$msgs[] = $msg;
230+
});
231+
232+
assertThat($msgs, is(emptyArray()));
233+
}
205234
}

tests/unit/Adapter/ArrayAdapterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,17 @@ public function testEnqueue()
107107

108108
assertThat(iterator_to_array($iterator), is(identicalTo($merged)));
109109
}
110+
111+
public function testPurge()
112+
{
113+
$iterator = $this->adapter->dequeue($this->factory, 10);
114+
115+
assertThat(iterator_to_array($iterator), is(nonEmptyArray()));
116+
117+
$this->adapter->purge();
118+
119+
$iterator = $this->adapter->dequeue($this->factory, 10);
120+
121+
assertThat(iterator_to_array($iterator), is(emptyArray()));
122+
}
110123
}

tests/unit/Adapter/SqsAdapterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,16 @@ public function testReceiveMessageWaitTimeSecondsOption()
220220
assertThat($iterator, is(anInstanceOf('Generator')));
221221
assertThat(iterator_to_array($iterator), is(equalTo($this->messages)));
222222
}
223+
224+
public function testPurge()
225+
{
226+
$adapter = new SqsAdapter($this->client, 'foo');
227+
$url = $this->stubCreateQueue('foo');
228+
229+
$this->client->shouldReceive('purgeQueue')->once()->with([
230+
'QueueUrl' => $url,
231+
])->andReturn($this->model);
232+
233+
assertThat($adapter->purge(), is(nullValue()));
234+
}
223235
}

0 commit comments

Comments
 (0)