Example usage for java.util.concurrent CompletableFuture get

List of usage examples for java.util.concurrent CompletableFuture get

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture get.

Prototype

@SuppressWarnings("unchecked")
public T get() throws InterruptedException, ExecutionException 

Source Link

Document

Waits if necessary for this future to complete, and then returns its result.

Usage

From source file:org.apache.bookkeeper.stream.storage.impl.TestStorageContainerStoreImpl.java

@Test
public void testGetNamespaceMockRootStorageContainerStore() throws Exception {
    String colName = "test-get-namespace-no-root-storage-container-store";

    GetNamespaceResponse getResp = GetNamespaceResponse.newBuilder().setCode(StatusCode.NAMESPACE_NOT_FOUND)
            .build();//  w  w  w  .ja v  a 2 s  .c  o m
    GetNamespaceRequest request = createGetNamespaceRequest(colName);

    when(mockRangeStoreService.getNamespace(request)).thenReturn(CompletableFuture.completedFuture(getResp));

    CompletableFuture<GetNamespaceResponse> getRespFuture = fromListenableFuture(
            rootRangeService.getNamespace(request));
    verify(mockRangeStoreService, times(1)).getNamespace(request);
    assertTrue(getResp == getRespFuture.get());
}

From source file:org.apache.bookkeeper.stream.storage.impl.TestStorageContainerStoreImpl.java

@Test
public void testCreateStreamMockRootStorageContainerStore() throws Exception {
    String colName = "test-create-namespace-mock-root-storage-container-store";
    String streamName = colName;/*from ww w  . jav a  2  s  . c o m*/

    CreateStreamResponse createResp = CreateStreamResponse.newBuilder().setCode(StatusCode.STREAM_EXISTS)
            .build();
    CreateStreamRequest createReq = createCreateStreamRequest(colName, streamName, DEFAULT_STREAM_CONF);
    when(mockRangeStoreService.createStream(createReq))
            .thenReturn(CompletableFuture.completedFuture(createResp));

    CompletableFuture<CreateStreamResponse> createRespFuture = fromListenableFuture(
            rootRangeService.createStream(createReq));
    verify(mockRangeStoreService, times(1)).createStream(createReq);
    assertTrue(createResp == createRespFuture.get());
}

From source file:org.apache.bookkeeper.stream.storage.impl.TestStorageContainerStoreImpl.java

@Test
public void testDeleteStreamMockRootStorageContainerStore() throws Exception {
    String colName = "test-delete-namespace-no-root-storage-container-store";
    String streamName = colName;//from w  w  w  . ja va  2s  .  c o m

    DeleteStreamResponse deleteResp = DeleteStreamResponse.newBuilder().setCode(StatusCode.STREAM_NOT_FOUND)
            .build();
    DeleteStreamRequest deleteReq = createDeleteStreamRequest(colName, streamName);
    when(mockRangeStoreService.deleteStream(deleteReq))
            .thenReturn(CompletableFuture.completedFuture(deleteResp));

    CompletableFuture<DeleteStreamResponse> deleteRespFuture = fromListenableFuture(
            rootRangeService.deleteStream(deleteReq));
    verify(mockRangeStoreService, times(1)).deleteStream(deleteReq);
    assertTrue(deleteResp == deleteRespFuture.get());
}

From source file:com.adobe.acs.commons.mcp.impl.processes.renovator.RenovatorTest.java

@Test
public void testHaltingScenario() throws DeserializeException, LoginException, RepositoryException,
        InterruptedException, ExecutionException, PersistenceException {
    assertEquals("Renovator: relocator test", instance.getName());
    Map<String, Object> values = new HashMap<>();
    values.put("sourceJcrPath", "/content/dam/folderA");
    values.put("destinationJcrPath", "/content/dam/folderB");
    instance.init(rr, values);//from ww w.ja  va  2s.com

    CompletableFuture<Boolean> f = new CompletableFuture<>();

    instance.defineAction("Halt", rr, am -> {
        instance.halt();
        try {
            assertTrue(instance.updateProgress() < 1.0);
            assertFalse(instance.getInfo().isIsRunning());
            f.complete(true);
        } catch (Throwable t) {
            f.completeExceptionally(t);
        }
    });
    instance.run(rr);
    assertTrue(f.get());
    verify(rr, atLeastOnce()).commit();
}

From source file:org.apache.bookkeeper.stream.storage.impl.TestStorageContainerStoreImpl.java

