Search in sources :

Example 51 with ClusterNode

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

the class SentinelWebFluxIntegrationTest method testCustomizedUrlCleaner.

@Test
public void testCustomizedUrlCleaner() throws Exception {
    final String fooPrefix = "/foo/";
    String url1 = fooPrefix + 1;
    String url2 = fooPrefix + 2;
    WebFluxCallbackManager.setUrlCleaner(((exchange, originUrl) -> {
        if (originUrl.startsWith(fooPrefix)) {
            return "/foo/*";
        }
        return originUrl;
    }));
    this.webClient.get().uri(url1).exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello 1");
    this.webClient.get().uri(url2).exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello 2");
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(fooPrefix + "*");
    assertEquals(2, cn.passQps(), 0.01);
    assertNull(ClusterBuilderSlot.getClusterNode(url1));
    assertNull(ClusterBuilderSlot.getClusterNode(url2));
    WebFluxCallbackManager.resetUrlCleaner();
}
Also used : WebEnvironment(org.springframework.boot.test.context.SpringBootTest.WebEnvironment) MediaType(org.springframework.http.MediaType) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) FlowRuleManager(com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager) ArrayList(java.util.ArrayList) WebFluxCallbackManager(com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager) StringUtil(com.alibaba.csp.sentinel.util.StringUtil) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) HttpStatus(org.springframework.http.HttpStatus) StringContains(org.hamcrest.core.StringContains) RuleConstant(com.alibaba.csp.sentinel.slots.block.RuleConstant) ClusterBuilderSlot(com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) After(org.junit.After) WebFluxTestApplication(com.alibaba.csp.sentinel.adapter.spring.webflux.test.WebFluxTestApplication) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) SpringRunner(org.springframework.test.context.junit4.SpringRunner) Assert(org.junit.Assert) Collections(java.util.Collections) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Before(org.junit.Before) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 52 with ClusterNode

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

the class MetricTimerListener method run.

@Override
public void run() {
    Map<Long, List<MetricNode>> maps = new TreeMap<>();
    for (Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
        ClusterNode node = e.getValue();
        Map<Long, MetricNode> metrics = node.metrics();
        aggregate(maps, metrics, node);
    }
    aggregate(maps, Constants.ENTRY_NODE.metrics(), Constants.ENTRY_NODE);
    if (!maps.isEmpty()) {
        for (Entry<Long, List<MetricNode>> entry : maps.entrySet()) {
            try {
                metricWriter.write(entry.getKey(), entry.getValue());
            } catch (Exception e) {
                RecordLog.warn("[MetricTimerListener] Write metric error", e);
            }
        }
    }
}
Also used : ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) List(java.util.List) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap)

Example 53 with ClusterNode

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

the class SentinelAnnotationIntegrationTest 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 : FooService(com.alibaba.csp.sentinel.annotation.aspectj.integration.service.FooService) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Test(org.junit.Test)

Example 54 with ClusterNode

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

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

Example 55 with ClusterNode

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

the class SentinelAnnotationIntegrationTest method testFallBackPrivateMethod.

@Test
public void testFallBackPrivateMethod() throws Exception {
    String resourceName = "apiFooWithFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    try {
        fooService.fooWithPrivateFallback(5758);
        fail("should not reach here");
    } catch (Exception ex) {
        // Should not be traced.
        assertThat(cn.exceptionQps()).isZero();
    }
    assertThat(fooService.fooWithPrivateFallback(5763)).isEqualTo("EEE...");
    // Test for blockHandler
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    assertThat(fooService.fooWithPrivateFallback(2221)).isEqualTo("Oops, 2221");
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) 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