Search in sources :

Example 26 with ClusterNode

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

the class MonoSentinelOperatorIntegrationTest method testEmitExceptionWhenFlowControlTriggered.

@Test
public void testEmitExceptionWhenFlowControlTriggered() {
    String resourceName = createResourceName("testEmitExceptionWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    StepVerifier.create(Mono.error(new IllegalStateException("some")).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 27 with ClusterNode

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

the class MonoSentinelOperatorIntegrationTest method testEmitSingleError.

@Test
public void testEmitSingleError() {
    String resourceName = createResourceName("testEmitSingleError");
    StepVerifier.create(Mono.error(new IllegalStateException()).transform(new SentinelReactorTransformer<>(resourceName))).expectError(IllegalStateException.class).verify();
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.totalException());
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 28 with ClusterNode

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

the class SentinelOkHttpInterceptorTest method testSentinelOkHttpInterceptor0.

@Test
public void testSentinelOkHttpInterceptor0() throws Exception {
    // With prefix
    SentinelOkHttpConfig config = new SentinelOkHttpConfig("okhttp:");
    String url0 = "http://localhost:" + port + "/okhttp/back";
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new SentinelOkHttpInterceptor(config)).build();
    Request request = new Request.Builder().url(url0).build();
    System.out.println(client.newCall(request).execute().body().string());
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(config.getResourcePrefix() + "GET:" + url0);
    assertNotNull(cn);
    Constants.ROOT.removeChildList();
    ClusterBuilderSlot.getClusterNodeMap().clear();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 29 with ClusterNode

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

the class SentinelJaxRsQuarkusAdapterTest method testGetHello.

@Test
public void testGetHello() {
    String url = "/test/hello";
    String resourceName = "GET:" + url;
    Response response = given().get(url);
    response.then().statusCode(200).body(equalTo(HELLO_STR));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    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);
}
Also used : Response(io.restassured.response.Response) 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) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 30 with ClusterNode

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

the class SentinelJaxRsQuarkusAdapterTest method testCustomFallback.

@Test
public void testCustomFallback() {
    String url = "/test/hello";
    String resourceName = "GET:" + url;
    SentinelJaxRsConfig.setJaxRsFallback(new SentinelJaxRsFallback() {

        @Override
        public javax.ws.rs.core.Response fallbackResponse(String route, Throwable cause) {
            return javax.ws.rs.core.Response.status(javax.ws.rs.core.Response.Status.OK).entity("Blocked by Sentinel (flow limiting)").type(MediaType.APPLICATION_JSON_TYPE).build();
        }

        @Override
        public Future<javax.ws.rs.core.Response> fallbackFutureResponse(final String route, final Throwable cause) {
            return new FutureTask<>(new Callable<javax.ws.rs.core.Response>() {

                @Override
                public javax.ws.rs.core.Response call() throws Exception {
                    return fallbackResponse(route, cause);
                }
            });
        }
    });
    configureRulesFor(resourceName, 0);
    Response response = given().get(url);
    response.then().statusCode(javax.ws.rs.core.Response.Status.OK.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Callable(java.util.concurrent.Callable) Response(io.restassured.response.Response) Future(java.util.concurrent.Future) SentinelJaxRsFallback(com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.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