22
33namespace BeyondCode \LaravelWebSockets \Test ;
44
5+ use Carbon \Carbon ;
6+
57class 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}
0 commit comments