Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Correct implementation of SqsQueue::size #18037

Merged
merged 4 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Illuminate/Queue/SqsQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public function __construct(SqsClient $sqs, $default, $prefix = '')
*/
public function size($queue = null)
{
return (int) $this->sqs->getQueueAttributes([
$response = $this->sqs->getQueueAttributes([
'QueueUrl' => $this->getQueue($queue),
])->get('ApproximateNumberOfMessages');
'AttributeNames' => ['ApproximateNumberOfMessages'],
]);

$attributes = $response->get('Attributes');

return (int) $attributes['ApproximateNumberOfMessages'];
}

/**
Expand Down
13 changes: 7 additions & 6 deletions tests/Queue/QueueSqsJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Illuminate\Tests\Queue;

use Mockery as m;
use Aws\Sqs\SqsClient;
use PHPUnit\Framework\TestCase;

class QueueSqsJobTest extends TestCase
Expand Down Expand Up @@ -37,11 +36,13 @@ public function setUp()
$this->mockedMessageId = 'e3cd03ee-59a3-4ad8-b0aa-ee2e3808ac81';
$this->mockedReceiptHandle = '0NNAq8PwvXuWv5gMtS9DJ8qEdyiUwbAjpp45w2m6M4SJ1Y+PxCh7R930NRB8ylSacEmoSnW18bgd4nK\/O6ctE+VFVul4eD23mA07vVoSnPI4F\/voI1eNCp6Iax0ktGmhlNVzBwaZHEr91BRtqTRM3QKd2ASF8u+IQaSwyl\/DGK+P1+dqUOodvOVtExJwdyDLy1glZVgm85Yw9Jf5yZEEErqRwzYz\/qSigdvW4sm2l7e4phRol\/+IjMtovOyH\/ukueYdlVbQ4OshQLENhUKe7RNN5i6bE\/e5x9bnPhfj2gbM';

$this->mockedJobData = ['Body' => $this->mockedPayload,
'MD5OfBody' => md5($this->mockedPayload),
'ReceiptHandle' => $this->mockedReceiptHandle,
'MessageId' => $this->mockedMessageId,
'Attributes' => ['ApproximateReceiveCount' => 1], ];
$this->mockedJobData = [
'Body' => $this->mockedPayload,
'MD5OfBody' => md5($this->mockedPayload),
'ReceiptHandle' => $this->mockedReceiptHandle,
'MessageId' => $this->mockedMessageId,
'Attributes' => ['ApproximateReceiveCount' => 1],
];
}

public function tearDown()
Expand Down
46 changes: 33 additions & 13 deletions tests/Queue/QueueSqsQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Aws\Result;
use Mockery as m;
use Aws\Sqs\SqsClient;
use PHPUnit\Framework\TestCase;

class QueueSqsQueueTest extends TestCase
Expand All @@ -16,7 +15,6 @@ public function tearDown()

public function setUp()
{

// Use Mockery to mock the SqsClient
$this->sqs = m::mock('Aws\Sqs\SqsClient');

Expand All @@ -35,17 +33,30 @@ public function setUp()
$this->mockedMessageId = 'e3cd03ee-59a3-4ad8-b0aa-ee2e3808ac81';
$this->mockedReceiptHandle = '0NNAq8PwvXuWv5gMtS9DJ8qEdyiUwbAjpp45w2m6M4SJ1Y+PxCh7R930NRB8ylSacEmoSnW18bgd4nK\/O6ctE+VFVul4eD23mA07vVoSnPI4F\/voI1eNCp6Iax0ktGmhlNVzBwaZHEr91BRtqTRM3QKd2ASF8u+IQaSwyl\/DGK+P1+dqUOodvOVtExJwdyDLy1glZVgm85Yw9Jf5yZEEErqRwzYz\/qSigdvW4sm2l7e4phRol\/+IjMtovOyH\/ukueYdlVbQ4OshQLENhUKe7RNN5i6bE\/e5x9bnPhfj2gbM';

$this->mockedSendMessageResponseModel = new Result(['Body' => $this->mockedPayload,
'MD5OfBody' => md5($this->mockedPayload),
'ReceiptHandle' => $this->mockedReceiptHandle,
'MessageId' => $this->mockedMessageId,
'Attributes' => ['ApproximateReceiveCount' => 1], ]);

$this->mockedReceiveMessageResponseModel = new Result(['Messages' => [0 => [
'Body' => $this->mockedPayload,
'MD5OfBody' => md5($this->mockedPayload),
'ReceiptHandle' => $this->mockedReceiptHandle,
'MessageId' => $this->mockedMessageId, ]]]);
$this->mockedSendMessageResponseModel = new Result([
'Body' => $this->mockedPayload,
'MD5OfBody' => md5($this->mockedPayload),
'ReceiptHandle' => $this->mockedReceiptHandle,
'MessageId' => $this->mockedMessageId,
'Attributes' => ['ApproximateReceiveCount' => 1],
]);

$this->mockedReceiveMessageResponseModel = new Result([
'Messages' => [
0 => [
'Body' => $this->mockedPayload,
'MD5OfBody' => md5($this->mockedPayload),
'ReceiptHandle' => $this->mockedReceiptHandle,
'MessageId' => $this->mockedMessageId,
],
],
]);

$this->mockedQueueAttributesResponseModel = new Result([
'Attributes' => [
'ApproximateNumberOfMessages' => 1,
],
]);
}

public function testPopProperlyPopsJobOffOfSqs()
Expand Down Expand Up @@ -91,6 +102,15 @@ public function testPushProperlyPushesJobOntoSqs()
$this->assertEquals($this->mockedMessageId, $id);
}

public function testSizeProperlyReadsSqsQueueSize()
{
$queue = $this->getMockBuilder('Illuminate\Queue\SqsQueue')->setMethods(['getQueue'])->setConstructorArgs([$this->sqs, $this->queueName, $this->account])->getMock();
$queue->expects($this->once())->method('getQueue')->with($this->queueName)->will($this->returnValue($this->queueUrl));
$this->sqs->shouldReceive('getQueueAttributes')->once()->with(['QueueUrl' => $this->queueUrl, 'AttributeNames' => ['ApproximateNumberOfMessages']])->andReturn($this->mockedQueueAttributesResponseModel);
$size = $queue->size($this->queueName);
$this->assertEquals($size, 1);
}

public function testGetQueueProperlyResolvesUrlWithPrefix()
{
$queue = new \Illuminate\Queue\SqsQueue($this->sqs, $this->queueName, $this->prefix);
Expand Down