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