コード例 #1
0
 /** {@inheritDoc} */
 @Override
 public void expire() {
   try {
     eventDispatcher.shutdown();
   } finally {
     super.expire();
   }
 }
コード例 #2
0
  @Test
  public void get_shouldJoinTheCreatedThread_givenAFutureReturnedByExecute() throws Exception {
    long startTime = System.nanoTime();
    Future<?> result = asyncExecutor.execute(suppressCheckedExceptions(() -> Thread.sleep(250L)));
    result.get();
    long endTime = System.nanoTime();

    assertThat(endTime - startTime, isGreaterThan(100 * 1000000L));
  }
コード例 #3
0
  @Test
  public void isDone_shouldReturnFalse_whenTheThreadIsFinished_givenAFutureReturnedByExecute()
      throws Exception {
    Future<?> result = asyncExecutor.execute(() -> {});

    Thread.sleep(50L); // give other thread a chance to finish...

    assertTrue(result.isDone());
  }
コード例 #4
0
 @Test
 public void get_shouldSucceed_whenTheThreadThrowsAnException_givenAFutureReturnedByExecute()
     throws Exception {
   Future<?> result =
       asyncExecutor.execute(
           () -> {
             throw new RuntimeException("blah");
           });
   result.get(100, TimeUnit.MILLISECONDS);
 }
コード例 #5
0
 /**
  * Submits {@link #notifySctpConnectionReadyInEventDispatcher()} to {@link #eventDispatcher} for
  * asynchronous execution.
  */
 private void notifySctpConnectionReady() {
   if (!isExpired()) {
     eventDispatcher.execute(
         new Runnable() {
           @Override
           public void run() {
             notifySctpConnectionReadyInEventDispatcher();
           }
         });
   }
 }
コード例 #6
0
 /**
  * Submits {@link #notifyChannelOpenedInEventDispatcher(WebRtcDataStream)} to {@link
  * #eventDispatcher} for asynchronous execution.
  *
  * @param dataChannel
  */
 private void notifyChannelOpened(final WebRtcDataStream dataChannel) {
   if (!isExpired()) {
     eventDispatcher.execute(
         new Runnable() {
           @Override
           public void run() {
             notifyChannelOpenedInEventDispatcher(dataChannel);
           }
         });
   }
 }
コード例 #7
0
  @Test
  public void cancel_shouldThrowAnUnsupportedOperationException_givenAFutureReturnedByExecute()
      throws Exception {
    Future<?> result = asyncExecutor.execute(() -> {});

    try {
      result.cancel(true);
      fail("Expected an exception");
    } catch (UnsupportedOperationException ignore) {
    }
  }
コード例 #8
0
  @Test
  public void
      getWithATimeout_shouldThrowATimeoutException_whenTheThreadDoesNotEndQuickly_givenAFutureReturnedByExecute()
          throws Exception {
    Future<?> result = asyncExecutor.execute(suppressCheckedExceptions(() -> Thread.sleep(250L)));

    try {
      result.get(50, TimeUnit.MILLISECONDS);
      fail("Expected an exception");
    } catch (TimeoutException ignore) {
    }
  }
コード例 #9
0
  @Test
  public void isDone_shouldReturnTrue_whenTheThreadIsAlive_givenAFutureReturnedByExecute()
      throws Exception {
    CountDownLatch latch = new CountDownLatch(1);

    Future<?> result = asyncExecutor.execute(suppressCheckedExceptions(latch::await));

    try {
      assertFalse(result.isDone());
    } finally {
      latch.countDown(); // don't want a thread waiting forever...
    }
  }
コード例 #10
0
  @Test
  public void execute_shouldCreateAndStartAThreadWithTheProvidedRunnableAndReturnAFuture()
      throws Exception {
    CountDownLatch latch = new CountDownLatch(1);

    Future<?> result = asyncExecutor.execute(latch::countDown);
    latch.await(5, TimeUnit.SECONDS); // give the new thread time to start to running

    verify(factory).newThread(any()); // The provided runnable will be wrapped, not passed directly
    assertEquals(0, latch.getCount()); // but the runnable will be executed
    assertNotNull(result);
    assertNotNull(createdThread);
    assertNotEquals(Thread.State.NEW, createdThread.getState());
  }
コード例 #11
0
 @Test
 public void getWithATimeout_shouldSucceed_whenTheThreadEndsQuickly_givenAFutureReturnedByExecute()
     throws Exception {
   Future<?> result = asyncExecutor.execute(suppressCheckedExceptions(() -> Thread.sleep(50L)));
   result.get(100, TimeUnit.MILLISECONDS);
 }
コード例 #12
0
  @Test
  public void isCancelled_shouldReturnFalse_givenAFutureReturnedByExecute() throws Exception {
    Future<?> result = asyncExecutor.execute(() -> {});

    assertFalse(result.isCancelled());
  }