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

ProducerV2 For SimpleClient #115

Merged
merged 3 commits into from
Jun 16, 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
2 changes: 1 addition & 1 deletion pkg/enqueue/Client/ProducerV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function sendCommand($command, $message, $needReply = false)
$message->setScope(Message::SCOPE_APP);

if ($needReply) {
return $this->rpcClient->callAsync(Config::COMMAND_TOPIC, $message, 60);
return $this->rpcClient->callAsync(Config::COMMAND_TOPIC, $message, 60000);
}

$this->realProducer->send(Config::COMMAND_TOPIC, $message);
Expand Down
9 changes: 5 additions & 4 deletions pkg/enqueue/Client/RpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ public function callAsync($topic, $message, $timeout)

$correlationId = $message->getCorrelationId();

$receive = function () use ($replyQueue, $timeout, $correlationId) {
$endTime = time() + ((int) ($timeout / 1000));
$receive = function (Promise $promise, $promiseTimeout) use ($replyQueue, $timeout, $correlationId) {
$runTimeout = $promiseTimeout ?: $timeout;
$endTime = time() + ((int) ($runTimeout / 1000));
$consumer = $this->context->createConsumer($replyQueue);

do {
if ($message = $consumer->receive($timeout)) {
if ($message = $consumer->receive($runTimeout)) {
if ($message->getCorrelationId() === $correlationId) {
$consumer->acknowledge($message);

Expand All @@ -94,7 +95,7 @@ public function callAsync($topic, $message, $timeout)
}
} while (time() < $endTime);

throw TimeoutException::create($timeout, $correlationId);
throw TimeoutException::create($runTimeout, $correlationId);
};

$receiveNoWait = function () use ($replyQueue, $correlationId) {
Expand Down
13 changes: 8 additions & 5 deletions pkg/enqueue/Rpc/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ public function getMessage()
/**
* Blocks until message received or timeout expired.
*
* @param int $timeout
*
* @throws TimeoutException if the wait timeout is reached
*
* @return PsrMessage
*/
public function receive()
public function receive($timeout = null)
{
if (null == $this->message) {
try {
if ($message = $this->doReceive($this->receiveCallback)) {
if ($message = $this->doReceive($this->receiveCallback, $this, $timeout)) {
$this->message = $message;
}
} finally {
Expand All @@ -89,7 +91,7 @@ public function receive()
public function receiveNoWait()
{
if (null == $this->message) {
if ($message = $this->doReceive($this->receiveNoWaitCallback)) {
if ($message = $this->doReceive($this->receiveNoWaitCallback, $this)) {
$this->message = $message;

call_user_func($this->finallyCallback, $this);
Expand Down Expand Up @@ -119,12 +121,13 @@ public function isDeleteReplyQueue()

/**
* @param \Closure $cb
* @param array $args
*
* @return PsrMessage
*/
private function doReceive(\Closure $cb)
private function doReceive(\Closure $cb, ...$args)
{
$message = call_user_func($cb, $this);
$message = call_user_func_array($cb, $args);

if (null !== $message && false == $message instanceof PsrMessage) {
throw new \RuntimeException(sprintf(
Expand Down
9 changes: 5 additions & 4 deletions pkg/enqueue/Rpc/RpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ public function callAsync(PsrDestination $destination, PsrMessage $message, $tim

$correlationId = $message->getCorrelationId();

$receive = function () use ($replyQueue, $timeout, $correlationId) {
$endTime = time() + ((int) ($timeout / 1000));
$receive = function (Promise $promise, $promiseTimeout) use ($replyQueue, $timeout, $correlationId) {
$runTimeout = $promiseTimeout ?: $timeout;
$endTime = time() + ((int) ($runTimeout / 1000));
$consumer = $this->context->createConsumer($replyQueue);

do {
if ($message = $consumer->receive($timeout)) {
if ($message = $consumer->receive($runTimeout)) {
if ($message->getCorrelationId() === $correlationId) {
$consumer->acknowledge($message);

Expand All @@ -82,7 +83,7 @@ public function callAsync(PsrDestination $destination, PsrMessage $message, $tim
}
} while (time() < $endTime);

throw TimeoutException::create($timeout, $correlationId);
throw TimeoutException::create($runTimeout, $correlationId);
};

$receiveNoWait = function () use ($replyQueue, $correlationId) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/enqueue/Tests/Client/RpcClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public function testShouldReceiveMessageAndAckMessageIfCorrelationEquals()
$consumer
->expects($this->once())
->method('receive')
->with(12345)
->willReturn($receivedMessage)
;
$consumer
Expand Down Expand Up @@ -202,7 +203,7 @@ public function testShouldReceiveMessageAndAckMessageIfCorrelationEquals()

$rpc = new RpcClient($this->createProducerMock(), $context);

$rpc->callAsync('topic', $message, 2)->receive();
$rpc->callAsync('topic', $message, 2)->receive(12345);
}

public function testShouldReceiveNoWaitMessageAndAckMessageIfCorrelationEquals()
Expand Down
27 changes: 26 additions & 1 deletion pkg/enqueue/Tests/Rpc/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,39 @@ public function testCouldSetGetDeleteReplyQueue()
public function testOnReceiveShouldCallReceiveCallBack()
{
$receiveInvoked = false;
$receivecb = function () use (&$receiveInvoked) {
$receivePromise = null;
$receiveTimeout = null;
$receivecb = function ($promise, $timout) use (&$receiveInvoked, &$receivePromise, &$receiveTimeout) {
$receiveInvoked = true;
$receivePromise = $promise;
$receiveTimeout = $timout;
};

$promise = new Promise($receivecb, function () {}, function () {});
$promise->receive();

$this->assertTrue($receiveInvoked);
$this->assertInstanceOf(Promise::class, $receivePromise);
$this->assertNull($receiveTimeout);
}

public function testOnReceiveShouldCallReceiveCallBackWithTimeout()
{
$receiveInvoked = false;
$receivePromise = null;
$receiveTimeout = null;
$receivecb = function ($promise, $timout) use (&$receiveInvoked, &$receivePromise, &$receiveTimeout) {
$receiveInvoked = true;
$receivePromise = $promise;
$receiveTimeout = $timout;
};

$promise = new Promise($receivecb, function () {}, function () {});
$promise->receive(12345);

$this->assertTrue($receiveInvoked);
$this->assertInstanceOf(Promise::class, $receivePromise);
$this->assertSame(12345, $receiveTimeout);
}

public function testOnReceiveNoWaitShouldCallReceiveNoWaitCallBack()
Expand Down
3 changes: 2 additions & 1 deletion pkg/enqueue/Tests/Rpc/RpcClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function testShouldReceiveMessageAndAckMessageIfCorrelationEquals()
$consumer
->expects($this->once())
->method('receive')
->with(12345)
->willReturn($receivedMessage)
;
$consumer
Expand Down Expand Up @@ -147,7 +148,7 @@ public function testShouldReceiveMessageAndAckMessageIfCorrelationEquals()

$rpc = new RpcClient($context);

$rpc->callAsync($queue, $message, 2)->receive();
$rpc->callAsync($queue, $message, 2)->receive(12345);
}

public function testShouldReceiveNoWaitMessageAndAckMessageIfCorrelationEquals()
Expand Down
13 changes: 13 additions & 0 deletions pkg/simple-client/SimpleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Enqueue\Client\Meta\QueueMetaRegistry;
use Enqueue\Client\Meta\TopicMetaRegistry;
use Enqueue\Client\ProducerInterface;
use Enqueue\Client\ProducerV2Interface;
use Enqueue\Client\RouterProcessor;
use Enqueue\Consumption\CallbackProcessor;
use Enqueue\Consumption\ExtensionInterface;
Expand Down Expand Up @@ -185,6 +186,18 @@ public function getProducer($setupBroker = false)
return $this->container->get('enqueue.client.producer');
}

/**
* @param bool $setupBroker
*
* @return ProducerV2Interface
*/
public function getProducerV2($setupBroker = false)
{
$setupBroker && $this->setupBroker();

return $this->container->get('enqueue.client.producer.v2');
}

public function setupBroker()
{
$this->getDriver()->setupBroker();
Expand Down
14 changes: 14 additions & 0 deletions pkg/simple-client/SimpleClientContainerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Enqueue\Client\Meta\QueueMetaRegistry;
use Enqueue\Client\Meta\TopicMetaRegistry;
use Enqueue\Client\Producer;
use Enqueue\Client\ProducerV2;
use Enqueue\Client\RouterProcessor;
use Enqueue\Client\RpcClient;
use Enqueue\Consumption\ChainExtension as ConsumptionChainExtension;
use Enqueue\Consumption\QueueConsumer;
use Enqueue\Symfony\TransportFactoryInterface;
Expand Down Expand Up @@ -93,6 +95,18 @@ public function load(array $configs, ContainerBuilder $container)
new Reference('enqueue.client.driver'),
]);

$container->register('enqueue.client.rpc', RpcClient::class)
->setArguments([
new Reference('enqueue.client.producer'),
new Reference('enqueue.transport.context'),
]);

$container->register('enqueue.client.producer.v2', ProducerV2::class)
->setArguments([
new Reference('enqueue.client.producer'),
new Reference('enqueue.client.rpc'),
]);

$container->register('enqueue.client.meta.topic_meta_registry', TopicMetaRegistry::class)
->setArguments([[]]);

Expand Down