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