Fix NPE when circuit break attriute is not set
This commit is contained in:
@@ -152,7 +152,7 @@ public class CircuitBreakerRetryPolicy implements RetryPolicy {
|
||||
retryable = this.policy.canRetry(this.context);
|
||||
}
|
||||
else if (time < this.openWindow) {
|
||||
if ((Boolean) getAttribute(CIRCUIT_OPEN) == false) {
|
||||
if (!hasAttribute(CIRCUIT_OPEN) || (Boolean) getAttribute(CIRCUIT_OPEN) == false) {
|
||||
logger.trace("Opening circuit");
|
||||
setAttribute(CIRCUIT_OPEN, true);
|
||||
}
|
||||
|
||||
@@ -147,6 +147,17 @@ public class CircuitBreakerRetryTemplateTests {
|
||||
assertEquals(RESULT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircuitOpensWhenRetryPolicyFirstTimeAttributeCircuitOpenNull() throws Throwable {
|
||||
MockNeverRetryPolicy mockNeverRetryPolicy = new MockNeverRetryPolicy();
|
||||
this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(mockNeverRetryPolicy));
|
||||
this.callback.setAttemptsBeforeSuccess(10);
|
||||
Object result = this.retryTemplate.execute(this.callback, this.recovery, this.state);
|
||||
assertEquals(RECOVERED, result);
|
||||
result = this.retryTemplate.execute(this.callback, this.recovery, this.state);
|
||||
assertEquals(RECOVERED, result);
|
||||
}
|
||||
|
||||
protected static class MockRetryCallback implements RetryCallback<Object, Exception> {
|
||||
|
||||
private int attemptsBeforeSuccess;
|
||||
@@ -185,4 +196,9 @@ public class CircuitBreakerRetryTemplateTests {
|
||||
|
||||
}
|
||||
|
||||
protected class MockNeverRetryPolicy extends NeverRetryPolicy {
|
||||
public boolean canRetry(RetryContext context) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user