|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertFalse; |
21 | 21 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
22 | 22 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; |
23 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 25 |
|
25 | 26 | import java.time.Duration; |
@@ -372,6 +373,70 @@ void runtimeCloseClosesPeers() throws Exception { |
372 | 373 | assertThrows(PeerClosedException.class, this::pair); |
373 | 374 | } |
374 | 375 |
|
| 376 | + @Test |
| 377 | + void openAfterShutdownFails() { |
| 378 | + // A createPeer that passed its check just before the runtime closed: |
| 379 | + // the runtime's threads are gone and drop the task that would create |
| 380 | + // the connection, so opening has to fail rather than wait forever. |
| 381 | + PeerImpl peer = new PeerImpl(rtc, manualOptions(), "late"); |
| 382 | + |
| 383 | + rtc.close(); |
| 384 | + |
| 385 | + assertThrows(PeerClosedException.class, () -> assertTimeoutPreemptively( |
| 386 | + Duration.ofSeconds(TIMEOUT_SECONDS), peer::open)); |
| 387 | + } |
| 388 | + |
| 389 | + @Test |
| 390 | + void openAfterCloseFails() throws Exception { |
| 391 | + // The closing runtime found the peer before its connection was created. |
| 392 | + // It may free the factory once the peer is closed, so the connection |
| 393 | + // must not be created after that. |
| 394 | + PeerImpl peer = new PeerImpl(rtc, manualOptions(), "closed-first"); |
| 395 | + |
| 396 | + await(peer.closeInternal()); |
| 397 | + |
| 398 | + assertThrows(PeerClosedException.class, peer::open); |
| 399 | + } |
| 400 | + |
| 401 | + @Test |
| 402 | + void createPeerRacingClose() throws Exception { |
| 403 | + // A peer created while its runtime closes is either refused or closed |
| 404 | + // with the runtime, and creating it never waits forever. |
| 405 | + for (int round = 0; round < 50; round++) { |
| 406 | + WebRtc runtime = WebRtc.builder() |
| 407 | + .audio(AudioMode.HEADLESS) |
| 408 | + .errorHandler(errors::add) |
| 409 | + .build(); |
| 410 | + CountDownLatch started = new CountDownLatch(1); |
| 411 | + |
| 412 | + CompletableFuture<List<Peer>> created = CompletableFuture.supplyAsync(() -> { |
| 413 | + List<Peer> peers = new ArrayList<>(); |
| 414 | + |
| 415 | + try { |
| 416 | + while (true) { |
| 417 | + peers.add(runtime.createPeer(manualOptions())); |
| 418 | + started.countDown(); |
| 419 | + } |
| 420 | + } |
| 421 | + catch (PeerClosedException e) { |
| 422 | + return peers; |
| 423 | + } |
| 424 | + }); |
| 425 | + |
| 426 | + // Closed while peers are being created, not before the first. |
| 427 | + started.await(); |
| 428 | + runtime.closeAsync().get(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 429 | + |
| 430 | + for (Peer peer : created.get(TIMEOUT_SECONDS, TimeUnit.SECONDS)) { |
| 431 | + assertEquals(PeerState.CLOSED, peer.state()); |
| 432 | + } |
| 433 | + } |
| 434 | + } |
| 435 | + |
| 436 | + private static PeerOptions manualOptions() { |
| 437 | + return PeerOptions.builder().negotiation(Negotiation.MANUAL).build(); |
| 438 | + } |
| 439 | + |
375 | 440 | @Test |
376 | 441 | void statsAfterConnecting() throws Exception { |
377 | 442 | Peer[] peers = pair(); |
|
0 commit comments