Skip to content

Commit 3111d38

Browse files
authored
Merge pull request #41 from phpinnacle/reconnect_heartbeat_fixes
* Bug fix of heartbeat watcher. Throw received exception on client close event. Update dependencies * php 8.1 or higher is required --------- Co-authored-by: Stepan Zolotarev <[email protected]>
2 parents 96ff3d8 + 7c68063 commit 3111d38

14 files changed

+68
-58
lines changed

Diff for: .github/workflows/continuous-integration.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Install PHP
1414
uses: shivammathur/setup-php@v2
1515
with:
16-
php-version: 8.0
16+
php-version: 8.1
1717
coverage: none
1818
tools: composer:v2
1919

@@ -33,7 +33,7 @@ jobs:
3333
- name: Install PHP
3434
uses: shivammathur/setup-php@v2
3535
with:
36-
php-version: 8.0
36+
php-version: 8.1
3737
coverage: none
3838
tools: composer:v2
3939

@@ -68,7 +68,7 @@ jobs:
6868
- name: Install PHP with extensions
6969
uses: shivammathur/setup-php@v2
7070
with:
71-
php-version: 8.0
71+
php-version: 8.1
7272
extensions: ${{ env.PHP_EXTENSIONS }}
7373
ini-values: ${{ env.PHP_INI_VALUES }}
7474
tools: composer:v2

