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));
}
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));
}
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());
}
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());
}
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());
}
Aggregations