Added tests for statistics loggers drivers

This commit is contained in:
Alex Renoki 2020-08-23 13:40:18 +03:00
parent 108a717c0a
commit 2a6d91aaf7
3 changed files with 70 additions and 2 deletions

View File

@ -125,4 +125,14 @@ class MemoryStatisticsLogger implements StatisticsLogger
return $this->statistics[$appId]; return $this->statistics[$appId];
} }
/**
* Get the saved statistics.
*
* @return array
*/
public function getStatistics(): array
{
return $this->statistics;
}
} }

View File

@ -3,6 +3,9 @@
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers; namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers;
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger;
use BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger;
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
use BeyondCode\LaravelWebSockets\Tests\TestCase; use BeyondCode\LaravelWebSockets\Tests\TestCase;
class StatisticsLoggerTest extends TestCase class StatisticsLoggerTest extends TestCase
@ -43,4 +46,50 @@ class StatisticsLoggerTest extends TestCase
$this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']); $this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
} }
/** @test */
public function it_counts_connections_with_memory_logger()
{
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
$logger = new MemoryStatisticsLogger(
$this->channelManager,
$this->statisticsDriver
);
$logger->webSocketMessage($connection->app->id);
$logger->apiMessage($connection->app->id);
$logger->connection($connection->app->id);
$logger->disconnection($connection->app->id);
$logger->save();
$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);
}
/** @test */
public function it_counts_connections_with_null_logger()
{
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
$logger = new NullStatisticsLogger(
$this->channelManager,
$this->statisticsDriver
);
$logger->webSocketMessage($connection->app->id);
$logger->apiMessage($connection->app->id);
$logger->connection($connection->app->id);
$logger->disconnection($connection->app->id);
$logger->save();
$this->assertCount(0, WebSocketsStatisticsEntry::all());
}
} }

View File

@ -31,6 +31,13 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
*/ */
protected $channelManager; protected $channelManager;
/**
* The used statistics driver.
*
* @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver
*/
protected $statisticsDriver;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -38,9 +45,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
{ {
parent::setUp(); parent::setUp();
$this->pusherServer = app(config('websockets.handlers.websocket')); $this->pusherServer = $this->app->make(config('websockets.handlers.websocket'));
$this->channelManager = app(ChannelManager::class); $this->channelManager = $this->app->make(ChannelManager::class);
$this->statisticsDriver = $this->app->make(StatisticsDriver::class);
StatisticsLogger::swap(new FakeStatisticsLogger( StatisticsLogger::swap(new FakeStatisticsLogger(
$this->channelManager, $this->channelManager,