use of com.google.common.util.concurrent.SettableFuture in project jackrabbit-oak by apache.
the class UploadStagingCacheTest method testPutMoveFileError.
/**
* Error in putting file to stage.
* @throws Exception
*/
@Test
public void testPutMoveFileError() throws Exception {
File empty = new File(folder.getRoot(), String.valueOf(System.currentTimeMillis()));
assertFalse(empty.exists());
Optional<SettableFuture<Integer>> future = stagingCache.put(ID_PREFIX + 0, empty);
// assert no file
assertFalse(future.isPresent());
assertEquals(1, stagingCache.getStats().getMissCount());
assertCacheStats(stagingCache, 0, 0, 0, 1);
}
use of com.google.common.util.concurrent.SettableFuture in project jackrabbit-oak by apache.
the class UploadStagingCacheTest method testDefaultStatsProvider.
@Test
public void testDefaultStatsProvider() throws Exception {
stagingCache = UploadStagingCache.build(root, null, 1, /*threads*/
8 * 1024, /* bytes */
uploader, null, /*cache*/
null, executor, null, 3000, 6000);
// add load
File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
Optional<SettableFuture<Integer>> future = stagingCache.put(ID_PREFIX + 0, f);
assertTrue(future.isPresent());
assertNotNull(stagingCache.getIfPresent(ID_PREFIX + 0));
assertCacheStats(stagingCache, 1, 4 * 1024, 1, 1);
}
use of com.google.common.util.concurrent.SettableFuture in project controller by opendaylight.
the class ConcurrentDOMDataBrokerTest method testSuccessfulSubmit.
private void testSuccessfulSubmit(final boolean doAsync) throws InterruptedException {
final CountDownLatch asyncCanCommitContinue = new CountDownLatch(1);
Answer<ListenableFuture<Boolean>> asyncCanCommit = invocation -> {
final SettableFuture<Boolean> future = SettableFuture.create();
if (doAsync) {
new Thread(() -> {
Uninterruptibles.awaitUninterruptibly(asyncCanCommitContinue, 10, TimeUnit.SECONDS);
future.set(true);
}).start();
} else {
future.set(true);
}
return future;
};
doAnswer(asyncCanCommit).when(mockCohort1).canCommit();
doReturn(Futures.immediateFuture(null)).when(mockCohort1).preCommit();
doReturn(Futures.immediateFuture(null)).when(mockCohort1).commit();
doReturn(Futures.immediateFuture(true)).when(mockCohort2).canCommit();
doReturn(Futures.immediateFuture(null)).when(mockCohort2).preCommit();
doReturn(Futures.immediateFuture(null)).when(mockCohort2).commit();
ListenableFuture<Void> future = coordinator.submit(transaction, Arrays.asList(mockCohort1, mockCohort2));
final CountDownLatch doneLatch = new CountDownLatch(1);
final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
Futures.addCallback(future, new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
doneLatch.countDown();
}
@Override
public void onFailure(final Throwable failure) {
caughtEx.set(failure);
doneLatch.countDown();
}
}, MoreExecutors.directExecutor());
asyncCanCommitContinue.countDown();
assertEquals("Submit complete", true, doneLatch.await(5, TimeUnit.SECONDS));
if (caughtEx.get() != null) {
Throwables.throwIfUnchecked(caughtEx.get());
throw new RuntimeException(caughtEx.get());
}
assertEquals("Task count", doAsync ? 1 : 0, futureExecutor.getTaskCount());
InOrder inOrder = inOrder(mockCohort1, mockCohort2);
inOrder.verify(mockCohort1).canCommit();
inOrder.verify(mockCohort2).canCommit();
inOrder.verify(mockCohort1).preCommit();
inOrder.verify(mockCohort2).preCommit();
inOrder.verify(mockCohort1).commit();
inOrder.verify(mockCohort2).commit();
}
use of com.google.common.util.concurrent.SettableFuture in project closure-templates by google.
the class DetachStateTest method testDetachOnEachIteration.
@Test
public void testDetachOnEachIteration() throws IOException {
CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param list : list<string>}", "prefix{\\n}", "{for $item in $list}", " loop-prefix{\\n}", " {$item}{\\n}", " loop-suffix{\\n}", "{/for}", "suffix");
CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
RenderContext context = getDefaultContext(templates);
List<SettableFuture<String>> futures = ImmutableList.of(SettableFuture.<String>create(), SettableFuture.<String>create(), SettableFuture.<String>create());
CompiledTemplate template = factory.create(asRecord(ImmutableMap.of("list", futures)), EMPTY_DICT);
BufferingAppendable output = LoggingAdvisingAppendable.buffering();
RenderResult result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(futures.get(0));
assertThat(output.getAndClearBuffer()).isEqualTo("prefix\nloop-prefix\n");
futures.get(0).set("first");
result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(futures.get(1));
assertThat(output.getAndClearBuffer()).isEqualTo("first\nloop-suffix\nloop-prefix\n");
futures.get(1).set("second");
result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(futures.get(2));
assertThat(output.getAndClearBuffer()).isEqualTo("second\nloop-suffix\nloop-prefix\n");
futures.get(2).set("third");
result = template.render(output, context);
assertThat(result).isEqualTo(RenderResult.done());
assertThat(output.toString()).isEqualTo("third\nloop-suffix\nsuffix");
}
use of com.google.common.util.concurrent.SettableFuture in project cdap by caskdata.
the class WorkflowTest method testOneActionWorkflow.
@Test(timeout = 120 * 1000L)
public void testOneActionWorkflow() throws Exception {
final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(OneActionWorkflowApp.class, TEMP_FOLDER_SUPPLIER);
final Injector injector = AppFabricTestHelper.getInjector();
final ProgramDescriptor programDescriptor = Iterators.filter(app.getPrograms().iterator(), input -> input.getProgramId().getType() == ProgramType.WORKFLOW).next();
final SettableFuture<String> completion = SettableFuture.create();
final ProgramController controller = AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER);
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State currentState, @Nullable Throwable cause) {
LOG.info("Initializing");
long nowSecs = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
setStartAndRunning(injector.getInstance(Store.class), controller.getProgramRunId().getParent(), controller.getProgramRunId().getRun(), nowSecs);
}
@Override
public void completed() {
LOG.info("Completed");
completion.set("Completed");
}
@Override
public void error(Throwable cause) {
LOG.info("Error", cause);
completion.setException(cause);
}
}, Threads.SAME_THREAD_EXECUTOR);
String run = completion.get();
Assert.assertEquals("Completed", run);
}
Aggregations