Search in sources :

Example 16 with ClusterNode

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

the class ProviderFilterTest 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) Test(org.junit.Test)

Example 17 with ClusterNode

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

the class ProviderFilterTest method testAsyncGetHello.

@Test
public void testAsyncGetHello() {
    String url = "/test/async-hello";
    String resourceName = "GET:" + url;
    Response response = given().get(url);
    response.then().statusCode(200).body(equalTo(HELLO_STR));
    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(io.restassured.response.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 18 with ClusterNode

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

the class SentinelAnnotationQuarkusAdapterTest method testNormalBlockHandlerAndFallback.

@Test
public void testNormalBlockHandlerAndFallback() throws Exception {
    assertThat(fooService.foo(1)).isEqualTo("Hello for 1");
    String resourceName = "apiFoo";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    // Test for biz exception.
    try {
        fooService.foo(5758);
        fail("should not reach here");
    } catch (Exception ex) {
        // Should not be traced.
        assertThat(cn.exceptionQps()).isZero();
    }
    try {
        fooService.foo(5763);
        fail("should not reach here");
    } catch (Exception ex) {
        assertThat(cn.exceptionQps()).isPositive();
    }
    // Test for blockHandler
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    assertThat(fooService.foo(1121)).isEqualTo("Oops, 1121");
    assertThat(cn.blockQps()).isPositive();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ArcUndeclaredThrowableException(io.quarkus.arc.ArcUndeclaredThrowableException) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 19 with ClusterNode

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

the class SentinelAnnotationQuarkusAdapterTest method testForeignBlockHandlerClass.

@Test
public void testForeignBlockHandlerClass() throws Exception {
    assertThat(fooService.random()).isNotEqualTo(FooUtil.BLOCK_FLAG);
    String resourceName = MethodUtil.resolveMethodName(FooService.class.getDeclaredMethod("random"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    assertThat(fooService.random()).isEqualTo(FooUtil.BLOCK_FLAG);
    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 20 with ClusterNode

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

the class ReactorSphUTest method testReactorEntryWithBizException.

@Test
public void testReactorEntryWithBizException() {
    String resourceName = createResourceName("testReactorEntryWithBizException");
    StepVerifier.create(ReactorSphU.entryWith(resourceName, Mono.error(new IllegalStateException()))).expectError(IllegalStateException.class).verify();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
    assertEquals(1, cn.totalException());
}
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