From d5a90d8440691111d7c19ffa548570f2ff7d5394 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 3 Sep 2020 16:50:37 +0300 Subject: [PATCH] Using the built-in Redis cache connection to handle non-pubsub features. --- src/PubSub/Drivers/RedisClient.php | 4 +-- .../PresenceChannelReplicationTest.php | 22 ++++++++++++--- .../HttpApi/FetchChannelsReplicationTest.php | 6 ++-- .../Logger/StatisticsLoggerTest.php | 28 +++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/PubSub/Drivers/RedisClient.php b/src/PubSub/Drivers/RedisClient.php index 40ae12a..6b922bc 100644 --- a/src/PubSub/Drivers/RedisClient.php +++ b/src/PubSub/Drivers/RedisClient.php @@ -226,7 +226,7 @@ class RedisClient extends LocalClient */ public function joinChannel($appId, string $channel, string $socketId, string $data) { - $this->publishClient->__call('hset', [$this->getTopicName($appId, $channel), $socketId, $data]); + $this->redis->hset($this->getTopicName($appId, $channel), $socketId, $data); DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_JOINED_CHANNEL, [ 'channel' => $channel, @@ -248,7 +248,7 @@ class RedisClient extends LocalClient */ public function leaveChannel($appId, string $channel, string $socketId) { - $this->publishClient->__call('hdel', [$this->getTopicName($appId, $channel), $socketId]); + $this->redis->hdel($this->getTopicName($appId, $channel), $socketId); DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_LEFT_CHANNEL, [ 'channel' => $channel, diff --git a/tests/Channels/PresenceChannelReplicationTest.php b/tests/Channels/PresenceChannelReplicationTest.php index e753f08..8f3fa27 100644 --- a/tests/Channels/PresenceChannelReplicationTest.php +++ b/tests/Channels/PresenceChannelReplicationTest.php @@ -4,9 +4,17 @@ namespace BeyondCode\LaravelWebSockets\Tests\Channels; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; use BeyondCode\LaravelWebSockets\Tests\TestCase; +use Illuminate\Support\Facades\Cache; class PresenceChannelReplicationTest extends TestCase { + /** + * The Redis manager instance. + * + * @var \Illuminate\Redis\RedisManager + */ + protected $redis; + /** * {@inheritdoc} */ @@ -15,6 +23,8 @@ class PresenceChannelReplicationTest extends TestCase parent::setUp(); $this->runOnlyOnRedisReplication(); + + $this->redis = Cache::getRedis(); } /** @test */ @@ -45,13 +55,17 @@ class PresenceChannelReplicationTest extends TestCase $this->pusherServer->onMessage($connection, $message); $this->getPublishClient() - ->assertCalledWithArgs('hset', [ + ->assertNotCalledWithArgs('hset', [ 'laravel_database_1234:presence-channel', $connection->socketId, json_encode($channelData), ]) ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel']) ->assertCalled('publish'); + + $this->assertNotNull( + $this->redis->hget('laravel_database_1234:presence-channel', $connection->socketId) + ); } /** @test */ @@ -82,7 +96,7 @@ class PresenceChannelReplicationTest extends TestCase ->assertEventDispatched('message'); $this->getPublishClient() - ->assertCalled('hset') + ->assertNotCalled('hset') ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel']) ->assertCalled('publish'); @@ -100,7 +114,7 @@ class PresenceChannelReplicationTest extends TestCase $this->pusherServer->onMessage($connection, $message); $this->getPublishClient() - ->assertCalled('hdel') + ->assertNotCalled('hdel') ->assertCalled('publish'); } @@ -129,7 +143,7 @@ class PresenceChannelReplicationTest extends TestCase $this->pusherServer->onMessage($connection, $message); $this->getPublishClient() - ->assertCalled('hset') + ->assertNotCalled('hset') ->assertcalledWithArgs('hgetall', ['laravel_database_1234:presence-channel']) ->assertCalled('publish'); } diff --git a/tests/HttpApi/FetchChannelsReplicationTest.php b/tests/HttpApi/FetchChannelsReplicationTest.php index 8c691c3..805b123 100644 --- a/tests/HttpApi/FetchChannelsReplicationTest.php +++ b/tests/HttpApi/FetchChannelsReplicationTest.php @@ -48,7 +48,7 @@ class FetchChannelsReplicationTest extends TestCase ->assertEventDispatched('message'); $this->getPublishClient() - ->assertCalled('hset') + ->assertNotCalled('hset') ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel']) ->assertCalled('publish') ->assertCalled('multi') @@ -88,7 +88,7 @@ class FetchChannelsReplicationTest extends TestCase ->assertEventDispatched('message'); $this->getPublishClient() - ->assertCalled('hset') + ->assertNotCalled('hset') ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1']) ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2']) ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2']) @@ -133,7 +133,7 @@ class FetchChannelsReplicationTest extends TestCase ->assertEventDispatched('message'); $this->getPublishClient() - ->assertCalled('hset') + ->assertNotCalled('hset') ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1']) ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2']) ->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2']) diff --git a/tests/Statistics/Logger/StatisticsLoggerTest.php b/tests/Statistics/Logger/StatisticsLoggerTest.php index 8374609..9c075b5 100644 --- a/tests/Statistics/Logger/StatisticsLoggerTest.php +++ b/tests/Statistics/Logger/StatisticsLoggerTest.php @@ -8,6 +8,7 @@ use BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger; use BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger; use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry; use BeyondCode\LaravelWebSockets\Tests\TestCase; +use Illuminate\Support\Facades\Cache; class StatisticsLoggerTest extends TestCase { @@ -32,6 +33,33 @@ class StatisticsLoggerTest extends TestCase /** @test */ public function it_counts_unique_connections_no_channel_subscriptions() { + $this->runOnlyOnLocalReplication(); + + $connections = []; + + $connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']); + $connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']); + $connections[] = $this->getConnectedWebSocketConnection(['channel-1']); + + $this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']); + + $this->pusherServer->onClose(array_pop($connections)); + $this->pusherServer->onClose(array_pop($connections)); + + StatisticsLogger::save(); + + $this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']); + } + + /** @test */ + public function it_counts_unique_connections_no_channel_subscriptions_on_redis() + { + $this->runOnlyOnRedisReplication(); + + $redis = Cache::getRedis(); + + $redis->hdel('laravel_database_1234', 'connections'); + $connections = []; $connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);