use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class ProviderQpsFlowControlHandler method isLimitNewRequest.
private boolean isLimitNewRequest(QpsController qpsController, AsyncResponse asyncResp) {
if (qpsController.isLimitNewRequest()) {
CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol");
asyncResp.producerFail(new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData));
return true;
} else {
return false;
}
}
use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class TestBizkeeperCommand method testGetCacheKeyConsumer.
@Test
public void testGetCacheKeyConsumer() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter().withRequestCacheEnabled(true).withRequestLogEnabled(false);
BizkeeperCommand bizkeeperCommand = new ConsumerBizkeeperCommand("groupname", invocation, HystrixObservableCommand.Setter.withGroupKey(CommandKey.toHystrixCommandGroupKey("groupname", invocation)).andCommandKey(CommandKey.toHystrixCommandKey("groupname", invocation)).andCommandPropertiesDefaults(setter));
String str = bizkeeperCommand.getCacheKey();
Assert.assertNull(str);
Response resp = Mockito.mock(Response.class);
Mockito.when(resp.isFailed()).thenReturn(false);
Assert.assertEquals(false, bizkeeperCommand.isFailedResponse(resp));
Mockito.when(resp.isFailed()).thenReturn(true);
InvocationException excp = Mockito.mock(InvocationException.class);
Mockito.when(resp.getResult()).thenReturn(excp);
Mockito.when(excp.getStatusCode()).thenReturn(400);
Assert.assertEquals(false, bizkeeperCommand.isFailedResponse(resp));
Mockito.when(resp.getResult()).thenReturn(excp);
Mockito.when(excp.getStatusCode()).thenReturn(490);
Assert.assertEquals(true, bizkeeperCommand.isFailedResponse(resp));
}
use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class TestConsumerQpsFlowControlHandler method testHandle.
@Test
public void testHandle() throws Exception {
String key = "svc.schema.opr";
QpsController qpsController = new QpsController("key", 12);
Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
Mockito.when(operationMeta.getMicroserviceQualifiedName()).thenReturn(key);
setQpsController(key, qpsController);
new MockUp<QpsController>() {
@Mock
public boolean isLimitNewRequest() {
return true;
}
};
new MockUp<QpsControllerManager>() {
@Mock
protected QpsController create(String qualifiedNameKey) {
return qpsController;
}
};
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("rejected by qps flowcontrol", ((CommonExceptionData) invocationException.getErrorData()).getMessage());
}
use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class TestResponse method test.
@Test
public void test() {
Response r = Response.create(200, "200", 2);
Assert.assertEquals(200, r.getStatusCode());
Assert.assertEquals(2, (int) r.getResult());
Response r1 = r.build();
Assert.assertEquals(r, r1);
r = Response.create(300, "300", 3);
Assert.assertEquals(300, r.getStatusCode());
Assert.assertEquals("300", r.getReasonPhrase());
Assert.assertEquals(3, ((InvocationException) r.getResult()).getErrorData());
r = Response.createSuccess(Status.OK, 2);
Assert.assertEquals(200, r.getStatusCode());
Assert.assertEquals(2, (int) r.getResult());
r = Response.success(2, Status.OK);
Assert.assertEquals(200, r.getStatusCode());
Assert.assertEquals(2, (int) r.getResult());
r = Response.createFail(InvocationType.CONSUMER, "abc");
Assert.assertEquals("CommonExceptionData [message=abc]", ((InvocationException) r.getResult()).getErrorData().toString());
Assert.assertEquals(490, r.getStatusCode());
r = Response.createFail(InvocationType.PRODUCER, "def");
Assert.assertEquals("CommonExceptionData [message=def]", ((InvocationException) r.getResult()).getErrorData().toString());
Assert.assertEquals(590, r.getStatusCode());
}
Aggregations