Search in sources :

Example 6 with FixedWindowStrategy

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

the class TestConsumerQpsFlowControlHandler method testQpsController.

@Test
public void testQpsController() {
    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 7 with FixedWindowStrategy

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

the class TestConsumerQpsFlowControlHandler method testHandle.

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

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

        @Mock
        protected QpsStrategy create(String qualifiedNameKey) {
            return qpsStrategy;
        }
    };
    handler.handle(invocation, asyncResp);
    ArgumentCaptor<InvocationException> captor = ArgumentCaptor.forClass(InvocationException.class);
    Mockito.verify(asyncResp).consumerFail(captor.capture());
    InvocationException invocationException = captor.getValue();
    assertEquals(QpsConst.TOO_MANY_REQUESTS_STATUS, invocationException.getStatus());
    assertEquals("consumer 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) 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