Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit ed41ad5

Browse files
committed
Moved tests across classes
1 parent 14a7944 commit ed41ad5

File tree

5 files changed

+380
-356
lines changed

5 files changed

+380
-356
lines changed

src/ChannelManagers/RedisChannelManager.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -598,17 +598,15 @@ public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $stric
598598
$stop = "({$stop}";
599599
}
600600

601-
return $this->publishClient->zrangebyscore(
602-
$this->getRedisKey(null, null, ['sockets']),
603-
$start, $stop
604-
)
605-
->then(function ($list) {
606-
return collect($list)->mapWithKeys(function ($appWithSocket) {
607-
[$appId, $socketId] = explode(':', $appWithSocket);
608-
609-
return [$socketId => $appId];
610-
})->toArray();
611-
});
601+
return $this->publishClient
602+
->zrangebyscore($this->getRedisKey(null, null, ['sockets']), $start, $stop)
603+
->then(function ($list) {
604+
return collect($list)->mapWithKeys(function ($appWithSocket) {
605+
[$appId, $socketId] = explode(':', $appWithSocket);
606+
607+
return [$socketId => $appId];
608+
})->toArray();
609+
});
612610
}
613611

614612
/**
@@ -621,8 +619,7 @@ public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $stric
621619
public function addChannelToSet($appId, string $channel)
622620
{
623621
return $this->publishClient->sadd(
624-
$this->getRedisKey($appId, null, ['channels']),
625-
$channel
622+
$this->getRedisKey($appId, null, ['channels']), $channel
626623
);
627624
}
628625

@@ -636,8 +633,7 @@ public function addChannelToSet($appId, string $channel)
636633
public function removeChannelFromSet($appId, string $channel)
637634
{
638635
return $this->publishClient->srem(
639-
$this->getRedisKey($appId, null, ['channels']),
640-
$channel
636+
$this->getRedisKey($appId, null, ['channels']), $channel
641637
);
642638
}
643639

@@ -712,8 +708,7 @@ public function unsubscribeFromTopic($appId, string $channel = null)
712708
protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId)
713709
{
714710
$this->publishClient->sadd(
715-
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']),
716-
$socketId
711+
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']), $socketId
717712
);
718713
}
719714

@@ -729,8 +724,7 @@ protected function addUserSocket($appId, string $channel, stdClass $user, string
729724
protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId)
730725
{
731726
$this->publishClient->srem(
732-
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']),
733-
$socketId
727+
$this->getRedisKey($appId, $channel, [$user->user_id, 'userSockets']), $socketId
734728
);
735729
}
736730

tests/PresenceChannelTest.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BeyondCode\LaravelWebSockets\Test;
44

55
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
6+
use Carbon\Carbon;
67
use Ratchet\ConnectionInterface;
78

89
class PresenceChannelTest extends TestCase
@@ -312,4 +313,146 @@ public function test_multiple_clients_with_same_user_id_trigger_member_added_and
312313
$this->assertCount(1, $sockets);
313314
});
314315
}
316+
317+
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
318+
{
319+
$this->runOnlyOnRedisReplication();
320+
321+
$activeConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 1]);
322+
$obsoleteConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 2]);
323+
324+
// The active connection just pinged, it should not be closed.
325+
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
326+
327+
// Make the connection look like it was lost 1 day ago.
328+
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
329+
330+
$this->channelManager
331+
->getGlobalConnectionsCount('1234', 'presence-channel')
332+
->then(function ($count) {
333+
$this->assertEquals(2, $count);
334+
});
335+
336+
$this->channelManager
337+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
338+
->then(function ($expiredConnections) {
339+
$this->assertCount(1, $expiredConnections);
340+
});
341+
342+
$this->channelManager
343+
->getChannelMembers('1234', 'presence-channel')
344+
->then(function ($members) {
345+
$this->assertCount(2, $members);
346+
});
347+
348+
$this->channelManager->removeObsoleteConnections();
349+
350+
$this->channelManager
351+
->getGlobalConnectionsCount('1234', 'presence-channel')
352+
->then(function ($count) {
353+
$this->assertEquals(1, $count);
354+
});
355+
356+
$this->channelManager
357+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
358+
->then(function ($expiredConnections) {
359+
$this->assertCount(0, $expiredConnections);
360+
});
361+
362+
$this->channelManager
363+
->getChannelMembers('1234', 'presence-channel')
364+
->then(function ($members) {
365+
$this->assertCount(1, $members);
366+
});
367+
}
368+
369+
public function test_events_are_processed_by_on_message_on_presence_channels()
370+
{
371+
$this->runOnlyOnRedisReplication();
372+
373+
$user = [
374+
'user_id' => 1,
375+
'user_info' => [
376+
'name' => 'Rick',
377+
],
378+
];
379+
380+
$connection = $this->newPresenceConnection('presence-channel', $user);
381+
382+
$encodedUser = json_encode($user);
383+
384+
$message = new Mocks\SignedMessage([
385+
'appId' => '1234',
386+
'serverId' => 'different_server_id',
387+
'event' => 'some-event',
388+
'data' => [
389+
'channel' => 'presence-channel',
390+
'channel_data' => $encodedUser,
391+
'test' => 'yes',
392+
],
393+
], $connection, 'presence-channel', $encodedUser);
394+
395+
$this->channelManager->onMessage(
396+
$this->channelManager->getRedisKey('1234', 'presence-channel'),
397+
$message->getPayload()
398+
);
399+
400+
// The message does not contain appId and serverId anymore.
401+
$message = new Mocks\SignedMessage([
402+
'event' => 'some-event',
403+
'data' => [
404+
'channel' => 'presence-channel',
405+
'channel_data' => $encodedUser,
406+
'test' => 'yes',
407+
],
408+
], $connection, 'presence-channel', $encodedUser);
409+
410+
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
411+
}
412+
413+
public function test_events_get_replicated_across_connections_for_presence_channels()
414+
{
415+
$this->runOnlyOnRedisReplication();
416+
417+
$connection = $this->newPresenceConnection('presence-channel');
418+
$receiver = $this->newPresenceConnection('presence-channel', ['user_id' => 2]);
419+
420+
$user = [
421+
'user_id' => 1,
422+
'user_info' => [
423+
'name' => 'Rick',
424+
],
425+
];
426+
427+
$encodedUser = json_encode($user);
428+
429+
$message = new Mocks\SignedMessage([
430+
'appId' => '1234',
431+
'serverId' => $this->channelManager->getServerId(),
432+
'event' => 'some-event',
433+
'data' => [
434+
'channel' => 'presence-channel',
435+
'channel_data' => $encodedUser,
436+
'test' => 'yes',
437+
],
438+
'socketId' => $connection->socketId,
439+
], $connection, 'presence-channel', $encodedUser);
440+
441+
$channel = $this->channelManager->find('1234', 'presence-channel');
442+
443+
$channel->broadcastToEveryoneExcept(
444+
$message->getPayloadAsObject(), $connection->socketId, '1234', true
445+
);
446+
447+
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
448+
449+
$this->getSubscribeClient()
450+
->assertNothingDispatched();
451+
452+
$this->getPublishClient()
453+
->assertCalledWithArgs('publish', [
454+
$this->channelManager->getRedisKey('1234', 'presence-channel'),
455+
$message->getPayload(),
456+
]);
457+
}
315458
}

tests/PrivateChannelTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BeyondCode\LaravelWebSockets\Test;
44

55
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
6+
use Carbon\Carbon;
67
use Ratchet\ConnectionInterface;
78

89
class PrivateChannelTest extends TestCase
@@ -153,4 +154,113 @@ public function test_local_connections_for_private_channels()
153154
}
154155
});
155156
}
157+
158+
public function test_not_ponged_connections_do_get_removed_for_private_channels()
159+
{
160+
$this->runOnlyOnRedisReplication();
161+
162+
$activeConnection = $this->newPrivateConnection('private-channel');
163+
$obsoleteConnection = $this->newPrivateConnection('private-channel');
164+
165+
// The active connection just pinged, it should not be closed.
166+
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
167+
168+
// Make the connection look like it was lost 1 day ago.
169+
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
170+
171+
$this->channelManager
172+
->getGlobalConnectionsCount('1234', 'private-channel')
173+
->then(function ($count) {
174+
$this->assertEquals(2, $count);
175+
});
176+
177+
$this->channelManager
178+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
179+
->then(function ($expiredConnections) {
180+
$this->assertCount(1, $expiredConnections);
181+
});
182+
183+
$this->channelManager->removeObsoleteConnections();
184+
185+
$this->channelManager
186+
->getGlobalConnectionsCount('1234', 'private-channel')
187+
->then(function ($count) {
188+
$this->assertEquals(1, $count);
189+
});
190+
191+
$this->channelManager
192+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
193+
->then(function ($expiredConnections) {
194+
$this->assertCount(0, $expiredConnections);
195+
});
196+
}
197+
198+
public function test_events_are_processed_by_on_message_on_private_channels()
199+
{
200+
$this->runOnlyOnRedisReplication();
201+
202+
$connection = $this->newPrivateConnection('private-channel');
203+
204+
$message = new Mocks\SignedMessage([
205+
'appId' => '1234',
206+
'serverId' => 'different_server_id',
207+
'event' => 'some-event',
208+
'data' => [
209+
'channel' => 'private-channel',
210+
'test' => 'yes',
211+
],
212+
], $connection, 'private-channel');
213+
214+
$this->channelManager->onMessage(
215+
$this->channelManager->getRedisKey('1234', 'private-channel'),
216+
$message->getPayload()
217+
);
218+
219+
// The message does not contain appId and serverId anymore.
220+
$message = new Mocks\SignedMessage([
221+
'event' => 'some-event',
222+
'data' => [
223+
'channel' => 'private-channel',
224+
'test' => 'yes',
225+
],
226+
], $connection, 'private-channel');
227+
228+
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
229+
}
230+
231+
public function test_events_get_replicated_across_connections_for_private_channels()
232+
{
233+
$this->runOnlyOnRedisReplication();
234+
235+
$connection = $this->newPrivateConnection('private-channel');
236+
$receiver = $this->newPrivateConnection('private-channel');
237+
238+
$message = new Mocks\SignedMessage([
239+
'appId' => '1234',
240+
'serverId' => $this->channelManager->getServerId(),
241+
'event' => 'some-event',
242+
'data' => [
243+
'channel' => 'private-channel',
244+
'test' => 'yes',
245+
],
246+
'socketId' => $connection->socketId,
247+
], $connection, 'private-channel');
248+
249+
$channel = $this->channelManager->find('1234', 'private-channel');
250+
251+
$channel->broadcastToEveryoneExcept(
252+
$message->getPayloadAsObject(), $connection->socketId, '1234', true
253+
);
254+
255+
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
256+
257+
$this->getSubscribeClient()
258+
->assertNothingDispatched();
259+
260+
$this->getPublishClient()
261+
->assertCalledWithArgs('publish', [
262+
$this->channelManager->getRedisKey('1234', 'private-channel'),
263+
$message->getPayload(),
264+
]);
265+
}
156266
}

0 commit comments

Comments
 (0)