com.alibaba.csp.sentinel.slots.block.flow.FlowException Java Examples

The following examples show how to use com.alibaba.csp.sentinel.slots.block.flow.FlowException. 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: ClusterNodeTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testTraceException() {
    ClusterNode clusterNode = new ClusterNode();

    Exception exception = new RuntimeException("test");

    // test count<=0, no exceptionQps added
    clusterNode.trace(exception, 0);
    clusterNode.trace(exception, -1);
    assertEquals(0, clusterNode.exceptionQps(), 0.01);
    assertEquals(0, clusterNode.totalException());

    // test count=1, not BlockException, 1 exceptionQps added
    clusterNode.trace(exception, 1);
    assertEquals(1, clusterNode.exceptionQps(), 0.01);
    assertEquals(1, clusterNode.totalException());

    // test count=1, BlockException, no exceptionQps added
    FlowException flowException = new FlowException("flow");
    clusterNode.trace(flowException, 1);
    assertEquals(1, clusterNode.exceptionQps(), 0.01);
    assertEquals(1, clusterNode.totalException());
}
 
Example #2
Source File: ZuulBlockFallbackProviderTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlowControlFallbackResponse() throws Exception {
    ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE);
    BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE,
        new FlowException("flow exception"));
    Assert.assertEquals(clientHttpResponse.getCode(), 429);
}
 
Example #3
Source File: MetricEntryCallbackTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void onBlocked() throws Exception {
    FakeMetricExtension extension = new FakeMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);

    MetricEntryCallback entryCallback = new MetricEntryCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn("origin1");
    int count = 2;
    Object[] args = {"args1", "args2"};
    entryCallback.onBlocked(new FlowException("xx"), context, resourceWrapper, null, count, args);
    Assert.assertEquals(extension.block, count);
}
 
Example #4
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 #5
Source File: DubboFallbackRegistryTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomFallback() {
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.setConsumerFallback(new DubboFallback() {
        @Override
        public Result handle(Invoker<?> invoker, Invocation invocation, BlockException e) {
            return new RpcResult("Error: " + e.getClass().getName());
        }
    });
    Result result = DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
    Assert.assertFalse("The invocation should not fail", result.hasException());
    Assert.assertEquals("Error: " + ex.getClass().getName(), result.getValue());
}
 
Example #6
Source File: DubboFallbackRegistryTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test(expected = SentinelRpcException.class)
public void testDefaultFallback() {
    // Test for default.
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
}
 
Example #7
Source File: ZuulBlockFallbackManagerTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProvider() throws Exception {
    MyNullResponseFallBackProvider myNullResponseFallBackProvider = new MyNullResponseFallBackProvider();
    ZuulBlockFallbackManager.registerProvider(myNullResponseFallBackProvider);
    Assert.assertEquals(myNullResponseFallBackProvider.getRoute(), ROUTE);
    Assert.assertNull(myNullResponseFallBackProvider.fallbackResponse(ROUTE, new FlowException("flow ex")));
}
 
Example #8
Source File: ZuulBlockFallbackProviderTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlowControlFallbackResponse() throws Exception {
    ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE);
    BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE,
        new FlowException("flow exception"));
    Assert.assertEquals(clientHttpResponse.getCode(), 429);
}
 
Example #9
Source File: DubboFallbackRegistryTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomFallback() {
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.setConsumerFallback(
            (invoker, invocation, e) -> AsyncRpcResult.newDefaultAsyncResult("Error: " + e.getClass().getName(), invocation));
    Result result = DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
    Assert.assertFalse("The invocation should not fail", result.hasException());
    Assert.assertEquals("Error: " + ex.getClass().getName(), result.getValue());
}
 
Example #10
Source File: DubboFallbackRegistryTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test(expected = SentinelRpcException.class)
public void testDefaultFallback() {
    // Test for default.
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
}
 
Example #11
Source File: ZuulBlockFallbackManagerTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProvider() throws Exception {
    MyNullResponseFallBackProvider myNullResponseFallBackProvider = new MyNullResponseFallBackProvider();
    ZuulBlockFallbackManager.registerProvider(myNullResponseFallBackProvider);
    Assert.assertEquals(myNullResponseFallBackProvider.getRoute(), ROUTE);
    Assert.assertNull(myNullResponseFallBackProvider.fallbackResponse(ROUTE, new FlowException("flow ex")));
}
 
