Search in sources :

Example 1 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class ClientFilterTest method testCustomResourceName.

@Test
public void testCustomResourceName() {
    final String url = "/test/hello/{name}";
    final String resourceName = "GET:" + url;
    Response response1 = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {

        @Override
        public Response get() {
            return client.target(host).path(url).resolveTemplate("name", "abc").request().get();
        }
    });
    assertEquals(200, response1.getStatus());
    assertEquals("Hello abc !", response1.readEntity(String.class));
    Response response2 = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {

        @Override
        public Response get() {
            return client.target(host).path(url).resolveTemplate("name", "def").request().get();
        }
    });
    assertEquals(javax.ws.rs.core.Response.Status.OK.getStatusCode(), response2.getStatus());
    assertEquals("Hello def !", response2.readEntity(String.class));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(2, cn.passQps(), 0.01);
    assertNull(ClusterBuilderSlot.getClusterNode("/test/hello/abc"));
    assertNull(ClusterBuilderSlot.getClusterNode("/test/hello/def"));
}
Also used : Response(javax.ws.rs.core.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 2 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class ClientFilterTest method testServerTimeout.

@Test
public void testServerTimeout() {
    final String url = "/test/delay/10";
    final String resourceName = "GET:/test/delay/{seconds}";
    try {
        SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {

            @Override
            public Response get() {
                return client.target(host).path(url).request().get();
            }
        });
    } catch (ProcessingException e) {
    // ignore
    }
    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) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 3 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class ClientFilterTest method testFutureGetServerTimeout.

@Test
public void testFutureGetServerTimeout() {
    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.get();
    } catch (Exception e) {
    // ignore
    }
    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) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 4 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class ClientFilterTest method testFutureGetTimeout.

@Test
public void testFutureGetTimeout() {
    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.get(1, TimeUnit.SECONDS);
    } catch (Exception e) {
    // ignore
    }
    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) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 5 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class ClientFilterTest method testClientAsyncGetHello.

@Test
public void testClientAsyncGetHello() throws InterruptedException, ExecutionException {
    final String url = "/test/async-hello";
    final String resourceName = "GET:" + url;
    Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {

        @Override
        public Future<Response> get() {
            return client.target(host).path(url).request().async().get();
        }
    });
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(HELLO_STR, future.get().readEntity(String.class));
    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)

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