Skip to content

Commit

Permalink
Improved TestStep creation performance (#2667)
Browse files Browse the repository at this point in the history
Refactored `TestAbortedExceptions` `Predicate` creation.

Performance gain (`Predicate` creation is about 50'000 times faster).

Fixes #2666
  • Loading branch information
jkronegg authored Dec 22, 2022
1 parent 285c9d3 commit f53abb6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- [Core] Improve test step creation performance ([#2666](https://github.com/cucumber/cucumber-jvm/issues/2666), Julien Kronegg)

## [7.10.1] - 2022-12-16
### Fixed
Expand All @@ -30,7 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- [Core] Emit exceptions on failure to handle test run finished events ([#2651](https://github.com/cucumber/cucumber-jvm/issues/2651) M.P. Korstanje)
- [Spring] @MockBean annotation not working with JUnit5 ([#2654](https://github.com/cucumber/cucumber-jvm/pull/2654) Alexander Kirilov, M.P. Korstanje)
- [Core] Improve expression creation performance ([cucumber-expressions/#187](https://github.com/cucumber/cucumber-expressions/pull/187), [cucumber-expressions/#189](https://github.com/cucumber/cucumber-expressions/pull/189), jkrongegg)
- [Core] Improve expression creation performance ([cucumber-expressions/#187](https://github.com/cucumber/cucumber-expressions/pull/187), [cucumber-expressions/#189](https://github.com/cucumber/cucumber-expressions/pull/189), Julien Kronegg)

## [7.9.0] - 2022-11-01
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.util.Arrays;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static io.cucumber.core.exception.UnrecoverableExceptions.rethrowIfUnrecoverable;

Expand All @@ -26,20 +25,19 @@ final class TestAbortedExceptions {

static Predicate<Throwable> createIsTestAbortedExceptionPredicate() {
ClassLoader defaultClassLoader = ClassLoaders.getDefaultClassLoader();
return Arrays.stream(TEST_ABORTED_EXCEPTIONS)
.flatMap(s -> {
return throwable -> Arrays.stream(TEST_ABORTED_EXCEPTIONS)
.anyMatch(s -> {
try {
Class<?> aClass = defaultClassLoader.loadClass(s);
return Stream.of(aClass);
return aClass.isInstance(throwable);
} catch (Throwable t) {
rethrowIfUnrecoverable(t);
log.debug(t,
() -> "Failed to load class" + s + ": will not be supported for aborted executions.");
() -> String.format(
"Failed to load class %s: will not be supported for aborted executions.", s));
}
return Stream.empty();
})
.map(throwable -> (Predicate<Throwable>) throwable::isInstance)
.reduce(__ -> false, Predicate::or);
return false;
});
}

private TestAbortedExceptions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.cucumber.core.runner;

import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;

import java.util.function.Predicate;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestAbortedExceptionsTest {
static class TestAbortedExceptionSubClass extends TestAbortedException {
}

@Test
void testPredicate() {
Predicate<Throwable> isTestAbortedExceptionPredicate = TestAbortedExceptions
.createIsTestAbortedExceptionPredicate();
assertFalse(isTestAbortedExceptionPredicate.test(new RuntimeException()));
assertTrue(isTestAbortedExceptionPredicate.test(new TestAbortedException()));
assertTrue(isTestAbortedExceptionPredicate.test(new TestAbortedExceptionSubClass()));
}

}

0 comments on commit f53abb6

Please sign in to comment.