Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Dec 20, 2024
1 parent 291915a commit 762915c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Servers/Reverb/Publishing/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function reconnect(): void
if ($this->clientReconnectionTimer >= $this->reconnectionTimeout()) {
Log::error("Failed to reconnect to Redis connection [{$this->name}] within {$this->reconnectionTimeout()} second limit");

exit;
throw new Exception("Failed to reconnect to Redis connection [{$this->name}] within {$this->reconnectionTimeout()} second limit");
}
Log::info("Attempting to reconnect Redis connection [{$this->name}]");
$this->connect();
Expand Down Expand Up @@ -170,7 +170,9 @@ protected function configureClientErrorHandler(): void
{
$this->client->on('close', function () {
$this->client = null;
Log::info("Disconnected fromRedis connection [{$this->name}]");

Log::info("Disconnected from Redis connection [{$this->name}]");

$this->reconnect();
});
}
Expand Down
78 changes: 74 additions & 4 deletions tests/Unit/Servers/Reverb/Publishing/RedisPubSubProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Laravel\Reverb\Servers\Reverb\Contracts\PubSubIncomingMessageHandler;
use Laravel\Reverb\Servers\Reverb\Publishing\RedisClientFactory;
use Laravel\Reverb\Servers\Reverb\Publishing\RedisPubSubProvider;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Promise;

Expand Down Expand Up @@ -45,15 +46,84 @@
$provider->connect(Mockery::mock(LoopInterface::class));
});

it('can successfully reconnect', function () {})->todo();
it('can successfully reconnect', function () {
$clientFactory = Mockery::mock(RedisClientFactory::class);
$loop = Mockery::mock(LoopInterface::class);

$loop->shouldReceive('addTimer')
->once()
->with(1, Mockery::any());

// Publisher client
$clientFactory->shouldReceive('make')
->once()
->andReturn(new Promise(fn () => throw new Exception));

// Subscriber client
$clientFactory->shouldReceive('make')
->once()
->andReturn(new Promise(fn (callable $resolve) => $resolve));

$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb');
$provider->connect($loop);
});

it('can timeout and fail when unable to reconnect', function () {
$clientFactory = Mockery::mock(RedisClientFactory::class);
$loop = Loop::get();

// Publisher client
$clientFactory->shouldReceive('make')
->once()
->andReturn(new Promise(fn () => throw new Exception));

// Subscriber client
$clientFactory->shouldReceive('make')
->once()
->andReturn(new Promise(fn (callable $resolve) => $resolve));

it('can timeout and fail when unable to reconnect', function () {})->todo();
$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb', ['host' => 'localhost', 'port' => 6379, 'timeout' => 1]);
$provider->connect($loop);

it('queues subscription events', function () {})->todo();
$loop->run();
})->throws(Exception::class, 'Failed to reconnect to Redis connection [publisher] within 1 second limit');

it('queues subscription events', function () {
$clientFactory = Mockery::mock(RedisClientFactory::class);

$clientFactory->shouldReceive('make')
->twice()
->andReturn(new Promise(fn (callable $resolve) => $resolve));

$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb');
$provider->connect(Mockery::mock(LoopInterface::class));
$provider->subscribe();

$subscribingClient = (new ReflectionProperty($provider, 'subscribingClient'))->getValue($provider);
$queuedSubscriptionEvents = (new ReflectionProperty($subscribingClient, 'queuedSubscriptionEvents'))->getValue($subscribingClient);

expect(array_keys($queuedSubscriptionEvents))->toBe(['subscribe', 'on']);
});

it('can process queued subscription events', function () {})->todo();

it('queues publish events', function () {})->todo();
it('queues publish events', function () {
$clientFactory = Mockery::mock(RedisClientFactory::class);

$clientFactory->shouldReceive('make')
->twice()
->andReturn(new Promise(fn (callable $resolve) => $resolve));

$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb');
$provider->connect(Mockery::mock(LoopInterface::class));
$provider->publish(['event' => 'first test']);
$provider->publish(['event' => 'second test']);

$publishingClient = (new ReflectionProperty($provider, 'publishingClient'))->getValue($provider);
$queuedPublishEvents = (new ReflectionProperty($publishingClient, 'queuedPublishEvents'))->getValue($publishingClient);

expect($queuedPublishEvents)->toBe([['event' => 'first test'], ['event' => 'second test']]);
});

it('can process queued publish events', function () {})->todo();

Expand Down

0 comments on commit 762915c

Please sign in to comment.