8

I would like to programmatically force a circuit breaker to open for a particular group. I thought I might be able to do that by setting the config on a command in a group to force open, and running that command. However, that doesn't seem to work. Is this possible? Should I take a different approach? Here's the test I tried that fails on the 2nd assertEquals call.

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class ForceCircuitBreakerCommandTest {

    @Test
    public void testForceOpen(){

        assertEquals(Boolean.TRUE, new FakeCommand().execute());

        new OpenCircuitBreakerCommand().execute();

        assertEquals(Boolean.FALSE, new FakeCommand().execute());

    }

    private class FakeCommand extends HystrixCommand<Boolean> {

        public FakeCommand(){
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup")));
        }

        @Override
        public Boolean run(){return Boolean.TRUE;}

        @Override
        public Boolean getFallback() {return Boolean.FALSE;}
    }

    private class OpenCircuitBreakerCommand extends HystrixCommand<Boolean> {

        public OpenCircuitBreakerCommand(){
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup"))
                    .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                                    .withCircuitBreakerForceOpen(true)));
        }

        @Override
        public Boolean run(){return Boolean.TRUE;}

        @Override
        public Boolean getFallback() {return Boolean.FALSE;}
    }
}
rzrelyea
  • 1,467
  • 1
  • 13
  • 19

3 Answers3

17

I have set custom properties such as "hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen" using

import com.netflix.config.ConfigurationManager;

ConfigurationManager.getConfigInstance()
    .setProperty("hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen",
    true);

ConfigurationManager is the Archaius instance that is used internally.

Richard Neish
  • 8,414
  • 4
  • 39
  • 69
1

This is an edit to the test using Senthilkumar Gopal's answer

@Test
public void testForceOpen() {

    assertEquals(Boolean.TRUE, new OpenCircuitBreakerCommand().execute());

    ConfigurationManager.getConfigInstance()
            .setProperty("hystrix.command.OpenCircuitBreakerCommand.circuitBreaker.forceOpen",
                    true);

    assertEquals(Boolean.FALSE, new OpenCircuitBreakerCommand().execute());
}
ggmoriyon
  • 31
  • 3
0

You don't necessarily have to use ConfigurationManager. That test needs to say:

@Test
public void testForceOpen() {
    assertEquals(Boolean.TRUE, new FakeCommand().execute());
    assertEquals(Boolean.FALSE, new OpenCircuitBreakerCommand().execute());
}
Laurel
  • 5,965
  • 14
  • 31
  • 57
ObviousChild
  • 119
  • 1
  • 9