예제 #1
0
 /** Test the method isDone() */
 @Test
 public void testIsDoneMethod() throws Exception {
   Callable<String> task = new BasicTestTask();
   IExecutorService executor = createSingleNodeExecutorService("isDoneMethod");
   Future future = executor.submit(task);
   if (future.isDone()) {
     assertTrue(future.isDone());
   }
   assertEquals(future.get(), BasicTestTask.RESULT);
   assertTrue(future.isDone());
 }
예제 #2
0
 /** Test for the issue 129. Repeatedly runs tasks and check for isDone() status after get(). */
 @Test
 public void testIsDoneMethod2() throws Exception {
   ExecutorService executor = createSingleNodeExecutorService("isDoneMethod2");
   for (int i = 0; i < COUNT; i++) {
     Callable<String> task1 = new BasicTestTask();
     Callable<String> task2 = new BasicTestTask();
     Future future1 = executor.submit(task1);
     Future future2 = executor.submit(task2);
     assertEquals(future2.get(), BasicTestTask.RESULT);
     assertTrue(future2.isDone());
     assertEquals(future1.get(), BasicTestTask.RESULT);
     assertTrue(future1.isDone());
   }
 }
 public static <V> boolean areAllTrue(List<Future<V>> results) {
   for (Future<V> result : results) {
     if (!result.isDone()) {
       return false;
     }
   }
   return true;
 }
예제 #4
0
 /** Test multiple Future.get() invocation */
 @Test
 public void testMultipleFutureGets() throws Exception {
   Callable<String> task = new BasicTestTask();
   ExecutorService executor = createSingleNodeExecutorService("isTwoGetFromFuture");
   Future<String> future = executor.submit(task);
   String s1 = future.get();
   assertEquals(s1, BasicTestTask.RESULT);
   assertTrue(future.isDone());
   String s2 = future.get();
   assertEquals(s2, BasicTestTask.RESULT);
   assertTrue(future.isDone());
   String s3 = future.get();
   assertEquals(s3, BasicTestTask.RESULT);
   assertTrue(future.isDone());
   String s4 = future.get();
   assertEquals(s4, BasicTestTask.RESULT);
   assertTrue(future.isDone());
 }
예제 #5
0
  @Test
  public void testCancellationAwareTask2() {
    Callable task1 = new CancellationAwareTask(5000);
    ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1);
    executor.submit(task1);

    Callable task2 = new BasicTestTask();
    Future future = executor.submit(task2);
    assertFalse(future.isDone());
    assertTrue(future.cancel(true));
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());

    try {
      future.get();
      fail("Should not complete the task successfully");
    } catch (CancellationException expected) {
    } catch (Exception e) {
      fail("Unexpected exception " + e);
    }
  }
예제 #6
0
  @Test
  public void testCancellationAwareTask() throws ExecutionException, InterruptedException {
    CancellationAwareTask task = new CancellationAwareTask(5000);
    ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask");
    Future future = executor.submit(task);
    try {
      future.get(2, TimeUnit.SECONDS);
      fail("Should throw TimeoutException!");
    } catch (TimeoutException expected) {
    }
    assertFalse(future.isDone());
    assertTrue(future.cancel(true));
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());

    try {
      future.get();
      fail("Should not complete the task successfully");
    } catch (CancellationException expected) {
    } catch (Exception e) {
      fail("Unexpected exception " + e);
    }
  }
