use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ClientFilterTest method testClientGetHello.
@Test
public void testClientGetHello() {
final String url = "/test/hello";
String resourceName = "GET:" + url;
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request().get();
}
});
assertEquals(200, response.getStatus());
assertEquals(HELLO_STR, response.readEntity(String.class));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
String context = "";
for (Node n : Constants.ROOT.getChildList()) {
if (n instanceof EntranceNode) {
String id = ((EntranceNode) n).getId().getName();
if (url.equals(id)) {
context = ((EntranceNode) n).getId().getName();
}
}
}
assertEquals("", context);
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ClientFilterTest method testClientCustomFallback.
@Test
public void testClientCustomFallback() {
final String url = "/test/hello";
final String resourceName = "GET:" + url;
configureRulesFor(resourceName, 0);
SentinelJaxRsConfig.setJaxRsFallback(new SentinelJaxRsFallback() {
@Override
public javax.ws.rs.core.Response fallbackResponse(String route, Throwable cause) {
return javax.ws.rs.core.Response.status(javax.ws.rs.core.Response.Status.OK).entity("Blocked by Sentinel (flow limiting)").type(MediaType.APPLICATION_JSON_TYPE).build();
}
@Override
public Future<Response> fallbackFutureResponse(final String route, final Throwable cause) {
return new FutureTask<>(new Callable<Response>() {
@Override
public Response call() throws Exception {
return fallbackResponse(route, cause);
}
});
}
});
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request().get();
}
});
assertEquals(javax.ws.rs.core.Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("Blocked by Sentinel (flow limiting)", response.readEntity(String.class));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ClientFilterTest method testCancelFuture.
@Test
public void testCancelFuture() {
final String url = "/test/delay/10";
final String resourceName = "GET:/test/delay/{seconds}";
try {
Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
@Override
public Future<Response> get() {
return client.target(host).path(url).request().async().get();
}
});
future.cancel(false);
} catch (Exception e) {
// ignore
}
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ClientFilterTest method testServerReturn400.
@Test
public void testServerReturn400() {
final String url = "/test/400";
final String resourceName = "GET:" + url;
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request().get();
}
});
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
assertEquals("test return 400", response.readEntity(String.class));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelGrpcClientInterceptorTest method testGrpcClientInterceptor.
@Test
public void testGrpcClientInterceptor() throws Exception {
final int port = 19328;
server.start(port, false);
client = new FooServiceClient("localhost", port, new SentinelGrpcClientInterceptor());
configureFlowRule(Integer.MAX_VALUE);
assertTrue(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(666).build()));
ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(fullMethodName, EntryType.OUT);
assertNotNull(clusterNode);
assertEquals(1, clusterNode.totalPass());
// Not allowed to pass.
configureFlowRule(0);
// The second request will be blocked.
assertFalse(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(666).build()));
assertEquals(1, clusterNode.blockRequest());
configureFlowRule(Integer.MAX_VALUE);
assertFalse(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(-1).build()));
assertEquals(1, clusterNode.totalException());
configureFlowRule(Integer.MAX_VALUE);
assertTrue(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(-2).build()));
assertTrue(clusterNode.avgRt() >= 1000);
server.stop();
}
Aggregations