Search in sources :

Example 11 with FlowRule

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

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

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

the class ReactorSphUTest method testReactorEntryNormalWhenFlowControlTriggered.

@Test
public void testReactorEntryNormalWhenFlowControlTriggered() {
    String resourceName = createResourceName("testReactorEntryNormalWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    StepVerifier.create(ReactorSphU.entryWith(resourceName, Mono.just(60)).subscribeOn(Schedulers.elastic()).map(e -> e * 3)).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 : StepVerifier(reactor.test.StepVerifier) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) ClusterBuilderSlot(com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot) Schedulers(reactor.core.scheduler.Schedulers) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) Collections(java.util.Collections) FlowRuleManager(com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager) ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) 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 14 with FlowRule

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

the class MonoSentinelOperatorIntegrationTest method testTransformMonoWithSentinelContextEnter.

@Test
public void testTransformMonoWithSentinelContextEnter() {
    String resourceName = createResourceName("testTransformMonoWithSentinelContextEnter");
    String contextName = "test_reactive_context";
    String origin = "originA";
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0).setLimitApp(origin).as(FlowRule.class)));
    StepVerifier.create(Mono.just(2).transform(new SentinelReactorTransformer<>(// Customized context with origin.
    new EntryConfig(resourceName, EntryType.OUT, new ContextConfig(contextName, origin))))).expectError(BlockException.class).verify();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
    assertEquals(1, cn.blockRequest());
    assertTrue(Constants.ROOT.getChildList().stream().filter(node -> node instanceof EntranceNode).map(e -> (EntranceNode) e).anyMatch(e -> e.getId().getName().equals(contextName)));
    FlowRuleManager.loadRules(new ArrayList<>());
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) StepVerifier(reactor.test.StepVerifier) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) FlowRuleManager(com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager) ArrayList(java.util.ArrayList) Constants(com.alibaba.csp.sentinel.Constants) Flux(reactor.core.publisher.Flux) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) ClusterBuilderSlot(com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot) Duration(java.time.Duration) EntryType(com.alibaba.csp.sentinel.EntryType) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Assert(org.junit.Assert) Collections(java.util.Collections) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Test(org.junit.Test)

Example 15 with FlowRule

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

the class MonoSentinelOperatorIntegrationTest method testEmitSingleValueWhenFlowControlTriggered.

@Test
public void testEmitSingleValueWhenFlowControlTriggered() {
    String resourceName = createResourceName("testEmitSingleValueWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    StepVerifier.create(Mono.just(1).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)

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