From 792bb7ba839835f57fb9b5701eb4517ebfb8d403 Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Thu, 10 Jan 2019 11:52:58 +0200 Subject: [PATCH 1/8] make timeout variables configurable, resolves #36 --- README.md | 7 ++++++- src/BowlerServiceProvider.php | 8 ++++++-- src/Connection.php | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fa6779c..c817244 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Tools like the Rabbitmq *[Management](https://www.rabbitmq.com/management.html)* ### Configuration -In order to configure rabbitmq host, port, username and password, add the following inside the connections array in config/queue.php file: +In order to configure rabbitmq host, port, username, password and configure the timeout values too, add the following inside the connections array in config/queue.php file: ```php 'rabbitmq' => [ @@ -42,9 +42,14 @@ In order to configure rabbitmq host, port, username and password, add the follow 'port' => port, 'username' => 'username', 'password' => 'password', + 'connection_timeout' => connection_timeout, + 'read_write_timeout' => read_write_timeout, + 'heartbeat' => heartbeat, ], ``` +The default value for `connection_timeout` and `read_write_timeout` is set to 30 (seconds) and `heartbeat` is set to 15 (seconds). + And register the service provider by adding `Vinelab\Bowler\BowlerServiceProvider::class` to the providers array in `config/app`. ### Producer diff --git a/src/BowlerServiceProvider.php b/src/BowlerServiceProvider.php index 79de40e..1410c9e 100644 --- a/src/BowlerServiceProvider.php +++ b/src/BowlerServiceProvider.php @@ -11,6 +11,7 @@ /** * @author Ali Issa * @author Kinane Domloje + * @author Charalampos Raftopoulos */ class BowlerServiceProvider extends ServiceProvider { @@ -34,8 +35,11 @@ public function register() $rbmqPort = config('queue.connections.rabbitmq.port'); $rbmqUsername = config('queue.connections.rabbitmq.username'); $rbmqPassword = config('queue.connections.rabbitmq.password'); - $this->app->bind(Connection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword) { - return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword); + $rbmqConnectionTimeout = (int) config('queue.connections.rabbitmq.connection_timeout'); + $rbmqReadWriteTimeout = (int) config('queue.connections.rabbitmq.read_write_timeout'); + $rbmqHeartbeat = (int) config('queue.connections.rabbitmq.heartbeat'); + $this->app->bind(Connection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat) { + return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat); }); $this->app->bind( diff --git a/src/Connection.php b/src/Connection.php index 6309102..ba263d4 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -58,18 +58,45 @@ class Connection */ private $password = 'guest'; + /** + * RabbitMQ connection timeout. + * + * @var int + */ + private $connectionTimeout = 30; + + /** + * RabbitMQ read/write timeout. + * + * @var int + */ + private $readWriteTimeout = 30; + + /** + * RabbitMQ heartbeat frequency. + * + * @var int + */ + private $heartbeat = 15; + /** * @param string $host the ip of the rabbitmq server, default: localhost * @param int $port. default: 5672 * @param string $username, default: guest * @param string $password, default: guest + * @param int $connectionTimeout, default: 30 + * @param int $readWriteTimeout, default: 30 + * @param int $heartbeat, default: 15 */ - public function __construct($host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest') + public function __construct($host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest', $connectionTimeout = 30, $readWriteTimeout = 30, $heartbeat = 15) { $this->host = $host; $this->poart = $port; $this->username = $username; $this->password = $password; + $this->connectionTimeout = $connectionTimeout; + $this->readWriteTimeout = $readWriteTimeout; + $this->heartbeat = $heartbeat; $this->connection = new AMQPStreamConnection( $host, @@ -81,11 +108,11 @@ public function __construct($host = 'localhost', $port = 5672, $username = 'gues $login_method = 'AMQPLAIN', $login_response = null, $locale = 'en_US', - $connection_timeout = 30, - $read_write_timeout = 30, + $connectionTimeout, + $readWriteTimeout, $context = null, $keepalive = false, - $heartbeat = 15 + $heartbeat ); $this->channel = $this->connection->channel(); From 05f9d08ef7985e7a37ba57b91469293aad65df74 Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Thu, 10 Jan 2019 11:53:25 +0200 Subject: [PATCH 2/8] Fix typ-o --- src/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index ba263d4..6a289d5 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -91,7 +91,7 @@ class Connection public function __construct($host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest', $connectionTimeout = 30, $readWriteTimeout = 30, $heartbeat = 15) { $this->host = $host; - $this->poart = $port; + $this->port = $port; $this->username = $username; $this->password = $password; $this->connectionTimeout = $connectionTimeout; From 9308b12473908ffc247f2f1af2ff356d4af92728 Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Thu, 10 Jan 2019 14:37:20 +0200 Subject: [PATCH 3/8] set defaults and avoid empty configuration issues --- src/BowlerServiceProvider.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/BowlerServiceProvider.php b/src/BowlerServiceProvider.php index 1410c9e..a5a0645 100644 --- a/src/BowlerServiceProvider.php +++ b/src/BowlerServiceProvider.php @@ -35,9 +35,10 @@ public function register() $rbmqPort = config('queue.connections.rabbitmq.port'); $rbmqUsername = config('queue.connections.rabbitmq.username'); $rbmqPassword = config('queue.connections.rabbitmq.password'); - $rbmqConnectionTimeout = (int) config('queue.connections.rabbitmq.connection_timeout'); - $rbmqReadWriteTimeout = (int) config('queue.connections.rabbitmq.read_write_timeout'); - $rbmqHeartbeat = (int) config('queue.connections.rabbitmq.heartbeat'); + $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout') ? (int) config('queue.connections.rabbitmq.connection_timeout') : 30; + $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout') ? (int) config('queue.connections.rabbitmq.read_write_timeout') : 30; + $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat') ? (int) config('queue.connections.rabbitmq.heartbeat') : 15; + $this->app->bind(Connection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat) { return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat); }); From 5d639fa8fae02ee5f2158202da188276b51b4b43 Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Thu, 10 Jan 2019 14:37:48 +0200 Subject: [PATCH 4/8] add configuration test --- tests/ConnectionTest.php | 12 ++++++++++++ tests/TestCase.php | 3 +++ 2 files changed, 15 insertions(+) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 098519a..1d7fe4a 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -46,4 +46,16 @@ public function test_fetching_consumers_default() $this->assertEquals('response', $response); } + + public function test_set_configurations_default() + { + $heartbeat = $this->app['config']->get('queue.connections.rabbitmq.heartbeat'); + $this->assertEquals(15, $heartbeat); + + $connectionTimeout = $this->app['config']->get('queue.connections.rabbitmq.connection_timeout'); + $this->assertEquals(30, $connectionTimeout); + + $readWriteTimeout = $this->app['config']->get('queue.connections.rabbitmq.read_write_timeout'); + $this->assertEquals(30, $readWriteTimeout); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index f9dc6b7..7fb815c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,5 +35,8 @@ protected function getEnvironmentSetUp($app) $app['config']->set('queue.connections.rabbitmq.port', 5672); $app['config']->set('queue.connections.rabbitmq.username', 'guest'); $app['config']->set('queue.connections.rabbitmq.password', 'guest'); + $app['config']->set('queue.connections.rabbitmq.heartbeat', 15); + $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 30); + $app['config']->set('queue.connections.rabbitmq.connection_timeout', 30); } } From ed3045d6a69c3bcd3b586f3289af5d1802cad24f Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Fri, 11 Jan 2019 16:33:56 +0200 Subject: [PATCH 5/8] enhance example in readme file --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c817244..d20e80e 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ In order to configure rabbitmq host, port, username, password and configure the 'port' => port, 'username' => 'username', 'password' => 'password', - 'connection_timeout' => connection_timeout, - 'read_write_timeout' => read_write_timeout, - 'heartbeat' => heartbeat, + 'connection_timeout' => 30, + 'read_write_timeout' => 30, + 'heartbeat' => 15, ], ``` From b632033e150affc20c879ff0319aac2a22b026b0 Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Fri, 11 Jan 2019 20:48:12 +0200 Subject: [PATCH 6/8] add test for connection with default params --- tests/ConnectionTest.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 1d7fe4a..9a38595 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -3,8 +3,11 @@ namespace Vinelab\Bowler\Tests; use Mockery as M; +use ReflectionClass; use Vinelab\Bowler\Connection; +use Illuminate\Support\Facades\Config; use Vinelab\Http\Client as HTTPClient; +use PhpAmqpLib\Connection\AMQPStreamConnection; /** * @author Abed Halawi @@ -21,7 +24,7 @@ public function test_fetching_consumers_default() $queueName = 'the-queue'; $mClient = M::mock(HTTPClient::class); $request = [ - 'url' => 'localhost:15672/api/queues/%2F/'.$queueName, + 'url' => 'localhost:15672/api/queues/%2F/' . $queueName, 'params' => ['columns' => 'consumer_details.consumer_tag'], 'auth' => [ 'username' => 'guest', @@ -47,15 +50,24 @@ public function test_fetching_consumers_default() $this->assertEquals('response', $response); } - public function test_set_configurations_default() + public function test_set_default_configurations_values() { - $heartbeat = $this->app['config']->get('queue.connections.rabbitmq.heartbeat'); - $this->assertEquals(15, $heartbeat); + $connection = $this->app[Connection::class]; + $this->assertEquals(30, $this->getProtectedProperty($connection, 'readWriteTimeout')); + $this->assertEquals(30, $this->getProtectedProperty($connection, 'connectionTimeout')); + $this->assertEquals(15, $this->getProtectedProperty($connection, 'heartbeat')); + } - $connectionTimeout = $this->app['config']->get('queue.connections.rabbitmq.connection_timeout'); - $this->assertEquals(30, $connectionTimeout); + public function test_set_altered_configurations_values() + { + $this->markTestIncomplete(); + } - $readWriteTimeout = $this->app['config']->get('queue.connections.rabbitmq.read_write_timeout'); - $this->assertEquals(30, $readWriteTimeout); + protected static function getProtectedProperty($class, $value) + { + $reflection = new ReflectionClass($class); + $property = $reflection->getProperty($value); + $property->setAccessible(true); + return $property->getValue($class); } } From 56a15e16ad3313246764bd82a40d825b99a9bcc1 Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Mon, 14 Jan 2019 12:52:03 +0200 Subject: [PATCH 7/8] [WIP] Test updated config values for connection --- tests/ConnectionTest.php | 46 +++++++++++++++++++++++++++++++++++++++- tests/TestCase.php | 6 +++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 9a38595..9a7b4ed 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -5,6 +5,7 @@ use Mockery as M; use ReflectionClass; use Vinelab\Bowler\Connection; +use PhpAmqpLib\Wire\IO\StreamIO; use Illuminate\Support\Facades\Config; use Vinelab\Http\Client as HTTPClient; use PhpAmqpLib\Connection\AMQPStreamConnection; @@ -60,7 +61,37 @@ public function test_set_default_configurations_values() public function test_set_altered_configurations_values() { - $this->markTestIncomplete(); + Config::set('queue.connections.rabbitmq.host', 'localhost'); + Config::set('queue.connections.rabbitmq.port', 5672); + Config::set('queue.connections.rabbitmq.username', 'default'); + Config::set('queue.connections.rabbitmq.password', 'default'); + // Config::set('queue.connections.rabbitmq.read_write_timeout', 60); + // Config::set('queue.connections.rabbitmq.connection_timeout', 60); + // Config::set('queue.connections.rabbitmq.heartbeat', 30); + + // $this->app['config']->set('queue.connections.rabbitmq.read_write_timeout', 120); + // $this->app['config']->set('queue.connections.rabbitmq.connection_timeout', 120); + + $rbmqHost = config('queue.connections.rabbitmq.host'); + $rbmqPort = config('queue.connections.rabbitmq.port'); + $rbmqUsername = config('queue.connections.rabbitmq.username'); + $rbmqPassword = config('queue.connections.rabbitmq.password'); + $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout'); + $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout'); + $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat'); + + $this->app->bind(AMQPStreamConnection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat) { + return new AMQPStreamConnection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, '/', false, 'AMQPLAIN', null, 'en_US', config('queue.connections.rabbitmq.connection_timeout'), config('queue.connections.rabbitmq.read_write_timeout'), null, false, config('queue.connections.rabbitmq.heartbeat')); + }); + + $connection = $this->app[Connection::class]; + + $conn = $connection->getConnection(); + $io = $this->getProtectedProperty($conn, 'io'); + + $this->assertEquals(60, $this->getProtectedProperty($io, 'read_write_timeout')); + $this->assertEquals(60, $this->getProtectedProperty($io, 'connection_timeout')); + $this->assertEquals(30, $this->getProtectedProperty($io, 'heartbeat')); } protected static function getProtectedProperty($class, $value) @@ -70,4 +101,17 @@ protected static function getProtectedProperty($class, $value) $property->setAccessible(true); return $property->getValue($class); } + + // protected function getEnvironmentSetUp($app) + // { + // parent::getEnvironmentSetUp($app); + + // $app['config']->set('queue.connections.rabbitmq.host', 'localhost'); + // $app['config']->set('queue.connections.rabbitmq.port', 5672); + // $app['config']->set('queue.connections.rabbitmq.username', 'guest'); + // $app['config']->set('queue.connections.rabbitmq.password', 'guest'); + // $app['config']->set('queue.connections.rabbitmq.heartbeat', 30); + // $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 60); + // $app['config']->set('queue.connections.rabbitmq.connection_timeout', 60); + // } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 7fb815c..a0f7207 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,8 +35,8 @@ protected function getEnvironmentSetUp($app) $app['config']->set('queue.connections.rabbitmq.port', 5672); $app['config']->set('queue.connections.rabbitmq.username', 'guest'); $app['config']->set('queue.connections.rabbitmq.password', 'guest'); - $app['config']->set('queue.connections.rabbitmq.heartbeat', 15); - $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 30); - $app['config']->set('queue.connections.rabbitmq.connection_timeout', 30); + // $app['config']->set('queue.connections.rabbitmq.heartbeat', 15); + // $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 30); + // $app['config']->set('queue.connections.rabbitmq.connection_timeout', 30); } } From a2af70eda2908376f01eb73bf7e04f84b77d13cb Mon Sep 17 00:00:00 2001 From: Charalampos Raftopoulos Date: Mon, 14 Jan 2019 16:32:38 +0200 Subject: [PATCH 8/8] Fix test for provider configuration, thanks to @mulkave --- src/BowlerServiceProvider.php | 20 ++++----- src/Connection.php | 45 +++++++++++++------- tests/ConnectionTest.php | 80 +++++++++++++---------------------- tests/TestCase.php | 3 -- 4 files changed, 68 insertions(+), 80 deletions(-) diff --git a/src/BowlerServiceProvider.php b/src/BowlerServiceProvider.php index a5a0645..4a18ba9 100644 --- a/src/BowlerServiceProvider.php +++ b/src/BowlerServiceProvider.php @@ -30,16 +30,16 @@ public function register() return $app['vinelab.bowler.registrator']; }); - // Bind connection to env configuration - $rbmqHost = config('queue.connections.rabbitmq.host'); - $rbmqPort = config('queue.connections.rabbitmq.port'); - $rbmqUsername = config('queue.connections.rabbitmq.username'); - $rbmqPassword = config('queue.connections.rabbitmq.password'); - $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout') ? (int) config('queue.connections.rabbitmq.connection_timeout') : 30; - $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout') ? (int) config('queue.connections.rabbitmq.read_write_timeout') : 30; - $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat') ? (int) config('queue.connections.rabbitmq.heartbeat') : 15; - - $this->app->bind(Connection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat) { + $this->app->bind(Connection::class, function () { + // Bind connection to env configuration + $rbmqHost = config('queue.connections.rabbitmq.host'); + $rbmqPort = config('queue.connections.rabbitmq.port'); + $rbmqUsername = config('queue.connections.rabbitmq.username'); + $rbmqPassword = config('queue.connections.rabbitmq.password'); + $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout') ? (int) config('queue.connections.rabbitmq.connection_timeout') : 30; + $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout') ? (int) config('queue.connections.rabbitmq.read_write_timeout') : 30; + $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat') ? (int) config('queue.connections.rabbitmq.heartbeat') : 15; + return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat); }); diff --git a/src/Connection.php b/src/Connection.php index 6a289d5..b4caf4b 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -98,22 +98,35 @@ public function __construct($host = 'localhost', $port = 5672, $username = 'gues $this->readWriteTimeout = $readWriteTimeout; $this->heartbeat = $heartbeat; - $this->connection = new AMQPStreamConnection( - $host, - $port, - $username, - $password, - $vhost = '/', - $insist = false, - $login_method = 'AMQPLAIN', - $login_response = null, - $locale = 'en_US', - $connectionTimeout, - $readWriteTimeout, - $context = null, - $keepalive = false, - $heartbeat - ); + $this->initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat); + } + + protected function initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost = '/',$insist = false, $login_method = 'AMQPLAIN', $login_response = null, $locale = 'en_US', $context = null, $keepalive = false) + { + $vhost = '/'; + $insist = false; + $login_method = 'AMQPLAIN'; + $login_response = null; + $locale = 'en_US'; + $context = null; + $keepalive = false; + + $this->connection = app()->makeWith(AMQPStreamConnection::class, [ + 'host' => $host, + 'port' => $port, + 'user' => $username, + 'password' => $password, + 'vhost' => $vhost, + 'insist' => $insist, + 'login_method' => $login_method, + 'login_response' => $login_response, + 'locale' => $locale, + 'connection_timeout' => $connectionTimeout, + 'read_write_timeout' => $readWriteTimeout, + 'context' => $context, + 'keepalive' => $keepalive, + 'heartbeat' => $heartbeat, + ]); $this->channel = $this->connection->channel(); } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 9a7b4ed..0cc3ddc 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -6,6 +6,7 @@ use ReflectionClass; use Vinelab\Bowler\Connection; use PhpAmqpLib\Wire\IO\StreamIO; +use Illuminate\Broadcasting\Channel; use Illuminate\Support\Facades\Config; use Vinelab\Http\Client as HTTPClient; use PhpAmqpLib\Connection\AMQPStreamConnection; @@ -53,65 +54,42 @@ public function test_fetching_consumers_default() public function test_set_default_configurations_values() { + $mConnection = $this->getMockBuilder(Connection::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->app->bind(Connection::class, function () use ($mConnection) { + return $mConnection; + }); $connection = $this->app[Connection::class]; - $this->assertEquals(30, $this->getProtectedProperty($connection, 'readWriteTimeout')); - $this->assertEquals(30, $this->getProtectedProperty($connection, 'connectionTimeout')); - $this->assertEquals(15, $this->getProtectedProperty($connection, 'heartbeat')); + + $this->assertAttributeEquals(15, 'heartbeat', $connection); + $this->assertAttributeEquals(30, 'readWriteTimeout', $connection); + $this->assertAttributeEquals(30, 'connectionTimeout', $connection); } public function test_set_altered_configurations_values() { - Config::set('queue.connections.rabbitmq.host', 'localhost'); - Config::set('queue.connections.rabbitmq.port', 5672); - Config::set('queue.connections.rabbitmq.username', 'default'); - Config::set('queue.connections.rabbitmq.password', 'default'); - // Config::set('queue.connections.rabbitmq.read_write_timeout', 60); - // Config::set('queue.connections.rabbitmq.connection_timeout', 60); - // Config::set('queue.connections.rabbitmq.heartbeat', 30); - - // $this->app['config']->set('queue.connections.rabbitmq.read_write_timeout', 120); - // $this->app['config']->set('queue.connections.rabbitmq.connection_timeout', 120); - - $rbmqHost = config('queue.connections.rabbitmq.host'); - $rbmqPort = config('queue.connections.rabbitmq.port'); - $rbmqUsername = config('queue.connections.rabbitmq.username'); - $rbmqPassword = config('queue.connections.rabbitmq.password'); - $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout'); - $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout'); - $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat'); - - $this->app->bind(AMQPStreamConnection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat) { - return new AMQPStreamConnection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, '/', false, 'AMQPLAIN', null, 'en_US', config('queue.connections.rabbitmq.connection_timeout'), config('queue.connections.rabbitmq.read_write_timeout'), null, false, config('queue.connections.rabbitmq.heartbeat')); + Config::set('queue.connections.rabbitmq.host', 'notlocal'); + Config::set('queue.connections.rabbitmq.port', 6666); + Config::set('queue.connections.rabbitmq.read_write_timeout', 60); + Config::set('queue.connections.rabbitmq.connection_timeout', 60); + Config::set('queue.connections.rabbitmq.heartbeat', 30); + + $mAMQPStreamConnection = M::mock(AMQPStreamConnection::class); + $this->app->bind(AMQPStreamConnection::class, function () use ($mAMQPStreamConnection) { + return $mAMQPStreamConnection; }); + $mChannel = M::mock(Channel::class); + $mAMQPStreamConnection->shouldReceive('channel')->once()->withNoArgs()->andReturn($mChannel); + $mAMQPStreamConnection->shouldReceive('close')->once()->withNoArgs(); + $mChannel->shouldReceive('close')->once()->withNoArgs(); + $connection = $this->app[Connection::class]; - - $conn = $connection->getConnection(); - $io = $this->getProtectedProperty($conn, 'io'); - $this->assertEquals(60, $this->getProtectedProperty($io, 'read_write_timeout')); - $this->assertEquals(60, $this->getProtectedProperty($io, 'connection_timeout')); - $this->assertEquals(30, $this->getProtectedProperty($io, 'heartbeat')); + $this->assertAttributeEquals(30, 'heartbeat', $connection); + $this->assertAttributeEquals(60, 'readWriteTimeout', $connection); + $this->assertAttributeEquals(60, 'connectionTimeout', $connection); } - - protected static function getProtectedProperty($class, $value) - { - $reflection = new ReflectionClass($class); - $property = $reflection->getProperty($value); - $property->setAccessible(true); - return $property->getValue($class); - } - - // protected function getEnvironmentSetUp($app) - // { - // parent::getEnvironmentSetUp($app); - - // $app['config']->set('queue.connections.rabbitmq.host', 'localhost'); - // $app['config']->set('queue.connections.rabbitmq.port', 5672); - // $app['config']->set('queue.connections.rabbitmq.username', 'guest'); - // $app['config']->set('queue.connections.rabbitmq.password', 'guest'); - // $app['config']->set('queue.connections.rabbitmq.heartbeat', 30); - // $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 60); - // $app['config']->set('queue.connections.rabbitmq.connection_timeout', 60); - // } } diff --git a/tests/TestCase.php b/tests/TestCase.php index a0f7207..f9dc6b7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,8 +35,5 @@ protected function getEnvironmentSetUp($app) $app['config']->set('queue.connections.rabbitmq.port', 5672); $app['config']->set('queue.connections.rabbitmq.username', 'guest'); $app['config']->set('queue.connections.rabbitmq.password', 'guest'); - // $app['config']->set('queue.connections.rabbitmq.heartbeat', 15); - // $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 30); - // $app['config']->set('queue.connections.rabbitmq.connection_timeout', 30); } }