Example #12
Source File: ZuulBlockFallbackProviderTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlowControlFallbackResponse() throws Exception {
    ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE);
    BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE,
        new FlowException("flow exception"));
    Assert.assertEquals(clientHttpResponse.getCode(), 429);
}
 
Example #13
Source File: DubboFallbackRegistryTest.java    From dubbo-sentinel-support with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomFallback() {
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.setConsumerFallback(
        (invoker, invocation, e) -> new RpcResult("Error: " + e.getClass().getName()));
    Result result = DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
    Assert.assertFalse("The invocation should not fail", result.hasException());
    Assert.assertEquals("Error: " + ex.getClass().getName(), result.getValue());
}
 
Example #14
Source File: DubboFallbackRegistryTest.java    From dubbo-sentinel-support with Apache License 2.0 5 votes vote down vote up
@Test(expected = SentinelRpcException.class)
public void testDefaultFallback() {
    // Test for default.
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
}
 
Example #15
Source File: MetricEntryCallbackTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void onBlocked() throws Exception {
    FakeMetricExtension extension = new FakeMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);

    MetricEntryCallback entryCallback = new MetricEntryCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn("origin1");
    int count = 2;
    Object[] args = {"args1", "args2"};
    entryCallback.onBlocked(new FlowException("xx"), context, resourceWrapper, null, count, args);
    Assert.assertEquals(extension.block, count);
}
 
Example #16
Source File: DubboFallbackRegistryTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomFallback() {
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.setConsumerFallback(new DubboFallback() {
        @Override
        public Result handle(Invoker<?> invoker, Invocation invocation, BlockException e) {
            return new RpcResult("Error: " + e.getClass().getName());
        }
    });
    Result result = DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
    Assert.assertFalse("The invocation should not fail", result.hasException());
    Assert.assertEquals("Error: " + ex.getClass().getName(), result.getValue());
}
 
Example #17
Source File: DubboFallbackRegistryTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test(expected = SentinelRpcException.class)
public void testDefaultFallback() {
    // Test for default.
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
}
 
Example #18
Source File: DubboFallbackRegistryTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomFallback() {
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.setConsumerFallback(
        (invoker, invocation, e) -> new RpcResult("Error: " + e.getClass().getName()));
    Result result = DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
    Assert.assertFalse("The invocation should not fail", result.hasException());
    Assert.assertEquals("Error: " + ex.getClass().getName(), result.getValue());
}
 
Example #19
Source File: DubboFallbackRegistryTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test(expected = SentinelRpcException.class)
public void testDefaultFallback() {
    // Test for default.
    BlockException ex = new FlowException("xxx");
    DubboFallbackRegistry.getConsumerFallback()
        .handle(null, null, ex);
}
 
Example #20
Source File: ZuulBlockFallbackManagerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProvider() throws Exception {
    MyNullResponseFallBackProvider myNullResponseFallBackProvider = new MyNullResponseFallBackProvider();
    ZuulBlockFallbackManager.registerProvider(myNullResponseFallBackProvider);
    Assert.assertEquals(myNullResponseFallBackProvider.getRoute(), ROUTE);
    Assert.assertNull(myNullResponseFallBackProvider.fallbackResponse(ROUTE, new FlowException("flow ex")));
}
 
Example #21
Source File: OkHttpFallbackTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test(expected = SentinelRpcException.class)
public void testDefaultOkHttpFallback() {
    BlockException e = new FlowException("xxx");
    OkHttpFallback fallback = new DefaultOkHttpFallback();
    fallback.handle(null, null, e);
}
 
Example #22
Source File: WebFluxTestController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@GetMapping("/error")
public Mono<?> apiError() {
    return Mono.error(new FlowException("testWebFluxError"));
}
 
Example #23
Source File: WebFluxTestController.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@GetMapping("/error")
public Mono<?> apiError() {
    return Mono.error(new FlowException("testWebFluxError"));
}