Skip to content

Commit

Permalink
Merge pull request #770 from mattrjacobs/circuit-forced-open-by-prope…
Browse files Browse the repository at this point in the history
…rties-queried-properly

Circuit forced open by properties queried properly
  • Loading branch information
mattrjacobs committed Apr 23, 2015
2 parents 2693292 + 0afc56c commit 5b4b449
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1832,11 +1832,22 @@ protected String getLogMessagePrefix() {
/**
* Whether the 'circuit-breaker' is open meaning that <code>execute()</code> will immediately return
* the <code>getFallback()</code> response and not attempt a HystrixCommand execution.
*
*
* 4 columns are ForcedOpen | ForcedClosed | CircuitBreaker open due to health ||| Expected Result
*
* T | T | T ||| OPEN (true)
* T | T | F ||| OPEN (true)
* T | F | T ||| OPEN (true)
* T | F | F ||| OPEN (true)
* F | T | T ||| CLOSED (false)
* F | T | F ||| CLOSED (false)
* F | F | T ||| OPEN (true)
* F | F | F ||| CLOSED (false)
*
* @return boolean
*/
public boolean isCircuitBreakerOpen() {
return circuitBreaker.isOpen();
return properties.circuitBreakerForceOpen().get() || (!properties.circuitBreakerForceClosed().get() && circuitBreaker.isOpen());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,53 @@ public void onNext(Boolean args) {
assertTrue(subscribeThread.get().getName().equals(mainThreadName));
}

/**
* Tests that the circuit-breaker reports itself as "OPEN" if set as forced-open
*/
@Test
public void testCircuitBreakerReportsOpenIfForcedOpen() {
HystrixCommand<Boolean> cmd = new HystrixCommand<Boolean>(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")).andCommandPropertiesDefaults(new HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(true))) {

@Override
protected Boolean run() throws Exception {
return true;
}

@Override
protected Boolean getFallback() {
return false;
}
};

assertFalse(cmd.execute()); //fallback should fire
System.out.println("RESULT : " + cmd.getExecutionEvents());
assertTrue(cmd.isCircuitBreakerOpen());
}

/**
* Tests that the circuit-breaker reports itself as "CLOSED" if set as forced-closed
*/
@Test
public void testCircuitBreakerReportsClosedIfForcedClosed() {
HystrixCommand<Boolean> cmd = new HystrixCommand<Boolean>(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")).andCommandPropertiesDefaults(
new HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(false).withCircuitBreakerForceClosed(true))) {

@Override
protected Boolean run() throws Exception {
return true;
}

@Override
protected Boolean getFallback() {
return false;
}
};

assertTrue(cmd.execute());
System.out.println("RESULT : " + cmd.getExecutionEvents());
assertFalse(cmd.isCircuitBreakerOpen());
}

/**
* Test that the circuit-breaker will 'trip' and prevent command execution on subsequent calls.
*/
Expand Down

0 comments on commit 5b4b449

Please sign in to comment.