Search in sources :

Example 46 with ClusterNode

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

the class CommonFilterTest method testUrlCleaner.

private void testUrlCleaner() throws Exception {
    final String fooPrefix = "/foo/";
    String url1 = fooPrefix + 1;
    String url2 = fooPrefix + 2;
    WebCallbackManager.setUrlCleaner(new UrlCleaner() {

        @Override
        public String clean(String originUrl) {
            if (originUrl.startsWith(fooPrefix)) {
                return "/foo/*";
            }
            return originUrl;
        }
    });
    this.mvc.perform(get(url1).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Hello 1"));
    this.mvc.perform(get(url2).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Hello 2"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(fooPrefix + "*");
    assertEquals(2, cn.passQps(), 0.01);
    assertNull(ClusterBuilderSlot.getClusterNode(url1));
    assertNull(ClusterBuilderSlot.getClusterNode(url2));
    WebCallbackManager.setUrlCleaner(new DefaultUrlCleaner());
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner) UrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner)

Example 47 with ClusterNode

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

the class CommonFilterTest method testCommonFilterMiscellaneous.

@Test
public void testCommonFilterMiscellaneous() throws Exception {
    Constants.ROOT.removeChildList();
    String url = "/hello";
    this.mvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(HELLO_STR));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(url);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
    String context = "";
    for (Node n : Constants.ROOT.getChildList()) {
        if (n instanceof EntranceNode) {
            String id = ((EntranceNode) n).getId().getName();
            if (url.equals(id)) {
                context = ((EntranceNode) n).getId().getName();
            }
        }
    }
    assertEquals("", context);
    testCommonBlockAndRedirectBlockPage(url, cn);
    // Test for url cleaner.
    testUrlCleaner();
    testUrlExclusion();
    testCustomOriginParser();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 48 with ClusterNode

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

the class SentinelSpringMvcIntegrationTest method testRuntimeException.

@Test
public void testRuntimeException() throws Exception {
    String url = "/runtimeException";
    configureExceptionRulesFor(url, 3, null);
    int repeat = 3;
    for (int i = 0; i < repeat; i++) {
        this.mvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(ResultWrapper.error().toJsonString()));
        ClusterNode cn = ClusterBuilderSlot.getClusterNode(url);
        assertNotNull(cn);
        assertEquals(i + 1, cn.passQps(), 0.01);
    }
    // This will be blocked and response json.
    this.mvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(ResultWrapper.blocked().toJsonString()));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(url);
    assertNotNull(cn);
    assertEquals(repeat, cn.passQps(), 0.01);
    assertEquals(1, cn.blockRequest(), 1);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 49 with ClusterNode

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

the class SentinelSpringMvcIntegrationTest method testTotalInterceptor.

@Test
public void testTotalInterceptor() throws Exception {
    String url = "/hello";
    String totalTarget = "my_spring_mvc_total_url_request";
    for (int i = 0; i < 3; i++) {
        this.mvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(HELLO_STR));
    }
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(totalTarget);
    assertNotNull(cn);
    assertEquals(3, cn.passQps(), 0.01);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 50 with ClusterNode

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

the class SentinelSpringMvcIntegrationTest method testBase.

@Test
public void testBase() throws Exception {
    String url = "/hello";
    this.mvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(HELLO_STR));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(url);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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