Skip to content

Instantly share code, notes, and snippets.

@lordofthejars
Created September 11, 2014 16:13
Show Gist options
  • Select an option

  • Save lordofthejars/9abc84212f050f12b53b to your computer and use it in GitHub Desktop.

Select an option

Save lordofthejars/9abc84212f050f12b53b to your computer and use it in GitHub Desktop.
@Test
public void should_execute_fallback_method_when_circuit_is_open() {
//Initialize HystrixRequestContext to be able to get some metrics
HystrixRequestContext context = HystrixRequestContext.initializeContext();
HystrixCommandMetrics creditCardMetrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey(HelloWorldRestCommand.class.getSimpleName()));
//We use Archaius to set the circuit as closed.
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.circuitBreaker.forceOpen", false);
String successMessage = new HelloWorldRestCommand().execute();
assertThat(successMessage, is("Hello World"));
//We use Archaius to open the circuit
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.circuitBreaker.forceOpen", true);
String failMessage = new HelloWorldRestCommand().execute();
assertThat(failMessage, is("Good Bye"));
//Prints Request => HelloWorldRestCommand[SUCCESS][19ms], HelloWorldRestCommand[SHORT_CIRCUITED, FALLBACK_SUCCESS][0ms]
System.out.println("Request => " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertThat(creditCardMetrics.getHealthCounts().getTotalRequests(), is(2));
assertThat(creditCardMetrics.getHealthCounts().getErrorCount(), is(1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment