diff --git a/src/Facades/StatisticsLogger.php b/src/Facades/StatisticsLogger.php index c43aab4..858d63f 100644 --- a/src/Facades/StatisticsLogger.php +++ b/src/Facades/StatisticsLogger.php @@ -3,7 +3,6 @@ namespace BeyondCode\LaravelWebSockets\Facades; use Illuminate\Support\Facades\Facade; -use BeyondCode\LaravelWebSockets\Statistics\Logger\FakeStatisticsLogger; use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface; /** @see \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger */ @@ -13,9 +12,4 @@ class StatisticsLogger extends Facade { return StatisticsLoggerInterface::class; } - - public static function fake() - { - static::swap(new FakeStatisticsLogger()); - } } diff --git a/src/Statistics/Logger/FakeStatisticsLogger.php b/src/Statistics/Logger/FakeStatisticsLogger.php deleted file mode 100644 index 63a8640..0000000 --- a/src/Statistics/Logger/FakeStatisticsLogger.php +++ /dev/null @@ -1,32 +0,0 @@ -getChannels($appId)) - ->sum(function ($channel) { - return count($channel->getSubscribedConnections()); - }); + ->flatMap(function (Channel $channel) { + return collect($channel->getSubscribedConnections())->pluck('socketId'); + }) + ->unique() + ->count(); } public function removeFromAllChannels(ConnectionInterface $connection) diff --git a/tests/Statistics/Logger/FakeStatisticsLogger.php b/tests/Statistics/Logger/FakeStatisticsLogger.php new file mode 100644 index 0000000..1f05bff --- /dev/null +++ b/tests/Statistics/Logger/FakeStatisticsLogger.php @@ -0,0 +1,23 @@ +statistics as $appId => $statistic) { + $currentConnectionCount = $this->channelManager->getConnectionCount($appId); + $statistic->reset($currentConnectionCount); + } + } + + public function getForAppId($appId): array + { + $statistic = $this->findOrMakeStatisticForAppId($appId); + + return $statistic->toArray(); + } +} diff --git a/tests/Statistics/Logger/StatisticsLoggerTest.php b/tests/Statistics/Logger/StatisticsLoggerTest.php new file mode 100644 index 0000000..23f0a6a --- /dev/null +++ b/tests/Statistics/Logger/StatisticsLoggerTest.php @@ -0,0 +1,46 @@ +getConnectedWebSocketConnection(['channel-1']); + $connections[] = $this->getConnectedWebSocketConnection(['channel-1']); + $connections[] = $this->getConnectedWebSocketConnection(['channel-1']); + + $this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']); + + $this->pusherServer->onClose(array_pop($connections)); + + StatisticsLogger::save(); + + $this->assertEquals(2, StatisticsLogger::getForAppId(1234)['peak_connection_count']); + } + + /** @test */ + public function it_counts_unique_connections_no_channel_subscriptions() + { + $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']); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index f37d10d..e10f9a5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,8 @@ namespace BeyondCode\LaravelWebSockets\Tests; +use Mockery; +use Clue\React\Buzz\Browser; use GuzzleHttp\Psr7\Request; use Ratchet\ConnectionInterface; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; @@ -10,6 +12,7 @@ use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider; use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; +use BeyondCode\LaravelWebSockets\Tests\Statistics\Logger\FakeStatisticsLogger; abstract class TestCase extends \Orchestra\Testbench\TestCase { @@ -27,7 +30,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase $this->channelManager = app(ChannelManager::class); - StatisticsLogger::fake(); + StatisticsLogger::swap(new FakeStatisticsLogger( + $this->channelManager, + Mockery::mock(Browser::class) + )); } protected function getPackageProviders($app)