Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 5ba24cb

Browse files
committed
Fixed tests for stats metrics
1 parent 1c889be commit 5ba24cb

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

src/Statistics/Logger/RedisStatisticsLogger.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ public function save()
165165
return;
166166
}
167167

168+
// Statistics come into a list where the keys are on even indexes
169+
// and the values are on odd indexes. This way, we know which
170+
// ones are keys and which ones are values and their get combined
171+
// later to form the key => value array
172+
173+
[$keys, $values] = collect($statistic)->partition(function ($value, $key) {
174+
return $key % 2 === 0;
175+
});
176+
177+
$statistic = array_combine($keys->all(), $values->all());
178+
168179
$this->createRecord($statistic, $appId);
169180

170181
$this->channelManager

tests/Statistics/Logger/RedisStatisticsLoggerTest.php

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
66
use BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger;
7+
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
78
use BeyondCode\LaravelWebSockets\Tests\TestCase;
89

910
class RedisStatisticsLoggerTest extends TestCase
@@ -76,50 +77,26 @@ public function it_counts_connections_with_redis_logger_with_no_data()
7677
{
7778
config(['cache.default' => 'redis']);
7879

79-
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
80-
8180
$logger = new RedisStatisticsLogger(
8281
$this->channelManager,
8382
$this->statisticsDriver
8483
);
8584

85+
$logger->resetAppTraces('1');
8686
$logger->resetAppTraces('1234');
8787

88-
$logger->webSocketMessage($connection->app->id);
89-
$logger->apiMessage($connection->app->id);
90-
$logger->connection($connection->app->id);
91-
$logger->disconnection($connection->app->id);
92-
93-
$logger->save();
94-
95-
$this->markTestIncomplete(
96-
'The numbers does not seem to match well.'
97-
);
98-
}
99-
100-
/** @test */
101-
public function it_counts_connections_with_redis_logger_with_existing_data()
102-
{
103-
config(['cache.default' => 'redis']);
104-
10588
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
10689

107-
$logger = new RedisStatisticsLogger(
108-
$this->channelManager,
109-
$this->statisticsDriver
110-
);
111-
112-
$logger->resetStatistics('1234', 0);
113-
114-
$logger->webSocketMessage($connection->app->id);
11590
$logger->apiMessage($connection->app->id);
116-
$logger->connection($connection->app->id);
117-
$logger->disconnection($connection->app->id);
11891

11992
$logger->save();
12093

121-
$this->markTestIncomplete(
122-
'The numbers does not seem to match well.'
123-
);
94+
$this->assertCount(1, WebSocketsStatisticsEntry::all());
95+
96+
$entry = WebSocketsStatisticsEntry::first();
97+
98+
$this->assertEquals(1, $entry->peak_connection_count);
99+
$this->assertEquals(1, $entry->websocket_message_count);
100+
$this->assertEquals(1, $entry->api_message_count);
124101
}
125102
}

0 commit comments

Comments
 (0)