예제 #7
0
 public static void main(String[] args) {
   ActiveObjectDemo d1 = new ActiveObjectDemo();
   // Prevents ConcurrentModificationException:
   List<Future<?>> results = new CopyOnWriteArrayList<Future<?>>();
   for (float f = 0.0f; f < 1.0f; f += 0.2f) results.add(d1.calculateFloat(f, f));
   for (int i = 0; i < 5; i++) results.add(d1.calculateInt(i, i));
   print("All asynch calls made");
   while (results.size() > 0) {
     for (Future<?> f : results)
       if (f.isDone()) {
         try {
           print(f.get());
         } catch (Exception e) {
           throw new RuntimeException(e);
         }
         results.remove(f);
       }
   }
   d1.shutdown();
 }
  public void testAwaitTerminationMakesSureTasksTransferredToBackendExecutorAreFinished()
      throws InterruptedException, ExecutionException {
    final AppScheduledExecutorService service = new AppScheduledExecutorService(getName());
    final List<LogInfo> log = Collections.synchronizedList(new ArrayList<>());

    int N = 20;
    int delay = 500;
    List<? extends Future<?>> futures =
        ContainerUtil.map(
            Collections.nCopies(N, ""),
            s ->
                service.schedule(
                    () -> {
                      TimeoutUtil.sleep(5000);
                      log.add(new LogInfo(0));
                    },
                    delay,
                    TimeUnit.MILLISECONDS));
    TimeoutUtil.sleep(delay);
    long start = System.currentTimeMillis();
    while (!service.delayQueue.isEmpty() && System.currentTimeMillis() < start + 20000) {
      // wait till all tasks transferred to backend
    }
    List<SchedulingWrapper.MyScheduledFutureTask> queuedTasks = new ArrayList<>(service.delayQueue);
    if (!queuedTasks.isEmpty()) {
      String s =
          queuedTasks
              .stream()
              .map(BoundedTaskExecutor::info)
              .collect(Collectors.toList())
              .toString();
      fail("Queued tasks left: " + s + ";\n" + queuedTasks);
    }
    service.shutdownAppScheduledExecutorService();
    assertTrue(service.awaitTermination(20, TimeUnit.SECONDS));

    for (Future<?> future : futures) {
      assertTrue(future.isDone());
    }
    assertEquals(log.toString(), N, log.size());
  }
  public void testDelayedWorks() throws InterruptedException {
    final AppScheduledExecutorService service = new AppScheduledExecutorService(getName());
    final List<LogInfo> log = Collections.synchronizedList(new ArrayList<>());
    assertFalse(service.isShutdown());
    assertFalse(service.isTerminated());

    service.invokeAll(
        Collections.nCopies(
            service.getBackendPoolCorePoolSize() + 1,
            Executors.callable(EmptyRunnable.getInstance()))); // pre-start all threads

    int delay = 1000;
    long start = System.currentTimeMillis();
    ScheduledFuture<?> f1 =
        service.schedule(
            () -> {
              log.add(new LogInfo(1));
              TimeoutUtil.sleep(10);
            },
            delay,
            TimeUnit.MILLISECONDS);
    assertFalse(service.isShutdown());
    assertFalse(service.isTerminated());
    ScheduledFuture<?> f2 =
        service.schedule(
            () -> {
              log.add(new LogInfo(2));
              TimeoutUtil.sleep(10);
            },
            delay,
            TimeUnit.MILLISECONDS);
    assertFalse(service.isShutdown());
    assertFalse(service.isTerminated());
    ScheduledFuture<?> f3 =
        service.schedule(
            () -> {
              log.add(new LogInfo(3));
              TimeoutUtil.sleep(10);
            },
            delay,
            TimeUnit.MILLISECONDS);

    assertFalse(service.isShutdown());
    assertFalse(service.isTerminated());
    Future<?> f4 = service.submit((Runnable) () -> log.add(new LogInfo(4)));

    assertFalse(service.isShutdown());
    assertFalse(service.isTerminated());
    assertFalse(f1.isDone());
    assertFalse(f2.isDone());
    assertFalse(f3.isDone());

    TimeoutUtil.sleep(delay / 2);
    long elapsed = System.currentTimeMillis() - start; // can be > delay/2 on overloaded agent
    assertEquals(String.valueOf(f1.isDone()), elapsed > delay, f1.isDone());
    assertEquals(String.valueOf(f2.isDone()), elapsed > delay, f2.isDone());
    assertEquals(String.valueOf(f3.isDone()), elapsed > delay, f3.isDone());
    assertTrue(f4.isDone());

    TimeoutUtil.sleep(delay / 2 + 500);
    assertTrue(f1.isDone());
    assertTrue(f2.isDone());
    assertTrue(f3.isDone());
    assertTrue(f4.isDone());

    assertEquals(4, log.size());
    assertEquals(4, log.get(0).runnable);
    List<Thread> threads =
        Arrays.asList(log.get(1).currentThread, log.get(2).currentThread, log.get(3).currentThread);
    assertEquals(
        threads.toString(), 3, new HashSet<>(threads).size()); // must be executed in parallel

    service.shutdownAppScheduledExecutorService();
    assertTrue(service.awaitTermination(10, TimeUnit.SECONDS));
  }