简体   繁体   中英

Testing Circuit Breaker (Hystrix Javanica) with Mockito

I'm securing a service-call with Javanica. I would like to test circuit breaker. About my conditions: JBoss, SpringFramework (but not Springboot!). I already configured Javanica and it works, tested by a simple methods call where I force to open the circuit breaker. I get the right exception:

short-circuited and fallback failed

I am trying to create a circuit breaker test which give me the "short-circuited and fallback failed" at exactly the 10 methods call. Where do i need to fix my mockito test?

I set circuitBreaker.forceOpen="true" and mock my service.

import static org.mockito.Mockito.when;

public class HystrixCircleBreakerTest extends AbstractMockitoTest {

    @Bean
    private ServiceAdapter serviceAdapter;

    @Mock
    private Service service;

    @Test
    public void circuitBreakerTest() {

        String errorMsg = "Timeout error";
        final RuntimeException timeOutException = new RuntimeException(errorMsg);

        when(service.getMediatorForContract(99177661)).then(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                Thread.sleep(1000L);
                throw timeOutException;
            }
        });

        Exception circleBreaker = new Exception();
        final String errorMsgCircuit = "Hystrix circuit short-circuited and is OPEN";
        RuntimeException runtimeException = new RuntimeException(errorMsgCircuit);

        for (int t = 0; t <= 10; t++) {
            System.out.println("Servicecall: " + t);
            try {
                serviceAdapter.getMediatorForContract("99177661");

            } catch (RuntimeException e) {
                System.out.println("Exception: " + e.getMessage());
                circleBreaker = e;
            }
        }
    }
}

Current results:

Servicecall: 0
Exception: Timeout error

Servicecall: 1
Exception: Timeout error

Servicecall: 2
Exception: Timeout error

Servicecall: 3
Exception: Timeout error

Servicecall: 4
Exception: Timeout error

Servicecall: 5
Exception: Timeout error

Servicecall: 6
Exception: Timeout error

Servicecall: 7
Exception: Timeout error

Servicecall: 8
Exception: Timeout error

Servicecall: 9
Exception: Timeout error

Servicecall: 10
Exception: Timeout error

Normally i should get in every call a "short-circuited and fallback failed"

It is because of servlet concept.

When you tested with mockito, it is used non-servlet. That`s just being worked inner application context.

However, you may remember to set up the Hystrix that is configured servlet in your application.

Hystrix needs servlet concept, it is being requested from other client. The servlet catches the request and suspends which is timeout or bad request...

I recommend that you write python script or other script and then request your application that is installed with Hystrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM