Skip to content

Commit

Permalink
Fix @RepeatUntilFailure extension with unknown iteration count
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Nov 5, 2024
1 parent d6f8372 commit 766f8dc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ include::include.adoc[]
* Fix mocking issue with the ByteBuddy MockMaker when using multiple classloaders in Java 21 spockIssue:2017[]
* Fix mocking of final classes via `@SpringBean` and `@SpringSpy` spockIssue:1960[]
* Size of data providers is no longer calculated multiple times but only once
* Fix exception when using `@RepeatUntilFailure` with a data provider with unknown iteration amount. spockPull:2031[]

== 2.4-M4 (2024-03-21)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.spockframework.runtime.extension.builtin;

import org.spockframework.runtime.DataIteratorFactory;
import org.spockframework.runtime.IDataIterator;
import org.spockframework.runtime.extension.*;
import org.spockframework.runtime.model.*;
Expand All @@ -8,6 +9,8 @@
import java.util.*;
import java.util.concurrent.ExecutionException;

import static org.spockframework.runtime.DataIteratorFactory.UNKNOWN_ITERATIONS;

public class RepeatUntilFailureExtension implements IAnnotationDrivenExtension<RepeatUntilFailure> {
@Override
public void visitFeatureAnnotation(RepeatUntilFailure annotation, FeatureInfo feature) {
Expand All @@ -32,8 +35,8 @@ public RepeatUntilFailureDataDriver(int maxAttempts) {
@Override
public void runIterations(IDataIterator dataIterator, IIterationRunner iterationRunner, List<ParameterInfo> parameters) {
int estimatedNumIterations = dataIterator.getEstimatedNumIterations();
int maxIterations = estimatedNumIterations * maxAttempts;
List<Object[]> arguments = new ArrayList<>(estimatedNumIterations);
int maxIterations = estimatedNumIterations == UNKNOWN_ITERATIONS ? UNKNOWN_ITERATIONS : (estimatedNumIterations * maxAttempts);
List<Object[]> arguments = estimatedNumIterations == UNKNOWN_ITERATIONS ? new ArrayList<>() : new ArrayList<>(estimatedNumIterations);
dataIterator.forEachRemaining(arguments::add);
for (int attempt = 0; attempt < maxAttempts; attempt++) {
for (Object[] args : arguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ class RepeatUntilFailureExtensionSpec extends EmbeddedSpecification {
result.testsSkippedCount == 2
}

def "repeats with unknown iteration count"() {
given:
runner.throwFailure = false

when:
def result = runner.runSpecBody """
@Shared
int count = 0
@RepeatUntilFailure
def "test"() {
expect:
++count < 3
where:
x << [1].iterator()
}
"""

then:
result.testsStartedCount == 1 + 3
result.testsSucceededCount == 1 + 2
result.testsFailedCount == 1
}

def "ignores aborted tests"() {
when:
Expand Down

0 comments on commit 766f8dc

Please sign in to comment.