Search in sources :

Example 86 with ClusterNode

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

the class SentinelAnnotationQuarkusAdapterTest method testFallbackWithNoParams.

@Test
public void testFallbackWithNoParams() throws Exception {
    assertThat(fooService.fooWithFallback(1)).isEqualTo("Hello for 1");
    String resourceName = "apiFooWithFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    // Fallback should be ignored for this.
    try {
        fooService.fooWithFallback(5758);
        fail("should not reach here");
    } catch (IllegalAccessException e) {
        assertThat(cn.exceptionQps()).isZero();
    }
    // Fallback should take effect.
    assertThat(fooService.fooWithFallback(5763)).isEqualTo("eee...");
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    // Fallback should not take effect for BlockException, as blockHandler is configured.
    assertThat(fooService.fooWithFallback(2221)).isEqualTo("Oops, 2221");
    assertThat(cn.blockQps()).isPositive();
}
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)

Example 87 with ClusterNode

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

the class SentinelAnnotationQuarkusAdapterTest method testDefaultFallbackWithSingleParam.

@Test
public void testDefaultFallbackWithSingleParam() {
    assertThat(fooService.anotherFoo(1)).isEqualTo("Hello for 1");
    String resourceName = "apiAnotherFooWithDefaultFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    // Default fallback should take effect.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    // Default fallback should also take effect for BlockException.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.blockQps()).isPositive();
}
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)

Example 88 with ClusterNode

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

the class SentinelJaxRsQuarkusAdapterTest method testUrlPathParam.

@Test
public void testUrlPathParam() {
    String url = "/test/hello/{name}";
    String resourceName = "GET:" + url;
    String url1 = "/test/hello/abc";
    Response response1 = given().get(url1);
    response1.then().statusCode(200).body(equalTo("Hello abc !"));
    String url2 = "/test/hello/def";
    Response response2 = given().get(url2);
    response2.then().statusCode(200).body(equalTo("Hello def !"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(2, cn.passQps(), 0.01);
    assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url1));
    assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url2));
}
Also used : Response(io.restassured.response.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 89 with ClusterNode

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

the class SentinelJaxRsQuarkusAdapterTest method testExceptionMapper.

@Test
public void testExceptionMapper() {
    String url = "/test/ex";
    String resourceName = "GET:" + url;
    Response response = given().get(url);
    response.then().statusCode(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).body(equalTo("test exception mapper"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
}
Also used : Response(io.restassured.response.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 90 with ClusterNode

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

the class SentinelJaxRsQuarkusAdapterTest method testCustomRequestOriginParser.

@Test
public void testCustomRequestOriginParser() {
    String url = "/test/hello";
    String resourceName = "GET:" + url;
    String limitOrigin = "appB";
    final String headerName = "X-APP";
    configureRulesFor(resourceName, 0, limitOrigin);
    SentinelJaxRsConfig.setRequestOriginParser(new RequestOriginParser() {

        @Override
        public String parseOrigin(ContainerRequestContext request) {
            String origin = request.getHeaderString(headerName);
            return origin != null ? origin : "";
        }
    });
    Response response = given().header(headerName, "appA").get(url);
    response.then().statusCode(200).body(equalTo(HELLO_STR));
    Response blockedResp = given().header(headerName, "appB").get(url);
    blockedResp.then().statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
    assertEquals(1, cn.blockQps(), 0.01);
}
Also used : Response(io.restassured.response.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) RequestOriginParser(com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser) 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