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
This commit is contained in:
Alex Bouma 2019-05-11 08:40:45 +02:00 committed by Marcel Pociot
parent d2146f7977
commit 5e84ef1ddc
6 changed files with 81 additions and 42 deletions

View File

@ -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());
}
}

View File

@ -1,32 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
use Ratchet\ConnectionInterface;
class FakeStatisticsLogger implements StatisticsLogger
{
public function webSocketMessage(ConnectionInterface $connection)
{
}
public function apiMessage($appId)
{
}
public function connection(ConnectionInterface $connection)
{
}
public function disconnection(ConnectionInterface $connection)
{
}
protected function initializeStatistics($id)
{
}
public function save()
{
}
}

View File

@ -55,9 +55,11 @@ class ArrayChannelManager implements ChannelManager
public function getConnectionCount(string $appId): int
{
return collect($this->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)

View File

@ -0,0 +1,23 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Logger;
use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
class FakeStatisticsLogger extends HttpStatisticsLogger
{
public function save()
{
foreach ($this->statistics as $appId => $statistic) {
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
$statistic->reset($currentConnectionCount);
}
}
public function getForAppId($appId): array
{
$statistic = $this->findOrMakeStatisticForAppId($appId);
return $statistic->toArray();
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
class StatisticsLoggerTest extends TestCase
{
/** @test */
public function it_counts_connections()
{
$connections = [];
$connections[] = $this->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']);
}
}

View File

@ -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)