Skip to content
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
5 changes: 5 additions & 0 deletions .ci/ubuntu/gha-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function start_toxiproxy
docker run --detach $docker_pull_args \
--name "$toxiproxy_docker_name" \
--hostname "$toxiproxy_docker_name" \
--add-host=host.docker.internal:host-gateway \
--publish 8474:8474 \
--publish 5673:5673 \
--network "$docker_network_name" \
Expand Down Expand Up @@ -133,3 +134,7 @@ start_rabbitmq
wait_rabbitmq

get_rabbitmq_id

export TOXIPROXY_HOST="${TOXIPROXY_HOST:-localhost}"
export TOXIPROXY_AMQP_TARGET="${TOXIPROXY_AMQP_TARGET:-host.docker.internal}"
export TOXIPROXY_AMQP_PORT="${TOXIPROXY_AMQP_PORT:-5673}"
4 changes: 4 additions & 0 deletions .ci/ubuntu/rabbitmq.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ ssl_options.fail_if_no_peer_cert = false
management.ssl.certfile = /etc/rabbitmq/certs/server_certificate.pem
management.ssl.keyfile = /etc/rabbitmq/certs/server_key.pem
management.ssl.cacertfile = /etc/rabbitmq/certs/ca_certificate.pem

# RabbitMQ 4.x blocks transient non-exclusive queues by default; the test suite
# still declares queues with the AMQP defaults (non-durable, non-exclusive).
deprecated_features.permit.transient_nonexcl_queues = true
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
name: PHP ${{ matrix.php }} + phpseclib ${{ matrix.phpseclib }}
env:
TOXIPROXY_HOST: localhost
TOXIPROXY_AMQP_TARGET: php-amqplib-rabbitmq
TOXIPROXY_AMQP_TARGET: host.docker.internal
TOXIPROXY_AMQP_PORT: 5673
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/AbstractConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function get_toxiproxy_amqp_port()
$this->markTestSkipped('TOXIPROXY_AMQP_PORT is not set');
}

return $port;
return (int) $port;
}

protected function assertConnectionClosed(AbstractConnection $connection)
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Connection/ConnectionAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function createUser($username, $password)
$userEndpoint = $this->getManagementBase() . 'api/users/' . $username;
$passwordHash = '';
if (!empty($password)) {
$salt = substr(md5(mt_rand()), 0, 4);
$salt = substr(md5((string) mt_rand()), 0, 4);
$passwordHash = base64_encode($salt . hash('sha256', $salt . $password, true));
}
$request = Request::put(
Expand Down
37 changes: 31 additions & 6 deletions tests/Functional/ToxiProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@ public function __destruct()
*/
public function open($host, $port, $listen)
{
$this->closeIfExists();

$payload = [
'name' => $this->name,
'upstream' => $host . ':' . $port,
'listen' => ':' . $listen,
];
$url = $this->api . '/proxies';
$request = Request::post($url, json_encode($payload), 'json');
$request->timeout(1);
$request->timeout(5);
$request->expectsJson();
$response = $request->send();
if ($response->code !== 201) {
throw new \RuntimeException('Cannot create Toxiproxy connection');
}
$this->listen = $listen;
$this->listen = (int) $listen;
$this->isOpen = true;
}

Expand All @@ -85,7 +87,7 @@ public function mode($type, $attributes = [], $direction = 'upstream', $toxicity
];
$url = sprintf('%s/proxies/%s/toxics', $this->api, $this->name);
$request = Request::post($url, json_encode($payload), 'json');
$request->timeout(1);
$request->timeout(5);
$request->expectsJson();
$response = $request->send();

Expand All @@ -94,6 +96,17 @@ public function mode($type, $attributes = [], $direction = 'upstream', $toxicity
}
}

private function closeIfExists()
{
$url = sprintf('%s/proxies/%s', $this->api, $this->name);
$request = Request::delete($url);
$request->timeout(5);
try {
$request->send();
} catch (\Exception $exception) {
}
}

/**
* Disable(block) proxy connection so no data can be transferred.
* @throws \Httpful\Exception\ConnectionErrorException
Expand All @@ -113,10 +126,22 @@ public function disable()
*/
public function close()
{
if (!$this->isOpen) {
return;
}

$url = sprintf('%s/proxies/%s', $this->api, $this->name);
$response = Request::delete($url)->send();
if ($response->code !== 204 && $response->code !== 404) {
throw new \RuntimeException('Cannot close Toxiproxy connection');
$request = Request::delete($url);
$request->timeout(5);
try {
$response = $request->send();
if ($response->code !== 204 && $response->code !== 404) {
throw new \RuntimeException('Cannot close Toxiproxy connection');
}
} catch (\Exception $exception) {
// Best-effort cleanup; a stale proxy is removed before the next open().
} finally {
$this->isOpen = false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

define('HOST', getenv('TEST_RABBITMQ_HOST') ? getenv('TEST_RABBITMQ_HOST') : 'localhost');
define('HOST6', getenv('TEST_RABBITMQ_HOST6') ? getenv('TEST_RABBITMQ_HOST6') : '[::1]');
define('PORT', getenv('TEST_RABBITMQ_PORT') ? getenv('TEST_RABBITMQ_PORT') : 5672);
define('PORT', (int)(getenv('TEST_RABBITMQ_PORT') ?: 5672));
define('USER', getenv('TEST_RABBITMQ_USER') ? getenv('TEST_RABBITMQ_USER') : 'guest');
define('PASS', getenv('TEST_RABBITMQ_PASS') ? getenv('TEST_RABBITMQ_PASS') : 'guest');
define('VHOST', '/');
Expand Down
Loading