Search in sources :

Example 76 with ClusterNode

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

the class MonoSentinelOperatorIntegrationTest method testMultipleReactorTransformerLatterFlowControl.

@Test
public void testMultipleReactorTransformerLatterFlowControl() {
    String resourceName1 = createResourceName("testMultipleReactorTransformerLatterFlowControl1");
    String resourceName2 = createResourceName("testMultipleReactorTransformerLatterFlowControl2");
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName2).setCount(0)));
    StepVerifier.create(Mono.just(2).transform(new SentinelReactorTransformer<>(resourceName1)).transform(new SentinelReactorTransformer<>(resourceName2))).expectError(BlockException.class).verify();
    ClusterNode cn1 = ClusterBuilderSlot.getClusterNode(resourceName1);
    assertNotNull(cn1);
    ClusterNode cn2 = ClusterBuilderSlot.getClusterNode(resourceName2);
    assertNotNull(cn2);
    assertEquals(1, cn2.blockRequest());
    assertEquals(1, cn1.totalSuccess());
    FlowRuleManager.loadRules(new ArrayList<>());
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Test(org.junit.Test)

Example 77 with ClusterNode

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

the class MonoSentinelOperatorIntegrationTest method testEmitSingleLongTimeRt.

@Test
public void testEmitSingleLongTimeRt() {
    String resourceName = createResourceName("testEmitSingleLongTimeRt");
    StepVerifier.create(Mono.just(2).delayElement(Duration.ofMillis(1000)).map(e -> e * 2).transform(new SentinelReactorTransformer<>(resourceName))).expectNext(4).verifyComplete();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1000, cn.avgRt(), 20);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 78 with ClusterNode

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

the class MonoSentinelOperatorIntegrationTest method testEmitEmptySuccess.

@Test
public void testEmitEmptySuccess() {
    String resourceName = createResourceName("testEmitEmptySuccess");
    StepVerifier.create(Mono.empty().transform(new SentinelReactorTransformer<>(resourceName))).verifyComplete();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 79 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode 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)

Example 80 with ClusterNode

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

the class ProviderFilterTest method testDefaultFallback.

@Test
public void testDefaultFallback() {
    String url = "/test/hello";
    String resourceName = "GET:" + url;
    configureRulesFor(resourceName, 0);
    Response response = given().get(url);
    response.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(0, cn.passQps(), 0.01);
}
Also used : Response(io.restassured.response.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