From 5ba24cb80c342d906c33162566a44907c82e66cf Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sun, 6 Sep 2020 10:53:03 +0300 Subject: [PATCH] Fixed tests for stats metrics --- .../Logger/RedisStatisticsLogger.php | 11 +++++ .../Logger/RedisStatisticsLoggerTest.php | 41 ++++--------------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/Statistics/Logger/RedisStatisticsLogger.php b/src/Statistics/Logger/RedisStatisticsLogger.php index b376567..5c070e8 100644 --- a/src/Statistics/Logger/RedisStatisticsLogger.php +++ b/src/Statistics/Logger/RedisStatisticsLogger.php @@ -165,6 +165,17 @@ class RedisStatisticsLogger implements StatisticsLogger return; } + // Statistics come into a list where the keys are on even indexes + // and the values are on odd indexes. This way, we know which + // ones are keys and which ones are values and their get combined + // later to form the key => value array + + [$keys, $values] = collect($statistic)->partition(function ($value, $key) { + return $key % 2 === 0; + }); + + $statistic = array_combine($keys->all(), $values->all()); + $this->createRecord($statistic, $appId); $this->channelManager diff --git a/tests/Statistics/Logger/RedisStatisticsLoggerTest.php b/tests/Statistics/Logger/RedisStatisticsLoggerTest.php index da75048..1b70b7f 100644 --- a/tests/Statistics/Logger/RedisStatisticsLoggerTest.php +++ b/tests/Statistics/Logger/RedisStatisticsLoggerTest.php @@ -4,6 +4,7 @@ namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger; +use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry; use BeyondCode\LaravelWebSockets\Tests\TestCase; class RedisStatisticsLoggerTest extends TestCase @@ -76,50 +77,26 @@ class RedisStatisticsLoggerTest extends TestCase { config(['cache.default' => 'redis']); - $connection = $this->getConnectedWebSocketConnection(['channel-1']); - $logger = new RedisStatisticsLogger( $this->channelManager, $this->statisticsDriver ); + $logger->resetAppTraces('1'); $logger->resetAppTraces('1234'); - $logger->webSocketMessage($connection->app->id); - $logger->apiMessage($connection->app->id); - $logger->connection($connection->app->id); - $logger->disconnection($connection->app->id); - - $logger->save(); - - $this->markTestIncomplete( - 'The numbers does not seem to match well.' - ); - } - - /** @test */ - public function it_counts_connections_with_redis_logger_with_existing_data() - { - config(['cache.default' => 'redis']); - $connection = $this->getConnectedWebSocketConnection(['channel-1']); - $logger = new RedisStatisticsLogger( - $this->channelManager, - $this->statisticsDriver - ); - - $logger->resetStatistics('1234', 0); - - $logger->webSocketMessage($connection->app->id); $logger->apiMessage($connection->app->id); - $logger->connection($connection->app->id); - $logger->disconnection($connection->app->id); $logger->save(); - $this->markTestIncomplete( - 'The numbers does not seem to match well.' - ); + $this->assertCount(1, WebSocketsStatisticsEntry::all()); + + $entry = WebSocketsStatisticsEntry::first(); + + $this->assertEquals(1, $entry->peak_connection_count); + $this->assertEquals(1, $entry->websocket_message_count); + $this->assertEquals(1, $entry->api_message_count); } }