use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.
the class TestAbstractRestInvocation method doInvoke.
@Test
public void doInvoke(@Mocked Endpoint endpoint, @Mocked OperationMeta operationMeta, @Mocked Object[] swaggerArguments, @Mocked SchemaMeta schemaMeta) throws Throwable {
Response response = Response.ok("ok");
Handler handler = (invocation, asyncResp) -> asyncResp.complete(response);
List<Handler> handlerChain = Arrays.asList(handler);
Deencapsulation.setField(invocation, "handlerList", handlerChain);
Holder<Response> result = new Holder<>();
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void sendResponse(Response response) {
result.value = response;
}
};
restInvocation.invocation = invocation;
restInvocation.doInvoke();
Assert.assertSame(response, result.value);
assertEquals(nanoTime, invocation.getInvocationStageTrace().getStartHandlersRequest());
assertEquals(nanoTime, invocation.getInvocationStageTrace().getFinishHandlersResponse());
}
use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.
the class TestAbstractRestInvocation method scheduleInvocation_flowControlReject.
@SuppressWarnings("deprecation")
@Test
public void scheduleInvocation_flowControlReject() {
new Expectations(operationMeta) {
{
operationMeta.getProviderQpsFlowControlHandler();
result = (Handler) (invocation, asyncResp) -> asyncResp.producerFail(new InvocationException(new HttpStatus(429, "Too Many Requests"), new CommonExceptionData("rejected by qps flowcontrol")));
}
};
Holder<Integer> status = new Holder<>();
Holder<String> reasonPhrase = new Holder<>();
Holder<Integer> endCount = new Holder<>(0);
Holder<String> responseBody = new Holder<>();
responseEx = new AbstractHttpServletResponse() {
@SuppressWarnings("deprecation")
@Override
public void setStatus(int sc, String sm) {
status.value = sc;
reasonPhrase.value = sm;
}
@Override
public void flushBuffer() {
endCount.value = endCount.value + 1;
}
@Override
public void setContentType(String type) {
assertEquals("application/json; charset=utf-8", type);
}
@Override
public void setBodyBuffer(Buffer bodyBuffer) {
responseBody.value = bodyBuffer.toString();
}
};
initRestInvocation();
restInvocation.scheduleInvocation();
assertEquals(Integer.valueOf(429), status.value);
assertEquals("Too Many Requests", reasonPhrase.value);
assertEquals("{\"message\":\"rejected by qps flowcontrol\"}", responseBody.value);
assertEquals(Integer.valueOf(1), endCount.value);
}
use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.
the class TestLoadBalanceHandler2 method testIsolationEventWithEndpoint.
@Test
public void testIsolationEventWithEndpoint() throws Exception {
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<>());
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();
DataCenterInfo info = new DataCenterInfo();
info.setName("test");
info.setRegion("test");
info.setAvailableZone("test");
myself.setDataCenterInfo(info);
MicroserviceInstance instance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test");
info.setAvailableZone("test");
List<String> allMatchEndpoint = new ArrayList<>();
allMatchEndpoint.add("rest://localhost:9090");
instance.setEndpoints(allMatchEndpoint);
instance.setDataCenterInfo(info);
instance.setInstanceId("instance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.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);
LoadbalanceHandler handler = null;
LoadBalancer loadBalancer = null;
ServiceCombServer server = null;
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertNull(server);
data.put("instance", instance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
// if errorThresholdPercentage greater than 0, it will activate.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "10");
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "10");
Holder<Integer> count = new Holder<>(0);
IsolationEndpointListener isolationEndpointListener = new IsolationEndpointListener(count);
EventManager.getEventBus().register(isolationEndpointListener);
Assert.assertEquals(0, count.value.intValue());
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
// no server is available
Assert.assertNull(server);
Assert.assertEquals(1, count.value.intValue());
EventManager.unregister(isolationEndpointListener);
}
use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.
the class TestLoadbalanceHandler method send_failed.
@Test
public void send_failed(@Injectable LoadBalancer loadBalancer) {
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
LoadBalancerStats stats = new LoadBalancerStats("test");
new Expectations(loadBalancer) {
{
loadBalancer.chooseServer(invocation);
result = server;
loadBalancer.getLoadBalancerStats();
result = stats;
}
};
sendResponse = Response.consumerFailResp(new SocketException());
Holder<Throwable> result = new Holder<>();
Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
result.value = (Throwable) resp.getResult();
}, loadBalancer);
Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
Assert.assertEquals("InvocationException: code=490;msg=CommonExceptionData [message=Unexpected consumer error, please check logs for details]", result.value.getMessage());
}
use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.
the class CseAsyncClientHttpRequestTest method testNormal.
@Test
public void testNormal() {
Holder<Invocation> holder = new Holder<>();
CseAsyncClientHttpRequest client = new CseAsyncClientHttpRequest(URI.create("cse://defaultMicroservice/" + CseAsyncClientHttpRequestTest.CseAsyncClientHttpRequestTestSchema.class.getSimpleName() + "/testbytes"), HttpMethod.POST) {
@Override
protected CompletableFuture<ClientHttpResponse> doAsyncInvoke(Invocation invocation) {
CompletableFuture<ClientHttpResponse> completableFuture = new CompletableFuture<>();
holder.value = invocation;
completableFuture.complete(new CseClientHttpResponse(Response.ok("result")));
return completableFuture;
}
};
byte[] body = "abc".getBytes();
client.setRequestBody(body);
client.executeAsync();
Assert.assertArrayEquals(body, (byte[]) holder.value.getInvocationArguments().get("input"));
}
Aggregations