Moved tests across classes
This commit is contained in:
parent
14a79447f5
commit
ed41ad5ca0
|
|
@ -598,10 +598,8 @@ class RedisChannelManager extends LocalChannelManager
|
||||||
$stop = "({$stop}";
|
$stop = "({$stop}";
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->publishClient->zrangebyscore(
|
return $this->publishClient
|
||||||
$this->getRedisKey(null, null, ['sockets']),
|
->zrangebyscore($this->getRedisKey(null, null, ['sockets']), $start, $stop)
|
||||||
$start, $stop
|
|
||||||
)
|
|
||||||
->then(function ($list) {
|
->then(function ($list) {
|
||||||
return collect($list)->mapWithKeys(function ($appWithSocket) {
|
return collect($list)->mapWithKeys(function ($appWithSocket) {
|
||||||
[$appId, $socketId] = explode(':', $appWithSocket);
|
[$appId, $socketId] = explode(':', $appWithSocket);
|
||||||
|
|
@ -621,8 +619,7 @@ class RedisChannelManager extends LocalChannelManager
|
||||||
public function addChannelToSet($appId, string $channel)
|
public function addChannelToSet($appId, string $channel)
|
||||||
{
|
{
|
||||||
return $this->publishClient->sadd(
|
return $this->publishClient->sadd(
|
||||||
$this->getRedisKey($appId, null, ['channels']),
|
$this->getRedisKey($appId, null, ['channels']), $channel
|
||||||
$channel
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -636,8 +633,7 @@ class RedisChannelManager extends LocalChannelManager
|
||||||
public function removeChannelFromSet($appId, string $channel)
|
public function removeChannelFromSet($appId, string $channel)
|
||||||
{
|
{
|
||||||
return $this->publishClient->srem(
|
return $this->publishClient->srem(
|
||||||
$this->getRedisKey($appId, null, ['channels']),
|
$this->getRedisKey($appId, null, ['channels']), $channel
|
||||||
$channel
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -712,8 +708,7 @@ class RedisChannelManager extends LocalChannelManager
|
||||||
protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId)
|
protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId)
|
||||||
{
|
{
|
||||||
$this->publishClient->sadd(
|
$this->publishClient->sadd(
|
||||||
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']),
|
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']), $socketId
|
||||||
$socketId
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -729,8 +724,7 @@ class RedisChannelManager extends LocalChannelManager
|
||||||
protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId)
|
protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId)
|
||||||
{
|
{
|
||||||
$this->publishClient->srem(
|
$this->publishClient->srem(
|
||||||
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']),
|
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']), $socketId
|
||||||
$socketId
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
namespace BeyondCode\LaravelWebSockets\Test;
|
namespace BeyondCode\LaravelWebSockets\Test;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
|
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
class PresenceChannelTest extends TestCase
|
class PresenceChannelTest extends TestCase
|
||||||
|
|
@ -312,4 +313,146 @@ class PresenceChannelTest extends TestCase
|
||||||
$this->assertCount(1, $sockets);
|
$this->assertCount(1, $sockets);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$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());
|
||||||
|
|
||||||
|
// Make the connection look like it was lost 1 day ago.
|
||||||
|
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getGlobalConnectionsCount('1234', 'presence-channel')
|
||||||
|
->then(function ($count) {
|
||||||
|
$this->assertEquals(2, $count);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
||||||
|
->then(function ($expiredConnections) {
|
||||||
|
$this->assertCount(1, $expiredConnections);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getChannelMembers('1234', 'presence-channel')
|
||||||
|
->then(function ($members) {
|
||||||
|
$this->assertCount(2, $members);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager->removeObsoleteConnections();
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getGlobalConnectionsCount('1234', 'presence-channel')
|
||||||
|
->then(function ($count) {
|
||||||
|
$this->assertEquals(1, $count);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
||||||
|
->then(function ($expiredConnections) {
|
||||||
|
$this->assertCount(0, $expiredConnections);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getChannelMembers('1234', 'presence-channel')
|
||||||
|
->then(function ($members) {
|
||||||
|
$this->assertCount(1, $members);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_events_are_processed_by_on_message_on_presence_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$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());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_events_get_replicated_across_connections_for_presence_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$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'),
|
||||||
|
$message->getPayload(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
namespace BeyondCode\LaravelWebSockets\Test;
|
namespace BeyondCode\LaravelWebSockets\Test;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
|
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
class PrivateChannelTest extends TestCase
|
class PrivateChannelTest extends TestCase
|
||||||
|
|
@ -153,4 +154,113 @@ class PrivateChannelTest extends TestCase
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_not_ponged_connections_do_get_removed_for_private_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$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());
|
||||||
|
|
||||||
|
// Make the connection look like it was lost 1 day ago.
|
||||||
|
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getGlobalConnectionsCount('1234', 'private-channel')
|
||||||
|
->then(function ($count) {
|
||||||
|
$this->assertEquals(2, $count);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
||||||
|
->then(function ($expiredConnections) {
|
||||||
|
$this->assertCount(1, $expiredConnections);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager->removeObsoleteConnections();
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getGlobalConnectionsCount('1234', 'private-channel')
|
||||||
|
->then(function ($count) {
|
||||||
|
$this->assertEquals(1, $count);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
||||||
|
->then(function ($expiredConnections) {
|
||||||
|
$this->assertCount(0, $expiredConnections);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_events_are_processed_by_on_message_on_private_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$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_get_replicated_across_connections_for_private_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$connection = $this->newPrivateConnection('private-channel');
|
||||||
|
$receiver = $this->newPrivateConnection('private-channel');
|
||||||
|
|
||||||
|
$message = new Mocks\SignedMessage([
|
||||||
|
'appId' => '1234',
|
||||||
|
'serverId' => $this->channelManager->getServerId(),
|
||||||
|
'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', [
|
||||||
|
$this->channelManager->getRedisKey('1234', 'private-channel'),
|
||||||
|
$message->getPayload(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\Test;
|
namespace BeyondCode\LaravelWebSockets\Test;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
class PublicChannelTest extends TestCase
|
class PublicChannelTest extends TestCase
|
||||||
|
|
@ -134,4 +135,113 @@ class PublicChannelTest extends TestCase
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_not_ponged_connections_do_get_removed_for_public_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$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());
|
||||||
|
|
||||||
|
// Make the connection look like it was lost 1 day ago.
|
||||||
|
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getGlobalConnectionsCount('1234', 'public-channel')
|
||||||
|
->then(function ($count) {
|
||||||
|
$this->assertEquals(2, $count);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
||||||
|
->then(function ($expiredConnections) {
|
||||||
|
$this->assertCount(1, $expiredConnections);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager->removeObsoleteConnections();
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getGlobalConnectionsCount('1234', 'public-channel')
|
||||||
|
->then(function ($count) {
|
||||||
|
$this->assertEquals(1, $count);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->channelManager
|
||||||
|
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
||||||
|
->then(function ($expiredConnections) {
|
||||||
|
$this->assertCount(0, $expiredConnections);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_events_are_processed_by_on_message_on_public_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$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_get_replicated_across_connections_for_public_channels()
|
||||||
|
{
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
|
||||||
|
$connection = $this->newActiveConnection(['public-channel']);
|
||||||
|
$receiver = $this->newActiveConnection(['public-channel']);
|
||||||
|
|
||||||
|
$message = new Mocks\Message([
|
||||||
|
'appId' => '1234',
|
||||||
|
'serverId' => $this->channelManager->getServerId(),
|
||||||
|
'event' => 'some-event',
|
||||||
|
'data' => [
|
||||||
|
'channel' => 'public-channel',
|
||||||
|
'test' => 'yes',
|
||||||
|
],
|
||||||
|
'socketId' => $connection->socketId,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$channel = $this->channelManager->find('1234', 'public-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', 'public-channel'),
|
||||||
|
$message->getPayload(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\Test;
|
namespace BeyondCode\LaravelWebSockets\Test;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
|
|
||||||
class ReplicationTest extends TestCase
|
class ReplicationTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -25,345 +23,14 @@ class ReplicationTest extends TestCase
|
||||||
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
|
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_events_get_replicated_across_connections_for_public_channels()
|
public function test_unsubscribe_from_topic_when_the_last_connection_leaves()
|
||||||
{
|
|
||||||
$connection = $this->newActiveConnection(['public-channel']);
|
|
||||||
$receiver = $this->newActiveConnection(['public-channel']);
|
|
||||||
|
|
||||||
$message = new Mocks\Message([
|
|
||||||
'appId' => '1234',
|
|
||||||
'serverId' => $this->channelManager->getServerId(),
|
|
||||||
'event' => 'some-event',
|
|
||||||
'data' => [
|
|
||||||
'channel' => 'public-channel',
|
|
||||||
'test' => 'yes',
|
|
||||||
],
|
|
||||||
'socketId' => $connection->socketId,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$channel = $this->channelManager->find('1234', 'public-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', 'public-channel'),
|
|
||||||
$message->getPayload(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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([
|
|
||||||
'appId' => '1234',
|
|
||||||
'serverId' => $this->channelManager->getServerId(),
|
|
||||||
'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', [
|
|
||||||
$this->channelManager->getRedisKey('1234', 'private-channel'),
|
|
||||||
$message->getPayload(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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'),
|
|
||||||
$message->getPayload(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_not_ponged_connections_do_get_removed_for_public_channels()
|
|
||||||
{
|
|
||||||
$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());
|
|
||||||
|
|
||||||
// Make the connection look like it was lost 1 day ago.
|
|
||||||
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getGlobalConnectionsCount('1234', 'public-channel')
|
|
||||||
->then(function ($count) {
|
|
||||||
$this->assertEquals(2, $count);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
|
||||||
->then(function ($expiredConnections) {
|
|
||||||
$this->assertCount(1, $expiredConnections);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager->removeObsoleteConnections();
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getGlobalConnectionsCount('1234', 'public-channel')
|
|
||||||
->then(function ($count) {
|
|
||||||
$this->assertEquals(1, $count);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
|
||||||
->then(function ($expiredConnections) {
|
|
||||||
$this->assertCount(0, $expiredConnections);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_not_ponged_connections_do_get_removed_for_private_channels()
|
|
||||||
{
|
|
||||||
$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());
|
|
||||||
|
|
||||||
// Make the connection look like it was lost 1 day ago.
|
|
||||||
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getGlobalConnectionsCount('1234', 'private-channel')
|
|
||||||
->then(function ($count) {
|
|
||||||
$this->assertEquals(2, $count);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
|
||||||
->then(function ($expiredConnections) {
|
|
||||||
$this->assertCount(1, $expiredConnections);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager->removeObsoleteConnections();
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getGlobalConnectionsCount('1234', 'private-channel')
|
|
||||||
->then(function ($count) {
|
|
||||||
$this->assertEquals(1, $count);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
|
||||||
->then(function ($expiredConnections) {
|
|
||||||
$this->assertCount(0, $expiredConnections);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
|
|
||||||
{
|
|
||||||
$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());
|
|
||||||
|
|
||||||
// Make the connection look like it was lost 1 day ago.
|
|
||||||
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getGlobalConnectionsCount('1234', 'presence-channel')
|
|
||||||
->then(function ($count) {
|
|
||||||
$this->assertEquals(2, $count);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
|
||||||
->then(function ($expiredConnections) {
|
|
||||||
$this->assertCount(1, $expiredConnections);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getChannelMembers('1234', 'presence-channel')
|
|
||||||
->then(function ($members) {
|
|
||||||
$this->assertCount(2, $members);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager->removeObsoleteConnections();
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getGlobalConnectionsCount('1234', 'presence-channel')
|
|
||||||
->then(function ($count) {
|
|
||||||
$this->assertEquals(1, $count);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
|
|
||||||
->then(function ($expiredConnections) {
|
|
||||||
$this->assertCount(0, $expiredConnections);
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->channelManager
|
|
||||||
->getChannelMembers('1234', 'presence-channel')
|
|
||||||
->then(function ($members) {
|
|
||||||
$this->assertCount(1, $members);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_events_are_processed_by_on_message_on_public_channels()
|
|
||||||
{
|
{
|
||||||
$connection = $this->newActiveConnection(['public-channel']);
|
$connection = $this->newActiveConnection(['public-channel']);
|
||||||
|
|
||||||
$message = new Mocks\Message([
|
$this->pusherServer->onClose($connection);
|
||||||
'appId' => '1234',
|
|
||||||
'serverId' => 'different_server_id',
|
|
||||||
'event' => 'some-event',
|
|
||||||
'data' => [
|
|
||||||
'channel' => 'public-channel',
|
|
||||||
'test' => 'yes',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->channelManager->onMessage(
|
$this->getSubscribeClient()
|
||||||
$this->channelManager->getRedisKey('1234', 'public-channel'),
|
->assertCalledWithArgs('unsubscribe', [$this->channelManager->getRedisKey('1234')])
|
||||||
$message->getPayload()
|
->assertCalledWithArgs('unsubscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
|
||||||
);
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue