laravel-websockets/tests/LocalPongRemovalTest.php

110 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace BeyondCode\LaravelWebSockets\Test;
use Carbon\Carbon;
class LocalPongRemovalTest extends TestCase
{
public function test_not_ponged_connections_do_get_removed_on_local_for_public_channels()
{
$this->runOnlyOnLocalReplication();
$activeConnection = $this->newActiveConnection(['public-channel']);
$obsoleteConnection = $this->newActiveConnection(['public-channel']);
// The active connection just pinged, it should not be closed.
$activeConnection->lastPongedAt = Carbon::now();
$obsoleteConnection->lastPongedAt = Carbon::now()->subDays(1);
$this->channelManager->updateConnectionInChannels($activeConnection);
$this->channelManager->updateConnectionInChannels($obsoleteConnection);
2020-12-07 18:36:39 +00:00
$this->channelManager->getGlobalConnectionsCount('1234', 'public-channel')->then(function ($count) {
$this->assertEquals(2, $count);
});
$this->channelManager->removeObsoleteConnections();
2020-12-07 18:36:39 +00:00
$this->channelManager->getGlobalConnectionsCount('1234', 'public-channel')->then(function ($count) {
$this->assertEquals(1, $count);
});
2020-12-07 18:36:39 +00:00
$this->channelManager->getLocalConnections()->then(function ($connections) use ($activeConnection) {
$connection = $connections[$activeConnection->socketId];
2020-12-07 18:36:39 +00:00
$this->assertEquals($activeConnection->socketId, $connection->socketId);
});
}
public function test_not_ponged_connections_do_get_removed_on_local_for_private_channels()
{
$this->runOnlyOnLocalReplication();
$activeConnection = $this->newPrivateConnection('private-channel');
$obsoleteConnection = $this->newPrivateConnection('private-channel');
// The active connection just pinged, it should not be closed.
$activeConnection->lastPongedAt = Carbon::now();
$obsoleteConnection->lastPongedAt = Carbon::now()->subDays(1);
$this->channelManager->updateConnectionInChannels($activeConnection);
$this->channelManager->updateConnectionInChannels($obsoleteConnection);
2020-12-07 18:36:39 +00:00
$this->channelManager->getGlobalConnectionsCount('1234', 'private-channel')->then(function ($count) {
$this->assertEquals(2, $count);
});
$this->channelManager->removeObsoleteConnections();
2020-12-07 18:36:39 +00:00
$this->channelManager->getGlobalConnectionsCount('1234', 'private-channel')->then(function ($count) {
$this->assertEquals(1, $count);
});
2020-12-07 18:36:39 +00:00
$this->channelManager->getLocalConnections()->then(function ($connections) use ($activeConnection) {
$connection = $connections[$activeConnection->socketId];
2020-12-07 18:36:39 +00:00
$this->assertEquals($activeConnection->socketId, $connection->socketId);
});
}
public function test_not_ponged_connections_do_get_removed_on_local_for_presence_channels()
{
$this->runOnlyOnLocalReplication();
$activeConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 1]);
$obsoleteConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 2]);
// The active connection just pinged, it should not be closed.
$activeConnection->lastPongedAt = Carbon::now();
$obsoleteConnection->lastPongedAt = Carbon::now()->subDays(1);
$this->channelManager->updateConnectionInChannels($activeConnection);
$this->channelManager->updateConnectionInChannels($obsoleteConnection);
2020-12-07 18:36:39 +00:00
$this->channelManager->getGlobalConnectionsCount('1234', 'presence-channel')->then(function ($count) {
$this->assertEquals(2, $count);
});
2020-12-07 18:36:39 +00:00
$this->channelManager->getChannelMembers('1234', 'presence-channel')->then(function ($members) {
$this->assertCount(2, $members);
});
$this->channelManager->removeObsoleteConnections();
2020-12-07 18:36:39 +00:00
$this->channelManager->getGlobalConnectionsCount('1234', 'presence-channel')->then(function ($count) {
$this->assertEquals(1, $count);
});
$this->channelManager->getLocalConnections()->then(function ($connections) use ($activeConnection) {
$connection = $connections[$activeConnection->socketId];
$this->assertEquals($activeConnection->socketId, $connection->socketId);
});
$this->channelManager->getChannelMembers('1234', 'presence-channel')->then(function ($members) {
$this->assertCount(1, $members);
});
}
}