Skip to content

Commit

Permalink
Fixes basis-company/nats.php/#30
Browse files Browse the repository at this point in the history
  • Loading branch information
digibeuk authored and nekufa committed Jun 17, 2024
1 parent e71e0d6 commit ac033b5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Client

private string $name = '';

/** @var array<Closure|Queue> */
private array $handlers = [];
private array $subscriptions = [];

Expand Down Expand Up @@ -240,4 +241,23 @@ public function skipInvalidMessages(bool $skipInvalidMessages): self
$this->skipInvalidMessages = $skipInvalidMessages;
return $this;
}

public function unsubscribeAll(): self
{
foreach ($this->subscriptions as $index => $subscription) {
unset($this->subscriptions[$index]);
$this->connection->sendMessage(new Unsubscribe(['sid' => $subscription['sid']]));
unset($this->handlers[$subscription['sid']]);
}

return $this;
}

public function disconnect(): self
{
$this->unsubscribeAll();
$this->connection->close();

return $this;
}
}
8 changes: 8 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,12 @@ private function processException(Throwable $e)
]));
}
}

public function close(): void
{
if ($this->socket) {
fclose($this->socket);
$this->socket = null;
}
}
}
17 changes: 17 additions & 0 deletions tests/Functional/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,21 @@ public function testInvalidTlsKey()
]);
$client->ping();
}

public function testCloseClosesSocket(): void
{
$client = $this->createClient([]);
self::assertTrue($client->ping());

$connection = $client->connection;

// Call the close method
$connection->close();

$property = new ReflectionProperty(Connection::class, 'socket');
$property->setAccessible(true);

// Assert that the socket is closed and set to null
self::assertNull($property->getValue($connection));
}
}
42 changes: 42 additions & 0 deletions tests/Functional/SubjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Functional;

use Basis\Nats\Client;
use Basis\Nats\Connection;
use Basis\Nats\Message\Payload;
use ReflectionProperty;
use Tests\FunctionalTestCase;
Expand Down Expand Up @@ -226,4 +227,45 @@ public function greet(Payload $payload): string
{
return 'Hello, ' . $payload->body;
}

public function testUnsubscribeAll(): void
{
$property = new ReflectionProperty(Client::class, 'handlers');
$property->setAccessible(true);

$client = $this->createClient();

$subjects = ['hello.request1', 'hello.request2'];
foreach ($subjects as $subject) {
$client->subscribe($subject, $this->greet(...));
}
self::assertCount(2, $property->getValue($client));

$client->unsubscribeAll();
self::assertCount(0, $property->getValue($client));
}

public function testDisconnect(): void
{
$property = new ReflectionProperty(Client::class, 'handlers');
$property->setAccessible(true);

$client = $this->createClient();
$connection = $client->connection;

$subjects = ['hello.request1', 'hello.request2'];
foreach ($subjects as $subject) {
$client->subscribe($subject, $this->greet(...));
}
self::assertCount(2, $property->getValue($client));

$client->disconnect();
self::assertCount(0, $property->getValue($client));

$property = new ReflectionProperty(Connection::class, 'socket');
$property->setAccessible(true);

// Assert that the socket is closed and set to null
self::assertNull($property->getValue($connection));
}
}

0 comments on commit ac033b5

Please sign in to comment.