|
18 | 18 | import java.util.concurrent.CountDownLatch; |
19 | 19 | import java.util.concurrent.TimeUnit; |
20 | 20 |
|
21 | | -import static chat.chatdtos.ChatMessage; |
22 | | -import static chat.chatdtos.PostChatToChannel; |
| 21 | +import static chat.chatdtos.*; |
23 | 22 |
|
24 | 23 | /** |
25 | 24 | * Created by mythz on 2/10/2017. |
@@ -263,4 +262,67 @@ public void test_Does_receive_messages() throws Exception { |
263 | 262 | assertEquals(2, msgs2.size()); |
264 | 263 | } |
265 | 264 | } |
| 265 | + |
| 266 | + public void test_Does_send_multiple_heartbeats() throws Exception { |
| 267 | + final CountDownLatch signal = new CountDownLatch(1); |
| 268 | + |
| 269 | + List<ServerEventMessage> heartbeats = new ArrayList<>(); |
| 270 | + try(ServerEventsClient client1 = new ServerEventsClient("http://chat.servicestack.net") |
| 271 | + .setOnConnect(e -> e.setHeartbeatIntervalMs(1000)) //change to 1s |
| 272 | + .setOnHeartbeat(e -> { |
| 273 | + heartbeats.add(e); |
| 274 | + if (heartbeats.size() >= 3) |
| 275 | + signal.countDown(); |
| 276 | + }) |
| 277 | + .start()) { |
| 278 | + |
| 279 | + assertTrue(signal.await(5, TimeUnit.SECONDS)); |
| 280 | + |
| 281 | + assertTrue(heartbeats.size() >= 3); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + public void test_Does_reconnect_on_lost_connection() throws Exception { |
| 286 | + |
| 287 | + List<ServerEventConnect> connectMsgs = new ArrayList<>(); |
| 288 | + List<ServerEventMessage> msgs1 = new ArrayList<>(); |
| 289 | + try(ServerEventsClient client1 = new ServerEventsClient("http://chat.servicestack.net") |
| 290 | + .setOnConnect(connectMsgs::add) |
| 291 | + .setOnMessage(msgs1::add) |
| 292 | + .start()) { |
| 293 | + |
| 294 | + while (connectMsgs.size() == 0){ |
| 295 | + Thread.sleep(100); |
| 296 | + } |
| 297 | + |
| 298 | + postChat(client1, "msg1 from client1"); |
| 299 | + |
| 300 | + while (msgs1.size() == 0){ |
| 301 | + Thread.sleep(100); |
| 302 | + } |
| 303 | + |
| 304 | + client1.getServiceClient().post(new ResetServerEvents()); |
| 305 | + |
| 306 | + try(ServerEventsClient client2 = new ServerEventsClient("http://chat.servicestack.net") |
| 307 | + .setOnConnect(connectMsgs::add) |
| 308 | + .start()) { |
| 309 | + |
| 310 | + while (connectMsgs.size() < 3){ //client1 + client1 reconnect + client2 |
| 311 | + Thread.sleep(100); |
| 312 | + } |
| 313 | + |
| 314 | + postChat(client2, "msg2 from client2"); |
| 315 | + |
| 316 | + while (msgs1.size() < 2){ //msg1 + msg2 |
| 317 | + Thread.sleep(100); |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + ServerEventMessage msg2 = msgs1.get(1); |
| 322 | + |
| 323 | + ChatMessage chatMsg2 = JsonUtils.fromJson(msg2.getJson(), ChatMessage.class); |
| 324 | + |
| 325 | + assertEquals("msg2 from client2", chatMsg2.getMessage()); |
| 326 | + } |
| 327 | + } |
266 | 328 | } |
0 commit comments