com.alibaba.csp.sentinel.node.ClusterNode Java Examples

The following examples show how to use com.alibaba.csp.sentinel.node.ClusterNode. 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: FlowRuleCheckerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testOtherOriginFlowSelectNode() {
    String originA = "appA";
    String originB = "appB";

    DefaultNode node = mock(DefaultNode.class);
    DefaultNode originNode = mock(DefaultNode.class);
    ClusterNode cn = mock(ClusterNode.class);
    when(node.getClusterNode()).thenReturn(cn);
    Context context = mock(Context.class);
    when(context.getOriginNode()).thenReturn(originNode);

    FlowRule ruleA = new FlowRule("testOtherOriginFlowSelectNode").setCount(1);
    ruleA.setLimitApp(originA);
    FlowRule ruleB = new FlowRule("testOtherOriginFlowSelectNode").setCount(2);
    ruleB.setLimitApp(RuleConstant.LIMIT_APP_OTHER);
    FlowRuleManager.loadRules(Arrays.asList(ruleA, ruleB));

    // Origin matches other, return the origin node.
    when(context.getOrigin()).thenReturn(originB);
    assertEquals(originNode, FlowRuleChecker.selectNodeByRequesterAndStrategy(ruleB, context, node));

    // Origin matches limitApp of an existing rule, so no nodes are selected.
    when(context.getOrigin()).thenReturn(originA);
    assertNull(FlowRuleChecker.selectNodeByRequesterAndStrategy(ruleB, context, node));
}
 
Example #2
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 #3
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 #4
Source File: CommonFilterMethodTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void testCommonBlockAndRedirectBlockPage(String url, ClusterNode cnGet, ClusterNode cnPost) throws Exception {
    configureRulesFor(GET + ":" + url, 0);
    // The request will be blocked and response is default block message.
    this.mvc.perform(get(url).accept(MediaType.TEXT_PLAIN))
            .andExpect(status().isTooManyRequests())
            .andExpect(content().string(FilterUtil.DEFAULT_BLOCK_MSG));
    assertEquals(1, cnGet.blockQps(), 0.01);

    // Test for post pass
    this.mvc.perform(post(url))
            .andExpect(status().isOk())
            .andExpect(content().string(HELLO_POST_STR));

    assertEquals(2, cnPost.passQps(), 0.01);


    FlowRuleManager.loadRules(null);
    WebServletConfig.setBlockPage("");
}
 
Example #5
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 #6
Source File: FlowRuleCheckerTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomOriginFlowSelectNode() {
    String origin = "appA";
    String limitAppB = "appB";

    DefaultNode node = mock(DefaultNode.class);
    DefaultNode originNode = mock(DefaultNode.class);
    ClusterNode cn = mock(ClusterNode.class);
    when(node.getClusterNode()).thenReturn(cn);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn(origin);
    when(context.getOriginNode()).thenReturn(originNode);

    FlowRule rule = new FlowRule("testCustomOriginFlowSelectNode").setCount(1);
    rule.setLimitApp(origin);
    // Origin matches, return the origin node.
    assertEquals(originNode, FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));

    rule.setLimitApp(limitAppB);
    // Origin mismatch, no node found.
    assertNull(FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));
}
 
Example #7
Source File: SentinelAnnotationQuarkusAdapterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultFallbackWithSingleParam() {
    assertThat(fooService.anotherFoo(1)).isEqualTo("Hello for 1");
    String resourceName = "apiAnotherFooWithDefaultFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    // Default fallback should take effect.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();

    FlowRuleManager.loadRules(Collections.singletonList(
            new FlowRule(resourceName).setCount(0)
    ));
    // Default fallback should also take effect for BlockException.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.blockQps()).isPositive();
}
 
Example #8
Source File: MonoSentinelOperatorIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleReactorTransformerLatterFlowControl() {
    String resourceName1 = createResourceName("testMultipleReactorTransformerLatterFlowControl1");
    String resourceName2 = createResourceName("testMultipleReactorTransformerLatterFlowControl2");
    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName2).setCount(0)
    ));
    StepVerifier.create(Mono.just(2)
        .transform(new SentinelReactorTransformer<>(resourceName1))
        .transform(new SentinelReactorTransformer<>(resourceName2)))
        .expectError(BlockException.class)
        .verify();

    ClusterNode cn1 = ClusterBuilderSlot.getClusterNode(resourceName1);
    assertNotNull(cn1);
    ClusterNode cn2 = ClusterBuilderSlot.getClusterNode(resourceName2);
    assertNotNull(cn2);
    assertEquals(1, cn2.blockRequest());
    assertEquals(1, cn1.totalSuccess());
    FlowRuleManager.loadRules(new ArrayList<>());
}
 
Example #9
Source File: CommonFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #10
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 #11
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultFallbackWithSingleParam() {
    assertThat(fooService.anotherFoo(1)).isEqualTo("Hello for 1");
    String resourceName = "apiAnotherFooWithDefaultFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    // Default fallback should take effect.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();

    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    // Default fallback should also take effect for BlockException.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.blockQps()).isPositive();
}
 
Example #12
Source File: CommonFilterTest.java    From Sentinel-Dashboard-Nacos 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 cn = ClusterBuilderSlot.getClusterNode(url);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);

    testCommonBlockAndRedirectBlockPage(url, cn);

    // Test for url cleaner.
    testUrlCleaner();

    testCustomOriginParser();
}
 
Example #13
Source File: MonoSentinelOperatorIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@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<>());
}
 
