Skip to content

Commit 59396e1

Browse files
igor-suhorukovsbrannen
authored andcommitted
Throw exception from user code in SpringFailOnTimeout even if a timeout occurs
Issue: SPR-16717
1 parent c4ef47b commit 59396e1

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ public void evaluate() throws Throwable {
8888
}
8989
else {
9090
long startTime = System.currentTimeMillis();
91-
try {
92-
this.next.evaluate();
93-
}
94-
finally {
95-
long elapsed = System.currentTimeMillis() - startTime;
96-
if (elapsed > this.timeout) {
97-
throw new TimeoutException(
98-
String.format("Test took %s ms; limit was %s ms.", elapsed, this.timeout));
99-
}
91+
this.next.evaluate();
92+
long elapsed = System.currentTimeMillis() - startTime;
93+
if (elapsed > this.timeout) {
94+
throw new TimeoutException(
95+
String.format("Test took %s ms; limit was %s ms.", elapsed, this.timeout));
10096
}
10197
}
10298
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.springframework.test.context.junit4.spr16716;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.model.Statement;
6+
import org.mockito.Mock;
7+
import org.mockito.junit.MockitoJUnitRunner;
8+
import org.mockito.stubbing.Answer;
9+
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
10+
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.TimeoutException;
13+
14+
import static org.mockito.Mockito.doAnswer;
15+
import static org.mockito.Mockito.doThrow;
16+
17+
/**
18+
* Validate SpringFailOnTimeout contract
19+
* <a href="https://jira.spring.io/browse/SPR-16716" target="_blank">SPR-16716</a>.
20+
*
21+
* @author Igor Suhorukov
22+
* @since 5.0.6
23+
*/
24+
@RunWith(MockitoJUnitRunner.class)
25+
public class SpringFailOnTimeoutExceptionTest {
26+
27+
@Mock
28+
private Statement statement;
29+
30+
@Test(expected = IllegalArgumentException.class)
31+
public void validateOriginalExceptionFromEvaluateMethod() throws Throwable {
32+
IllegalArgumentException expectedException = new IllegalArgumentException();
33+
doThrow(expectedException).when(statement).evaluate();
34+
new SpringFailOnTimeout(statement, TimeUnit.SECONDS.toMillis(1)).evaluate();
35+
}
36+
37+
@Test(expected = TimeoutException.class)
38+
public void validateTimeoutException() throws Throwable {
39+
doAnswer((Answer<Void>) invocation -> {
40+
TimeUnit.MILLISECONDS.sleep(50);
41+
return null;
42+
}).when(statement).evaluate();
43+
new SpringFailOnTimeout(statement, TimeUnit.MILLISECONDS.toMillis(1)).evaluate();
44+
}
45+
}

0 commit comments

Comments
 (0)