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

Commit 7365189

Browse files
committed
Fixed tests
1 parent bb1a030 commit 7365189

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

tests/PresenceChannelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ public function test_presence_channel_broadcast_member_events()
152152

153153
$this->channelManager
154154
->getChannelMembers('1234', 'presence-channel')
155-
->then(function ($members) {
155+
->then(function ($members) use ($rick) {
156156
$this->assertCount(1, $members);
157-
$this->assertEquals(1, $members[0]->user_id);
157+
$this->assertEquals(1, $members[$rick->socketId]->user_id);
158158
});
159159
}
160160

tests/ReplicationTest.php

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace BeyondCode\LaravelWebSockets\Test;
44

5+
use Carbon\Carbon;
6+
57
class ReplicationTest extends TestCase
68
{
79
/**
@@ -61,13 +63,23 @@ public function test_events_get_replicated_across_connections()
6163

6264
public function test_not_ponged_connections_do_get_removed_for_public_channels()
6365
{
64-
$connection = $this->newActiveConnection(['public-channel']);
66+
$activeConnection = $this->newActiveConnection(['public-channel']);
67+
$obsoleteConnection = $this->newActiveConnection(['public-channel']);
68+
69+
// The active connection just pinged, it should not be closed.
70+
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
6571

6672
// Make the connection look like it was lost 1 day ago.
67-
$this->channelManager->addConnectionToSet($connection, now()->subDays(1));
73+
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
6874

6975
$this->channelManager
70-
->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
76+
->getGlobalConnectionsCount('1234', 'public-channel')
77+
->then(function ($count) {
78+
$this->assertEquals(2, $count);
79+
});
80+
81+
$this->channelManager
82+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
7183
->then(function ($expiredConnections) {
7284
$this->assertCount(1, $expiredConnections);
7385
});
@@ -77,25 +89,35 @@ public function test_not_ponged_connections_do_get_removed_for_public_channels()
7789
$this->channelManager
7890
->getGlobalConnectionsCount('1234', 'public-channel')
7991
->then(function ($count) {
80-
$this->assertEquals(0, $count);
92+
$this->assertEquals(1, $count);
8193
});
8294

8395
$this->channelManager
84-
->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
96+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
8597
->then(function ($expiredConnections) {
8698
$this->assertCount(0, $expiredConnections);
8799
});
88100
}
89101

90102
public function test_not_ponged_connections_do_get_removed_for_private_channels()
91103
{
92-
$connection = $this->newPrivateConnection('private-channel');
104+
$activeConnection = $this->newPrivateConnection('private-channel');
105+
$obsoleteConnection = $this->newPrivateConnection('private-channel');
106+
107+
// The active connection just pinged, it should not be closed.
108+
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
93109

94110
// Make the connection look like it was lost 1 day ago.
95-
$this->channelManager->addConnectionToSet($connection, now()->subDays(1));
111+
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
112+
113+
$this->channelManager
114+
->getGlobalConnectionsCount('1234', 'private-channel')
115+
->then(function ($count) {
116+
$this->assertEquals(2, $count);
117+
});
96118

97119
$this->channelManager
98-
->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
120+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
99121
->then(function ($expiredConnections) {
100122
$this->assertCount(1, $expiredConnections);
101123
});
@@ -105,53 +127,63 @@ public function test_not_ponged_connections_do_get_removed_for_private_channels(
105127
$this->channelManager
106128
->getGlobalConnectionsCount('1234', 'private-channel')
107129
->then(function ($count) {
108-
$this->assertEquals(0, $count);
130+
$this->assertEquals(1, $count);
109131
});
110132

111133
$this->channelManager
112-
->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
134+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
113135
->then(function ($expiredConnections) {
114136
$this->assertCount(0, $expiredConnections);
115137
});
116138
}
117139

118140
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
119141
{
120-
$connection = $this->newPresenceConnection('presence-channel');
142+
$activeConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 1]);
143+
$obsoleteConnection = $this->newPresenceConnection('presence-channel', ['user_id' => 2]);
144+
145+
// The active connection just pinged, it should not be closed.
146+
$this->channelManager->addConnectionToSet($activeConnection, Carbon::now());
121147

122148
// Make the connection look like it was lost 1 day ago.
123-
$this->channelManager->addConnectionToSet($connection, now()->subDays(1));
149+
$this->channelManager->addConnectionToSet($obsoleteConnection, Carbon::now()->subDays(1));
124150

125151
$this->channelManager
126-
->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
152+
->getGlobalConnectionsCount('1234', 'presence-channel')
153+
->then(function ($count) {
154+
$this->assertEquals(2, $count);
155+
});
156+
157+
$this->channelManager
158+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
127159
->then(function ($expiredConnections) {
128160
$this->assertCount(1, $expiredConnections);
129161
});
130162

131163
$this->channelManager
132164
->getChannelMembers('1234', 'presence-channel')
133165
->then(function ($members) {
134-
$this->assertCount(1, $members);
166+
$this->assertCount(2, $members);
135167
});
136168

137169
$this->channelManager->removeObsoleteConnections();
138170

139171
$this->channelManager
140-
->getGlobalConnectionsCount('1234', 'private-channel')
172+
->getGlobalConnectionsCount('1234', 'presence-channel')
141173
->then(function ($count) {
142-
$this->assertEquals(0, $count);
174+
$this->assertEquals(1, $count);
143175
});
144176

145177
$this->channelManager
146-
->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
178+
->getConnectionsFromSet(0, Carbon::now()->subMinutes(2)->format('U'))
147179
->then(function ($expiredConnections) {
148180
$this->assertCount(0, $expiredConnections);
149181
});
150182

151183
$this->channelManager
152184
->getChannelMembers('1234', 'presence-channel')
153185
->then(function ($members) {
154-
$this->assertCount(0, $members);
186+
$this->assertCount(1, $members);
155187
});
156188
}
157189
}

tests/TriggerEventTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function test_it_fires_the_event_to_public_channel()
6565

6666
$this->statisticsCollector
6767
->getAppStatistics('1234')
68-
->then(function ($statistics) {
68+
->then(function ($statistic) {
6969
$this->assertEquals([
7070
'peak_connections_count' => 1,
7171
'websocket_messages_count' => 1,
@@ -106,7 +106,7 @@ public function test_it_fires_the_event_to_presence_channel()
106106

107107
$this->statisticsCollector
108108
->getAppStatistics('1234')
109-
->then(function ($statistics) {
109+
->then(function ($statistic) {
110110
$this->assertEquals([
111111
'peak_connections_count' => 1,
112112
'websocket_messages_count' => 1,
@@ -147,7 +147,7 @@ public function test_it_fires_the_event_to_private_channel()
147147

148148
$this->statisticsCollector
149149
->getAppStatistics('1234')
150-
->then(function ($statistics) {
150+
->then(function ($statistic) {
151151
$this->assertEquals([
152152
'peak_connections_count' => 1,
153153
'websocket_messages_count' => 1,

0 commit comments

Comments
 (0)