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

Commit 5e84ef1

Browse files
stayallivempociot
authored andcommitted
Fix connection counting (#74)
* Fix connection counting * Remove unused method from FakeStatisticsLogger * Add tests for testing the connection counting * StyleCI fixes * Do not use Laravel 5.7.10 testing helper * CS
1 parent d2146f7 commit 5e84ef1

File tree

6 files changed

+81
-42
lines changed

6 files changed

+81
-42
lines changed

src/Facades/StatisticsLogger.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace BeyondCode\LaravelWebSockets\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6-
use BeyondCode\LaravelWebSockets\Statistics\Logger\FakeStatisticsLogger;
76
use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface;
87

98
/** @see \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger */
@@ -13,9 +12,4 @@ protected static function getFacadeAccessor()
1312
{
1413
return StatisticsLoggerInterface::class;
1514
}
16-
17-
public static function fake()
18-
{
19-
static::swap(new FakeStatisticsLogger());
20-
}
2115
}

src/Statistics/Logger/FakeStatisticsLogger.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public function getChannels(string $appId): array
5555
public function getConnectionCount(string $appId): int
5656
{
5757
return collect($this->getChannels($appId))
58-
->sum(function ($channel) {
59-
return count($channel->getSubscribedConnections());
60-
});
58+
->flatMap(function (Channel $channel) {
59+
return collect($channel->getSubscribedConnections())->pluck('socketId');
60+
})
61+
->unique()
62+
->count();
6163
}
6264

6365
public function removeFromAllChannels(ConnectionInterface $connection)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Logger;
4+
5+
use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
6+
7+
class FakeStatisticsLogger extends HttpStatisticsLogger
8+
{
9+
public function save()
10+
{
11+
foreach ($this->statistics as $appId => $statistic) {
12+
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
13+
$statistic->reset($currentConnectionCount);
14+
}
15+
}
16+
17+
public function getForAppId($appId): array
18+
{
19+
$statistic = $this->findOrMakeStatisticForAppId($appId);
20+
21+
return $statistic->toArray();
22+
}
23+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers;
4+
5+
use BeyondCode\LaravelWebSockets\Tests\TestCase;
6+
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
7+
8+
class StatisticsLoggerTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_counts_connections()
12+
{
13+
$connections = [];
14+
15+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1']);
16+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1']);
17+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1']);
18+
19+
$this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
20+
21+
$this->pusherServer->onClose(array_pop($connections));
22+
23+
StatisticsLogger::save();
24+
25+
$this->assertEquals(2, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
26+
}
27+
28+
/** @test */
29+
public function it_counts_unique_connections_no_channel_subscriptions()
30+
{
31+
$connections = [];
32+
33+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);
34+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);
35+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1']);
36+
37+
$this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
38+
39+
$this->pusherServer->onClose(array_pop($connections));
40+
$this->pusherServer->onClose(array_pop($connections));
41+
42+
StatisticsLogger::save();
43+
44+
$this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
45+
}
46+
}

tests/TestCase.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace BeyondCode\LaravelWebSockets\Tests;
44

5+
use Mockery;
6+
use Clue\React\Buzz\Browser;
57
use GuzzleHttp\Psr7\Request;
68
use Ratchet\ConnectionInterface;
79
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
@@ -10,6 +12,7 @@
1012
use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider;
1113
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
1214
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
15+
use BeyondCode\LaravelWebSockets\Tests\Statistics\Logger\FakeStatisticsLogger;
1316

1417
abstract class TestCase extends \Orchestra\Testbench\TestCase
1518
{
@@ -27,7 +30,10 @@ public function setUp(): void
2730

2831
$this->channelManager = app(ChannelManager::class);
2932

30-
StatisticsLogger::fake();
33+
StatisticsLogger::swap(new FakeStatisticsLogger(
34+
$this->channelManager,
35+
Mockery::mock(Browser::class)
36+
));
3137
}
3238

3339
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)