Search in sources :

Example 6 with ClusterNode

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

Example 7 with ClusterNode

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

Example 8 with ClusterNode

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

Example 9 with ClusterNode

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

Example 10 with ClusterNode

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