2
2
3
3
namespace BeyondCode \LaravelWebSockets \Test ;
4
4
5
+ use Carbon \Carbon ;
6
+
5
7
class ReplicationTest extends TestCase
6
8
{
7
9
/**
@@ -61,13 +63,23 @@ public function test_events_get_replicated_across_connections()
61
63
62
64
public function test_not_ponged_connections_do_get_removed_for_public_channels ()
63
65
{
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 ());
65
71
66
72
// 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 ));
68
74
69
75
$ 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 ' ))
71
83
->then (function ($ expiredConnections ) {
72
84
$ this ->assertCount (1 , $ expiredConnections );
73
85
});
@@ -77,25 +89,35 @@ public function test_not_ponged_connections_do_get_removed_for_public_channels()
77
89
$ this ->channelManager
78
90
->getGlobalConnectionsCount ('1234 ' , 'public-channel ' )
79
91
->then (function ($ count ) {
80
- $ this ->assertEquals (0 , $ count );
92
+ $ this ->assertEquals (1 , $ count );
81
93
});
82
94
83
95
$ this ->channelManager
84
- ->getConnectionsFromSet (0 , now ()->subMinutes (2 )->format ('U ' ))
96
+ ->getConnectionsFromSet (0 , Carbon:: now ()->subMinutes (2 )->format ('U ' ))
85
97
->then (function ($ expiredConnections ) {
86
98
$ this ->assertCount (0 , $ expiredConnections );
87
99
});
88
100
}
89
101
90
102
public function test_not_ponged_connections_do_get_removed_for_private_channels ()
91
103
{
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 ());
93
109
94
110
// 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
+ });
96
118
97
119
$ this ->channelManager
98
- ->getConnectionsFromSet (0 , now ()->subMinutes (2 )->format ('U ' ))
120
+ ->getConnectionsFromSet (0 , Carbon:: now ()->subMinutes (2 )->format ('U ' ))
99
121
->then (function ($ expiredConnections ) {
100
122
$ this ->assertCount (1 , $ expiredConnections );
101
123
});
@@ -105,53 +127,63 @@ public function test_not_ponged_connections_do_get_removed_for_private_channels(
105
127
$ this ->channelManager
106
128
->getGlobalConnectionsCount ('1234 ' , 'private-channel ' )
107
129
->then (function ($ count ) {
108
- $ this ->assertEquals (0 , $ count );
130
+ $ this ->assertEquals (1 , $ count );
109
131
});
110
132
111
133
$ this ->channelManager
112
- ->getConnectionsFromSet (0 , now ()->subMinutes (2 )->format ('U ' ))
134
+ ->getConnectionsFromSet (0 , Carbon:: now ()->subMinutes (2 )->format ('U ' ))
113
135
->then (function ($ expiredConnections ) {
114
136
$ this ->assertCount (0 , $ expiredConnections );
115
137
});
116
138
}
117
139
118
140
public function test_not_ponged_connections_do_get_removed_for_presence_channels ()
119
141
{
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 ());
121
147
122
148
// 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 ));
124
150
125
151
$ 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 ' ))
127
159
->then (function ($ expiredConnections ) {
128
160
$ this ->assertCount (1 , $ expiredConnections );
129
161
});
130
162
131
163
$ this ->channelManager
132
164
->getChannelMembers ('1234 ' , 'presence-channel ' )
133
165
->then (function ($ members ) {
134
- $ this ->assertCount (1 , $ members );
166
+ $ this ->assertCount (2 , $ members );
135
167
});
136
168
137
169
$ this ->channelManager ->removeObsoleteConnections ();
138
170
139
171
$ this ->channelManager
140
- ->getGlobalConnectionsCount ('1234 ' , 'private -channel ' )
172
+ ->getGlobalConnectionsCount ('1234 ' , 'presence -channel ' )
141
173
->then (function ($ count ) {
142
- $ this ->assertEquals (0 , $ count );
174
+ $ this ->assertEquals (1 , $ count );
143
175
});
144
176
145
177
$ this ->channelManager
146
- ->getConnectionsFromSet (0 , now ()->subMinutes (2 )->format ('U ' ))
178
+ ->getConnectionsFromSet (0 , Carbon:: now ()->subMinutes (2 )->format ('U ' ))
147
179
->then (function ($ expiredConnections ) {
148
180
$ this ->assertCount (0 , $ expiredConnections );
149
181
});
150
182
151
183
$ this ->channelManager
152
184
->getChannelMembers ('1234 ' , 'presence-channel ' )
153
185
->then (function ($ members ) {
154
- $ this ->assertCount (0 , $ members );
186
+ $ this ->assertCount (1 , $ members );
155
187
});
156
188
}
157
189
}
0 commit comments