Fix NPE when circuit break attriute is not set

This commit is contained in:
Oscar Arellano
2020-06-09 12:19:37 -05:00
committed by GitHub
parent f3ccdb298a
commit cfabf26c41
2 changed files with 17 additions and 1 deletions

View File

@@ -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);
}

View File

@@ -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;
}
}
}