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

STOMP. add additional configuration #1018

Merged
merged 6 commits into from
Feb 3, 2020
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
21 changes: 21 additions & 0 deletions pkg/stomp/StompConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Interop\Queue\ConnectionFactory;
use Interop\Queue\Context;
use Stomp\Network\Connection;
use Stomp\Network\Observer\HeartbeatEmitter;
use Stomp\Network\Observer\ServerAliveObserver;

class StompConnectionFactory implements ConnectionFactory
{
Expand Down Expand Up @@ -88,11 +90,22 @@ private function establishConnection(): BufferedStompClient
$scheme = (true === $config['ssl_on']) ? 'ssl' : 'tcp';
$uri = $scheme.'://'.$config['host'].':'.$config['port'];
$connection = new Connection($uri, $config['connection_timeout']);
$connection->setWriteTimeout($config['write_timeout']);
$connection->setReadTimeout($config['read_timeout']);

if ($config['send_heartbeat']) {
$connection->getObservers()->addObserver(new HeartbeatEmitter($connection));
}

if ($config['receive_heartbeat']) {
$connection->getObservers()->addObserver(new ServerAliveObserver());
}

$this->stomp = new BufferedStompClient($connection, $config['buffer_size']);
$this->stomp->setLogin($config['login'], $config['password']);
$this->stomp->setVhostname($config['vhost']);
$this->stomp->setSync($config['sync']);
$this->stomp->setHeartbeat($config['send_heartbeat'], $config['receive_heartbeat']);

$this->stomp->connect();
}
Expand Down Expand Up @@ -128,6 +141,10 @@ private function parseDsn(string $dsn): array
'sync' => $dsn->getBool('sync'),
'lazy' => $dsn->getBool('lazy'),
'ssl_on' => $dsn->getBool('ssl_on'),
'write_timeout' => $dsn->getDecimal('write_timeout'),
'read_timeout' => $dsn->getDecimal('read_timeout'),
'send_heartbeat' => $dsn->getDecimal('send_heartbeat'),
'receive_heartbeat' => $dsn->getDecimal('receive_heartbeat'),
]), function ($value) { return null !== $value; });
}

Expand All @@ -145,6 +162,10 @@ private function defaultConfig(): array
'sync' => false,
'lazy' => true,
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
];
}
}
49 changes: 49 additions & 0 deletions pkg/stomp/Tests/Functional/StompConnectionFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Enqueue\Stomp\Tests\Functional;

use Enqueue\Stomp\StompConnectionFactory;
use Enqueue\Test\RabbitmqStompExtension;
use Stomp\Network\Observer\Exception\HeartbeatException;
use Stomp\Network\Observer\HeartbeatEmitter;
use Stomp\Network\Observer\ServerAliveObserver;

/**
* @group functional
*/
class StompConnectionFactoryTest extends \PHPUnit\Framework\TestCase
{
use RabbitmqStompExtension;

public function testShouldNotCreateConnectionWithSendHeartbeat()
{
$dsn = $this->getDsn().'?send_heartbeat=2000';
$factory = new StompConnectionFactory($dsn);
$this->expectException(HeartbeatException::class);
$factory->createContext()->getStomp();
}

public function testShouldCreateConnectionWithSendHeartbeat()
{
$dsn = $this->getDsn().'?send_heartbeat=2000&read_timeout=1';
$factory = new StompConnectionFactory($dsn);
$context = $factory->createContext();

$observers = $context->getStomp()->getConnection()->getObservers()->getObservers();
$this->assertAttributeEquals([2000, 0], 'heartbeat', $context->getStomp());
$this->assertCount(1, $observers);
$this->assertInstanceOf(HeartbeatEmitter::class, $observers[0]);
}

public function testShouldCreateConnectionWithReceiveHeartbeat()
{
$dsn = $this->getDsn().'?receive_heartbeat=2000';
$factory = new StompConnectionFactory($dsn);
$context = $factory->createContext();

$observers = $context->getStomp()->getConnection()->getObservers()->getObservers();
$this->assertAttributeEquals([0, 2000], 'heartbeat', $context->getStomp());
$this->assertCount(1, $observers);
$this->assertInstanceOf(ServerAliveObserver::class, $observers[0]);
}
}
36 changes: 36 additions & 0 deletions pkg/stomp/Tests/StompConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public static function provideConfigs()
'sync' => false,
'lazy' => true,
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -83,6 +87,10 @@ public static function provideConfigs()
'sync' => false,
'lazy' => true,
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -100,6 +108,10 @@ public static function provideConfigs()
'sync' => false,
'lazy' => true,
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -118,6 +130,10 @@ public static function provideConfigs()
'lazy' => false,
'foo' => 'bar',
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -136,6 +152,10 @@ public static function provideConfigs()
'lazy' => false,
'foo' => 'bar',
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -154,6 +174,10 @@ public static function provideConfigs()
'lazy' => false,
'foo' => 'bar',
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -173,6 +197,10 @@ public static function provideConfigs()
'foo' => 'bar',
'ssl_on' => false,
'baz' => 'bazVal',
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -190,6 +218,10 @@ public static function provideConfigs()
'sync' => false,
'lazy' => true,
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];

Expand All @@ -208,6 +240,10 @@ public static function provideConfigs()
'lazy' => true,
'foo' => 'bar',
'ssl_on' => false,
'write_timeout' => 3,
'read_timeout' => 60,
'send_heartbeat' => 0,
'receive_heartbeat' => 0,
],
];
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/test/RabbitmqStompExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

trait RabbitmqStompExtension
{
private function getDsn()
{
return getenv('RABITMQ_STOMP_DSN');
}

private function buildStompContext(): StompContext
{
if (false == $dsn = getenv('RABITMQ_STOMP_DSN')) {
if (false == $dsn = $this->getDsn()) {
throw new SkippedTestError('Functional tests are not allowed in this environment');
}

Expand Down