1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<?php
namespace PhpAmqpLib\Tests\Functional;
use PhpAmqpLib\Channel\AbstractChannel;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Connection\AMQPConnectionConfig;
use PhpAmqpLib\Connection\AMQPConnectionFactory;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Tests\TestCaseCompat;
abstract class AbstractConnectionTestCase extends TestCaseCompat
{
public static $blocked = false;
protected function connection_create(
string $type = 'stream',
string $host = HOST,
int $port = PORT,
array $options = array()
): AbstractConnection {
$timeout = $options['timeout'] ?? 1;
$lazy = $options['lazy'] ?? false;
$config = new AMQPConnectionConfig();
$config->setIsLazy($lazy);
if ($type === 'ssl') {
$config->setIoType(AMQPConnectionConfig::IO_TYPE_STREAM);
$config->setIsSecure(true);
$config->setNetworkProtocol($options['protocol'] ?? 'ssl');
$config->setSslCryptoMethod($options['ssl']['crypto_method'] ?? null);
$config->setSslCaCert($options['ssl']['cafile'] ?? null);
$config->setSslCaPath($options['ssl']['capath'] ?? null);
$config->setSslCert($options['ssl']['local_cert'] ?? null);
$config->setSslKey($options['ssl']['local_pk'] ?? null);
$config->setSslVerify($options['ssl']['verify_peer'] ?? null);
$config->setSslVerifyName($options['ssl']['verify_peer_name'] ?? null);
$config->setSslPassPhrase($options['ssl']['passphrase'] ?? null);
$config->setSslCiphers($options['ssl']['ciphers'] ?? null);
} else {
$config->setIoType($type);
}
$config->setHost($host);
$config->setPort($port);
$config->setKeepalive($options['keepalive'] ?? false);
$config->setHeartbeat($options['heartbeat'] ?? 0);
$config->setReadTimeout($timeout);
$config->setWriteTimeout($timeout);
$config->setConnectionTimeout($options['connectionTimeout'] ?? $timeout);
$config->setSendBufferSize(16384);
$connection = AMQPConnectionFactory::create($config);
if (!$lazy) {
$this->assertTrue($connection->isConnected());
}
return $connection;
}
protected function queue_bind(AMQPChannel $channel, $exchange_name, &$queue_name)
{
$channel->exchange_declare($exchange_name, AMQPExchangeType::DIRECT);
list($queue_name, ,) = $channel->queue_declare();
$channel->queue_bind($queue_name, $exchange_name, $queue_name);
}
/**
* @param string $connectionType
* @param array $options
* @return AMQPChannel
*/
protected function channel_create($connectionType, $options = [])
{
$connection = $this->connection_create($connectionType, HOST, PORT, $options);
$channel = $connection->channel();
$this->assertTrue($channel->is_open());
return $channel;
}
/**
* @param string $name
* @return ToxiProxy
*/
protected function create_proxy($name = 'amqp_connection')
{
$host = trim(getenv('TOXIPROXY_AMQP_TARGET'));
if (empty($host)) {
$host = HOST;
}
$proxy = new ToxiProxy($name, $this->get_toxiproxy_host());
$proxy->open($host, PORT, $this->get_toxiproxy_amqp_port());
return $proxy;
}
protected function get_toxiproxy_host()
{
$host = getenv('TOXIPROXY_HOST');
if (!$host) {
$this->markTestSkipped('TOXIPROXY_HOST is not set');
}
return $host;
}
protected function get_toxiproxy_amqp_port()
{
$port = getenv('TOXIPROXY_AMQP_PORT');
if (!$port) {
$this->markTestSkipped('TOXIPROXY_AMQP_PORT is not set');
}
return $port;
}
protected function assertConnectionClosed(AbstractConnection $connection)
{
$this->assertFalse($connection->isConnected());
$this->assertNotNull($connection->getIO());
// all channels must be closed
foreach ($connection->channels as $ch) {
if ($ch instanceof AMQPChannel) {
$this->assertFalse($ch->is_open());
}
if ($ch instanceof AbstractConnection) {
$this->assertFalse($ch->isConnected());
}
}
$this->assertNotEmpty($connection->channels);
}
protected function assertChannelClosed(AbstractChannel $channel)
{
$this->assertFalse($channel->is_open());
$this->assertEmpty($channel->callbacks);
}
}
// mock low level IO write functions
namespace PhpAmqpLib\Wire\IO;
function fwrite()
{
if (\PhpAmqpLib\Tests\Functional\AbstractConnectionTestCase::$blocked) {
return 0;
}
return call_user_func_array('\fwrite', func_get_args());
}
namespace PhpAmqpLib\Wire\IO;
function socket_write()
{
if (\PhpAmqpLib\Tests\Functional\AbstractConnectionTestCase::$blocked) {
return 0;
}
return call_user_func_array('\socket_write', func_get_args());
}
|