Java Code Examples for com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot#getClusterNode()

The following examples show how to use com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot#getClusterNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientAsyncGetHello() throws InterruptedException, ExecutionException {
    final String url = "/test/async-hello";
    final String resourceName = "GET:" + url;

    Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
        @Override
        public Future<Response> get() {
            return client.target(host).path(url).request()
                    .async()
                    .get();
        }
    });

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(HELLO_STR, future.get().readEntity(String.class));

    cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
 
Example 2
Source File: SentinelGrpcClientInterceptorTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testGrpcClientInterceptor() throws Exception {
    final int port = 19328;

    configureFlowRule(threshold);
    server.start(port, false);

    FooServiceClient client = new FooServiceClient("localhost", port, new SentinelGrpcClientInterceptor());

    assertTrue(sendRequest(client));
    ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(resourceName, EntryType.OUT);
    assertNotNull(clusterNode);
    assertEquals(1, clusterNode.totalRequest() - clusterNode.blockRequest());

    // Not allowed to pass.
    configureFlowRule(0);

    // The second request will be blocked.
    assertFalse(sendRequest(client));
    assertEquals(1, clusterNode.blockRequest());

    server.stop();
}
 
Example 3
Source File: CommonFilterMethodTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommonFilterMiscellaneous() throws Exception {
    String url = "/hello";
    this.mvc.perform(get(url))
            .andExpect(status().isOk())
            .andExpect(content().string(HELLO_STR));

    ClusterNode cnGet = ClusterBuilderSlot.getClusterNode(GET + COLON + url);
    assertNotNull(cnGet);
    assertEquals(1, cnGet.passQps(), 0.01);


    ClusterNode cnPost = ClusterBuilderSlot.getClusterNode(POST + COLON + url);
    assertNull(cnPost);

    this.mvc.perform(post(url))
            .andExpect(status().isOk())
            .andExpect(content().string(HELLO_POST_STR));

    cnPost = ClusterBuilderSlot.getClusterNode(POST + COLON + url);
    assertNotNull(cnPost);
    assertEquals(1, cnPost.passQps(), 0.01);

    testCommonBlockAndRedirectBlockPage(url, cnGet, cnPost);
}
 
Example 4
Source File: SentinelAnnotationQuarkusAdapterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlockHandlerNotFound() {
    assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
    String resourceName = "apiBaz";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    FlowRuleManager.loadRules(Collections.singletonList(
            new FlowRule(resourceName).setCount(0)
    ));

    Assertions.assertThrows(ArcUndeclaredThrowableException.class, () -> {
        fooService.baz("Sentinel");
    });
}
 
Example 5
Source File: MonoSentinelOperatorIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@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<>());
}
 
Example 6
Source File: SentinelJaxRsQuarkusAdapterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncGetHello() {
    String url = "/test/async-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);
}
 
Example 7
Source File: ProviderFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testUrlPathParam() {
    String url = "/test/hello/{name}";
    String resourceName = "GET:" + url;

    String url1 = "/test/hello/abc";
    Response response1 = given().get(url1);
    response1.then().statusCode(200).body(equalTo("Hello abc !"));

    String url2 = "/test/hello/def";
    Response response2 = given().get(url2);
    response2.then().statusCode(200).body(equalTo("Hello def !"));

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(2, cn.passQps(), 0.01);

    assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url1));
    assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url2));
}
 
Example 8
Source File: SentinelJaxRsQuarkusAdapterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testUrlPathParam() {
    String url = "/test/hello/{name}";
    String resourceName = "GET:" + url;

    String url1 = "/test/hello/abc";
    Response response1 = given().get(url1);
    response1.then().statusCode(200).body(equalTo("Hello abc !"));

    String url2 = "/test/hello/def";
    Response response2 = given().get(url2);
    response2.then().statusCode(200).body(equalTo("Hello def !"));

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(2, cn.passQps(), 0.01);

    assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url1));
    assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url2));
}
 
Example 9
Source File: SentinelOkHttpInterceptorTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testSentinelOkHttpInterceptor0() throws Exception {

    String url0 = "http://localhost:" + port + "/okhttp/back";
    SentinelOkHttpConfig.setPrefix("okhttp:");
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(new SentinelOkHttpInterceptor())
            .build();
    Request request = new Request.Builder()
            .url(url0)
            .build();
    System.out.println(client.newCall(request).execute().body().string());
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(SentinelOkHttpConfig.getPrefix() + "GET:" + url0);
    assertNotNull(cn);

    Constants.ROOT.removeChildList();
    ClusterBuilderSlot.getClusterNodeMap().clear();
}
 
Example 10
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test(expected = UndeclaredThrowableException.class)
public void testBlockHandlerNotFound() {
    assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
    String resourceName = "apiBaz";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    FlowRuleManager.loadRules(Collections.singletonList(
            new FlowRule(resourceName).setCount(0)
    ));
    fooService.baz("Sentinel");
}
 
Example 11
Source File: FetchClusterNodeByIdCommandHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String id = request.getParam("id");
    if (StringUtil.isEmpty(id)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Invalid parameter: empty clusterNode name"));
    }
    ClusterNode node = ClusterBuilderSlot.getClusterNode(id);
    if (node != null) {
        return CommandResponse.ofSuccess(JSON.toJSONString(NodeVo.fromClusterNode(id, node)));
    } else {
        return CommandResponse.ofSuccess("{}");
    }
}
 
Example 12
Source File: SentinelAnnotationQuarkusAdapterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 13
Source File: SentinelSpringMvcIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 14
Source File: FluxSentinelOperatorTestIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: FluxSentinelOperatorTestIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 16
Source File: SentinelAnnotationInterceptorIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test(expected = FlowException.class)
public void testBlockHandlerNotFound() {
    assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
    String resourceName = "apiBaz";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    FlowRuleManager.loadRules(Collections.singletonList(
            new FlowRule(resourceName).setCount(0)
    ));
    fooService.baz("Sentinel");
}
 
Example 17
Source File: SentinelJaxRsQuarkusAdapterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultFallback() {
    String url = "/test/hello";
    String resourceName = "GET:" + url;
    configureRulesFor(resourceName, 0);
    Response response = given().get(url);
    response.then().statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode())
            .body(equalTo("Blocked by Sentinel (flow limiting)"));

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
}
 
Example 18
Source File: SentinelWebFluxIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testWebFluxFilterBasic() throws Exception {
    String url = "/hello";
    this.webClient.get()
        .uri(url)
        .accept(MediaType.TEXT_PLAIN)
        .exchange()
        .expectStatus().isOk()
        .expectBody(String.class).isEqualTo(HELLO_STR);

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(url);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
 
Example 19
Source File: ReactorSphUTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testReactorEntryWithCommon() {
    String resourceName = createResourceName("testReactorEntryWithCommon");
    StepVerifier.create(ReactorSphU.entryWith(resourceName, Mono.just(60))
        .subscribeOn(Schedulers.elastic())
        .map(e -> e * 3))
        .expectNext(180)
        .verifyComplete();

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
 
Example 20
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@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();
}