Java Code Examples for java.util.concurrent.CompletableFuture#orTimeout()

The following examples show how to use java.util.concurrent.CompletableFuture#orTimeout() . 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: FutureBenchmark.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Object completableFutureWithTimeout() {
	CompletableFuture<Integer> future = new CompletableFuture<>();

	future.orTimeout(1, TimeUnit.SECONDS);

	future.complete(Integer.valueOf(1));

	return future.join();
}
 
Example 2
Source File: CompletableFutureTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * orTimeout completes with TimeoutException if not complete
 */
public void testOrTimeout_timesOut() {
    long timeoutMillis = timeoutMillis();
    CompletableFuture<Integer> f = new CompletableFuture<>();
    long startTime = System.nanoTime();
    f.orTimeout(timeoutMillis, MILLISECONDS);
    checkCompletedWithTimeoutException(f);
    assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
}
 
Example 3
Source File: CompletableFutureTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * orTimeout completes normally if completed before timeout
 */
public void testOrTimeout_completed() {
    for (Integer v1 : new Integer[] { 1, null })
{
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletableFuture<Integer> g = new CompletableFuture<>();
    long startTime = System.nanoTime();
    f.complete(v1);
    f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
    g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
    g.complete(v1);
    checkCompletedNormally(f, v1);
    checkCompletedNormally(g, v1);
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
}}
 
Example 4
Source File: CompletableFutureUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void testTimeoutTriggered () throws Exception {
    CompletableFuture<Object> future = new CompletableFuture<>();
    future.orTimeout(1, TimeUnit.SECONDS);
    
    Thread.sleep(1100);
    
    assertTrue(future.isDone());
    
    try {
        future.get();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof TimeoutException);
    }
}
 
Example 5
Source File: CompletableFutureUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void testTimeoutNotTriggered () throws Exception {
    Object input = new Object();
    CompletableFuture<Object> future = new CompletableFuture<>();
    
    future.orTimeout(1, TimeUnit.SECONDS);
    
    Thread.sleep(100);
    
    future.complete(input);
    
    Thread.sleep(1000);
    
    assertTrue(future.isDone());
    assertSame(input, future.get());
}