@Test
public void testDeleteNamespaceMockRootStorageContainerStore() throws Exception {
    String colName = "test-delete-namespace-no-root-storage-container-store";

    DeleteNamespaceResponse deleteResp = DeleteNamespaceResponse.newBuilder()
            .setCode(StatusCode.NAMESPACE_NOT_FOUND).build();
    DeleteNamespaceRequest request = createDeleteNamespaceRequest(colName);

    when(mockRangeStoreService.deleteNamespace(request))
            .thenReturn(CompletableFuture.completedFuture(deleteResp));

    CompletableFuture<DeleteNamespaceResponse> deleteRespFuture = fromListenableFuture(
            rootRangeService.deleteNamespace(request));
    verify(mockRangeStoreService, times(1)).deleteNamespace(request);
    assertTrue(deleteResp == deleteRespFuture.get());
}

From source file:org.apache.flink.runtime.blob.BlobCacheGetTest.java

/**
 * [FLINK-6020] Tests that concurrent get operations don't concurrently access the BlobStore to
 * download a blob.//from  w ww  . j a  v  a 2  s .c  om
 *
 * @param jobId
 *       job ID to use (or <tt>null</tt> if job-unrelated)
 * @param blobType
 *       whether the BLOB should become permanent or transient
 * @param cacheAccessesHAStore
 *       whether the cache has access to the {@link BlobServer}'s HA store or not
 */
