Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to FT tests to take advantage of @HelidonTest #2370

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,42 @@ static <T> Gauge<T> getGauge(Method method, String name) {

static long getCounter(Object bean, String methodName, String name,
Class<?>... params) throws Exception {
Method method = getRealClass(bean).getMethod(methodName, params);
Method method = findMethod(getRealClass(bean), methodName, params);
return getCounter(method, name).getCount();
}

static Histogram getHistogram(Object bean, String methodName, String name,
Class<?>... params) throws Exception {
Method method = getRealClass(bean).getMethod(methodName, params);
Method method = findMethod(getRealClass(bean), methodName, params);
return getHistogram(method, name);
}

static <T> Gauge<T> getGauge(Object bean, String methodName, String name,
Class<?>... params) throws Exception {
Method method = getRealClass(bean).getMethod(methodName, params);
Method method = findMethod(getRealClass(bean), methodName, params);
return getGauge(method, name);
}

/**
* Attempts to find a method even if not accessible.
*
* @param beanClass bean class.
* @param methodName name of method.
* @param params param types.
* @return method found.
* @throws NoSuchMethodException if not found.
*/
private static Method findMethod(Class<?> beanClass, String methodName,
Class<?>... params) throws NoSuchMethodException {
try {
Method method = beanClass.getDeclaredMethod(methodName, params);
method.setAccessible(true);
return method;
} catch (Exception e) {
return beanClass.getMethod(methodName, params);
}
}

// -- Global --------------------------------------------------------------

static final String INVOCATIONS_TOTAL = "invocations.total";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,31 @@
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.enterprise.context.Dependent;

import org.eclipse.microprofile.faulttolerance.Asynchronous;
import org.eclipse.microprofile.faulttolerance.Fallback;

/**
* Class AsynchronousBean.
* A stateful bean that defines some async methods.
*/
@Dependent
public class AsynchronousBean {
class AsynchronousBean {

private AtomicBoolean called = new AtomicBoolean(false);

public boolean wasCalled() {
boolean wasCalled() {
return called.get();
}

void reset() {
called.set(false);
}

/**
* Normal asynchronous call.
*
* @return A future.
*/
@Asynchronous
public CompletableFuture<String> async() {
CompletableFuture<String> async() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::async", "success");
return CompletableFuture.completedFuture("success");
Expand All @@ -58,13 +59,13 @@ public CompletableFuture<String> async() {
*/
@Asynchronous
@Fallback(fallbackMethod = "onFailure")
public CompletableFuture<String> asyncWithFallback() {
CompletableFuture<String> asyncWithFallback() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::asyncWithFallback", "failure");
return CompletableFuture.failedFuture(new RuntimeException("Oops"));
}

public CompletableFuture<String> onFailure() {
CompletableFuture<String> onFailure() {
FaultToleranceTest.printStatus("AsynchronousBean::onFailure", "success");
return CompletableFuture.completedFuture("fallback");
}
Expand All @@ -76,13 +77,13 @@ public CompletableFuture<String> onFailure() {
*/
@Asynchronous
@Fallback(fallbackMethod = "onFailureFuture")
public Future<String> asyncWithFallbackFuture() {
Future<String> asyncWithFallbackFuture() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::asyncWithFallbackFuture", "failure");
return CompletableFuture.failedFuture(new RuntimeException("Oops"));
}

public Future<String> onFailureFuture() {
Future<String> onFailureFuture() {
FaultToleranceTest.printStatus("AsynchronousBean::onFailure", "success");
return CompletableFuture.completedFuture("fallback");
}
Expand All @@ -93,7 +94,7 @@ public Future<String> onFailureFuture() {
*
* @return A future.
*/
public CompletableFuture<String> notAsync() {
CompletableFuture<String> notAsync() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::notAsync", "success");
return CompletableFuture.completedFuture("success");
Expand All @@ -105,7 +106,7 @@ public CompletableFuture<String> notAsync() {
* @return A completion stage.
*/
@Asynchronous
public CompletionStage<String> asyncCompletionStage() {
CompletionStage<String> asyncCompletionStage() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::asyncCompletionStage", "success");
return CompletableFuture.completedFuture("success");
Expand All @@ -118,7 +119,7 @@ public CompletionStage<String> asyncCompletionStage() {
*/
@Asynchronous
@Fallback(fallbackMethod = "onFailure")
public CompletionStage<String> asyncCompletionStageWithFallback() {
CompletionStage<String> asyncCompletionStageWithFallback() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::asyncCompletionStageWithFallback", "failure");
return CompletableFuture.failedFuture(new RuntimeException("Oops"));
Expand All @@ -130,7 +131,7 @@ public CompletionStage<String> asyncCompletionStageWithFallback() {
* @return A completable future.
*/
@Asynchronous
public CompletableFuture<String> asyncCompletableFuture() {
CompletableFuture<String> asyncCompletableFuture() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::asyncCompletableFuture", "success");
return CompletableFuture.completedFuture("success");
Expand All @@ -143,7 +144,7 @@ public CompletableFuture<String> asyncCompletableFuture() {
*/
@Asynchronous
@Fallback(fallbackMethod = "onFailure")
public CompletableFuture<String> asyncCompletableFutureWithFallback() {
CompletableFuture<String> asyncCompletableFutureWithFallback() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::asyncCompletableFutureWithFallback", "success");
return CompletableFuture.completedFuture("success");
Expand All @@ -157,7 +158,7 @@ public CompletableFuture<String> asyncCompletableFutureWithFallback() {
*/
@Asynchronous
@Fallback(fallbackMethod = "onFailure")
public CompletableFuture<String> asyncCompletableFutureWithFallbackFailure() {
CompletableFuture<String> asyncCompletableFutureWithFallbackFailure() {
called.set(true);
FaultToleranceTest.printStatus("AsynchronousBean::asyncCompletableFutureWithFallbackFailure", "failure");
CompletableFuture<String> future = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,23 +24,22 @@
import org.eclipse.microprofile.faulttolerance.Asynchronous;

/**
* Class AsynchronousCallerBean.
* A bean that invokes a {@code Callable} in another thread by using the
* {@code @Asynchronous} annotation.
*/
@ApplicationScoped
public class AsynchronousCallerBean {
class AsynchronousCallerBean {

@Asynchronous
public <T> CompletableFuture<T> submit(Callable<T> callable) {
<T> CompletableFuture<T> submit(Callable<T> callable) {
CompletableFuture<T> future = new CompletableFuture<>();
try {
T o = callable.call();
CompletableFuture<T> f = CompletableFuture.completedFuture(o);
return f;
future.complete(o);
}
catch (Exception e) {
CompletableFuture<T> f = new CompletableFuture<T>();
f.completeExceptionally(e);
return f;
catch (Throwable t) {
future.completeExceptionally(t);
}
return future;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@

package io.helidon.microprofile.faulttolerance;

import javax.inject.Inject;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Future;

import io.helidon.microprofile.tests.junit5.AddBean;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

/**
* Class AsynchronousTest.
* Tests for asynchronous invocations using {@code Asynchronous}.
*/
public class AsynchronousTest extends FaultToleranceTest {
@AddBean(AsynchronousBean.class)
class AsynchronousTest extends FaultToleranceTest {

@Inject
private AsynchronousBean bean;

@Override
void reset() {
bean.reset();
}

@Test
public void testAsync() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsync() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletableFuture<String> future = bean.async();
future.get();
assertThat(bean.wasCalled(), is(true));
}

@Test
public void testAsyncWithFallback() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncWithFallback() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletableFuture<String> future = bean.asyncWithFallback();
String value = future.get();
Expand All @@ -50,15 +59,13 @@ public void testAsyncWithFallback() throws Exception {
}

@Test
public void testAsyncWithFallbackFuture() {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncWithFallbackFuture() {
Future<String> future = bean.asyncWithFallbackFuture(); // fallback ignored with Future
assertCompleteExceptionally(future, RuntimeException.class);
}

@Test
public void testAsyncNoGet() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncNoGet() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletableFuture<String> future = bean.async();
while (!future.isDone()) {
Expand All @@ -68,26 +75,23 @@ public void testAsyncNoGet() throws Exception {
}

@Test
public void testNotAsync() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testNotAsync() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletableFuture<String> future = bean.notAsync();
assertThat(bean.wasCalled(), is(true));
future.get();
}

@Test
public void testAsyncCompletionStage() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncCompletionStage() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletionStage<String> completionStage = bean.asyncCompletionStage();
completionStage.toCompletableFuture().get();
assertThat(bean.wasCalled(), is(true));
}

@Test
public void testAsyncCompletionStageWithFallback() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncCompletionStageWithFallback() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletionStage<String> completionStage = bean.asyncCompletionStageWithFallback();
String value = completionStage.toCompletableFuture().get();
Expand All @@ -96,26 +100,23 @@ public void testAsyncCompletionStageWithFallback() throws Exception {
}

@Test
public void testAsyncCompletableFuture() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncCompletableFuture() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletableFuture<String> completableFuture = bean.asyncCompletableFuture();
completableFuture.get();
assertThat(bean.wasCalled(), is(true));
}

@Test
public void testAsyncCompletableFutureWithFallback() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncCompletableFutureWithFallback() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletableFuture<String> completableFuture = bean.asyncCompletableFutureWithFallback();
completableFuture.get();
assertThat(bean.wasCalled(), is(true));
}

@Test
public void testAsyncCompletableFutureWithFallbackFailure() throws Exception {
AsynchronousBean bean = newBean(AsynchronousBean.class);
void testAsyncCompletableFutureWithFallbackFailure() throws Exception {
assertThat(bean.wasCalled(), is(false));
CompletableFuture<String> completableFuture = bean.asyncCompletableFutureWithFallbackFailure();
assertThat(completableFuture.get(), is("fallback"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,11 +17,11 @@
package io.helidon.microprofile.faulttolerance;

/**
* Class BaseFallbackBean.
* Provides common methods to handle fallbacks.
*/
public class BaseFallbackBean {
class BaseFallbackBean {

protected String onFailureBase () {
protected String onFailureBase() {
FaultToleranceTest.printStatus("FallbackBean::onFailure()", "success");
return "fallback";
}
Expand Down
Loading