How to force a Hystrix circuit breaker open?
Asked Answered
J

3

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;}
    }
}
Joviality answered 20/3, 2015 at 11:37 Comment(0)
C
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.

Chapeau answered 21/3, 2015 at 3:18 Comment(0)
I
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());
}
Ibiza answered 1/3, 2018 at 10:17 Comment(1)
Please provide context, what you have edited and whyWu
M
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());
}
Maitland answered 28/6, 2016 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.