|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BeyondCode\LaravelWebSockets\Test; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | + |
| 7 | +class LocalPongRemovalTest extends TestCase |
| 8 | +{ |
| 9 | + public function test_not_ponged_connections_do_get_removed_on_local_for_public_channels() |
| 10 | + { |
| 11 | + $this->runOnlyOnLocalReplication(); |
| 12 | + |
| 13 | + $activeConnection = $this->newActiveConnection(['public-channel']); |
| 14 | + $obsoleteConnection = $this->newActiveConnection(['public-channel']); |
| 15 | + |
| 16 | + // The active connection just pinged, it should not be closed. |
| 17 | + $activeConnection->lastPongedAt = Carbon::now(); |
| 18 | + $obsoleteConnection->lastPongedAt = Carbon::now()->subDays(1); |
| 19 | + |
| 20 | + $this->channelManager->updateConnectionInChannels($activeConnection); |
| 21 | + $this->channelManager->updateConnectionInChannels($obsoleteConnection); |
| 22 | + |
| 23 | + $this->channelManager |
| 24 | + ->getGlobalConnectionsCount('1234', 'public-channel') |
| 25 | + ->then(function ($count) { |
| 26 | + $this->assertEquals(2, $count); |
| 27 | + }); |
| 28 | + |
| 29 | + $this->channelManager->removeObsoleteConnections(); |
| 30 | + |
| 31 | + $this->channelManager |
| 32 | + ->getGlobalConnectionsCount('1234', 'public-channel') |
| 33 | + ->then(function ($count) { |
| 34 | + $this->assertEquals(1, $count); |
| 35 | + }); |
| 36 | + |
| 37 | + $this->channelManager |
| 38 | + ->getLocalConnections() |
| 39 | + ->then(function ($connections) use ($activeConnection) { |
| 40 | + $connection = $connections[$activeConnection->socketId]; |
| 41 | + |
| 42 | + $this->assertEquals($activeConnection->socketId, $connection->socketId); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_not_ponged_connections_do_get_removed_on_local_for_private_channels() |
| 47 | + { |
| 48 | + $this->runOnlyOnLocalReplication(); |
| 49 | + |
| 50 | + $activeConnection = $this->newPrivateConnection('private-channel'); |
| 51 | + $obsoleteConnection = $this->newPrivateConnection('private-channel'); |
| 52 | + |
| 53 | + // The active connection just pinged, it should not be closed. |
| 54 | + $activeConnection->lastPongedAt = Carbon::now(); |
| 55 | + $obsoleteConnection->lastPongedAt = Carbon::now()->subDays(1); |
| 56 | + |
| 57 | + $this->channelManager->updateConnectionInChannels($activeConnection); |
| 58 | + $this->channelManager->updateConnectionInChannels($obsoleteConnection); |
| 59 | + |
| 60 | + $this->channelManager |
| 61 | + ->getGlobalConnectionsCount('1234', 'private-channel') |
| 62 | + ->then(function ($count) { |
| 63 | + $this->assertEquals(2, $count); |
| 64 | + }); |
| 65 | + |
| 66 | + $this->channelManager->removeObsoleteConnections(); |
| 67 | + |
| 68 | + $this->channelManager |
| 69 | + ->getGlobalConnectionsCount('1234', 'private-channel') |
| 70 | + ->then(function ($count) { |
| 71 | + $this->assertEquals(1, $count); |
| 72 | + }); |
| 73 | + |
| 74 | + $this->channelManager |
| 75 | + ->getLocalConnections() |
| 76 | + ->then(function ($connections) use ($activeConnection) { |
| 77 | + $connection = $connections[$activeConnection->socketId]; |
| 78 | + |
| 79 | + $this->assertEquals($activeConnection->socketId, $connection->socketId); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_not_ponged_connections_do_get_removed_on_local_for_presence_channels() |
| 84 | + { |
| 85 | + $this->runOnlyOnLocalReplication(); |
| 86 | + |
| 87 | + $activeConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 1]); |
| 88 | + $obsoleteConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 2]); |
| 89 | + |
| 90 | + // The active connection just pinged, it should not be closed. |
| 91 | + $activeConnection->lastPongedAt = Carbon::now(); |
| 92 | + $obsoleteConnection->lastPongedAt = Carbon::now()->subDays(1); |
| 93 | + |
| 94 | + $this->channelManager->updateConnectionInChannels($activeConnection); |
| 95 | + $this->channelManager->updateConnectionInChannels($obsoleteConnection); |
| 96 | + |
| 97 | + $this->channelManager |
| 98 | + ->getGlobalConnectionsCount('1234', 'presence-channel') |
| 99 | + ->then(function ($count) { |
| 100 | + $this->assertEquals(2, $count); |
| 101 | + }); |
| 102 | + |
| 103 | + $this->channelManager |
| 104 | + ->getChannelMembers('1234', 'presence-channel') |
| 105 | + ->then(function ($members) { |
| 106 | + $this->assertCount(2, $members); |
| 107 | + }); |
| 108 | + |
| 109 | + $this->channelManager->removeObsoleteConnections(); |
| 110 | + |
| 111 | + $this->channelManager |
| 112 | + ->getGlobalConnectionsCount('1234', 'presence-channel') |
| 113 | + ->then(function ($count) { |
| 114 | + $this->assertEquals(1, $count); |
| 115 | + }); |
| 116 | + |
| 117 | + $this->channelManager |
| 118 | + ->getLocalConnections() |
| 119 | + ->then(function ($connections) use ($activeConnection) { |
| 120 | + $connection = $connections[$activeConnection->socketId]; |
| 121 | + |
| 122 | + $this->assertEquals($activeConnection->socketId, $connection->socketId); |
| 123 | + }); |
| 124 | + |
| 125 | + $this->channelManager |
| 126 | + ->getChannelMembers('1234', 'presence-channel') |
| 127 | + ->then(function ($members) { |
| 128 | + $this->assertCount(1, $members); |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
0 commit comments