|
4 | 4 | import io.socket.util.Optional;
|
5 | 5 | import org.json.JSONException;
|
6 | 6 | import org.json.JSONObject;
|
| 7 | +import org.junit.Assert; |
7 | 8 | import org.junit.Test;
|
8 | 9 | import org.junit.runner.RunWith;
|
9 | 10 | import org.junit.runners.JUnit4;
|
10 | 11 |
|
11 | 12 | import java.util.Timer;
|
12 | 13 | import java.util.TimerTask;
|
13 |
| -import java.util.concurrent.BlockingQueue; |
14 |
| -import java.util.concurrent.LinkedBlockingQueue; |
| 14 | +import java.util.concurrent.*; |
15 | 15 |
|
16 | 16 | import static java.util.Collections.singletonMap;
|
17 |
| -import static org.hamcrest.CoreMatchers.is; |
18 |
| -import static org.hamcrest.CoreMatchers.not; |
| 17 | +import static org.hamcrest.CoreMatchers.*; |
19 | 18 | import static org.junit.Assert.assertThat;
|
20 | 19 | import static org.junit.Assert.fail;
|
21 | 20 |
|
@@ -287,4 +286,113 @@ public void call(Object... args) {
|
287 | 286 | assertThat(values.take(), is("first"));
|
288 | 287 | assertThat(values.take(), is("second"));
|
289 | 288 | }
|
| 289 | + |
| 290 | + @Test(timeout = TIMEOUT) |
| 291 | + public void shouldTimeoutAfterTheGivenDelayWhenSocketIsNotConnected() throws InterruptedException { |
| 292 | + final BlockingQueue<Boolean> values = new LinkedBlockingQueue<>(); |
| 293 | + |
| 294 | + socket = client(); |
| 295 | + |
| 296 | + socket.emit("event", new AckWithTimeout(50) { |
| 297 | + @Override |
| 298 | + public void onSuccess(Object... args) { |
| 299 | + fail(); |
| 300 | + } |
| 301 | + |
| 302 | + @Override |
| 303 | + public void onTimeout() { |
| 304 | + values.offer(true); |
| 305 | + } |
| 306 | + }); |
| 307 | + |
| 308 | + assertThat(values.take(), is(true)); |
| 309 | + } |
| 310 | + |
| 311 | + @Test(timeout = TIMEOUT) |
| 312 | + public void shouldTimeoutWhenTheServerDoesNotAcknowledgeTheEvent() throws InterruptedException { |
| 313 | + final BlockingQueue<Boolean> values = new LinkedBlockingQueue<>(); |
| 314 | + |
| 315 | + socket = client(); |
| 316 | + |
| 317 | + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { |
| 318 | + @Override |
| 319 | + public void call(Object... args) { |
| 320 | + socket.emit("unknown", new AckWithTimeout(50) { |
| 321 | + @Override |
| 322 | + public void onTimeout() { |
| 323 | + values.offer(true); |
| 324 | + } |
| 325 | + |
| 326 | + @Override |
| 327 | + public void onSuccess(Object... args) { |
| 328 | + fail(); |
| 329 | + } |
| 330 | + }); |
| 331 | + } |
| 332 | + }); |
| 333 | + |
| 334 | + socket.connect(); |
| 335 | + |
| 336 | + assertThat(values.take(), is(true)); |
| 337 | + } |
| 338 | + |
| 339 | + @Test(timeout = TIMEOUT) |
| 340 | + public void shouldTimeoutWhenTheServerDoesNotAcknowledgeTheEventInTime() throws InterruptedException { |
| 341 | + final BlockingQueue<Boolean> values = new LinkedBlockingQueue<>(); |
| 342 | + |
| 343 | + socket = client(); |
| 344 | + |
| 345 | + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { |
| 346 | + @Override |
| 347 | + public void call(Object... args) { |
| 348 | + socket.emit("ack", new AckWithTimeout(0) { |
| 349 | + @Override |
| 350 | + public void onTimeout() { |
| 351 | + values.offer(true); |
| 352 | + } |
| 353 | + |
| 354 | + @Override |
| 355 | + public void onSuccess(Object... args) { |
| 356 | + fail(); |
| 357 | + } |
| 358 | + }); |
| 359 | + } |
| 360 | + }); |
| 361 | + |
| 362 | + socket.connect(); |
| 363 | + |
| 364 | + assertThat(values.take(), is(true)); |
| 365 | + } |
| 366 | + |
| 367 | + @Test(timeout = TIMEOUT) |
| 368 | + public void shouldNotTimeoutWhenTheServerDoesAcknowledgeTheEvent() throws InterruptedException { |
| 369 | + final BlockingQueue<Object> values = new LinkedBlockingQueue<>(); |
| 370 | + |
| 371 | + socket = client(); |
| 372 | + |
| 373 | + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { |
| 374 | + @Override |
| 375 | + public void call(Object... args) { |
| 376 | + socket.emit("ack", 1, "2", new byte[] { 3 }, new AckWithTimeout(200) { |
| 377 | + @Override |
| 378 | + public void onTimeout() { |
| 379 | + fail(); |
| 380 | + } |
| 381 | + |
| 382 | + @Override |
| 383 | + public void onSuccess(Object... args) { |
| 384 | + for (Object arg : args) { |
| 385 | + values.offer(arg); |
| 386 | + } |
| 387 | + } |
| 388 | + }); |
| 389 | + } |
| 390 | + }); |
| 391 | + |
| 392 | + socket.connect(); |
| 393 | + |
| 394 | + assertThat((Integer) values.take(), is(1)); |
| 395 | + assertThat((String) values.take(), is("2")); |
| 396 | + assertThat((byte[]) values.take(), is(new byte[] { 3 })); |
| 397 | + } |
290 | 398 | }
|
0 commit comments