fix tests

This commit is contained in:
freek 2018-12-03 14:44:26 +01:00
parent 3d94b97158
commit 2a23f2b6fb
6 changed files with 140 additions and 71 deletions

View File

@ -2,13 +2,19 @@
namespace BeyondCode\LaravelWebSockets\Facades; namespace BeyondCode\LaravelWebSockets\Facades;
use BeyondCode\LaravelWebSockets\Statistics\Logging\FakeStatisticsLogger;
use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Facade;
/** @see \BeyondCode\LaravelWebSockets\Statistics\Logging\StatisticsLogger */ /** @see \BeyondCode\LaravelWebSockets\Statistics\Logging\HttpStatisticsLogger */
class StatisticsLogger extends Facade class StatisticsLogger extends Facade
{ {
protected static function getFacadeAccessor() protected static function getFacadeAccessor()
{ {
return 'websockets.statisticslogger'; return 'websockets.statisticslogger';
} }
public static function fake()
{
static::swap(new FakeStatisticsLogger());
}
} }

View File

@ -0,0 +1,42 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Client;
use Ratchet\ConnectionInterface;
class FakeStatisticsLogger implements StatisticsLogger
{
public function logWebSocketMessage(ConnectionInterface $connection)
{
}
public function logApiMessage($appId)
{
}
public function logConnection(ConnectionInterface $connection)
{
}
public function logDisconnection(ConnectionInterface $connection)
{
}
protected function initializeStatistics($id)
{
}
public function save()
{
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Client;
use Ratchet\ConnectionInterface;
class HttpStatisticsLogger implements StatisticsLogger
{
/** @var Statistic[] */
protected $statistics = [];
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
protected $channelManager;
public function __construct(ChannelManager $channelManager, Client $client)
{
$this->channelManager = $channelManager;
$this->client = $client;
}
public function logWebSocketMessage(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->logWebSocketMessage();
}
public function logApiMessage($appId)
{
$this->initializeStatistics($appId);
$this->statistics[$appId]->logApiMessage();
}
public function logConnection(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->logConnection();
}
public function logDisconnection(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->logDisconnection();
}
protected function initializeStatistics($id)
{
if (!isset($this->statistics[$id])) {
$this->statistics[$id] = new Statistic($id);
}
}
public function save()
{
foreach ($this->statistics as $appId => $statistic) {
if (! $statistic->isEnabled()) {
continue;
}
$this->client->postAsync(
action([WebsocketStatisticsEntriesController::class, 'store']),
$statistic->toArray()
);
// Reset connection and message count
$currentConnectionCount = collect($this->channelManager->getChannels($appId))
->sum(function ($channel) {
return count($channel->getSubscribedConnections());
});
$statistic->reset($currentConnectionCount);
}
}
}

View File

@ -2,80 +2,17 @@
namespace BeyondCode\LaravelWebSockets\Statistics\Logging; namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Client;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
class StatisticsLogger interface StatisticsLogger
{ {
/** @var Statistic[] */ public function logWebSocketMessage(ConnectionInterface $connection);
protected $statistics = [];
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ public function logApiMessage($appId);
protected $channelManager;
public function __construct(ChannelManager $channelManager, Client $client) public function logConnection(ConnectionInterface $connection);
{
$this->channelManager = $channelManager;
$this->client = $client; public function logDisconnection(ConnectionInterface $connection);
}
public function logWebSocketMessage(ConnectionInterface $connection) public function save();
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->logWebSocketMessage();
}
public function logApiMessage($appId)
{
$this->initializeStatistics($appId);
$this->statistics[$appId]->logApiMessage();
}
public function logConnection(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->logConnection();
}
public function logDisconnection(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->logDisconnection();
}
protected function initializeStatistics($id)
{
if (!isset($this->statistics[$id])) {
$this->statistics[$id] = new Statistic($id);
}
}
public function save()
{
foreach ($this->statistics as $appId => $statistic) {
if (! $statistic->isEnabled()) {
continue;
}
$this->client->postAsync(
action([WebsocketStatisticsEntriesController::class, 'store']),
$statistic->toArray()
);
// Reset connection and message count
$currentConnectionCount = collect($this->channelManager->getChannels($appId))
->sum(function ($channel) {
return count($channel->getSubscribedConnections());
});
$statistic->reset($currentConnectionCount);
}
}
} }

View File

@ -8,7 +8,7 @@ use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize; use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
use BeyondCode\LaravelWebSockets\Server\Router; use BeyondCode\LaravelWebSockets\Server\Router;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\Statistics\Logging\StatisticsLogger; use BeyondCode\LaravelWebSockets\Statistics\Logging\HttpStatisticsLogger;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use BeyondCode\LaravelWebSockets\Apps\AppProvider; use BeyondCode\LaravelWebSockets\Apps\AppProvider;

View File

@ -2,6 +2,7 @@
namespace BeyondCode\LaravelWebSockets\Tests; namespace BeyondCode\LaravelWebSockets\Tests;
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
@ -29,6 +30,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
$this->channelManager = app(ChannelManager::class); $this->channelManager = app(ChannelManager::class);
Route::webSockets(); Route::webSockets();
StatisticsLogger::fake();
} }
protected function getPackageProviders($app) protected function getPackageProviders($app)