下面列出了com.google.common.util.concurrent.Callables#returning ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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());
}
@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);
}
@Test
public void testConstructor_nullStalenessCheck() {
try {
new PluggableJob<>("name", Callables.returning((Void) null), null);
fail("Expected NPE");
} catch (NullPointerException ex) {
}
}
@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());
}
@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());
}
@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());
}
@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());
}
@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]);
}
@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());
}
@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());
}
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");
}
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");
}
@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
}
}
@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);
}
@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());
}
public void setInputFiles(@Nullable List<File> inputFiles) {
this.inputFiles = Callables.returning(inputFiles);
}
public void setInputDir(@NonNull File inputDir) {
this.inputDir = Callables.returning(inputDir);
}
public void setJavaResourcesInputDir(@NonNull File javaResourcesInputDir) {
this.javaResourcesInputDir = Callables.returning(javaResourcesInputDir);
}
public void setInputLibraries(@NonNull List<File> inputLibraries) {
this.inputLibraries = Callables.returning(inputLibraries);
}