Skip to content

Commit bd3a168

Browse files
jsalinaspolojordi9
andcommitted
Throw an exception when during polling conditions there is an exception invoking the closure.
Co-authored-by: jordi9 <[email protected]>
1 parent 761f88e commit bd3a168

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

Diff for: spock-core/src/main/java/spock/lang/Retry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ enum Mode {
103103
ITERATION,
104104

105105
/**
106-
* Retry the the feature together with the setup and cleanup methods.
106+
* Retry the feature together with the setup and cleanup methods.
107107
*/
108108
SETUP_FEATURE_CLEANUP
109109
}

Diff for: spock-core/src/main/java/spock/util/concurrent/PollingConditions.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.spockframework.lang.ConditionBlock;
2020
import org.spockframework.runtime.GroovyRuntimeUtil;
2121
import org.spockframework.runtime.SpockTimeoutError;
22+
import org.spockframework.runtime.UnallowedExceptionThrownError;
2223
import org.spockframework.util.Beta;
2324

2425
/**
@@ -47,6 +48,7 @@ public class PollingConditions {
4748
private double initialDelay = 0;
4849
private double delay = 0.1;
4950
private double factor = 1.0;
51+
private boolean swallow = false;
5052

5153
/**
5254
* Returns the timeout (in seconds) until which the conditions have to be satisfied.
@@ -126,7 +128,6 @@ public void setFactor(double factor) {
126128
* Repeatedly evaluates the specified conditions until they are satisfied or the timeout has elapsed.
127129
*
128130
* @param conditions the conditions to evaluate
129-
*
130131
* @throws InterruptedException if evaluation is interrupted
131132
*/
132133
@ConditionBlock
@@ -138,11 +139,10 @@ public void eventually(Closure<?> conditions) throws InterruptedException {
138139
* Repeatedly evaluates the specified conditions until they are satisfied or the specified timeout (in seconds) has elapsed.
139140
*
140141
* @param conditions the conditions to evaluate
141-
*
142142
* @throws InterruptedException if evaluation is interrupted
143143
*/
144144
@ConditionBlock
145-
public void within(double seconds, Closure<?> conditions) throws InterruptedException {
145+
public void within(double seconds, Closure<?> conditions) throws InterruptedException {
146146
long timeoutMillis = toMillis(seconds);
147147
long start = System.currentTimeMillis();
148148
long lastAttempt = 0;
@@ -151,13 +151,16 @@ public void within(double seconds, Closure<?> conditions) throws InterruptedExce
151151
long currDelay = toMillis(delay);
152152
int attempts = 0;
153153

154-
while(true) {
154+
while (true) {
155155
try {
156156
attempts++;
157157
lastAttempt = System.currentTimeMillis();
158158
GroovyRuntimeUtil.invokeClosure(conditions);
159159
return;
160160
} catch (Throwable e) {
161+
if (swallow && !(e instanceof AssertionError)) {
162+
throw new UnallowedExceptionThrownError(e.getClass(), e);
163+
}
161164
long elapsedTime = lastAttempt - start;
162165
if (elapsedTime >= timeoutMillis) {
163166
String msg = String.format("Condition not satisfied after %1.2f seconds and %d attempts", elapsedTime / 1000d, attempts);

Diff for: spock-specs/specs.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ configurations {
1010
}
1111

1212
dependencies {
13-
testCompile project(":spock-core")
14-
testCompile project(":spock-junit4")
13+
testImplementation project(":spock-core")
14+
testImplementation project(":spock-junit4")
1515

1616
testRuntime libs.asm
1717
testRuntime libs.bytebuddy
@@ -30,7 +30,7 @@ targetCompatibility = javaVersion
3030
// necessary to make @NotYetImplemented transform work (transform that ships
3131
// with Groovy and statically references third-party class junit.framwork.AssertionFailedError)
3232
tasks.withType(GroovyCompile) {
33-
groovyClasspath += configurations.junit
33+
groovyClasspath += configurations.junit
3434
}
3535

3636
ext.spockLogFileDir = file("$buildDir/spock/logFiles")

Diff for: spock-specs/src/test/groovy/spock/util/concurrent/PollingConditionsSpec.groovy

+23-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package spock.util.concurrent
1616

1717
import org.spockframework.runtime.ConditionNotSatisfiedError
1818
import org.spockframework.runtime.SpockTimeoutError
19+
import org.spockframework.runtime.UnallowedExceptionThrownError
1920
import spock.lang.Issue
2021
import spock.lang.Specification
2122

@@ -126,12 +127,32 @@ class PollingConditionsSpec extends Specification {
126127
then:
127128
condition.eventually {
128129
try {
129-
sleep 200;
130-
assert secondAttempt;
130+
sleep 200
131+
assert secondAttempt
131132
} finally {
132133
secondAttempt = true
133134
}
134135
}
135136
}
137+
138+
def "throw exception when unexpected exceptions while polling"() {
139+
given:
140+
def iteration = 0
141+
142+
when:
143+
new PollingConditions(swallow: true).eventually {
144+
try {
145+
if (iteration < 2) {
146+
throw new IllegalStateException("An exception is thrown")
147+
}
148+
assert true
149+
} finally {
150+
iteration++
151+
}
152+
}
153+
then:
154+
def ex = thrown(UnallowedExceptionThrownError)
155+
ex.message == "Expected no exception of type 'java.lang.IllegalStateException' to be thrown, but got it nevertheless"
156+
}
136157
}
137158

0 commit comments

Comments
 (0)