Example #14
Source File: CommonFilterMethodTest.java    From Sentinel-Dashboard-Nacos 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 #15
Source File: CommonFilterMethodTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void testCommonBlockAndRedirectBlockPage(String url, ClusterNode cnGet, ClusterNode cnPost) throws Exception {
    configureRulesFor(GET + ":" + url, 0);
    // The request will be blocked and response is default block message.
    this.mvc.perform(get(url).accept(MediaType.TEXT_PLAIN))
            .andExpect(status().isOk())
            .andExpect(content().string(FilterUtil.DEFAULT_BLOCK_MSG));
    assertEquals(1, cnGet.blockQps(), 0.01);

    // Test for post pass
    this.mvc.perform(post(url))
            .andExpect(status().isOk())
            .andExpect(content().string(HELLO_POST_STR));

    assertEquals(2, cnPost.passQps(), 0.01);


    FlowRuleManager.loadRules(null);
    WebServletConfig.setBlockPage("");
}
 
Example #16
Source File: SentinelGrpcServerInterceptorTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testGrpcServerInterceptor() throws Exception {
    final int port = 19329;
    client = new FooServiceClient("localhost", port);

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

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

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

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

    server.stop();
}
 
Example #17
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 #18
Source File: NodeVo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * {@link ClusterNode} holds total statistics of the same resource name.
 *
 * @param name resource name.
 * @param node the ClusterNode to be presented.
 * @return node view object.
 */
public static NodeVo fromClusterNode(String name, ClusterNode node) {
    if (node == null) {
        return null;
    }
    NodeVo vo = new NodeVo();
    vo.resource = name;
    vo.threadNum = node.curThreadNum();
    vo.passQps = (long) node.passQps();
    vo.blockQps = (long) node.blockQps();
    vo.totalQps = (long) node.totalQps();
    vo.averageRt = (long) node.avgRt();
    vo.successQps = (long) node.successQps();
    vo.exceptionQps = (long) node.exceptionQps();
    vo.oneMinuteException = node.totalException();
    vo.oneMinutePass = node.totalRequest() - node.blockRequest();
    vo.oneMinuteBlock = node.blockRequest();
    vo.oneMinuteTotal = node.totalRequest();
    vo.timestamp = System.currentTimeMillis();
    return vo;
}
 
Example #19
Source File: ClientFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testServerReturn500() {
    final String url = "/test/ex";
    final String resourceName = "GET:" + url;
    Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
        @Override
        public Response get() {
            return client.target(host).path(url).request()
                    .get();
        }
    });
    assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
    assertEquals("test exception mapper", response.readEntity(String.class));


    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
 
Example #20
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testClassLevelDefaultFallbackWithSingleParam() {
    assertThat(barService.anotherBar(1)).isEqualTo("Hello for 1");
    String resourceName = "apiAnotherBarWithDefaultFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    assertThat(barService.doSomething(1)).isEqualTo("do something");
    String resourceName1 = "com.alibaba.csp.sentinel.annotation.aspectj.integration.service.BarService:doSomething(int)";
    ClusterNode cn1 = ClusterBuilderSlot.getClusterNode(resourceName1);
    assertThat(cn1).isNotNull();
    assertThat(cn1.passQps()).isPositive();

    assertThat(barService.anotherBar(5758)).isEqualTo("eee...");
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();

    assertThat(barService.doSomething(5758)).isEqualTo("GlobalFallback:doFallback");
    assertThat(cn1.exceptionQps()).isPositive();
    assertThat(cn1.blockQps()).isZero();
}
 
Example #21
Source File: ClientFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCancelFuture() {
    final String url = "/test/delay/10";
    final String resourceName = "GET:/test/delay/{seconds}";
    try {
        Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
            @Override
            public Future<Response> get() {
                return client.target(host).path(url).request()
                        .async()
                        .get();
            }
        });
        future.cancel(false);
    } catch (Exception e) {
        //ignore
    }

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
}
 
Example #22
Source File: FetchSimpleClusterNodeCommandHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    /*
     * type==notZero means nodes whose totalRequest <= 0 will be ignored.
     */
    String type = request.getParam("type");
    List<NodeVo> list = new ArrayList<NodeVo>();
    Map<ResourceWrapper, ClusterNode> map = ClusterBuilderSlot.getClusterNodeMap();
    if (map == null) {
        return CommandResponse.ofSuccess(JSONArray.toJSONString(list));
    }
    for (Map.Entry<ResourceWrapper, ClusterNode> entry : map.entrySet()) {
        if ("notZero".equalsIgnoreCase(type)) {
            if (entry.getValue().totalRequest() > 0) {
                list.add(NodeVo.fromClusterNode(entry.getKey(), entry.getValue()));
            }
        } else {
            list.add(NodeVo.fromClusterNode(entry.getKey(), entry.getValue()));
        }
    }
    return CommandResponse.ofSuccess(JSONArray.toJSONString(list));
}
 
Example #23
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultFallbackWithSingleParam() {
    assertThat(fooService.anotherFoo(1)).isEqualTo("Hello for 1");
    String resourceName = "apiAnotherFooWithDefaultFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    // Default fallback should take effect.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();

    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    // Default fallback should also take effect for BlockException.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.blockQps()).isPositive();
}
 
Example #24
Source File: CommonFilterContextTest.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 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(url, context);
}
 
Example #25
Source File: FluxSentinelOperatorTestIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmitMultipleValuesWhenFlowControlTriggered() {
    String resourceName = createResourceName("testEmitMultipleValuesWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    StepVerifier.create(Flux.just(1, 3, 5)
        .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 #26
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 #27
Source File: CommonFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #28
Source File: ReactorSphUTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testReactorEntryWithBizException() {
    String resourceName = createResourceName("testReactorEntryWithBizException");
    StepVerifier.create(ReactorSphU.entryWith(resourceName, Mono.error(new IllegalStateException())))
        .expectError(IllegalStateException.class)
        .verify();

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
    assertEquals(1, cn.totalException());
}
 
Example #29
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 #30
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);
}