Search in sources :

Example 96 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class SentinelAnnotationInterceptorIntegrationTest 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)

Example 97 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class FluxSentinelOperatorTestIntegrationTest method testEmitMultipleValuesWhenFlowControlTriggered.

@Test
public void testEmitMultipleValuesWhenFlowControlTriggered() {
    String resourceName = createResourceName("testEmitMultipleValuesWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    StepVerifier.create(Flux.just(1, 3, 5).map(e -> e * 2).transform(new SentinelReactorTransformer<>(resourceName))).expectError(BlockException.class).verify();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
    assertEquals(1, cn.blockRequest());
    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 98 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule 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 99 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class SentinelAnnotationQuarkusAdapterTest method testBlockHandlerNotFound.

@Test
public void testBlockHandlerNotFound() {
    assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
    String resourceName = "apiBaz";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    assertThrows(ArcUndeclaredThrowableException.class, () -> {
        fooService.baz("Sentinel");
    });
}
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 100 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule 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)

Aggregations

FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)113 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)31 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)22 List (java.util.List)17 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)11 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 Test (org.junit.jupiter.api.Test)6 ClusterFlowConfig (com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig)5 TypeReference (com.alibaba.fastjson.TypeReference)5 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)5 Before (org.junit.Before)5 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)4 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)3 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)3 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)3 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)3 IOException (java.io.IOException)3