2020-09-10 19:59:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Test;
|
|
|
|
|
|
2020-09-17 11:18:15 +00:00
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
class ReplicationTest extends TestCase
|
|
|
|
|
{
|
2020-09-15 17:46:19 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function setUp(): void
|
2020-09-10 19:59:26 +00:00
|
|
|
{
|
2020-09-15 17:46:19 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->runOnlyOnRedisReplication();
|
2020-09-15 17:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_publishing_client_gets_subscribed()
|
|
|
|
|
{
|
|
|
|
|
$this->newActiveConnection(['public-channel']);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2020-09-15 17:46:19 +00:00
|
|
|
$this->getSubscribeClient()
|
|
|
|
|
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234')])
|
|
|
|
|
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_events_get_replicated_across_connections()
|
|
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
|
|
|
|
$message = [
|
|
|
|
|
'appId' => '1234',
|
2020-09-15 17:46:19 +00:00
|
|
|
'serverId' => $this->channelManager->getServerId(),
|
2020-09-10 19:59:26 +00:00
|
|
|
'event' => 'some-event',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'public-channel',
|
|
|
|
|
'test' => 'yes',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$channel = $this->channelManager->find('1234', 'public-channel');
|
|
|
|
|
|
|
|
|
|
$channel->broadcastToEveryoneExcept(
|
|
|
|
|
(object) $message, null, '1234', true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('some-event', [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'serverId' => $this->channelManager->getServerId(),
|
|
|
|
|
'data' => ['channel' => 'public-channel', 'test' => 'yes'],
|
|
|
|
|
]);
|
2020-09-15 17:46:19 +00:00
|
|
|
|
|
|
|
|
$this->getSubscribeClient()
|
|
|
|
|
->assertNothingDispatched();
|
|
|
|
|
|
|
|
|
|
$this->getPublishClient()
|
|
|
|
|
->assertCalledWithArgs('publish', [
|
|
|
|
|
$this->channelManager->getRedisKey('1234', 'public-channel'),
|
|
|
|
|
json_encode($message),
|
|
|
|
|
]);
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
2020-09-15 09:30:17 +00:00
|
|
|
|
|
|
|
|
public function test_not_ponged_connections_do_get_removed_for_public_channels()
|
|
|
|
|
{
|
2020-09-17 11:18:15 +00:00
|
|
|
$activeConnection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
$obsoleteConnection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
|
|
|
|
// The active connection just pinged, it should not be closed.
|
|
|
|
|
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
|
2020-09-15 09:30:17 +00:00
|
|
|
|
|
|
|
|
// Make the connection look like it was lost 1 day ago.
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
2020-09-15 09:30:17 +00:00
|
|
|
|
|
|
|
|
$this->channelManager
|
2020-09-17 11:18:15 +00:00
|
|
|
->getGlobalConnectionsCount('1234', 'public-channel')
|
|
|
|
|
->then(function ($count) {
|
|
|
|
|
$this->assertEquals(2, $count);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
2020-09-15 09:30:17 +00:00
|
|
|
->then(function ($expiredConnections) {
|
|
|
|
|
$this->assertCount(1, $expiredConnections);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager->removeObsoleteConnections();
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'public-channel')
|
|
|
|
|
->then(function ($count) {
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->assertEquals(1, $count);
|
2020-09-15 09:30:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
2020-09-17 11:18:15 +00:00
|
|
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
2020-09-15 09:30:17 +00:00
|
|
|
->then(function ($expiredConnections) {
|
|
|
|
|
$this->assertCount(0, $expiredConnections);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_not_ponged_connections_do_get_removed_for_private_channels()
|
|
|
|
|
{
|
2020-09-17 11:18:15 +00:00
|
|
|
$activeConnection = $this->newPrivateConnection('private-channel');
|
|
|
|
|
$obsoleteConnection = $this->newPrivateConnection('private-channel');
|
|
|
|
|
|
|
|
|
|
// The active connection just pinged, it should not be closed.
|
|
|
|
|
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
|
2020-09-15 09:30:17 +00:00
|
|
|
|
|
|
|
|
// Make the connection look like it was lost 1 day ago.
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'private-channel')
|
|
|
|
|
->then(function ($count) {
|
|
|
|
|
$this->assertEquals(2, $count);
|
|
|
|
|
});
|
2020-09-15 09:30:17 +00:00
|
|
|
|
|
|
|
|
$this->channelManager
|
2020-09-17 11:18:15 +00:00
|
|
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
2020-09-15 09:30:17 +00:00
|
|
|
->then(function ($expiredConnections) {
|
|
|
|
|
$this->assertCount(1, $expiredConnections);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager->removeObsoleteConnections();
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'private-channel')
|
|
|
|
|
->then(function ($count) {
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->assertEquals(1, $count);
|
2020-09-15 09:30:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
2020-09-17 11:18:15 +00:00
|
|
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
2020-09-15 09:30:17 +00:00
|
|
|
->then(function ($expiredConnections) {
|
|
|
|
|
$this->assertCount(0, $expiredConnections);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
|
|
|
|
|
{
|
2020-09-17 11:18:15 +00:00
|
|
|
$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.
|
|
|
|
|
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
|
2020-09-15 09:30:17 +00:00
|
|
|
|
|
|
|
|
// Make the connection look like it was lost 1 day ago.
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
2020-09-15 09:30:17 +00:00
|
|
|
|
|
|
|
|
$this->channelManager
|
2020-09-17 11:18:15 +00:00
|
|
|
->getGlobalConnectionsCount('1234', 'presence-channel')
|
|
|
|
|
->then(function ($count) {
|
|
|
|
|
$this->assertEquals(2, $count);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
2020-09-15 09:30:17 +00:00
|
|
|
->then(function ($expiredConnections) {
|
|
|
|
|
$this->assertCount(1, $expiredConnections);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getChannelMembers('1234', 'presence-channel')
|
|
|
|
|
->then(function ($members) {
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->assertCount(2, $members);
|
2020-09-15 09:30:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager->removeObsoleteConnections();
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
2020-09-17 11:18:15 +00:00
|
|
|
->getGlobalConnectionsCount('1234', 'presence-channel')
|
2020-09-15 09:30:17 +00:00
|
|
|
->then(function ($count) {
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->assertEquals(1, $count);
|
2020-09-15 09:30:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
2020-09-17 11:18:15 +00:00
|
|
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
2020-09-15 09:30:17 +00:00
|
|
|
->then(function ($expiredConnections) {
|
|
|
|
|
$this->assertCount(0, $expiredConnections);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getChannelMembers('1234', 'presence-channel')
|
|
|
|
|
->then(function ($members) {
|
2020-09-17 11:18:15 +00:00
|
|
|
$this->assertCount(1, $members);
|
2020-09-15 09:30:17 +00:00
|
|
|
});
|
|
|
|
|
}
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|