use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.
the class HighwayClient method send.
public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
HighwayClientConnectionPool tcpClientPool = clientMgr.findClientPool(invocation.isSync());
OperationMeta operationMeta = invocation.getOperationMeta();
OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
HighwayClientConnection tcpClient = tcpClientPool.findOrCreateClient(invocation.getEndpoint().getEndpoint());
HighwayClientPackage clientPackage = new HighwayClientPackage(invocation, operationProtobuf, tcpClient);
LOGGER.debug("Sending request by highway, qualifiedName={}, endpoint={}.", invocation.getMicroserviceQualifiedName(), invocation.getEndpoint().getEndpoint());
tcpClient.send(clientPackage, ar -> {
// 此时是在网络线程中,转换线程
invocation.getResponseExecutor().execute(() -> {
if (ar.failed()) {
// 只会是本地异常
asyncResp.consumerFail(ar.cause());
return;
}
// 处理应答
try {
Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, ar.result(), tcpClient.getProtobufFeature());
asyncResp.complete(response);
} catch (Throwable e) {
asyncResp.consumerFail(e);
}
});
});
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.
the class TestHighwayTransport method testSendException.
@Test
public void testSendException() throws Exception {
Invocation invocation = Mockito.mock(Invocation.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
commonHighwayMock(invocation);
Holder<Boolean> sended = new Holder<>(false);
new MockUp<HighwayClient>() {
@Mock
public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
sended.value = true;
}
};
transport.send(invocation, asyncResp);
Assert.assertTrue(sended.value);
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.
the class TestInvoker method completableFutureInvoke_failed.
@Test
public void completableFutureInvoke_failed(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
Throwable error = new Error("failed");
Response response = Response.createConsumerFail(error);
new MockUp<InvokerUtils>() {
@Mock
void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
asyncResp.handle(response);
;
}
};
Invoker invoker = new Invoker("test", null, IPerson.class);
CompletableFuture<Object> future = invoker.completableFutureInvoke(invocation, consumerOperation);
future.whenComplete((result, ex) -> {
Assert.assertEquals(null, result);
Assert.assertSame(error, ex);
});
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.
the class TestVertxHttpMethod method testHandleResponse.
@Test
public void testHandleResponse() {
boolean status = false;
try {
Invocation invocation = mock(Invocation.class);
AsyncResponse asyncResp = mock(AsyncResponse.class);
HttpClientResponse httpResponse = mock(HttpClientResponse.class);
OperationMeta operationMeta = mock(OperationMeta.class);
RestOperationMeta swaggerRestOperation = mock(RestOperationMeta.class);
Endpoint endpoint = mock(Endpoint.class);
when(invocation.getOperationMeta()).thenReturn(operationMeta);
URLPathBuilder urlPathBuilder = mock(URLPathBuilder.class);
when(swaggerRestOperation.getPathBuilder()).thenReturn(urlPathBuilder);
operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
when(invocation.getEndpoint()).thenReturn(endpoint);
String contentType = httpResponse.getHeader("Content-Type");
ProduceProcessor produceProcessor = mock(ProduceProcessor.class);
when(swaggerRestOperation.findProduceProcessor(contentType)).thenReturn(produceProcessor);
this.handleResponse(invocation, httpResponse, asyncResp);
} catch (Exception ex) {
status = true;
}
Assert.assertFalse(status);
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.
the class TestVertxHttpMethod method testCreateRequest.
@Test
public void testCreateRequest() {
HttpClient client = mock(HttpClient.class);
Invocation invocation = mock(Invocation.class);
OperationMeta operationMeta = mock(OperationMeta.class);
Endpoint endpoint = mock(Endpoint.class);
URIEndpointObject address = mock(URIEndpointObject.class);
when(invocation.getEndpoint()).thenReturn(endpoint);
when(endpoint.getAddress()).thenReturn(address);
when(address.isSslEnabled()).thenReturn(false);
when(invocation.getOperationMeta()).thenReturn(operationMeta);
RestOperationMeta swaggerRestOperation = mock(RestOperationMeta.class);
when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
IpPort ipPort = mock(IpPort.class);
when(ipPort.getPort()).thenReturn(10);
when(ipPort.getHostOrIp()).thenReturn("ever");
AsyncResponse asyncResp = mock(AsyncResponse.class);
List<HttpMethod> methods = new ArrayList<>(Arrays.asList(HttpMethod.GET, HttpMethod.PUT, HttpMethod.POST, HttpMethod.DELETE, HttpMethod.PATCH));
for (HttpMethod method : methods) {
when(swaggerRestOperation.getHttpMethod()).thenReturn(method.toString());
HttpClientRequest obj = VertxHttpMethod.INSTANCE.createRequest(client, invocation, ipPort, "good", asyncResp);
Assert.assertNull(obj);
}
}
Aggregations