Search in sources :

Example 81 with ClusterNode

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);
}
Also used : Response(javax.ws.rs.core.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 82 with ClusterNode

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);
}
Also used : Response(javax.ws.rs.core.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 83 with ClusterNode

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();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Connection(okhttp3.Connection) DefaultOkHttpFallback(com.alibaba.csp.sentinel.adapter.okhttp.fallback.DefaultOkHttpFallback) OkHttpResourceExtractor(com.alibaba.csp.sentinel.adapter.okhttp.extractor.OkHttpResourceExtractor) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 84 with ClusterNode

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();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 85 with ClusterNode

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");
    });
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)92 Test (org.junit.Test)61 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)26 Response (io.restassured.response.Response)14 Node (com.alibaba.csp.sentinel.node.Node)13 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)13 Test (org.junit.jupiter.api.Test)13 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)13 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)11 Response (javax.ws.rs.core.Response)11 Context (com.alibaba.csp.sentinel.context.Context)10 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)10 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)9 ArrayList (java.util.ArrayList)7 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)6 ClusterBuilderSlot (com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot)6 Collections (java.util.Collections)6 Assert (org.junit.Assert)6