diff --git a/tests/PresenceChannelTest.php b/tests/PresenceChannelTest.php index 55ad3d1..2317fca 100644 --- a/tests/PresenceChannelTest.php +++ b/tests/PresenceChannelTest.php @@ -152,9 +152,9 @@ class PresenceChannelTest extends TestCase $this->channelManager ->getChannelMembers('1234', 'presence-channel') - ->then(function ($members) { + ->then(function ($members) use ($rick) { $this->assertCount(1, $members); - $this->assertEquals(1, $members[0]->user_id); + $this->assertEquals(1, $members[$rick->socketId]->user_id); }); } diff --git a/tests/ReplicationTest.php b/tests/ReplicationTest.php index e864c6b..8eaad91 100644 --- a/tests/ReplicationTest.php +++ b/tests/ReplicationTest.php @@ -2,6 +2,8 @@ namespace BeyondCode\LaravelWebSockets\Test; +use Carbon\Carbon; + class ReplicationTest extends TestCase { /** @@ -61,13 +63,23 @@ class ReplicationTest extends TestCase public function test_not_ponged_connections_do_get_removed_for_public_channels() { - $connection = $this->newActiveConnection(['public-channel']); + $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($connection, now()->subDays(1)); + $this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1)); $this->channelManager - ->getConnectionsFromSet(0, now()->subMinutes(2)->format('U')) + ->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); }); @@ -77,11 +89,11 @@ class ReplicationTest extends TestCase $this->channelManager ->getGlobalConnectionsCount('1234', 'public-channel') ->then(function ($count) { - $this->assertEquals(0, $count); + $this->assertEquals(1, $count); }); $this->channelManager - ->getConnectionsFromSet(0, now()->subMinutes(2)->format('U')) + ->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U')) ->then(function ($expiredConnections) { $this->assertCount(0, $expiredConnections); }); @@ -89,13 +101,23 @@ class ReplicationTest extends TestCase public function test_not_ponged_connections_do_get_removed_for_private_channels() { - $connection = $this->newPrivateConnection('private-channel'); + $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($connection, now()->subDays(1)); + $this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1)); $this->channelManager - ->getConnectionsFromSet(0, now()->subMinutes(2)->format('U')) + ->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); }); @@ -105,11 +127,11 @@ class ReplicationTest extends TestCase $this->channelManager ->getGlobalConnectionsCount('1234', 'private-channel') ->then(function ($count) { - $this->assertEquals(0, $count); + $this->assertEquals(1, $count); }); $this->channelManager - ->getConnectionsFromSet(0, now()->subMinutes(2)->format('U')) + ->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U')) ->then(function ($expiredConnections) { $this->assertCount(0, $expiredConnections); }); @@ -117,13 +139,23 @@ class ReplicationTest extends TestCase public function test_not_ponged_connections_do_get_removed_for_presence_channels() { - $connection = $this->newPresenceConnection('presence-channel'); + $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($connection, now()->subDays(1)); + $this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1)); $this->channelManager - ->getConnectionsFromSet(0, now()->subMinutes(2)->format('U')) + ->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); }); @@ -131,19 +163,19 @@ class ReplicationTest extends TestCase $this->channelManager ->getChannelMembers('1234', 'presence-channel') ->then(function ($members) { - $this->assertCount(1, $members); + $this->assertCount(2, $members); }); $this->channelManager->removeObsoleteConnections(); $this->channelManager - ->getGlobalConnectionsCount('1234', 'private-channel') + ->getGlobalConnectionsCount('1234', 'presence-channel') ->then(function ($count) { - $this->assertEquals(0, $count); + $this->assertEquals(1, $count); }); $this->channelManager - ->getConnectionsFromSet(0, now()->subMinutes(2)->format('U')) + ->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U')) ->then(function ($expiredConnections) { $this->assertCount(0, $expiredConnections); }); @@ -151,7 +183,7 @@ class ReplicationTest extends TestCase $this->channelManager ->getChannelMembers('1234', 'presence-channel') ->then(function ($members) { - $this->assertCount(0, $members); + $this->assertCount(1, $members); }); } } diff --git a/tests/TriggerEventTest.php b/tests/TriggerEventTest.php index 5b85d12..9b087bd 100644 --- a/tests/TriggerEventTest.php +++ b/tests/TriggerEventTest.php @@ -65,7 +65,7 @@ class TriggerEventTest extends TestCase $this->statisticsCollector ->getAppStatistics('1234') - ->then(function ($statistics) { + ->then(function ($statistic) { $this->assertEquals([ 'peak_connections_count' => 1, 'websocket_messages_count' => 1, @@ -106,7 +106,7 @@ class TriggerEventTest extends TestCase $this->statisticsCollector ->getAppStatistics('1234') - ->then(function ($statistics) { + ->then(function ($statistic) { $this->assertEquals([ 'peak_connections_count' => 1, 'websocket_messages_count' => 1, @@ -147,7 +147,7 @@ class TriggerEventTest extends TestCase $this->statisticsCollector ->getAppStatistics('1234') - ->then(function ($statistics) { + ->then(function ($statistic) { $this->assertEquals([ 'peak_connections_count' => 1, 'websocket_messages_count' => 1,