laravel-websockets/tests/ReplicationTest.php

370 lines
13 KiB
PHP
Raw Normal View History

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
{
/**
* {@inheritdoc}
*/
public function setUp(): void
2020-09-10 19:59:26 +00:00
{
parent::setUp();
2020-09-10 19:59:26 +00:00
$this->runOnlyOnRedisReplication();
}
public function test_publishing_client_gets_subscribed()
{
$this->newActiveConnection(['public-channel']);
2020-09-10 19:59:26 +00:00
$this->getSubscribeClient()
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234')])
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
}
2020-09-18 09:15:49 +00:00
public function test_events_get_replicated_across_connections_for_public_channels()
{
2020-09-10 19:59:26 +00:00
$connection = $this->newActiveConnection(['public-channel']);
2020-09-18 09:15:49 +00:00
$receiver = $this->newActiveConnection(['public-channel']);
2020-09-10 19:59:26 +00:00
2020-09-18 09:15:49 +00:00
$message = new Mocks\Message([
2020-09-10 19:59:26 +00:00
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
2020-09-10 19:59:26 +00:00
'event' => 'some-event',
'data' => [
'channel' => 'public-channel',
'test' => 'yes',
],
2020-09-18 09:15:49 +00:00
'socketId' => $connection->socketId,
]);
2020-09-10 19:59:26 +00:00
$channel = $this->channelManager->find('1234', 'public-channel');
$channel->broadcastToEveryoneExcept(
2020-09-18 09:15:49 +00:00
$message->getPayloadAsObject(), $connection->socketId, '1234', true
2020-09-10 19:59:26 +00:00
);
2020-09-18 09:15:49 +00:00
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
$this->getSubscribeClient()
->assertNothingDispatched();
$this->getPublishClient()
->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'public-channel'),
2020-09-18 09:16:14 +00:00
$message->getPayload(),
2020-09-18 09:15:49 +00:00
]);
}
public function test_events_get_replicated_across_connections_for_private_channels()
{
$connection = $this->newPrivateConnection('private-channel');
$receiver = $this->newPrivateConnection('private-channel');
$message = new Mocks\SignedMessage([
2020-09-10 19:59:26 +00:00
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
2020-09-18 09:15:49 +00:00
'event' => 'some-event',
'data' => [
'channel' => 'private-channel',
'test' => 'yes',
],
'socketId' => $connection->socketId,
], $connection, 'private-channel');
$channel = $this->channelManager->find('1234', 'private-channel');
$channel->broadcastToEveryoneExcept(
$message->getPayloadAsObject(), $connection->socketId, '1234', true
);
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
$this->getSubscribeClient()
->assertNothingDispatched();
$this->getPublishClient()
->assertCalledWithArgs('publish', [
2020-09-18 09:15:49 +00:00
$this->channelManager->getRedisKey('1234', 'private-channel'),
2020-09-18 09:16:14 +00:00
$message->getPayload(),
2020-09-18 09:15:49 +00:00
]);
}
public function test_events_get_replicated_across_connections_for_presence_channels()
{
$connection = $this->newPresenceConnection('presence-channel');
$receiver = $this->newPresenceConnection('presence-channel', ['user_id' => 2]);
$user = [
'user_id' => 1,
'user_info' => [
'name' => 'Rick',
],
];
$encodedUser = json_encode($user);
$message = new Mocks\SignedMessage([
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
'event' => 'some-event',
'data' => [
'channel' => 'presence-channel',
'channel_data' => $encodedUser,
'test' => 'yes',
],
'socketId' => $connection->socketId,
], $connection, 'presence-channel', $encodedUser);
$channel = $this->channelManager->find('1234', 'presence-channel');
$channel->broadcastToEveryoneExcept(
$message->getPayloadAsObject(), $connection->socketId, '1234', true
);
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
$this->getSubscribeClient()
->assertNothingDispatched();
$this->getPublishClient()
->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'presence-channel'),
2020-09-18 09:16:14 +00:00
$message->getPayload(),
]);
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-18 09:15:49 +00:00
public function test_events_are_processed_by_on_message_on_public_channels()
{
$connection = $this->newActiveConnection(['public-channel']);
$message = new Mocks\Message([
'appId' => '1234',
'serverId' => 'different_server_id',
'event' => 'some-event',
'data' => [
'channel' => 'public-channel',
'test' => 'yes',
],
]);
$this->channelManager->onMessage(
$this->channelManager->getRedisKey('1234', 'public-channel'),
$message->getPayload()
);
// The message does not contain appId and serverId anymore.
$message = new Mocks\Message([
'event' => 'some-event',
'data' => [
'channel' => 'public-channel',
'test' => 'yes',
],
]);
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
}
public function test_events_are_processed_by_on_message_on_private_channels()
{
$connection = $this->newPrivateConnection('private-channel');
$message = new Mocks\SignedMessage([
'appId' => '1234',
'serverId' => 'different_server_id',
'event' => 'some-event',
'data' => [
'channel' => 'private-channel',
'test' => 'yes',
],
], $connection, 'private-channel');
$this->channelManager->onMessage(
$this->channelManager->getRedisKey('1234', 'private-channel'),
$message->getPayload()
);
// The message does not contain appId and serverId anymore.
$message = new Mocks\SignedMessage([
'event' => 'some-event',
'data' => [
'channel' => 'private-channel',
'test' => 'yes',
],
], $connection, 'private-channel');
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
}
public function test_events_are_processed_by_on_message_on_presence_channels()
{
$user = [
'user_id' => 1,
'user_info' => [
'name' => 'Rick',
],
];
$connection = $this->newPresenceConnection('presence-channel', $user);
$encodedUser = json_encode($user);
$message = new Mocks\SignedMessage([
'appId' => '1234',
'serverId' => 'different_server_id',
'event' => 'some-event',
'data' => [
'channel' => 'presence-channel',
'channel_data' => $encodedUser,
'test' => 'yes',
],
], $connection, 'presence-channel', $encodedUser);
$this->channelManager->onMessage(
$this->channelManager->getRedisKey('1234', 'presence-channel'),
$message->getPayload()
);
// The message does not contain appId and serverId anymore.
$message = new Mocks\SignedMessage([
'event' => 'some-event',
'data' => [
'channel' => 'presence-channel',
'channel_data' => $encodedUser,
'test' => 'yes',
],
], $connection, 'presence-channel', $encodedUser);
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
}
2020-09-10 19:59:26 +00:00
}