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

[SNSQS] Fix issue with delay #909

Merged
merged 1 commit into from
Jun 25, 2019
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
15 changes: 13 additions & 2 deletions pkg/snsqs/SnsQsProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,28 @@ public function send(Destination $destination, Message $message): void
}
}

/**
* Delivery delay is supported by SQSProducer.
*
* @param int|null $deliveryDelay
*
* @return Producer
*/
public function setDeliveryDelay(int $deliveryDelay = null): Producer
{
$this->getSnsProducer()->setDeliveryDelay($deliveryDelay);
$this->getSqsProducer()->setDeliveryDelay($deliveryDelay);

return $this;
}

/**
* Delivery delay is supported by SQSProducer.
*
* @return int|null
*/
public function getDeliveryDelay(): ?int
{
return $this->getSnsProducer()->getDeliveryDelay();
return $this->getSqsProducer()->getDeliveryDelay();
}

public function setPriority(int $priority = null): Producer
Expand Down
132 changes: 132 additions & 0 deletions pkg/snsqs/Tests/SnsQsProducerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Enqueue\SnsQs\Tests;

use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsProducer;
use Enqueue\SnsQs\SnsQsMessage;
use Enqueue\SnsQs\SnsQsProducer;
use Enqueue\SnsQs\SnsQsQueue;
use Enqueue\SnsQs\SnsQsTopic;
use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsProducer;
use Enqueue\Test\ClassExtensionTrait;
use Interop\Queue\Destination;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Producer;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;

class SnsQsProducerTest extends TestCase
{
use ClassExtensionTrait;

public function testShouldImplementProducerInterface()
{
$this->assertClassImplements(Producer::class, SnsQsProducer::class);
}

public function testCouldBeConstructedWithRequiredArguments()
{
new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock());
}

public function testShouldThrowIfMessageIsInvalidType()
{
$this->expectException(InvalidMessageException::class);
$this->expectExceptionMessage('The message must be an instance of Enqueue\SnsQs\SnsQsMessage but it is Double\Message\P1');

$producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock());

$message = $this->prophesize(Message::class)->reveal();

$producer->send(new SnsQsTopic(''), $message);
}

public function testShouldThrowIfDestinationOfInvalidType()
{
$this->expectException(InvalidDestinationException::class);

$producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock());

$destination = $this->prophesize(Destination::class)->reveal();

$producer->send($destination, new SnsQsMessage());
}

public function testShouldSetDeliveryDelayToSQSProducer()
{
$delay = 10;

$sqsProducerStub = $this->prophesize(SqsProducer::class);
$sqsProducerStub->setDeliveryDelay(Argument::is($delay))->shouldBeCalledTimes(1);

$sqsMock = $this->createSqsContextMock();
$sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal());

$producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock);

$producer->setDeliveryDelay($delay);
}

public function testShouldGetDeliveryDelayFromSQSProducer()
{
$delay = 10;

$sqsProducerStub = $this->prophesize(SqsProducer::class);
$sqsProducerStub->getDeliveryDelay()->willReturn($delay);

$sqsMock = $this->createSqsContextMock();
$sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal());

$producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock);

$this->assertEquals($delay, $producer->getDeliveryDelay());
}

public function testShouldSendSnsTopicMessageToSnsProducer()
{
$snsMock = $this->createSnsContextMock();
$destination = new SnsQsTopic('');

$snsProducerStub = $this->prophesize(SnsProducer::class);
$snsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce();

$snsMock->method('createProducer')->willReturn($snsProducerStub->reveal());

$producer = new SnsQsProducer($snsMock, $this->createSqsContextMock());
$producer->send($destination, new SnsQsMessage());
}

public function testShouldSendSqsMessageToSqsProducer()
{
$sqsMock = $this->createSqsContextMock();
$destination = new SnsQsQueue('');

$snsProducerStub = $this->prophesize(SqsProducer::class);
$snsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce();

$sqsMock->method('createProducer')->willReturn($snsProducerStub->reveal());

$producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock);
$producer->send($destination, new SnsQsMessage());
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|SnsContext
*/
private function createSnsContextMock(): SnsContext
{
return $this->createMock(SnsContext::class);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|SqsContext
*/
private function createSqsContextMock(): SqsContext
{
return $this->createMock(SqsContext::class);
}
}