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);
}
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.
the class TestVertxHttpMethod method testDoMethod.
@Test
public void testDoMethod(@Mocked HttpClient httpClient, @Injectable URIEndpointObject address) throws Exception {
Context context = new MockUp<Context>() {
@Mock
public void runOnContext(Handler<Void> action) {
action.handle(null);
}
}.getMockInstance();
HttpClientWithContext httpClientWithContext = new HttpClientWithContext(httpClient, context);
Invocation invocation = mock(Invocation.class);
AsyncResponse asyncResp = mock(AsyncResponse.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);
when(endpoint.getAddress()).thenReturn(address);
when(request.exceptionHandler(Mockito.any())).then(answer -> null);
Map<String, Object> map = new HashMap<>();
when(invocation.getHandlerContext()).then(answer -> map);
;
this.doMethod(httpClientWithContext, invocation, asyncResp);
Assert.assertTrue(true);
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.
the class TestInvoker method completableFutureInvoke_normal.
@Test
public void completableFutureInvoke_normal(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
Response response = Response.ok("1");
new MockUp<InvokerUtils>() {
@Mock
void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
asyncResp.handle(response);
;
}
};
new Expectations() {
{
consumerOperation.getResponseMapper();
result = mapper;
mapper.mapResponse(response);
result = 1;
}
};
Invoker invoker = new Invoker("test", null, IPerson.class);
CompletableFuture<Object> future = invoker.completableFutureInvoke(invocation, consumerOperation);
future.whenComplete((result, ex) -> {
Assert.assertEquals(1, result);
Assert.assertEquals(null, ex);
});
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project java-chassis by ServiceComb.
the class HighwayClient method send.
public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
invocation.getInvocationStageTrace().startGetConnection();
HighwayClientConnection tcpClient = findClientPool(invocation);
invocation.getInvocationStageTrace().startClientFiltersRequest();
OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(invocation);
HighwayClientPackage clientPackage = createClientPackage(invocation, operationProtobuf);
invocation.onStartSendRequest();
tcpClient.send(clientPackage, ar -> {
invocation.getInvocationStageTrace().finishWriteToBuffer(clientPackage.getFinishWriteToBuffer());
invocation.getInvocationStageTrace().finishReceiveResponse();
// 此时是在网络线程中,转换线程
invocation.getResponseExecutor().execute(() -> {
invocation.getInvocationStageTrace().startClientFiltersResponse();
if (ar.failed()) {
// 只会是本地异常
invocation.getInvocationStageTrace().finishClientFiltersResponse();
if (ar.cause() instanceof TimeoutException) {
// give an accurate cause for timeout exception
// The timeout period of 30000ms has been exceeded while executing GET /xxx for server 1.1.1.1:8080
// should not copy the message to invocationException to avoid leak server ip address
LOGGER.info("Request timeout, Details: {}.", ar.cause().getMessage());
asyncResp.consumerFail(new InvocationException(Status.REQUEST_TIMEOUT, new CommonExceptionData("Request Timeout.")));
return;
}
asyncResp.consumerFail(ar.cause());
return;
}
// 处理应答
try {
Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, ar.result());
invocation.getInvocationStageTrace().finishClientFiltersResponse();
asyncResp.complete(response);
} catch (Throwable e) {
invocation.getInvocationStageTrace().finishClientFiltersResponse();
asyncResp.consumerFail(e);
}
});
});
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project java-chassis by ServiceComb.
the class TestLoadBalanceHandler2 method testConfigEndpoint.
@Test
public void testConfigEndpoint() {
ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
InvocationRuntimeType invocationRuntimeType = Mockito.mock(InvocationRuntimeType.class);
SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
MicroserviceMeta microserviceMeta = Mockito.mock(MicroserviceMeta.class);
when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
when(schemaMeta.getMicroserviceName()).thenReturn("testMicroserviceName");
when(microserviceMeta.getAppId()).thenReturn("testApp");
when(referenceConfig.getVersionRule()).thenReturn("0.0.0+");
when(referenceConfig.getTransport()).thenReturn("rest");
Invocation invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, new HashMap<>());
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
Transport transport = Mockito.mock(Transport.class);
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
// set up data
MicroserviceInstance myself = new MicroserviceInstance();
MicroserviceInstance findInstance = new MicroserviceInstance();
List<String> findEndpoint = new ArrayList<>();
findEndpoint.add("rest://localhost:9092");
findInstance.setEndpoints(findEndpoint);
findInstance.setInstanceId("findInstance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.setTransportManager(transportManager);
SCBEngine.getInstance().setTransportManager(transportManager);
LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
mockUpInstanceCacheManager(instanceCacheManager);
when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
when(transportManager.findTransport("rest")).thenReturn(transport);
data.put("findInstance", findInstance);
parent.cacheVersion(1);
LoadbalanceHandler handler = new LoadbalanceHandler();
try {
handler.handle(invocation, asyncResp);
} catch (Exception e) {
}
Assert.assertEquals("rest://localhost:9092", invocation.getEndpoint().getEndpoint());
// reset
invocation.setEndpoint(null);
// success
invocation.addLocalContext("scb-endpoint", "rest://127.0.0.1:8080?sslEnabled=true&protocol=http2");
try {
handler.handle(invocation, asyncResp);
} catch (Exception e) {
}
Assert.assertEquals("rest://127.0.0.1:8080?sslEnabled=true&protocol=http2", invocation.getEndpoint().getEndpoint());
// reset
invocation.setEndpoint(null);
// endpoint format is not correct
invocation.addLocalContext("scb-endpoint", "127.0.0.1:8080");
try {
handler.handle(invocation, asyncResp);
Assert.assertEquals("endpoint's format is not correct, throw exception", " but not throw exception");
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("Illegal character in scheme name"));
}
// reset
invocation.setEndpoint(null);
// transport is not find
invocation.addLocalContext("scb-endpoint", "my://127.0.0.1:8080?sslEnabled=true&protocol=http2");
try {
handler.handle(invocation, asyncResp);
Assert.assertEquals("endpoint's transport not found, throw exception", "but not throw exception");
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("the endpoint's transport is not found."));
}
}
Aggregations