Search in sources :

Example 1 with FilterInvoker

use of com.alipay.sofa.rpc.filter.FilterInvoker in project Sentinel by alibaba.

the class AbstractSofaRpcFilterTest method testNeedToLoadProviderAndConsumer.

@Test
public void testNeedToLoadProviderAndConsumer() {
    SentinelSofaRpcProviderFilter providerFilter = new SentinelSofaRpcProviderFilter();
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId(Serializer.class.getName());
    providerConfig.setId("AAA");
    FilterInvoker providerInvoker = new FilterInvoker(null, null, providerConfig);
    assertTrue(providerFilter.needToLoad(providerInvoker));
    SentinelSofaRpcConsumerFilter consumerFilter = new SentinelSofaRpcConsumerFilter();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId(Serializer.class.getName());
    consumerConfig.setId("BBB");
    FilterInvoker consumerInvoker = new FilterInvoker(null, null, consumerConfig);
    assertTrue(consumerFilter.needToLoad(consumerInvoker));
    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(providerInvoker));
    assertTrue(consumerFilter.needToLoad(consumerInvoker));
    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
    assertTrue(providerFilter.needToLoad(providerInvoker));
    RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(providerInvoker));
    assertFalse(consumerFilter.needToLoad(consumerInvoker));
}
Also used : FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Serializer(com.alipay.sofa.rpc.codec.Serializer) Test(org.junit.Test)

Example 2 with FilterInvoker

use of com.alipay.sofa.rpc.filter.FilterInvoker in project Sentinel by alibaba.

the class AbstractSofaRpcFilterTest method testNeedToLoadProvider.

@Test
public void testNeedToLoadProvider() {
    SentinelSofaRpcProviderFilter providerFilter = new SentinelSofaRpcProviderFilter();
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId(Serializer.class.getName());
    providerConfig.setId("AAA");
    FilterInvoker invoker = new FilterInvoker(null, null, providerConfig);
    assertTrue(providerFilter.needToLoad(invoker));
    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(invoker));
    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
    assertTrue(providerFilter.needToLoad(invoker));
    RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(invoker));
}
Also used : FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Serializer(com.alipay.sofa.rpc.codec.Serializer) Test(org.junit.Test)

Example 3 with FilterInvoker

use of com.alipay.sofa.rpc.filter.FilterInvoker in project Sentinel by alibaba.

the class SentinelSofaRpcProviderFilterTest method testInvokeSentinelWorks.

@Test
public void testInvokeSentinelWorks() {
    SentinelSofaRpcProviderFilter filter = new SentinelSofaRpcProviderFilter();
    final String applicationName = "demo-provider";
    final String interfaceResourceName = "com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService";
    final String methodResourceName = "com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService#sayHello(java.lang.String,int)";
    SofaRequest request = mock(SofaRequest.class);
    when(request.getRequestProp("app")).thenReturn(applicationName);
    when(request.getInvokeType()).thenReturn(RpcConstants.INVOKER_TYPE_SYNC);
    when(request.getInterfaceName()).thenReturn(interfaceResourceName);
    when(request.getMethodName()).thenReturn("sayHello");
    when(request.getMethodArgSigs()).thenReturn(new String[] { "java.lang.String", "int" });
    when(request.getMethodArgs()).thenReturn(new Object[] { "Sentinel", 2020 });
    FilterInvoker filterInvoker = mock(FilterInvoker.class);
    when(filterInvoker.invoke(request)).thenAnswer(new Answer<SofaResponse>() {

        @Override
        public SofaResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            verifyInvocationStructure(applicationName, interfaceResourceName, methodResourceName);
            SofaResponse response = new SofaResponse();
            response.setAppResponse("Hello Sentinel 2020");
            return response;
        }
    });
    // Before invoke
    assertNull(ContextUtil.getContext());
    // Do invoke
    SofaResponse response = filter.invoke(filterInvoker, request);
    assertEquals("Hello Sentinel 2020", response.getAppResponse());
    verify(filterInvoker).invoke(request);
    // After invoke, make sure exit context
    assertNull(ContextUtil.getContext());
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 4 with FilterInvoker

use of com.alipay.sofa.rpc.filter.FilterInvoker in project Sentinel by alibaba.

