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);
}
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);
}
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());
}
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());
}
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());
}
Aggregations