Search in sources :

Example 11 with AbstractQpsStrategy

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

Example 12 with AbstractQpsStrategy

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

the class QpsControllerManagerTest method initTestQpsControllerManager.

/**
 * Init testQpsControllerManager to test search function.
 */
private void initTestQpsControllerManager(boolean isProvider, QpsControllerManager testQpsControllerManager, Invocation invocation, OperationMeta operationMeta) {
    // pojo.server.test
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server";
            operationMeta.getSchemaQualifiedName();
            result = "server.test";
        }
    };
    QpsStrategy qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    if (isProvider) {
        Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    } else {
        Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    }
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    // pojo.server.test2
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server";
            operationMeta.getSchemaQualifiedName();
            result = "server.test2";
        }
    };
    testQpsControllerManager.getOrCreate("pojo", invocation);
    if (isProvider) {
        Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    } else {
        Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    }
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    // pojo.server.tes
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server";
            operationMeta.getSchemaQualifiedName();
            result = "server.tes";
        }
    };
    testQpsControllerManager.getOrCreate("pojo", invocation);
    if (isProvider) {
        Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    } else {
        Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    }
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    // pojo.server2.test
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server2";
            operationMeta.getSchemaQualifiedName();
            result = "server2.test";
        }
    };
    testQpsControllerManager.getOrCreate("pojo", invocation);
    if (isProvider) {
        Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    } else {
        Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    }
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    // pojo.serve.test
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "serve";
            operationMeta.getSchemaQualifiedName();
            result = "serve.test";
        }
    };
    testQpsControllerManager.getOrCreate("pojo", invocation);
    if (isProvider) {
        Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    } else {
        Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    }
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    // pojo2.server.test
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server";
            operationMeta.getSchemaQualifiedName();
            result = "server.test";
        }
    };
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo2", invocation);
    if (isProvider) {
        Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    } else {
        Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    }
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    // poj.server.test
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server";
            operationMeta.getSchemaQualifiedName();
            result = "server.test";
        }
    };
    qpsStrategy = testQpsControllerManager.getOrCreate("poj", invocation);
    if (isProvider) {
        Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    } else {
        Assert.assertEquals(Config.CONSUMER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    }
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
}
Also used : Expectations(mockit.Expectations) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy)

Example 13 with AbstractQpsStrategy

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

the class QpsControllerManagerTest method testGetOrCreateCommon.

private void testGetOrCreateCommon(boolean isProvider, QpsControllerManager testQpsControllerManager, Invocation invocation, OperationMeta operationMeta) {
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getSchemaQualifiedName();
            result = "server.test";
        }
    };
    setConfigWithDefaultPrefix(isProvider, "pojo.server", 200);
    QpsStrategy qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo.server", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(200 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getSchemaQualifiedName();
            result = "server2.test";
        }
    };
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(100 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getSchemaQualifiedName();
            result = "serve.test";
        }
    };
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(100 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    // pojo.server.test
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getSchemaQualifiedName();
            result = "server.test";
        }
    };
    setConfigWithDefaultPrefix(isProvider, "pojo.server.test", 300);
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo.server.test", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(300 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getSchemaQualifiedName();
            result = "server.test2";
        }
    };
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo.server", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(200 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getSchemaQualifiedName();
            result = "server.tes";
        }
    };
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo.server", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(200 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
}
Also used : Expectations(mockit.Expectations) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy)

Example 14 with AbstractQpsStrategy

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

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