Search in sources :

Example 6 with AbstractQpsStrategy

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

the class QpsControllerManagerTest method testGetOrCreate.

@Test
public void testGetOrCreate(@Mocked Invocation invocation, @Mocked OperationMeta operationMeta) {
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server";
            operationMeta.getSchemaQualifiedName();
            result = "server.test";
        }
    };
    QpsControllerManager testQpsControllerManager = new QpsControllerManager(false);
    initTestQpsControllerManager(false, testQpsControllerManager, invocation, operationMeta);
    // pojo
    setConfigWithDefaultPrefix(false, "pojo", 100);
    QpsStrategy qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(100 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo2", invocation);
    Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    qpsStrategy = testQpsControllerManager.getOrCreate("poj", invocation);
    Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    ArchaiusUtils.setProperty("servicecomb.flowcontrol.Consumer.qps.limit.poj.server", 10000);
    qpsStrategy = testQpsControllerManager.getOrCreate("poj", invocation);
    Assert.assertEquals("poj.server", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(((AbstractQpsStrategy) qpsStrategy).getQpsLimit(), (Long) 10000L);
    ArchaiusUtils.setProperty("servicecomb.flowcontrol.Consumer.qps.limit.poj.server.test", 20000);
    qpsStrategy = testQpsControllerManager.getOrCreate("poj", invocation);
    Assert.assertEquals("poj.server.test", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(((AbstractQpsStrategy) qpsStrategy).getQpsLimit(), (Long) 20000L);
    testGetOrCreateCommon(false, testQpsControllerManager, invocation, operationMeta);
}
Also used : Expectations(mockit.Expectations) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) Test(org.junit.Test)

Example 7 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy 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 8 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy 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 9 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy 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 10 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy 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)

Aggregations

AbstractQpsStrategy (org.apache.servicecomb.qps.strategy.AbstractQpsStrategy)17 Test (org.junit.Test)10 FixedWindowStrategy (org.apache.servicecomb.qps.strategy.FixedWindowStrategy)7 Expectations (mockit.Expectations)5 MockUp (mockit.MockUp)4 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)3 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)2 DynamicProperty (com.netflix.config.DynamicProperty)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ServiceCombException (org.apache.servicecomb.foundation.common.exceptions.ServiceCombException)1 IStrategyFactory (org.apache.servicecomb.qps.strategy.IStrategyFactory)1 ExpectedException (org.junit.rules.ExpectedException)1