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

Catch known exceptions (broken pipe) #1042

Merged
merged 2 commits into from
Feb 13, 2024
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 @@ -12,6 +12,7 @@

import jakarta.inject.Inject;

import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
Expand All @@ -26,6 +27,7 @@
import io.quarkus.test.configuration.PropertyLookup;
import io.quarkus.test.logging.Log;
import io.quarkus.test.services.quarkus.ProdQuarkusApplicationManagedResourceBuilder;
import io.quarkus.test.utils.KnownExceptionChecker;
import io.quarkus.test.utils.ReflectionUtils;

public class QuarkusScenarioBootstrap
Expand Down Expand Up @@ -168,6 +170,18 @@ private void launchService(Service service) {
extensions.forEach(ext -> ext.onServiceLaunch(scenario, service));
try {
service.start();
} catch (RuntimeException runtimeException) {
// catch known failures is service launch.
// mostly searching for "Broken pipe" failure - https://github.com/quarkusio/quarkus/issues/38334
if (KnownExceptionChecker.checkForKnownException(runtimeException)) {
Log.error("Known exception catched, printing it");
runtimeException.printStackTrace();
// abort test execution
Assumptions.abort();
} else {
scenarioOnError(runtimeException);
throw runtimeException;
}
} catch (Throwable throwable) {
scenarioOnError(throwable);
throw throwable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.test.listener;

import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;

import io.quarkus.test.logging.Log;
import io.quarkus.test.utils.KnownExceptionChecker;

/**
* Extension is masking exceptions, caused by known issues which cannot be easily disabled.
* Originally created for <a href="https://github.com/quarkusio/quarkus/issues/38334">Broken pipe</a> issue.
* In case that related exception is thrown, this extension will swallow it, and make test marked as skipped.
*/
public class QuarkusTestExceptionFilter implements TestExecutionExceptionHandler {

@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
// if exception is caused by known issue, swallow it, otherwise propagate it
if (KnownExceptionChecker.checkForKnownException(throwable)) {
Log.error("Skipping test " + context.getDisplayName() + " known exception found. Printing stack trace");
throwable.printStackTrace();
// skip the test
Assumptions.abort();
} else {
throw throwable;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import org.junit.jupiter.api.extension.ExtendWith;

import io.quarkus.test.bootstrap.QuarkusScenarioBootstrap;
import io.quarkus.test.listener.QuarkusTestExceptionFilter;
import io.quarkus.test.scenarios.execution.condition.QuarkusScenarioExecutionConditions;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(QuarkusScenarioBootstrap.class)
@ExtendWith(QuarkusScenarioExecutionConditions.class)
@ExtendWith(QuarkusTestExceptionFilter.class)
@Inherited
public @interface QuarkusScenario {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.quarkus.test.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import io.quarkus.test.logging.Log;

public final class KnownExceptionChecker {

/**
* Define all handlers, that can be used to ignore exceptions.
* If you want to ignore another exception, just add another predicate and enable it via system property.
*/
private static final Map<String, Predicate<Throwable>> ALL_EXCEPTION_HANDLERS = Map.of("broken-pipe", throwable -> {
if (throwable.getMessage().contains("Broken pipe")) {
return true;
}
if (throwable.getCause() != null && checkForKnownException(throwable.getCause())) {
return true;
}
for (Throwable t : throwable.getSuppressed()) {
if (checkForKnownException(t)) {
return true;
}
}
return false;
});

/**
* List to store enabled handlers, which are going to be effective.
*/
private static List<Predicate<Throwable>> enabledExceptionsHandlers = new ArrayList<>();

/*
* Read system property and assign desired handlers as enabled ones
*/
static {
String enableHandlersParam = System.getProperty("ts.global.ignore-known-issue");
if (enableHandlersParam != null && !enableHandlersParam.isEmpty()) {
String[] splitParameters = enableHandlersParam.split(",");
for (String enableHandler : splitParameters) {
if (ALL_EXCEPTION_HANDLERS.containsKey(enableHandler.trim())) {
enabledExceptionsHandlers.add(ALL_EXCEPTION_HANDLERS.get(enableHandler.trim()));
} else {
Log.warn("Trying to enable unknown exception handler: " + enableHandler);
}
}
}
}

private KnownExceptionChecker() {
}

/**
* Check if thrown message contains any of the known exception messages.
* Method is recursive, it will check the exception itself and all causes and suppressed ones as well.
*
* @param throwable Exception to check
* @return true if any known exception message is included, false otherwise
*/
public static boolean checkForKnownException(Throwable throwable) {
for (Predicate<Throwable> p : enabledExceptionsHandlers) {
if (p.test(throwable)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import org.junit.jupiter.api.extension.ExtendWith;

import io.quarkus.test.bootstrap.QuarkusScenarioBootstrap;
import io.quarkus.test.listener.QuarkusTestExceptionFilter;
import io.quarkus.test.services.Operator;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(QuarkusScenarioBootstrap.class)
@ExtendWith(QuarkusTestExceptionFilter.class)
@Inherited
public @interface OpenShiftScenario {
OpenShiftDeploymentStrategy deployment() default OpenShiftDeploymentStrategy.Build;
Expand Down
Loading