Search in sources :

Example 1 with FixedWindowStrategy

use of org.apache.servicecomb.qps.strategy.FixedWindowStrategy in project java-chassis by ServiceComb.

the class TestConsumerQpsFlowControlHandler method testHandleIsLimitNewRequestAsFalse.

@Test
public void testHandleIsLimitNewRequestAsFalse() throws Exception {
    String key = "service.schema.id";
    AbstractQpsStrategy qpsStrategy = new FixedWindowStrategy();
    qpsStrategy.setKey("service");
    qpsStrategy.setQpsLimit(12L);
    Mockito.when(invocation.getMicroserviceName()).thenReturn("service");
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    Mockito.when(operationMeta.getSchemaQualifiedName()).thenReturn("schema.id");
    setQpsController(key, qpsStrategy);
    new MockUp<QpsStrategy>() {

        @Mock
        public boolean isLimitNewRequest() {
            return false;
        }
    };
    new MockUp<QpsControllerManager>() {

        @Mock
        protected QpsStrategy create(String qualifiedNameKey) {
            return qpsStrategy;
        }
    };
    handler.handle(invocation, asyncResp);
    Mockito.verify(invocation).next(asyncResp);
}
Also used : AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) MockUp(mockit.MockUp) FixedWindowStrategy(org.apache.servicecomb.qps.strategy.FixedWindowStrategy) Test(org.junit.Test)

Example 2 with FixedWindowStrategy

use of org.apache.servicecomb.qps.strategy.FixedWindowStrategy in project java-chassis by ServiceComb.

the class TestQpsStrategy method testFixedWindowStrategy.

@Test
public void testFixedWindowStrategy() {
    AbstractQpsStrategy qpsStrategy = new FixedWindowStrategy();
    qpsStrategy.setKey("abc");
    qpsStrategy.setQpsLimit(100L);
    Assert.assertEquals(false, qpsStrategy.isLimitNewRequest());
    qpsStrategy.setQpsLimit(1L);
    Assert.assertEquals(true, qpsStrategy.isLimitNewRequest());
}
Also used : AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) FixedWindowStrategy(org.apache.servicecomb.qps.strategy.FixedWindowStrategy) Test(org.junit.Test)

Example 3 with FixedWindowStrategy

use of org.apache.servicecomb.qps.strategy.FixedWindowStrategy in project java-chassis by ServiceComb.

the class TestProviderQpsFlowControlHandler method testQpsController.

@Test
public void testQpsController() {
    AbstractQpsStrategy qpsStrategy = new FixedWindowStrategy();
    qpsStrategy.setKey("abc");
    qpsStrategy.setQpsLimit(100L);
    assertFalse(qpsStrategy.isLimitNewRequest());
    qpsStrategy.setQpsLimit(1L);
    assertTrue(qpsStrategy.isLimitNewRequest());
}
Also used : AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) FixedWindowStrategy(org.apache.servicecomb.qps.strategy.FixedWindowStrategy) Test(org.junit.Test)

Example 4 with FixedWindowStrategy

use of org.apache.servicecomb.qps.strategy.FixedWindowStrategy in project java-chassis by ServiceComb.

the class TestProviderQpsFlowControlHandler method testHandle.

@Test
public void testHandle() throws Exception {
    Mockito.when(invocation.getContext(Const.SRC_MICROSERVICE)).thenReturn("test");
    OperationMeta mockOperationMeta = QpsControllerManagerTest.getMockOperationMeta("pojo", "server", "opr");
    Mockito.when(invocation.getOperationMeta()).thenReturn(mockOperationMeta);
    Mockito.when(invocation.getSchemaId()).thenReturn("server");
    new MockUp<QpsControllerManager>() {

        @Mock
        protected QpsStrategy create(String qualifiedNameKey) {
            AbstractQpsStrategy strategy = new FixedWindowStrategy();
            strategy.setKey(qualifiedNameKey);
            strategy.setQpsLimit(1L);
            return strategy;
        }
    };
    handler.handle(invocation, asyncResp);
    handler.handle(invocation, asyncResp);
    ArgumentCaptor<InvocationException> captor = ArgumentCaptor.forClass(InvocationException.class);
    Mockito.verify(asyncResp, times(1)).producerFail(captor.capture());
    InvocationException invocationException = captor.getValue();
    assertEquals(QpsConst.TOO_MANY_REQUESTS_STATUS, invocationException.getStatus());
    assertEquals("provider request rejected by qps flowcontrol", ((CommonExceptionData) invocationException.getErrorData()).getMessage());
}
Also used : AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MockUp(mockit.MockUp) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) FixedWindowStrategy(org.apache.servicecomb.qps.strategy.FixedWindowStrategy) Test(org.junit.Test)

Example 5 with FixedWindowStrategy

use of org.apache.servicecomb.qps.strategy.FixedWindowStrategy in project java-chassis by ServiceComb.

the class TestProviderQpsFlowControlHandler method testHandleIsLimitNewRequestAsFalse.

@Test
public void testHandleIsLimitNewRequestAsFalse() throws Exception {
    Mockito.when(invocation.getContext(Const.SRC_MICROSERVICE)).thenReturn("test");
    OperationMeta mockOperationMeta = QpsControllerManagerTest.getMockOperationMeta("pojo", "server", "opr");
    Mockito.when(invocation.getOperationMeta()).thenReturn(mockOperationMeta);
    Mockito.when(invocation.getSchemaId()).thenReturn("server");
    new MockUp<QpsControllerManager>() {

        @Mock
        protected QpsStrategy create(String qualifiedNameKey) {
            AbstractQpsStrategy strategy = new FixedWindowStrategy();
            strategy.setKey(qualifiedNameKey);
            strategy.setQpsLimit(1L);
            return strategy;
        }
    };
    handler.handle(invocation, asyncResp);
    Mockito.verify(invocation, times(0)).next(asyncResp);
    Mockito.verify(asyncResp, times(0)).producerFail(Mockito.any(Exception.class));
}
Also used : AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) MockUp(mockit.MockUp) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) ExpectedException(org.junit.rules.ExpectedException) FixedWindowStrategy(org.apache.servicecomb.qps.strategy.FixedWindowStrategy) Test(org.junit.Test)

Aggregations

AbstractQpsStrategy (org.apache.servicecomb.qps.strategy.AbstractQpsStrategy)7 FixedWindowStrategy (org.apache.servicecomb.qps.strategy.FixedWindowStrategy)7 Test (org.junit.Test)7 MockUp (mockit.MockUp)4 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)3 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)2 ExpectedException (org.junit.rules.ExpectedException)1