简体   繁体   English

使用Mockito测试断路器(Hystrix Javanica)

[英]Testing Circuit Breaker (Hystrix Javanica) with Mockito

I'm securing a service-call with Javanica. 我正在用Javanica保护服务电话。 I would like to test circuit breaker. 我想测试断路器。 About my conditions: JBoss, SpringFramework (but not Springboot!). 关于我的条件:JBoss,SpringFramework(但不是Springboot!)。 I already configured Javanica and it works, tested by a simple methods call where I force to open the circuit breaker. 我已经配置了Javanica,并且可以正常运行,并通过一个简单的方法调用进行了测试,在其中我强制打开断路器。 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. 我正在尝试创建一个断路器测试,该测试在恰好有10个方法调用时给了我“短路和后备失败”的信息。 Where do i need to fix my mockito test? 我需要在哪里修复我的模拟测试?

I set circuitBreaker.forceOpen="true" and mock my service. 我设置了circuitBreaker.forceOpen =“ true”并模拟了我的服务。

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. 这是因为servlet概念。

When you tested with mockito, it is used non-servlet. 当您使用嘲笑测试时,它用于非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. 但是,您可能还记得在应用程序中设置配置了servlet的Hystrix。

Hystrix needs servlet concept, it is being requested from other client. Hystrix需要servlet概念,它是从其他客户端请求的。 The servlet catches the request and suspends which is timeout or bad request... servlet捕获请求并挂起超时或错误的请求...

I recommend that you write python script or other script and then request your application that is installed with Hystrix. 我建议您编写python脚本或其他脚本,然后请求与Hystrix一起安装的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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