Skip to content

Commit 01c7f38

Browse files
notrixHafsah Haynes
andauthored
Close RPC client channel on exceptions (#31)
Co-authored-by: Hafsah Haynes <hafsah@surfsharkbiz.com>
1 parent d34844a commit 01c7f38

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

RabbitMq/RpcClient.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,14 @@ public function getReplies()
6363
$consumer_tag = $this->getChannel()->basic_consume($this->getQueueName(), '', false, true, false, false, array($this, 'processMessage'));
6464
}
6565

66-
while (count($this->replies) < $this->requests) {
67-
$this->getChannel()->wait(null, false, $this->timeout);
66+
try {
67+
while (count($this->replies) < $this->requests) {
68+
$this->getChannel()->wait(null, false, $this->timeout);
69+
}
70+
} finally {
71+
$this->getChannel()->basic_cancel($consumer_tag);
6872
}
6973

70-
$this->getChannel()->basic_cancel($consumer_tag);
7174
$this->directConsumerTag = null;
7275
$this->requests = 0;
7376
$this->timeout = 0;
@@ -87,7 +90,7 @@ public function processMessage(AMQPMessage $msg)
8790

8891
$this->replies[$msg->get('correlation_id')] = $messageBody;
8992
}
90-
93+
9194
protected function getQueueName()
9295
{
9396
if (null === $this->queueName) {

Tests/RabbitMq/RpcClientTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace OldSound\RabbitMqBundle\Tests\RabbitMq;
44

55
use OldSound\RabbitMqBundle\RabbitMq\RpcClient;
6+
use PhpAmqpLib\Exception\AMQPTimeoutException;
67
use PhpAmqpLib\Message\AMQPMessage;
78
use PHPUnit\Framework\TestCase;
89

@@ -55,9 +56,6 @@ public function testProcessMessageWithNotifyMethod()
5556
$this->assertSame($expectedNotify, $notified);
5657
}
5758

58-
/**
59-
* @expectedException \InvalidArgumentException
60-
*/
6159
public function testInvalidParameterOnNotify()
6260
{
6361
/** @var RpcClient $client */
@@ -66,6 +64,34 @@ public function testInvalidParameterOnNotify()
6664
->disableOriginalConstructor()
6765
->getMock();
6866

67+
$this->expectException('\InvalidArgumentException');
68+
6969
$client->notify('not a callable');
7070
}
71+
72+
public function testChannelCancelOnGetRepliesException()
73+
{
74+
$client = $this->getMockBuilder('\OldSound\RabbitMqBundle\RabbitMq\RpcClient')
75+
->setMethods(null)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
79+
$channel = $this->createMock('\PhpAmqpLib\Channel\AMQPChannel');
80+
$channel->expects($this->any())
81+
->method('getChannelId')
82+
->willReturn('test');
83+
$channel->expects($this->once())
84+
->method('wait')
85+
->willThrowException(new AMQPTimeoutException());
86+
87+
$this->expectException('\PhpAmqpLib\Exception\AMQPTimeoutException');
88+
89+
$channel->expects($this->once())
90+
->method('basic_cancel');
91+
92+
$client->setChannel($channel);
93+
$client->addRequest('a', 'b', 'c');
94+
95+
$client->getReplies();
96+
}
7197
}

0 commit comments

Comments
 (0)