Search in sources :

Example 1 with SentinelJaxRsFallback

use of com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback 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 2 with SentinelJaxRsFallback

use of com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback in project Sentinel by alibaba.

the class SentinelJaxRsQuarkusAdapterTest method testCustomFallback.

@Test
public void testCustomFallback() {
    String url = "/test/hello";
    String resourceName = "GET:" + url;
    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<javax.ws.rs.core.Response> fallbackFutureResponse(final String route, final Throwable cause) {
            return new FutureTask<>(new Callable<javax.ws.rs.core.Response>() {

                @Override
                public javax.ws.rs.core.Response call() throws Exception {
                    return fallbackResponse(route, cause);
                }
            });
        }
    });
    configureRulesFor(resourceName, 0);
    Response response = given().get(url);
    response.then().statusCode(javax.ws.rs.core.Response.Status.OK.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Callable(java.util.concurrent.Callable) Response(io.restassured.response.Response) Future(java.util.concurrent.Future) SentinelJaxRsFallback(com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with SentinelJaxRsFallback

use of com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback in project Sentinel by alibaba.

the class ProviderFilterTest method testCustomFallback.

@Test
public void testCustomFallback() {
    String url = "/test/hello";
    String resourceName = "GET:" + url;
    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<javax.ws.rs.core.Response> fallbackFutureResponse(final String route, final Throwable cause) {
            return new FutureTask<>(new Callable<javax.ws.rs.core.Response>() {

                @Override
                public javax.ws.rs.core.Response call() throws Exception {
                    return fallbackResponse(route, cause);
                }
            });
        }
    });
    configureRulesFor(resourceName, 0);
    Response response = given().get(url);
    response.then().statusCode(javax.ws.rs.core.Response.Status.OK.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Callable(java.util.concurrent.Callable) Response(io.restassured.response.Response) Future(java.util.concurrent.Future) SentinelJaxRsFallback(com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback) Test(org.junit.Test)

Aggregations

SentinelJaxRsFallback (com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback)3 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)3 Response (io.restassured.response.Response)2 Callable (java.util.concurrent.Callable)2 Future (java.util.concurrent.Future)2 Test (org.junit.Test)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.jupiter.api.Test)1