use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ClientFilterTest method testClientFallback.
@Test
public void testClientFallback() {
final String url = "/test/hello";
final String resourceName = "GET:" + url;
configureRulesFor(resourceName, 0);
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.TOO_MANY_REQUESTS.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 testServerReturn500.
@Test
public void testServerReturn500() {
final String url = "/test/ex";
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.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
assertEquals("test exception mapper", 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 SentinelOkHttpInterceptorTest method testSentinelOkHttpInterceptor1.
@Test
public void testSentinelOkHttpInterceptor1() throws Exception {
String url0 = "http://localhost:" + port + "/okhttp/back/1";
SentinelOkHttpConfig config = new SentinelOkHttpConfig(new OkHttpResourceExtractor() {
@Override
public String extract(Request request, Connection connection) {
String regex = "/okhttp/back/";
String url = request.url().toString();
if (url.contains(regex)) {
url = url.substring(0, url.indexOf(regex) + regex.length()) + "{id}";
}
return request.method() + ":" + url;
}
}, new DefaultOkHttpFallback());
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new SentinelOkHttpInterceptor(config)).build();
Request request = new Request.Builder().url(url0).build();
System.out.println(client.newCall(request).execute().body().string());
String url1 = config.getResourcePrefix() + "GET:http://localhost:" + port + "/okhttp/back/{id}";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(url1);
assertNotNull(cn);
Constants.ROOT.removeChildList();
ClusterBuilderSlot.getClusterNodeMap().clear();
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationQuarkusAdapterTest method testAnnotationExceptionsToIgnore.
@Test
public void testAnnotationExceptionsToIgnore() {
assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
String resourceName = "apiBaz";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
assertThrows(IllegalMonitorStateException.class, () -> {
fooService.baz("fail");
});
assertThat(cn.exceptionQps()).isZero();
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationQuarkusAdapterTest method testBlockHandlerNotFound.
@Test
public void testBlockHandlerNotFound() {
assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
String resourceName = "apiBaz";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
assertThrows(ArcUndeclaredThrowableException.class, () -> {
fooService.baz("Sentinel");
});
}
Aggregations