use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testMinimalCompletionStage_minimality.
/**
* Minimal completion stages throw UOE for most non-CompletionStage methods
*/
public void testMinimalCompletionStage_minimality() {
if (!testImplementationDetails)
return;
Function<Method, String> toSignature = method -> method.getName() + Arrays.toString(method.getParameterTypes());
Predicate<Method> isNotStatic = method -> (method.getModifiers() & Modifier.STATIC) == 0;
List<Method> minimalMethods = RefStreams.of(Object.class, CompletionStage.class).flatMap(klazz -> RefStreams.of(klazz.getMethods())).filter(isNotStatic).collect(Collectors.toList());
// Methods from CompletableFuture permitted NOT to throw UOE
String[] signatureWhitelist = { "newIncompleteFuture[]", "defaultExecutor[]", "minimalCompletionStage[]", "copy[]" };
Set<String> permittedMethodSignatures = RefStreams.concat(StreamSupport.stream(minimalMethods).map(toSignature), RefStreams.of(signatureWhitelist)).collect(Collectors.toSet());
List<Method> allMethods = RefStreams.of(CompletableFuture.class.getMethods()).filter(isNotStatic).filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))).collect(Collectors.toList());
List<CompletionStage<Integer>> stages = new ArrayList<CompletionStage<Integer>>();
CompletionStage<Integer> min = new CompletableFuture<Integer>().minimalCompletionStage();
stages.add(min);
stages.add(min.thenApply(x -> x));
stages.add(CompletableFuture.completedStage(1));
stages.add(CompletableFuture.failedStage(new CFException()));
List<Method> bugs = new ArrayList<>();
for (Method method : allMethods) {
Class<?>[] parameterTypes = method.getParameterTypes();
Object[] args = new Object[parameterTypes.length];
// Manufacture boxed primitives for primitive params
for (int i = 0; i < args.length; i++) {
Class<?> type = parameterTypes[i];
if (type == boolean.class)
args[i] = false;
else if (type == int.class)
args[i] = 0;
else if (type == long.class)
args[i] = 0L;
}
for (CompletionStage<Integer> stage : stages) {
try {
method.invoke(stage, args);
bugs.add(method);
} catch (java.lang.reflect.InvocationTargetException expected) {
if (!(expected.getCause() instanceof UnsupportedOperationException)) {
bugs.add(method);
// expected.getCause().printStackTrace();
}
} catch (Exception bad) {
throw new Error(bad);
}
}
}
if (!bugs.isEmpty())
throw new Error("Methods did not throw UOE: " + bugs);
}
use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testMinimalCompletionStage.
/**
* minimalCompletionStage returns a CompletableFuture that is
* completed normally, with the same value, when source is.
*/
public void testMinimalCompletionStage() {
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletionStage<Integer> g = f.minimalCompletionStage();
AtomicInteger x = new AtomicInteger(0);
AtomicReference<Throwable> r = new AtomicReference<>();
checkIncomplete(f);
g.whenComplete((v, e) -> {
if (e != null)
r.set(e);
else
x.set(v);
});
f.complete(1);
checkCompletedNormally(f, 1);
assertEquals(x.get(), 1);
assertNull(r.get());
}
use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testToCompletableFutureGarbageRetention.
/**
* Checks for garbage retention when MinimalStage.toCompletableFuture()
* is invoked many times.
* 8161600: Garbage retention when source CompletableFutures are never completed
*
* As of 2016-07, fails with OOME:
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
*/
public void testToCompletableFutureGarbageRetention() throws Throwable {
final int n = expensiveTests ? 900_000 : 10;
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
CompletionStage minimal = neverCompleted.minimalCompletionStage();
for (int i = 0; i < n; i++) assertTrue(minimal.toCompletableFuture().cancel(true));
}
use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testMinimalCompletionStage_toCompletableFuture_mutable.
/**
* minimalStage.toCompletableFuture() gives mutable CompletableFuture
*/
public void testMinimalCompletionStage_toCompletableFuture_mutable() {
for (Integer v1 : new Integer[] { 1, null }) {
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletionStage<Integer> minimal = f.minimalCompletionStage();
CompletableFuture<Integer> g = minimal.toCompletableFuture();
assertTrue(g.complete(v1));
checkCompletedNormally(g, v1);
checkIncomplete(f);
checkIncomplete(minimal.toCompletableFuture());
}
}
use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testMinimalCompletionStage2.
/**
* minimalCompletionStage returns a CompletableFuture that is
* completed exceptionally when source is.
*/
public void testMinimalCompletionStage2() {
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletionStage<Integer> g = f.minimalCompletionStage();
AtomicInteger x = new AtomicInteger(0);
AtomicReference<Throwable> r = new AtomicReference<>();
g.whenComplete((v, e) -> {
if (e != null)
r.set(e);
else
x.set(v);
});
checkIncomplete(f);
CFException ex = new CFException();
f.completeExceptionally(ex);
checkCompletedExceptionally(f, ex);
assertEquals(x.get(), 0);
assertEquals(r.get().getCause(), ex);
}
Aggregations