Java Code Examples for com.google.common.util.concurrent.Callables#returning()

The following examples show how to use com.google.common.util.concurrent.Callables#returning() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleThreadedSchedulerTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetResultOfQueuedTaskBeforeItExecutes() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    em.submit(MutableMap.of("tag", "category1"), newLatchAwaiter(latch));
    
    BasicTask<Integer> t = new BasicTask<Integer>(Callables.returning(123));
    Future<Integer> future = em.submit(MutableMap.of("tag", "category1"), t);

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            latch.countDown();
        }});
    thread.start();
    assertEquals(future.get(), (Integer)123);
}
 
Example 2
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaleFiresFutureListener() throws InterruptedException {
  Object obj = new Object();
  final PluggableJob<Object> job =
      new PluggableJob<>("name", Callables.returning(obj), unused -> true);
  assertFalse(job.getFuture().isDone());
  final boolean[] listenerRun = new boolean[] {false};
  job.getFuture().addListener(() -> {
    listenerRun[0] = true;
    assertTrue(job.getFuture().isCancelled());
  }, MoreExecutors.directExecutor());
  assertFalse(listenerRun[0]);
  job.schedule();
  job.join();
  assertTrue(listenerRun[0]);
  assertEquals("Should be CANCEL", IStatus.CANCEL, job.getResult().getSeverity());
}
 
Example 3
Source File: PackageSplitAbi.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
@NonNull
public synchronized ImmutableList<ApkOutputFile> getOutputSplitFiles() {

    if (outputFiles == null) {
        ImmutableList.Builder<ApkOutputFile> builder = ImmutableList.builder();
        for (String split : splits) {
            String apkName = getApkName(split);
            ApkOutputFile apkOutput = new ApkOutputFile(
                    OutputFile.OutputType.SPLIT,
                    ImmutableList.of(FilterDataImpl.build(OutputFile.ABI, apkName)),
                    Callables.returning(new File(outputDirectory, apkName)));
            builder.add(apkOutput);
        }

        outputFiles = builder.build();
    }
    return outputFiles;
}
 
Example 4
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnSuccess_normal() throws InterruptedException {
  Object obj = new Object();
  PluggableJob<Object> job = new PluggableJob<>("name", Callables.returning(obj));
  final boolean[] listenerRun = new boolean[] {false};
  job.onSuccess(MoreExecutors.directExecutor(), () -> listenerRun[0] = true);
  assertFalse(listenerRun[0]);
  job.schedule();
  job.join();
  assertTrue(listenerRun[0]);
  assertTrue(job.isComputationComplete());
}
 
Example 5
Source File: BasicTaskExecutionTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void cancelAfterRun() throws Exception {
    BasicTask<Integer> t = new BasicTask<Integer>(Callables.returning(42));
    em.submit(MutableMap.of("tag", "A"), t);

    assertEquals(t.get(), (Integer)42);
    t.cancel(true);
    assertFalse(t.isCancelled());
    assertFalse(t.isError());
    assertTrue(t.isDone());
}
 
Example 6
Source File: SingleThreadedSchedulerTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetResultOfQueuedTaskAfterItExecutes() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    em.submit(MutableMap.of("tag", "category1"), newLatchAwaiter(latch));
    
    BasicTask<Integer> t = new BasicTask<Integer>(Callables.returning(123));
    Future<Integer> future = em.submit(MutableMap.of("tag", "category1"), t);

    latch.countDown();
    assertEquals(future.get(), (Integer)123);
}
 
Example 7
Source File: SingleThreadedSchedulerTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetResultOfQueuedTaskBeforeItExecutesWithTimeout() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    em.submit(MutableMap.of("tag", "category1"), newLatchAwaiter(latch));
    
    BasicTask<Integer> t = new BasicTask<Integer>(Callables.returning(123));
    Future<Integer> future = em.submit(MutableMap.of("tag", "category1"), t);

    try {
        assertEquals(future.get(10, TimeUnit.MILLISECONDS), (Integer)123);
        fail();
    } catch (TimeoutException e) {
        // success
    }
}
 
Example 8
Source File: ValueResolverTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public void testTaskFactoryGetImmediately() {
    TaskFactory<TaskAdaptable<String>> taskFactory = new TaskFactory<TaskAdaptable<String>>() {
        @Override public TaskAdaptable<String> newTask() {
            return new BasicTask<>(Callables.returning("myval"));
        }
    };
    String result = Tasks.resolving(taskFactory).as(String.class).context(app).immediately(true).get();
    assertEquals(result, "myval");
}
 