Diff for: composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=8.0",
21+
"php": ">=8.1",
2222
"amphp/amp": "v2.6.*",
2323
"amphp/socket": "v1.2.*",
2424
"phpinnacle/buffer": "v1.2.*",
2525
"evenement/evenement": "v3.0.*"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "v9.5.*",
29-
"vimeo/psalm": "v4.19.*",
30-
"phpstan/phpstan": "v1.4.*"
28+
"phpunit/phpunit": "10.* || 11.*",
29+
"vimeo/psalm": "5.*",
30+
"phpstan/phpstan": "^1.10"
3131
},
3232
"prefer-stable": true,
3333
"autoload": {
@@ -43,7 +43,7 @@
4343
"scripts": {
4444
"psalm": "./vendor/bin/psalm --config=psalm.xml",
4545
"phpstan": "./vendor/bin/phpstan analyse src --level 9",
46-
"tests": "./vendor/bin/phpunit --configuration phpunit.xml --verbose",
46+
"tests": "./vendor/bin/phpunit --configuration phpunit.xml",
4747
"coverage": "./vendor/bin/phpunit --configuration phpunit.xml --coverage-html ./coverage --verbose"
4848
},
4949
"extra": {

Diff for: phpunit.xml

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false"
3-
backupStaticAttributes="false" beStrictAboutTestsThatDoNotTestAnything="false" colors="true" verbose="true"
4-
convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true"
5-
failOnRisky="true" failOnWarning="true" stopOnFailure="false"
6-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
7-
<coverage>
8-
<include>
9-
<directory>./</directory>
10-
</include>
11-
<exclude>
12-
<directory>./tests</directory>
13-
<directory>./vendor</directory>
14-
</exclude>
15-
</coverage>
16-
<php>
17-
<ini name="error_reporting" value="-1"/>
18-
<env name="RIDGE_TEST_DSN" value="amqp://guest:[email protected]:5672/?heartbeat=0"/>
19-
</php>
20-
<testsuites>
21-
<testsuite name="PHPinnacle Test Suite">
22-
<directory>./tests/</directory>
23-
</testsuite>
24-
</testsuites>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
beStrictAboutTestsThatDoNotTestAnything="false"
6+
colors="true"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
stopOnFailure="false"
10+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">
11+
<php>
12+
<ini name="error_reporting" value="-1"/>
13+
<env name="RIDGE_TEST_DSN" value="amqp://guest:[email protected]:5672/?heartbeat=0"/>
14+
</php>
15+
<testsuites>
16+
<testsuite name="PHPinnacle Test Suite">
17+
<directory>./tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
<source>
21+
<include>
22+
<directory>./</directory>
23+
</include>
24+
<exclude>
25+
<directory>./tests</directory>
26+
<directory>./vendor</directory>
27+
</exclude>
28+
</source>
2529
</phpunit>

Diff for: psalm.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="true"
4-
useDocblockTypes="true"
53
useDocblockPropertyTypes="true"
64
strictBinaryOperands="true"
75
findUnusedPsalmSuppress="true"
@@ -21,5 +19,6 @@
2119
<issueHandlers>
2220
<PropertyNotSetInConstructor errorLevel="suppress"/>
2321
<UnnecessaryVarAnnotation errorLevel="suppress"/>
22+
<RiskyTruthyFalsyComparison errorLevel="suppress"/>
2423
</issueHandlers>
2524
</psalm>

Diff for: src/Channel.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ function () use ($outOfBand) {
149149
}
150150

151151
/**
152+
* @psalm-suppress InvalidReturnType
153+
* @psalm-suppress InvalidReturnStatement
154+
*
152155
* @return Promise<void>
153156
*
154157
* @throws \PHPinnacle\Ridge\Exception\ChannelException
@@ -1148,9 +1151,12 @@ public function doPublish
11481151
$buffer->appendUint8(206);
11491152

11501153
if (!empty($body)) {
1151-
/* @phpstan-ignore-next-line */
1154+
/**
1155+
* @phpstan-ignore-next-line
1156+
* @psalm-suppress ArgumentTypeCoercion
1157+
*/
11521158
$chunks = \str_split($body, $this->properties->maxFrame());
1153-
1159+
/** @psalm-suppress RedundantConditionGivenDocblockType */
11541160
if ($chunks !== false) {
11551161
foreach ($chunks as $chunk) {
11561162
$buffer
@@ -1197,6 +1203,8 @@ static function (Protocol\AbstractFrame $frame) use ($deferred) {
11971203
public function forceClose(\Throwable $exception): void
11981204
{
11991205
if ($this->state !== self::STATE_CLOSED) {
1206+
$this->connection->cancel($this->id);
1207+
12001208
$this->state = self::STATE_CLOSED;
12011209
$this->commandWaitQueue->cancel($exception);
12021210
$this->emit(self::EVENT_CHANNEL_CLOSED, [$exception]);

Diff for: src/Client.php

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function __construct(Config $config)
8383
}
8484
$this->channels = [];
8585
$this->commandWaitQueue->cancel($exception);
86+
87+
throw $exception;
8688
}
8789
});
8890
}
@@ -186,6 +188,9 @@ function(): void
186188
}
187189

188190
/**
191+
* @psalm-suppress InvalidReturnType
192+
* @psalm-suppress InvalidReturnStatement
193+
*
189194
* @return Promise<void>
190195
*
191196
* @throws \PHPinnacle\Ridge\Exception\ClientException

Diff for: src/Connection.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,12 @@ function () {
160160
$this->lastRead = Loop::now();
161161

162162
/**
163-
* @psalm-var callable(AbstractFrame):Promise<bool> $callback
163+
* @psalm-suppress PossiblyInvalidArgument
164+
*
165+
* @var callable(AbstractFrame):Promise<bool> $callback
164166
*/
165167
foreach ($this->callbacks[(int)$frame->channel][$class] ?? [] as $i => $callback) {
168+
/** @phpstan-ignore-next-line */
166169
if (yield call($callback, $frame)) {
167170
unset($this->callbacks[(int)$frame->channel][$class][$i]);
168171
}
@@ -187,9 +190,9 @@ public function heartbeat(int $timeout): void
187190
* We run the callback even more often to avoid race conditions if the loop is a bit under pressure
188191
* otherwise we could miss heartbeats in rare conditions
189192
*/
190-
$interval = $timeout / 2;
193+
$interval = (int) ($timeout / 2);
191194
$this->heartbeatWatcherId = Loop::repeat(
192-
$interval / 3,
195+
(int) ($interval / 3),
193196
function (string $watcherId) use ($interval, $timeout){
194197
$currentTime = Loop::now();
195198

Diff for: tests/AsyncTest.php renamed to tests/AsyncTestCase.php

+5-14
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,17 @@
1313
use Amp\Loop;
1414
use function Amp\call;
1515

16-
abstract class AsyncTest extends RidgeTest
16+
abstract class AsyncTestCase extends RidgeTestCase
1717
{
1818
/**
19-
* @var string
19+
* @var string|null
2020
*/
2121
private $realTestName;
2222

23-
/**
24-
* @codeCoverageIgnore Invoked before code coverage data is being collected.
25-
*
26-
* @param string $name
27-
*/
28-
public function setName(string $name): void
23+
protected function runTest(): mixed
2924
{
30-
parent::setName($name);
25+
$this->realTestName = $this->name();
3126

32-
$this->realTestName = $name;
33-
}
34-
35-
protected function runTest()
36-
{
3727
parent::setName('runTestAsync');
3828

3929
return parent::runTest();
@@ -49,6 +39,7 @@ protected function runTestAsync(...$args)
4939

5040
yield $client->connect();
5141

42+
$args = $args ?: [];
5243
\array_unshift($args, $client);
5344

5445
$return = yield call([$this, $this->realTestName], ...$args);

Diff for: tests/BufferTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHPinnacle\Ridge\Buffer;
1414
use PHPinnacle\Ridge\Exception;
1515

16-
class BufferTest extends RidgeTest
16+
class BufferTest extends RidgeTestCase
1717
{
1818
public function testTimestamp()
1919
{

Diff for: tests/ChannelTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use PHPinnacle\Ridge\Message;
1919
use PHPinnacle\Ridge\Queue;
2020

21-
class ChannelTest extends AsyncTest
21+
class ChannelTest extends AsyncTestCase
2222
{
2323
public function testOpenNotReadyChannel(Client $client)
2424
{

Diff for: tests/ClientConnectTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHPinnacle\Ridge\Client;
1717
use PHPinnacle\Ridge\Message;
1818

19-
class ClientConnectTest extends RidgeTest
19+
class ClientConnectTest extends RidgeTestCase
2020
{
2121
public function testConnect()
2222
{

Diff for: tests/ClientTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PHPinnacle\Ridge\Client;
1515
use PHPinnacle\Ridge\Message;
1616

17-
class ClientTest extends AsyncTest
17+
class ClientTest extends AsyncTestCase
1818
{
1919
public function testOpenChannel(Client $client)
2020
{

Diff for: tests/ConfigTest.php renamed to tests/ConfigTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use PHPinnacle\Ridge\Config;
1414

15-
class ConfigTest extends RidgeTest
15+
class ConfigTestCase extends RidgeTestCase
1616
{
1717
public function testCreate()
1818
{

Diff for: tests/RidgeTest.php renamed to tests/RidgeTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPinnacle\Ridge\Config;
1616
use PHPUnit\Framework\TestCase;
1717

18-
abstract class RidgeTest extends TestCase
18+
abstract class RidgeTestCase extends TestCase
1919
{
2020
/**
2121
* @param mixed $value

0 commit comments

Comments
 (0)