Skip to content

Commit

Permalink
[1.x] Re-subscribes to the scaling channel when the underlying connec…
Browse files Browse the repository at this point in the history
…tion is lost (#251)

* Re-subscribe to the scaling channel when the connection drops

* add tests

* Update RedisPubSubProvider.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
ashiquzzaman33 and taylorotwell authored Oct 4, 2024
1 parent 1cf239d commit 65631f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Servers/Reverb/Publishing/RedisPubSubProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public function subscribe(): void
$this->subscribingClient->on('message', function (string $channel, string $payload) {
$this->messageHandler->handle($payload);
});

$this->subscribingClient->on('unsubscribe', function (string $channel) {
if ($this->channel === $channel) {
$this->subscribingClient->subscribe($channel);
}
});
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Servers/Reverb/Publishing/RedisPubSubProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Clue\React\Redis\Client;
use Laravel\Reverb\Servers\Reverb\Contracts\PubSubIncomingMessageHandler;
use Laravel\Reverb\Servers\Reverb\Publishing\RedisPubSubProvider;
use Laravel\Reverb\Servers\Reverb\Publishing\RedisClientFactory;
use React\EventLoop\LoopInterface;

it('resubscribes to the scaling channel on unsubscribe event', function (){

$channel = "reverb";
$subscribingClient = Mockery::mock(Client::class);

$subscribingClient->shouldReceive('on')
->with('unsubscribe', Mockery::on(function ($callback) use ($channel) {
$callback($channel);
return true;
}))->once();

$subscribingClient->shouldReceive('on')
->with('message', Mockery::any())
->zeroOrMoreTimes();

$subscribingClient->shouldReceive('subscribe')
->twice()
->with($channel);


$clientFactory = Mockery::mock(RedisClientFactory::class);

// The first call to make() will return a publishing client
$clientFactory->shouldReceive('make')
->once()
->andReturn(Mockery::mock(Client::class));

$clientFactory->shouldReceive('make')
->once()
->andReturn($subscribingClient);

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

$provider->subscribe();
});

0 comments on commit 65631f1

Please sign in to comment.