Search in sources :

Example 21 with ClusterNode

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

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

the class FluxSentinelOperatorTestIntegrationTest method testEmitMultipleValueSuccess.

@Test
public void testEmitMultipleValueSuccess() {
    String resourceName = createResourceName("testEmitMultipleSuccess");
    StepVerifier.create(Flux.just(1, 2).map(e -> e * 2).transform(new SentinelReactorTransformer<>(resourceName))).expectNext(2).expectNext(4).verifyComplete();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
Also used : Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) ClusterBuilderSlot(com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) 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) Test(org.junit.Test)

Example 23 with ClusterNode

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

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

the class MonoSentinelOperatorIntegrationTest method testFluxToMonoNextThenCancelSuccess.

@Test
public void testFluxToMonoNextThenCancelSuccess() {
    String resourceName = createResourceName("testFluxToMonoNextThenCancelSuccess");
    StepVerifier.create(Flux.range(1, 10).map(e -> e * 2).next().transform(new SentinelReactorTransformer<>(resourceName))).expectNext(2).verifyComplete();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
Also used : 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) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 25 with ClusterNode

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

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