Skip to content

Commit 3c3d3e2

Browse files
TekillDavertMik
authored andcommitted
Added AMQP Module option to use only one channel (#4281)
1 parent f25ce7c commit 3c3d3e2

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

src/Codeception/Module/AMQP.php

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22
namespace Codeception\Module;
33

4+
use Codeception\Exception\ModuleException as ModuleException;
45
use Codeception\Lib\Interfaces\RequiresPackage;
56
use Codeception\Module as CodeceptionModule;
6-
use Codeception\Exception\ModuleException as ModuleException;
77
use Codeception\TestInterface;
88
use Exception;
99
use PhpAmqpLib\Channel\AMQPChannel;
1010
use PhpAmqpLib\Connection\AMQPStreamConnection;
11-
use PhpAmqpLib\Message\AMQPMessage;
1211
use PhpAmqpLib\Exception\AMQPProtocolChannelException;
12+
use PhpAmqpLib\Message\AMQPMessage;
1313

1414
/**
1515
* This module interacts with message broker software that implements
@@ -27,6 +27,7 @@
2727
* * vhost: '/' - vhost to connect
2828
* * cleanup: true - defined queues will be purged before running every test.
2929
* * queues: [mail, twitter] - queues to cleanup
30+
* * single_channel - create and use only one channel during test execution
3031
*
3132
* ### Example
3233
*
@@ -39,6 +40,7 @@
3940
* password: 'guest'
4041
* vhost: '/'
4142
* queues: [queue1, queue2]
43+
* single_channel: false
4244
*
4345
* ## Public Properties
4446
*
@@ -47,12 +49,13 @@
4749
class AMQP extends CodeceptionModule implements RequiresPackage
4850
{
4951
protected $config = [
50-
'host' => 'localhost',
51-
'username' => 'guest',
52-
'password' => 'guest',
53-
'port' => '5672',
54-
'vhost' => '/',
55-
'cleanup' => true,
52+
'host' => 'localhost',
53+
'username' => 'guest',
54+
'password' => 'guest',
55+
'port' => '5672',
56+
'vhost' => '/',
57+
'cleanup' => true,
58+
'single_channel' => false
5659
];
5760

5861
/**
@@ -61,9 +64,9 @@ class AMQP extends CodeceptionModule implements RequiresPackage
6164
public $connection;
6265

6366
/**
64-
* @var AMQPChannel
67+
* @var int
6568
*/
66-
protected $channel;
69+
protected $channelId;
6770

6871
protected $requiredFields = ['host', 'username', 'password', 'vhost'];
6972

@@ -115,7 +118,7 @@ public function pushToExchange($exchange, $message, $routing_key = null)
115118
$message = $message instanceof AMQPMessage
116119
? $message
117120
: new AMQPMessage($message);
118-
$this->connection->channel()->basic_publish($message, $exchange, $routing_key);
121+
$this->getChannel()->basic_publish($message, $exchange, $routing_key);
119122
}
120123

121124
/**
@@ -137,8 +140,8 @@ public function pushToQueue($queue, $message)
137140
? $message
138141
: new AMQPMessage($message);
139142

140-
$this->connection->channel()->queue_declare($queue);
141-
$this->connection->channel()->basic_publish($message, '', $queue);
143+
$this->getChannel()->queue_declare($queue);
144+
$this->getChannel()->basic_publish($message, '', $queue);
142145
}
143146

144147
/**
@@ -176,7 +179,7 @@ public function declareExchange(
176179
$arguments = null,
177180
$ticket = null
178181
) {
179-
return $this->connection->channel()->exchange_declare(
182+
return $this->getChannel()->exchange_declare(
180183
$exchange,
181184
$type,
182185
$passive,
@@ -221,7 +224,7 @@ public function declareQueue(
221224
$arguments = null,
222225
$ticket = null
223226
) {
224-
return $this->connection->channel()->queue_declare(
227+
return $this->getChannel()->queue_declare(
225228
$queue,
226229
$passive,
227230
$durable,
@@ -263,7 +266,7 @@ public function bindQueueToExchange(
263266
$arguments = null,
264267
$ticket = null
265268
) {
266-
return $this->connection->channel()->queue_bind(
269+
return $this->getChannel()->queue_bind(
267270
$queue,
268271
$exchange,
269272
$routing_key,
@@ -291,7 +294,7 @@ public function bindQueueToExchange(
291294
*/
292295
public function seeMessageInQueueContainsText($queue, $text)
293296
{
294-
$msg = $this->connection->channel()->basic_get($queue);
297+
$msg = $this->getChannel()->basic_get($queue);
295298
if (!$msg) {
296299
$this->fail("Message was not received");
297300
}
@@ -316,7 +319,7 @@ public function seeMessageInQueueContainsText($queue, $text)
316319
*/
317320
public function grabMessageFromQueue($queue)
318321
{
319-
$message = $this->connection->channel()->basic_get($queue);
322+
$message = $this->getChannel()->basic_get($queue);
320323
return $message;
321324
}
322325

@@ -337,7 +340,7 @@ public function purgeQueue($queueName = '')
337340
throw new ModuleException(__CLASS__, "'$queueName' doesn't exist in queues config list");
338341
}
339342

340-
$this->connection->channel()->queue_purge($queueName, true);
343+
$this->getChannel()->queue_purge($queueName, true);
341344
}
342345

343346
/**
@@ -354,6 +357,17 @@ public function purgeAllQueues()
354357
$this->cleanup();
355358
}
356359

360+
/**
361+
* @return AMQPChannel
362+
*/
363+
protected function getChannel()
364+
{
365+
if ($this->config['single_channel'] && $this->channelId === null) {
366+
$this->channelId = $this->connection->get_free_channel_id();
367+
}
368+
return $this->connection->channel($this->channelId);
369+
}
370+
357371
protected function cleanup()
358372
{
359373
if (!isset($this->config['queues'])) {
@@ -364,7 +378,7 @@ protected function cleanup()
364378
}
365379
foreach ($this->config['queues'] as $queue) {
366380
try {
367-
$this->connection->channel()->queue_purge($queue);
381+
$this->getChannel()->queue_purge($queue);
368382
} catch (AMQPProtocolChannelException $e) {
369383
// ignore if exchange/queue doesn't exist and rethrow exception if it's something else
370384
if ($e->getCode() !== 404) {

0 commit comments

Comments
 (0)