From 21a43b17ae255df6754a7ab5b29adec0c0d24736 Mon Sep 17 00:00:00 2001 From: J08nY Date: Thu, 1 Aug 2024 16:18:54 +0200 Subject: Add timeouts to reader tests. --- .../java/cz/crcs/ectester/reader/AppTests.java | 41 +++++++---- .../test/java/cz/crcs/ectester/reader/XFail.java | 86 ++++++++++++++++++++++ .../cz/crcs/ectester/reader/XFaillExtension.java | 86 ++++++++++++++++++++++ 3 files changed, 199 insertions(+), 14 deletions(-) create mode 100644 reader/src/test/java/cz/crcs/ectester/reader/XFail.java create mode 100644 reader/src/test/java/cz/crcs/ectester/reader/XFaillExtension.java (limited to 'reader/src') diff --git a/reader/src/test/java/cz/crcs/ectester/reader/AppTests.java b/reader/src/test/java/cz/crcs/ectester/reader/AppTests.java index ae8618c..a34d4fd 100644 --- a/reader/src/test/java/cz/crcs/ectester/reader/AppTests.java +++ b/reader/src/test/java/cz/crcs/ectester/reader/AppTests.java @@ -1,11 +1,12 @@ package cz.crcs.ectester.reader; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junitpioneer.jupiter.DisabledUntil; import org.junitpioneer.jupiter.StdIo; import org.junitpioneer.jupiter.StdOut; +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; public class AppTests { @@ -37,62 +38,74 @@ public class AppTests { // Add StdIo to all the suite tests when this is resolved: https://github.com/junit-pioneer/junit-pioneer/issues/822 @Test + @XFail(value = "JCardSim sometimes times-out.") public void defaultSuite() { - ECTesterReader.main(new String[]{"-t", "default", "-s"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "default", "-s"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void testVectorSuite() { - ECTesterReader.main(new String[]{"-t", "test-vectors", "-s"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "test-vectors", "-s"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void compressionSuite() { - ECTesterReader.main(new String[]{"-t", "compression", "-s"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "compression", "-s"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void wrongSuite() { - ECTesterReader.main(new String[]{"-t", "wrong", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "wrong", "-s", "-y"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void degenerateSuite() { - ECTesterReader.main(new String[]{"-t", "degenerate", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "degenerate", "-s", "-y"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void cofactorSuite() { - ECTesterReader.main(new String[]{"-t", "cofactor", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "cofactor", "-s", "-y"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void compositeSuite() { - ECTesterReader.main(new String[]{"-t", "composite", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "composite", "-s", "-y"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void invalidSuite() { - ECTesterReader.main(new String[]{"-t", "invalid", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "invalid", "-s", "-y"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void edgeCasesSuite() { - ECTesterReader.main(new String[]{"-t", "edge-cases", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "edge-cases", "-s", "-y"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void signatureSuite() { - ECTesterReader.main(new String[]{"-t", "signature", "-s"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "signature", "-s"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void twistSuite() { - ECTesterReader.main(new String[]{"-t", "twist", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "twist", "-s", "-y"})); } @Test + @XFail(value = "JCardSim sometimes times-out.") public void miscellaneousSuite() { - ECTesterReader.main(new String[]{"-t", "miscellaneous", "-s", "-y"}); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> ECTesterReader.main(new String[]{"-t", "miscellaneous", "-s", "-y"})); } } diff --git a/reader/src/test/java/cz/crcs/ectester/reader/XFail.java b/reader/src/test/java/cz/crcs/ectester/reader/XFail.java new file mode 100644 index 0000000..f42f530 --- /dev/null +++ b/reader/src/test/java/cz/crcs/ectester/reader/XFail.java @@ -0,0 +1,86 @@ +/* + * Copyright 2016-2023 the original author or authors. + * Taken from https://github.com/junit-pioneer/junit-pioneer/blob/98cef28462c8b7ab66231cc5b7e8daef3b329f67/src/main/java/org/junitpioneer/jupiter/ExpectedToFail.java + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ + +package cz.crcs.ectester.reader; + +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * {@code @ExpectedToFail} is a JUnit Jupiter extension to mark test methods as temporarily + * 'expected to fail'. Such test methods will still be executed but when they result in a test + * failure or error the test will be aborted. However, if the test method unexpectedly executes + * successfully, it is marked as failure to let the developer know that the test is now + * successful and that the {@code @ExpectedToFail} annotation can be removed. + * + *
The big difference compared to JUnit's {@link org.junit.jupiter.api.Disabled @Disabled} + * annotation is that the developer is informed as soon as a test is successful again. + * This helps to avoid creating duplicate tests by accident and counteracts the accumulation + * of disabled tests over time.
+ * + *Further, the {@link #withExceptions()} attribute can be used to restrict the extension's behavior + * to specific exceptions. That is, only if the test method ends up throwing one of the specified exceptions + * will the test be aborted. This can, for example, be used when the production code temporarily throws + * an {@link UnsupportedOperationException} because some feature has not been implemented yet, but the + * test method is already implemented and should not fail on a failing assertion. + *
+ * + *The annotation can only be used on methods and as meta-annotation on other annotation types. + * Similar to {@code @Disabled}, it has to be used in addition to a "testable" annotation, such + * as {@link org.junit.jupiter.api.Test @Test}. Otherwise the annotation has no effect.
+ * + *Important: This annotation is not intended as a way to mark test methods + * which intentionally cause exceptions. Such test methods should use + * {@link org.junit.jupiter.api.Assertions#assertThrows(Class, org.junit.jupiter.api.function.Executable) assertThrows} + * or similar means to explicitly test for a specific exception class being thrown by a + * specific action.
+ * + *For more details and examples, see
+ * the documentation on @ExpectedToFail.
This method is used for exceptions that abort test execution and should + * have higher precedence than aborted exceptions thrown by this extension.
+ */ + private static boolean shouldPreserveException(Throwable t) { + // Note: Ideally would use the same logic JUnit uses to determine if exception is aborting + // execution, see its class OpenTest4JAndJUnit4AwareThrowableCollector + return t instanceof TestAbortedException; + } + + private static XFail getExpectedToFailAnnotation(ExtensionContext context) { + return AnnotationSupport + .findAnnotation(context.getRequiredTestMethod(), XFail.class) + .orElseThrow(() -> new IllegalStateException("@XFail is missing.")); + + } + +} \ No newline at end of file -- cgit v1.2.3-70-g09d2