Skip to content

Commit 642ca0b

Browse files
Merge pull request #14 from agdula/master
add beforeRetryMethod parameter to RetryOnFailure
2 parents bab7491 + a1e5899 commit 642ca0b

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@ class ExampleSpec extends Specification {
3535
It can be applied to a super class:
3636
```groovy
3737
38-
@RetryOnFailure(times=4)
38+
@RetryOnFailure
3939
class BaseSpec extends Specification {
40-
40+
void beforeRetryAfterFailure(){
41+
// will before every repeat of the test ( if child class will not override it )
42+
}
4143
}
4244
4345
class ChildSpec extends BaseSpec {
4446
4547
void 'do something flaky'() {
4648
// will try to run four times before failing
4749
}
50+
51+
void beforeRetryAfterFailure(){
52+
// will before every repeat of the test
53+
}
4854
}
4955
```
5056

@@ -54,3 +60,6 @@ The `times` argument is optional; with none specified, the runner will attempt t
5460
The latest release of this extension works with Spock 1.0 and is available on [bintray](https://bintray.com/anotherchrisberry/spock-retry/spock-retry).
5561

5662
If you are using Spock 0.7, you'll need to grab the code from the [initial commit](https://github.com/anotherchrisberry/spock-retry/tree/e3135038fb796b2c44efda3adc29970dc40b09d5). Sorry, I'm not sure the best way to go about this. You could grab the three files in [this directory](https://github.com/anotherchrisberry/spock-retry/tree/e3135038fb796b2c44efda3adc29970dc40b09d5/src/main/groovy/com/anotherchrisberry/spock/extensions/retry) and include them in your project.
63+
64+
65+

src/main/groovy/com/anotherchrisberry/spock/extensions/retry/RetryInterceptor.groovy

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class RetryInterceptor implements IMethodInterceptor {
1111

1212
static Logger LOG = LoggerFactory.getLogger(RetryInterceptor.class);
1313

14+
private static final String BEFORE_RETRY_METHOD_NAME = "beforeRetry"
15+
1416
Integer retryMax
1517

1618
RetryInterceptor(int retryMax) {
@@ -49,6 +51,15 @@ class RetryInterceptor implements IMethodInterceptor {
4951
LOG.info("Retry caught failure ${attempts + 1} / ${retryMax + 1} while setting up", t2)
5052
}
5153
}
54+
55+
if(invocation.target.respondsTo(BEFORE_RETRY_METHOD_NAME)) {
56+
try {
57+
invocation.target."$BEFORE_RETRY_METHOD_NAME"()
58+
} catch (Throwable t2) {
59+
// increment counter, since this is the start of the re-run
60+
LOG.info("Retry caught failure when invoking $BEFORE_RETRY_METHOD_NAME ", t2)
61+
}
62+
}
5263
}
5364
}
5465
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.anotherchrisberry.spock.extensions.retry
2+
3+
import spock.lang.Specification
4+
5+
class RetryOnFailureBeforeRetryMethod extends Specification{
6+
7+
static Integer classLevelTries = 0
8+
static Integer onRetry = 0
9+
10+
@RetryOnFailure(times=3)
11+
void 'expect beforeRetry method is called once'() {
12+
13+
when:
14+
if (classLevelTries < 1) {
15+
classLevelTries ++
16+
throw new RuntimeException('no problem')
17+
}
18+
19+
then:
20+
onRetry == 1
21+
}
22+
23+
void beforeRetry(){
24+
onRetry ++
25+
}
26+
}

0 commit comments

Comments
 (0)