use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.
the class TestVertxRestTransport method testSendException.
@Test
public void testSendException() {
boolean validAssert;
Invocation invocation = Mockito.mock(Invocation.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
URIEndpointObject endpoint = Mockito.mock(URIEndpointObject.class);
Endpoint end = Mockito.mock(Endpoint.class);
Mockito.when(invocation.getEndpoint()).thenReturn(end);
Mockito.when(invocation.getEndpoint().getAddress()).thenReturn(endpoint);
try {
validAssert = true;
instance.send(invocation, asyncResp);
} catch (Exception e) {
validAssert = false;
}
Assert.assertFalse(validAssert);
}
use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.
the class TestShutdownHookHandler method testShutdownHookHandlerCount.
@Test
public void testShutdownHookHandlerCount(@Mocked Response response) throws Exception {
Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", false);
ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
Assert.assertEquals(0, handler.getActiveCount());
// no reply
Invocation invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
}
}.getMockInstance();
handler.handle(invocation, asyncResp -> {
});
Assert.assertEquals(1, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
// normal
invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
asyncResp.handle(response);
}
}.getMockInstance();
handler.handle(invocation, asyncResp -> {
});
Assert.assertEquals(2, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
// next exception
invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
throw new Error();
}
}.getMockInstance();
try {
handler.handle(invocation, asyncResp -> {
});
Assert.assertFalse(true);
} catch (Throwable e) {
Assert.assertEquals(3, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
}
// reply exception
// TODO: should be fixed
// try {
// handler.handle(invocation, asyncResp -> {
// throw new Error();
// });
//
// Assert.assertFalse(true);
// } catch (Throwable e) {
// Assert.assertEquals(3, requestCounter.get());
// Assert.assertEquals(1, handler.getActiveCount());
// }
}
use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.
the class TestInvokerUtils method testReactiveInvoke.
@Test
public void testReactiveInvoke() {
Invocation invocation = Mockito.mock(Invocation.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
boolean validAssert;
try {
InvokerUtils.reactiveInvoke(invocation, asyncResp);
validAssert = true;
} catch (Exception e) {
validAssert = false;
}
Assert.assertTrue(validAssert);
}
use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.
the class VertxHttpMethod method handleResponse.
protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
ProduceProcessor produceProcessor = null;
String contentType = httpResponse.getHeader("Content-Type");
if (contentType != null) {
String contentTypeForFind = contentType;
int idx = contentType.indexOf(';');
if (idx != -1) {
contentTypeForFind = contentType.substring(0, idx);
}
produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
}
if (produceProcessor == null) {
String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
asyncResp.fail(invocation.getInvocationType(), exception);
return;
}
ProduceProcessor finalProduceProcessor = produceProcessor;
httpResponse.bodyHandler(responseBuf -> {
invocation.getResponseExecutor().execute(() -> {
try {
ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
for (String headerName : responseMeta.getHeaders().keySet()) {
List<String> headerValues = httpResponse.headers().getAll(headerName);
for (String headerValue : headerValues) {
response.getHeaders().addHeader(headerName, headerValue);
}
}
asyncResp.complete(response);
} catch (Throwable e) {
asyncResp.fail(invocation.getInvocationType(), e);
}
});
});
}
use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.
the class TestVertxGetMethod method testVertxGetMethod.
@Test
public void testVertxGetMethod() {
HttpClient client = Mockito.mock(HttpClient.class);
Invocation invocation = Mockito.mock(Invocation.class);
IpPort ipPort = Mockito.mock(IpPort.class);
Mockito.when(ipPort.getPort()).thenReturn(10);
assertEquals(10, ipPort.getPort());
Mockito.when(ipPort.getHostOrIp()).thenReturn("ever");
assertEquals("ever", ipPort.getHostOrIp());
RestOperationMeta operation = Mockito.mock(RestOperationMeta.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
HttpClientRequest obj = VertxGetMethod.INSTANCE.createRequest(client, invocation, ipPort, "good", operation, asyncResp);
Assert.assertNull(obj);
}
Aggregations