private void testConcurrentGetOperations(final JobID jobId, final BlobKey.BlobType blobType,
        final boolean cacheAccessesHAStore) throws IOException, InterruptedException, ExecutionException {
    final Configuration config = new Configuration();
    config.setString(BlobServerOptions.STORAGE_DIRECTORY, temporaryFolder.newFolder().getAbsolutePath());

    final BlobStore blobStoreServer = mock(BlobStore.class);
    final BlobStore blobStoreCache = mock(BlobStore.class);

    final int numberConcurrentGetOperations = 3;
    final List<CompletableFuture<File>> getOperations = new ArrayList<>(numberConcurrentGetOperations);

    final byte[] data = { 1, 2, 3, 4, 99, 42 };

    final ExecutorService executor = Executors.newFixedThreadPool(numberConcurrentGetOperations);

    try (final BlobServer server = new BlobServer(config, blobStoreServer);
            final BlobCacheService cache = new BlobCacheService(config,
                    cacheAccessesHAStore ? blobStoreServer : blobStoreCache,
                    new InetSocketAddress("localhost", server.getPort()))) {

        server.start();

        // upload data first
        final BlobKey blobKey = put(server, jobId, data, blobType);

        // now try accessing it concurrently (only HA mode will be able to retrieve it from HA store!)
        for (int i = 0; i < numberConcurrentGetOperations; i++) {
            CompletableFuture<File> getOperation = CompletableFuture.supplyAsync(() -> {
                try {
                    File file = get(cache, jobId, blobKey);
                    // check that we have read the right data
                    validateGetAndClose(new FileInputStream(file), data);
                    return file;
                } catch (IOException e) {
                    throw new CompletionException(
                            new FlinkException("Could not read blob for key " + blobKey + '.', e));
                }
            }, executor);

            getOperations.add(getOperation);
        }

        FutureUtils.ConjunctFuture<Collection<File>> filesFuture = FutureUtils.combineAll(getOperations);

        if (blobType == PERMANENT_BLOB) {
            // wait until all operations have completed and check that no exception was thrown
            filesFuture.get();
        } else {
            // wait for all futures to complete (do not abort on expected exceptions) and check
            // that at least one succeeded
            int completedSuccessfully = 0;
            for (CompletableFuture<File> op : getOperations) {
                try {
                    op.get();
                    ++completedSuccessfully;
                } catch (Throwable t) {
                    // transient BLOBs get deleted upon first access and only one request will be successful while all others will have an IOException caused by a FileNotFoundException
                    if (!(ExceptionUtils.getRootCause(t) instanceof FileNotFoundException)) {
                        // ignore
                        org.apache.flink.util.ExceptionUtils.rethrowIOException(t);
                    }
                }
            }
            // multiple clients may have accessed the BLOB successfully before it was
            // deleted, but always at least one:
            assertThat(completedSuccessfully, greaterThanOrEqualTo(1));
        }
    } finally {
        executor.shutdownNow();
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java

@Test
public void shouldAwaitToExpectedValueAndDrainOnAdd() throws Exception {
    final CompletableFuture<List<Result>> future = resultQueue.await(3);
    resultQueue.add(new Result("test1"));
    resultQueue.add(new Result("test2"));

    // shouldn't complete until the third item is in play
    assertThat(future.isDone(), is(false));

    resultQueue.add(new Result("test3"));

    final List<Result> results = future.get();
    assertEquals("test1", results.get(0).getString());
    assertEquals("test2", results.get(1).getString());
    assertEquals("test3", results.get(2).getString());
    assertEquals(3, results.size());//  w w w. ja v a2s .c o  m

    assertThat(resultQueue.isEmpty(), is(true));
}

From source file:com.ikanow.aleph2.harvest.script.services.TestScriptHarvestService.java

@Test
public void testStopScript() throws InterruptedException, ExecutionException {
    final ScriptHarvestService harvester = new ScriptHarvestService();
    harvester.onInit(getFakeContext());/*from   w ww. ja  va2 s.co  m*/

    final String tmp_dir = System.getProperty("java.io.tmpdir");
    final String file_path = tmp_dir + File.separator + "test3";
    System.out.println("file: " + file_path);
    final File file = new File(file_path);
    try {
        file.delete();
    } catch (Exception e) {
    } //cleanup if the file exists from previous test

    //have to put quotes around the path on windows systems
    final String script = new StringBuilder().append("touch \"" + file_path + "\"\n").append("for (( ; ; ))\n")
            .append("do\n").append(" echo \"iteration\" >> \"" + file_path + "\"\n").append(" sleep 1\n")
            .append("done\n").toString();
    final DataBucketBean bucket = getTestbucket("/test/script1", Optional.of(script), Optional.empty(),
            Optional.empty(), new HashMap<String, String>(), new ArrayList<String>());
    final CompletableFuture<BasicMessageBean> future = harvester.onNewSource(bucket, getFakeContext(), true);
    final BasicMessageBean response = future.get();
    assertTrue(response.message(), response.success());

    //test if file was created
    final long curr_time = System.currentTimeMillis();
    while (System.currentTimeMillis() < curr_time + 5000) {
        if (file.exists())
            break;
        Thread.sleep(300);
    }
    assertTrue(file.exists());

    //test periodicPoll still thinks its running
    assertTrue(harvester.onPeriodicPoll(bucket, getFakeContext()).get().success());

    //stop the source
    final CompletableFuture<BasicMessageBean> future_stop = harvester.onUpdatedSource(bucket, bucket, false,
            Optional.empty(), getFakeContext());
    final BasicMessageBean response_stop = future_stop.get();
    assertTrue(response_stop.message(), response_stop.success());

    //test file stopped growing or something
    long last_mod = file.lastModified();
    Thread.sleep(1500);
    assertEquals(file.lastModified(), last_mod);

    //cleanup
    file.delete();
}

From source file:org.apache.flink.test.recovery.JobManagerHAProcessFailureRecoveryITCase.java

@Test
public void testDispatcherProcessFailure() throws Exception {
    final Time timeout = Time.seconds(30L);
    final File zookeeperStoragePath = temporaryFolder.newFolder();

    // Config/*from   w w w  . java2s  . c  om*/
    final int numberOfJobManagers = 2;
    final int numberOfTaskManagers = 2;
    final int numberOfSlotsPerTaskManager = 2;

    assertEquals(PARALLELISM, numberOfTaskManagers * numberOfSlotsPerTaskManager);

    // Job managers
    final DispatcherProcess[] dispatcherProcesses = new DispatcherProcess[numberOfJobManagers];

    // Task managers
    TaskManagerRunner[] taskManagerRunners = new TaskManagerRunner[numberOfTaskManagers];

    HighAvailabilityServices highAvailabilityServices = null;

    LeaderRetrievalService leaderRetrievalService = null;

    // Coordination between the processes goes through a directory
    File coordinateTempDir = null;

    // Cluster config
    Configuration config = ZooKeeperTestUtils.createZooKeeperHAConfig(zooKeeper.getConnectString(),
            zookeeperStoragePath.getPath());
    // Task manager configuration
    config.setString(TaskManagerOptions.MANAGED_MEMORY_SIZE, "4m");
    config.setInteger(TaskManagerOptions.NETWORK_NUM_BUFFERS, 100);
    config.setInteger(TaskManagerOptions.NUM_TASK_SLOTS, 2);

    final RpcService rpcService = AkkaRpcServiceUtils.createRpcService("localhost", 0, config);

    try {
        final Deadline deadline = TestTimeOut.fromNow();

        // Coordination directory
        coordinateTempDir = temporaryFolder.newFolder();

        // Start first process
        dispatcherProcesses[0] = new DispatcherProcess(0, config);
        dispatcherProcesses[0].startProcess();

        highAvailabilityServices = HighAvailabilityServicesUtils.createAvailableOrEmbeddedServices(config,
                TestingUtils.defaultExecutor());

        // Start the task manager process
        for (int i = 0; i < numberOfTaskManagers; i++) {
            taskManagerRunners[i] = new TaskManagerRunner(config, ResourceID.generate());
            taskManagerRunners[i].start();
        }

        // Leader listener
        TestingListener leaderListener = new TestingListener();
        leaderRetrievalService = highAvailabilityServices.getDispatcherLeaderRetriever();
        leaderRetrievalService.start(leaderListener);

        // Initial submission
        leaderListener.waitForNewLeader(deadline.timeLeft().toMillis());

        String leaderAddress = leaderListener.getAddress();
        UUID leaderId = leaderListener.getLeaderSessionID();

        final CompletableFuture<DispatcherGateway> dispatcherGatewayFuture = rpcService.connect(leaderAddress,
                DispatcherId.fromUuid(leaderId), DispatcherGateway.class);
        final DispatcherGateway dispatcherGateway = dispatcherGatewayFuture.get();

        // Wait for all task managers to connect to the leading job manager
        waitForTaskManagers(numberOfTaskManagers, dispatcherGateway, deadline.timeLeft());

        final File coordinateDirClosure = coordinateTempDir;
        final Throwable[] errorRef = new Throwable[1];

        // we trigger program execution in a separate thread
        Thread programTrigger = new Thread("Program Trigger") {
            @Override
            public void run() {
                try {
                    testJobManagerFailure(zooKeeper.getConnectString(), coordinateDirClosure,
                            zookeeperStoragePath);
                } catch (Throwable t) {
                    t.printStackTrace();
                    errorRef[0] = t;
                }
            }
        };

        //start the test program
        programTrigger.start();

        // wait until all marker files are in place, indicating that all tasks have started
        AbstractTaskManagerProcessFailureRecoveryTest.waitForMarkerFiles(coordinateTempDir,
                READY_MARKER_FILE_PREFIX, PARALLELISM, deadline.timeLeft().toMillis());

        // Kill one of the job managers and trigger recovery
        dispatcherProcesses[0].destroy();

        dispatcherProcesses[1] = new DispatcherProcess(1, config);
        dispatcherProcesses[1].startProcess();

        // we create the marker file which signals the program functions tasks that they can complete
        AbstractTaskManagerProcessFailureRecoveryTest
                .touchFile(new File(coordinateTempDir, PROCEED_MARKER_FILE));

        programTrigger.join(deadline.timeLeft().toMillis());

        // We wait for the finish marker file. We don't wait for the program trigger, because
        // we submit in detached mode.
        AbstractTaskManagerProcessFailureRecoveryTest.waitForMarkerFiles(coordinateTempDir,
                FINISH_MARKER_FILE_PREFIX, 1, deadline.timeLeft().toMillis());

        // check that the program really finished
        assertFalse("The program did not finish in time", programTrigger.isAlive());

        // check whether the program encountered an error
        if (errorRef[0] != null) {
            Throwable error = errorRef[0];
            error.printStackTrace();
            fail("The program encountered a " + error.getClass().getSimpleName() + " : " + error.getMessage());
        }
    } catch (Throwable t) {
        // Print early (in some situations the process logs get too big
        // for Travis and the root problem is not shown)
        t.printStackTrace();

        for (DispatcherProcess p : dispatcherProcesses) {
            if (p != null) {
                p.printProcessLog();
            }
        }

        throw t;
    } finally {
        for (int i = 0; i < numberOfTaskManagers; i++) {
            if (taskManagerRunners[i] != null) {
                taskManagerRunners[i].close();
            }
        }

        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }

        for (DispatcherProcess dispatcherProcess : dispatcherProcesses) {
            if (dispatcherProcess != null) {
                dispatcherProcess.destroy();
            }
        }

        if (highAvailabilityServices != null) {
            highAvailabilityServices.closeAndCleanupAllData();
        }

        RpcUtils.terminateRpcService(rpcService, timeout);

        // Delete coordination directory
        if (coordinateTempDir != null) {
            try {
                FileUtils.deleteDirectory(coordinateTempDir);
            } catch (Throwable ignored) {
            }
        }
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java

@Test
public void shouldAwaitEverythingAndFlushOnMarkCompleted() throws Exception {
    final CompletableFuture<List<Result>> future = resultQueue.await(4);
    resultQueue.add(new Result("test1"));
    resultQueue.add(new Result("test2"));
    resultQueue.add(new Result("test3"));

    assertThat(future.isDone(), is(false));
    resultQueue.markComplete();/*from  w  w  w  . ja v a 2s . c  o  m*/
    assertThat(future.isDone(), is(true));

    final List<Result> results = future.get();
    assertEquals("test1", results.get(0).getString());
    assertEquals("test2", results.get(1).getString());
    assertEquals("test3", results.get(2).getString());
    assertEquals(3, results.size());

    assertThat(resultQueue.isEmpty(), is(true));
}