-
Notifications
You must be signed in to change notification settings - Fork 342
Avoid final field modifications in Cucumber and Spock instrumentations #11851
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
Open
daniel-mohedano
wants to merge
1
commit into
daniel.mohedano/fix-cucumber-retries
Choose a base branch
from
daniel.mohedano/jep-500-cucumber-spock
base: daniel.mohedano/fix-cucumber-retries
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
....4/src/main/java/datadog/trace/instrumentation/junit5/CucumberRetryDescriptorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package datadog.trace.instrumentation.junit5; | ||
|
|
||
| import datadog.trace.instrumentation.junit5.execution.RetryDescriptorFactory; | ||
| import datadog.trace.util.MethodHandles; | ||
| import io.cucumber.core.gherkin.Pickle; | ||
| import java.lang.invoke.MethodHandle; | ||
| import java.util.function.UnaryOperator; | ||
| import org.junit.platform.commons.util.ClassLoaderUtils; | ||
| import org.junit.platform.engine.ConfigurationParameters; | ||
| import org.junit.platform.engine.TestDescriptor; | ||
| import org.junit.platform.engine.TestSource; | ||
| import org.junit.platform.engine.UniqueId; | ||
|
|
||
| /** | ||
| * Reconstructs the Cucumber retry descriptor ({@code PickleDescriptor}) through its own constructor | ||
| * with a transformed unique id to avoid final-field mutations (JEP 500). | ||
| */ | ||
| public final class CucumberRetryDescriptorFactory implements RetryDescriptorFactory { | ||
|
|
||
| private static final MethodHandles METHOD_HANDLES = | ||
| new MethodHandles(ClassLoaderUtils.getDefaultClassLoader()); | ||
|
|
||
| private static final String PACKAGE = "io.cucumber.junit.platform.engine."; | ||
|
|
||
| private static final MethodHandle CONSTRUCTOR_7_24 = | ||
| METHOD_HANDLES.constructor( | ||
| PACKAGE + "CucumberTestDescriptor$PickleDescriptor", | ||
| JUnitPlatformUtils.loadClass(PACKAGE + "CucumberConfiguration"), | ||
| UniqueId.class, | ||
| String.class, | ||
| TestSource.class, | ||
| Pickle.class); | ||
| private static final MethodHandle CONSTRUCTOR_7_7 = | ||
| METHOD_HANDLES.constructor( | ||
| PACKAGE + "NodeDescriptor$PickleDescriptor", | ||
| ConfigurationParameters.class, | ||
| UniqueId.class, | ||
| String.class, | ||
| TestSource.class, | ||
| Pickle.class); | ||
| private static final MethodHandle CONSTRUCTOR_6_0 = | ||
| METHOD_HANDLES.constructor( | ||
| PACKAGE + "PickleDescriptor", | ||
| ConfigurationParameters.class, | ||
| UniqueId.class, | ||
| String.class, | ||
| TestSource.class, | ||
| Pickle.class); | ||
| private static final MethodHandle CONSTRUCTOR_5_4 = | ||
| METHOD_HANDLES.constructor( | ||
| PACKAGE + "PickleDescriptor", | ||
| UniqueId.class, | ||
| String.class, | ||
| TestSource.class, | ||
| Pickle.class); | ||
|
|
||
| // 7.24+ stores the configuration on the descriptor, read it back for the reconstruction. | ||
| private static final MethodHandle CONFIGURATION_GETTER = | ||
| METHOD_HANDLES.privateFieldGetter( | ||
| PACKAGE + "CucumberTestDescriptor$PickleDescriptor", "configuration"); | ||
|
|
||
| // The Pickle field was renamed pickleEvent -> pickle; resolved lazily off the descriptor's class. | ||
| private volatile MethodHandle pickleGetter; | ||
|
|
||
| @Override | ||
| public TestDescriptor copy(TestDescriptor original, UnaryOperator<UniqueId> idTransform) { | ||
| if (!"PickleDescriptor".equals(original.getClass().getSimpleName())) { | ||
| return null; // only the leaf scenario descriptor is retried; containers are filtered earlier | ||
| } | ||
| Object pickle = readPickle(original); | ||
| if (pickle == null) { | ||
| return null; | ||
| } | ||
| UniqueId newId = idTransform.apply(original.getUniqueId()); | ||
| String name = original.getDisplayName(); | ||
| TestSource source = original.getSource().orElse(null); | ||
|
|
||
| if (CONSTRUCTOR_7_24 != null) { | ||
| Object configuration = METHOD_HANDLES.invoke(CONFIGURATION_GETTER, original); | ||
| return configuration == null | ||
| ? null | ||
| : METHOD_HANDLES.invoke(CONSTRUCTOR_7_24, configuration, newId, name, source, pickle); | ||
| } | ||
| if (CONSTRUCTOR_7_7 != null) { | ||
| return METHOD_HANDLES.invoke( | ||
| CONSTRUCTOR_7_7, new EmptyConfigurationParameters(), newId, name, source, pickle); | ||
| } | ||
| if (CONSTRUCTOR_6_0 != null) { | ||
| return METHOD_HANDLES.invoke( | ||
| CONSTRUCTOR_6_0, new EmptyConfigurationParameters(), newId, name, source, pickle); | ||
| } | ||
| if (CONSTRUCTOR_5_4 != null) { | ||
| return METHOD_HANDLES.invoke(CONSTRUCTOR_5_4, newId, name, source, pickle); | ||
| } | ||
| return null; // unknown cucumber version -> fall back to the generic clone | ||
| } | ||
|
|
||
| private Object readPickle(TestDescriptor descriptor) { | ||
| MethodHandle getter = pickleGetter; | ||
| if (getter == null) { | ||
| getter = METHOD_HANDLES.privateFieldGetter(descriptor.getClass(), "pickle"); | ||
| if (getter == null) { | ||
| getter = METHOD_HANDLES.privateFieldGetter(descriptor.getClass(), "pickleEvent"); | ||
| } | ||
| pickleGetter = getter; | ||
| } | ||
| return getter != null ? METHOD_HANDLES.invoke(getter, descriptor) : null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...-5.4/src/main/java/datadog/trace/instrumentation/junit5/EmptyConfigurationParameters.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package datadog.trace.instrumentation.junit5; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import org.junit.platform.engine.ConfigurationParameters; | ||
|
|
||
| /** | ||
| * NO-OP {@link ConfigurationParameters}, used when reconstructing a Cucumber retry descriptor for | ||
| * engine versions (6.0–7.23) whose {@code PickleDescriptor} constructor consumes the configuration | ||
| * to compute exclusive resources but does not store it (so it cannot be read back). | ||
| */ | ||
| @SuppressWarnings("deprecation") // ConfigurationParameters#size() is deprecated in newer platforms | ||
| public final class EmptyConfigurationParameters implements ConfigurationParameters { | ||
|
|
||
| @Override | ||
| public Optional<String> get(String key) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Boolean> getBoolean(String key) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return 0; | ||
| } | ||
|
|
||
| public Set<String> keySet() { | ||
| return Collections.emptySet(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...k-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockRetryDescriptorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package datadog.trace.instrumentation.junit5; | ||
|
|
||
| import datadog.trace.instrumentation.junit5.execution.RetryDescriptorFactory; | ||
| import datadog.trace.util.MethodHandles; | ||
| import java.lang.invoke.MethodHandle; | ||
| import java.util.function.UnaryOperator; | ||
| import org.junit.platform.commons.util.ClassLoaderUtils; | ||
| import org.junit.platform.engine.TestDescriptor; | ||
| import org.junit.platform.engine.UniqueId; | ||
| import org.spockframework.runtime.IterationNode; | ||
| import org.spockframework.runtime.SimpleFeatureNode; | ||
| import org.spockframework.runtime.model.FeatureInfo; | ||
| import org.spockframework.runtime.model.IterationInfo; | ||
| import spock.config.RunnerConfiguration; | ||
|
|
||
| /** | ||
| * Reconstructs the Spock retry descriptor through its own constructor (with a transformed unique | ||
| * id) instead of cloning + mutating the final {@code uniqueId} field to avoid JEP 500 warnings. | ||
| * | ||
| * <p>The leaf descriptor reaching the retry advice is either a {@link SimpleFeatureNode} | ||
| * (non-parametric feature, on every supported tag) or an {@link IterationNode} (a parametric {@code | ||
| * where:} row). | ||
| */ | ||
| public final class SpockRetryDescriptorFactory implements RetryDescriptorFactory { | ||
|
|
||
| private static final MethodHandles METHOD_HANDLES = | ||
| new MethodHandles(ClassLoaderUtils.getDefaultClassLoader()); | ||
|
|
||
| private static final MethodHandle SIMPLE_FEATURE_NODE_CONSTRUCTOR = | ||
| METHOD_HANDLES.constructor( | ||
| SimpleFeatureNode.class, | ||
| UniqueId.class, | ||
| RunnerConfiguration.class, | ||
| FeatureInfo.class, | ||
| IterationNode.class); | ||
|
|
||
| private static final MethodHandle ITERATION_NODE_CONSTRUCTOR = | ||
| METHOD_HANDLES.constructor( | ||
| IterationNode.class, UniqueId.class, RunnerConfiguration.class, IterationInfo.class); | ||
|
|
||
| private static final MethodHandle SIMPLE_FEATURE_NODE_DELEGATE = | ||
| METHOD_HANDLES.privateFieldGetter(SimpleFeatureNode.class, "delegate"); | ||
|
|
||
| private static final MethodHandle ITERATION_NODE_INFO = | ||
| METHOD_HANDLES.privateFieldGetter(IterationNode.class, "iterationInfo"); | ||
|
|
||
| @Override | ||
| public TestDescriptor copy(TestDescriptor original, UnaryOperator<UniqueId> idTransform) { | ||
| if (original instanceof SimpleFeatureNode) { | ||
| return copySimpleFeatureNode((SimpleFeatureNode) original, idTransform); | ||
| } | ||
| if (original instanceof IterationNode) { | ||
| return copyIterationNode((IterationNode) original, idTransform); | ||
| } | ||
| return null; // unknown Spock node type -> fall back to the generic clone | ||
| } | ||
|
|
||
| private static TestDescriptor copySimpleFeatureNode( | ||
| SimpleFeatureNode original, UnaryOperator<UniqueId> idTransform) { | ||
| if (SIMPLE_FEATURE_NODE_CONSTRUCTOR == null || ITERATION_NODE_CONSTRUCTOR == null) { | ||
| return null; | ||
| } | ||
| RunnerConfiguration configuration = original.getConfiguration(); | ||
| FeatureInfo featureInfo = original.getNodeInfo(); | ||
| IterationNode originalDelegate = METHOD_HANDLES.invoke(SIMPLE_FEATURE_NODE_DELEGATE, original); | ||
| if (originalDelegate == null) { | ||
| return null; | ||
| } | ||
| IterationInfo iterationInfo = METHOD_HANDLES.invoke(ITERATION_NODE_INFO, originalDelegate); | ||
|
|
||
| UniqueId newId = idTransform.apply(original.getUniqueId()); | ||
| // keep the delegate a proper child of the copy and distinct across attempts | ||
| UniqueId.Segment delegateSegment = originalDelegate.getUniqueId().getLastSegment(); | ||
| UniqueId newDelegateId = newId.append(delegateSegment.getType(), delegateSegment.getValue()); | ||
|
|
||
| IterationNode delegate = | ||
| METHOD_HANDLES.invoke( | ||
| ITERATION_NODE_CONSTRUCTOR, newDelegateId, configuration, iterationInfo); | ||
| if (delegate == null) { | ||
| return null; | ||
| } | ||
| return METHOD_HANDLES.invoke( | ||
| SIMPLE_FEATURE_NODE_CONSTRUCTOR, newId, configuration, featureInfo, delegate); | ||
| } | ||
|
|
||
| private static TestDescriptor copyIterationNode( | ||
| IterationNode original, UnaryOperator<UniqueId> idTransform) { | ||
| if (ITERATION_NODE_CONSTRUCTOR == null) { | ||
| return null; | ||
| } | ||
| RunnerConfiguration configuration = original.getConfiguration(); | ||
| IterationInfo iterationInfo = METHOD_HANDLES.invoke(ITERATION_NODE_INFO, original); | ||
| if (iterationInfo == null) { | ||
| return null; | ||
| } | ||
| UniqueId newId = idTransform.apply(original.getUniqueId()); | ||
| return METHOD_HANDLES.invoke(ITERATION_NODE_CONSTRUCTOR, newId, configuration, iterationInfo); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Cucumber 6.0–7.23,
PickleDescriptorderives its execution mode and tag-based exclusive resources from theConfigurationParameterspassed to this constructor (for example, the 7.23 source computes both fromparameters: https://github.com/cucumber/cucumber-jvm/blob/v7.23.0/cucumber-junit-platform-engine/src/main/java/io/cucumber/junit/platform/engine/NodeDescriptor.java). PassingEmptyConfigurationParametershere means retries for suites that configure non-default feature execution mode orcucumber.execution.exclusive-resources.*are rebuilt with default concurrent mode and no resource locks, while the previous shallow clone preserved those fields; those retries can therefore be scheduled differently from the original scenario.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the execution mode and exclusive resources are read by the
HierarchicalTestExecutorServicewhen planning on how to run a test task. But the retry descriptors are only ever executed by us manually in the execution instrumentation throughcurrentTask.execute()without scheduling, so the fields are never used.