Example 9
Source File: ValueResolverTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public void testTaskFactoryGet() {
    TaskFactory<TaskAdaptable<String>> taskFactory = new TaskFactory<TaskAdaptable<String>>() {
        @Override public TaskAdaptable<String> newTask() {
            return new BasicTask<>(Callables.returning("myval"));
        }
    };
    String result = Tasks.resolving(taskFactory).as(String.class).context(app).get();
    assertEquals(result, "myval");
}
 
Example 10
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsCurrent_stale() throws InterruptedException {
  Object obj = new Object();
  final boolean[] isStale = new boolean[] { false };
  PluggableJob<Object> job = new PluggableJob<>("name", Callables.returning(obj),
      unused -> isStale[0]);
  assertTrue(job.isCurrent());
  job.schedule(); // should self-cancel
  job.join();
  assertTrue(job.isCurrent());
  isStale[0] = true;
  // should now be stale 
  assertFalse("Stale jobs should not be current", job.isCurrent());
}
 
Example 11
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsCurrent_abandon() throws InterruptedException {
  Object obj = new Object();
  PluggableJob<Object> job = new PluggableJob<>("name", Callables.returning(obj));
  assertTrue(job.isCurrent());
  job.schedule(); // should be stale and cancelled
  job.join();
  assertTrue(job.isCurrent());
  job.abandon();
  assertFalse("Abandoned jobs should not be current", job.isCurrent());
}
 
Example 12
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnSuccess_abandon() throws InterruptedException {
  Object obj = new Object();
  PluggableJob<Object> job =
      new PluggableJob<>("name", Callables.returning(obj), unused -> true);
  final boolean[] listenerRun = new boolean[] {false};
  job.onSuccess(MoreExecutors.directExecutor(), () -> listenerRun[0] = true);
  assertFalse(listenerRun[0]);
  job.schedule(); // should be stale and cancelled
  job.join();
  assertFalse("onSuccess should not have been called", listenerRun[0]);
}
 
Example 13
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompleteness_normal() throws InterruptedException {
  Object obj = new Object();
  PluggableJob<Object> job = new PluggableJob<>("name", Callables.returning(obj));
  assertFalse(job.isComputationComplete());
  job.schedule();
  job.join();
  assertTrue(job.isComputationComplete());
  assertFalse(job.getComputationError().isPresent());
  assertTrue(job.getComputationResult().isPresent());
  assertEquals(obj, job.getComputationResult().get());
  assertEquals(obj, job.getComputation().get());
}
 
Example 14
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaleCancelsFuture() throws InterruptedException {
  Object obj = new Object();
  PluggableJob<Object> job = new PluggableJob<>("name", Callables.returning(obj), unused -> true);
  job.schedule();
  job.join();
  assertEquals("Should be CANCEL", IStatus.CANCEL, job.getResult().getSeverity());
  assertTrue(job.getFuture().isCancelled());
}
 
Example 15
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testScheduled() throws InterruptedException, ExecutionException {
  Object obj = new Object();
  PluggableJob<Object> job = new PluggableJob<>("name", Callables.returning(obj));
  assertFalse(job.getFuture().isDone());
  assertFalse(job.getFuture().isCancelled());
  job.schedule();
  job.join();
  assertTrue(job.getFuture().isDone());
  assertFalse(job.getFuture().isCancelled());
  assertSame(obj, job.getFuture().get());
  assertEquals("Should be OK", IStatus.OK, job.getResult().getSeverity());
}
 
Example 16
Source File: PluggableJobTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructor_nullStalenessCheck() {
  try {
    new PluggableJob<>("name", Callables.returning((Void) null), null);
    fail("Expected NPE");
  } catch (NullPointerException ex) {
  }
}
 
Example 17
Source File: PostCompilationData.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void setInputLibraries(@NonNull List<File> inputLibraries) {
    this.inputLibraries = Callables.returning(inputLibraries);
}
 
Example 18
Source File: PostCompilationData.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void setJavaResourcesInputDir(@NonNull File javaResourcesInputDir) {
    this.javaResourcesInputDir = Callables.returning(javaResourcesInputDir);
}
 
Example 19
Source File: PostCompilationData.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void setInputDir(@NonNull File inputDir) {
    this.inputDir = Callables.returning(inputDir);
}
 
Example 20
Source File: PostCompilationData.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void setInputFiles(@Nullable List<File> inputFiles) {
    this.inputFiles = Callables.returning(inputFiles);
}