the class SofaRpcFallbackRegistryTest method testCustomFallback.

@Test
public void testCustomFallback() {
    // Test invoke custom provider fallback
    SofaRpcFallbackRegistry.setProviderFallback(new SofaRpcFallback() {

        @Override
        public SofaResponse handle(FilterInvoker invoker, SofaRequest request, BlockException ex) {
            SofaResponse response = new SofaResponse();
            response.setAppResponse("test provider response");
            return response;
        }
    });
    SofaResponse providerResponse = SofaRpcFallbackRegistry.getProviderFallback().handle(null, null, null);
    assertNotNull(providerResponse);
    assertEquals("test provider response", providerResponse.getAppResponse());
    // Test invoke custom consumer fallback
    SofaRpcFallbackRegistry.setConsumerFallback(new SofaRpcFallback() {

        @Override
        public SofaResponse handle(FilterInvoker invoker, SofaRequest request, BlockException ex) {
            SofaResponse response = new SofaResponse();
            response.setAppResponse("test consumer response");
            return response;
        }
    });
    SofaResponse consumerResponse = SofaRpcFallbackRegistry.getConsumerFallback().handle(null, null, null);
    assertNotNull(consumerResponse);
    assertEquals("test consumer response", consumerResponse.getAppResponse());
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 5 with FilterInvoker

use of com.alipay.sofa.rpc.filter.FilterInvoker in project sofa-rpc by sofastack.

the class HystrixFilterAsyncTest method testHystrixThreadPoolRejectedFallback.

@Test
public void testHystrixThreadPoolRejectedFallback() throws InterruptedException {
    // 强制开启熔断
    SetterFactory setterFactory = new SetterFactory() {

        @Override
        public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) {
            String groupKey = invoker.getConfig().getInterfaceId() + "thread_pool_rejected";
            String commandKey = request.getMethodName() + "thread_pool_rejected";
            return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)).andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(false)).andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(1));
        }
    };
    providerConfig = defaultServer(2000);
    providerConfig.export();
    consumerConfig = defaultClient().setTimeout(10000);
    SofaHystrixConfig.registerFallbackFactory(consumerConfig, new HystrixServiceFallbackFactory());
    SofaHystrixConfig.registerSetterFactory(consumerConfig, setterFactory);
    HystrixService HystrixService = consumerConfig.refer();
    // wait server ok
    Thread.sleep(3000);
    for (int i = 0; i < 20; i++) {
        long start = System.currentTimeMillis();
        HystrixService.sayHello("abc", 24);
        Future future = SofaResponseFuture.getFuture();
        // 第一个请求用于阻塞线程池,其他请求会直接线程池拒绝
        if (i > 0) {
            Assert.assertTrue(future.isDone());
            String result = (String) SofaResponseFuture.getResponse(10000, true);
            Assert.assertEquals("fallback abc from server! age: 24, error: java.util.concurrent.RejectedExecutionException", result);
            Assert.assertTrue((System.currentTimeMillis() - start) < HYSTRIX_DEFAULT_TIMEOUT);
        }
    }
    Thread.sleep(3000);
    // 只有第一个线程执行了,所以服务端只会收到一个请求
    Assert.assertEquals(1, ((InvokeCounterHystrixService) providerConfig.getRef()).getExecuteCount());
}
Also used : HystrixCommand(com.netflix.hystrix.HystrixCommand) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) SofaResponseFuture(com.alipay.sofa.rpc.api.future.SofaResponseFuture) Future(java.util.concurrent.Future) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Aggregations

FilterInvoker (com.alipay.sofa.rpc.filter.FilterInvoker)8 Test (org.junit.Test)8 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)5 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)4 Serializer (com.alipay.sofa.rpc.codec.Serializer)3 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)3 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)3 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)1 SofaResponseFuture (com.alipay.sofa.rpc.api.future.SofaResponseFuture)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 Filter (com.alipay.sofa.rpc.filter.Filter)1 EchoService (com.alipay.sofa.rpc.test.EchoService)1 HelloService (com.alipay.sofa.rpc.test.HelloService)1 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)1 HystrixCommand (com.netflix.hystrix.HystrixCommand)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Future (java.util.concurrent.Future)1