use of com.alipay.sofa.rpc.core.response.SofaResponse 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.core.response.SofaResponse in project Sentinel by alibaba.
the class SentinelSofaRpcConsumerFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
// Now only support sync invoke.
if (request.getInvokeType() != null && !RpcConstants.INVOKER_TYPE_SYNC.equals(request.getInvokeType())) {
return invoker.invoke(request);
}
String interfaceResourceName = getInterfaceResourceName(request);
String methodResourceName = getMethodResourceName(request);
Entry interfaceEntry = null;
Entry methodEntry = null;
try {
interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT);
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, getMethodArguments(request));
SofaResponse response = invoker.invoke(request);
traceResponseException(response, interfaceEntry, methodEntry);
return response;
} catch (BlockException e) {
return SofaRpcFallbackRegistry.getConsumerFallback().handle(invoker, request, e);
} catch (Throwable t) {
throw traceOtherException(t, interfaceEntry, methodEntry);
} finally {
if (methodEntry != null) {
methodEntry.exit(1, getMethodArguments(request));
}
if (interfaceEntry != null) {
interfaceEntry.exit();
}
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse 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.core.response.SofaResponse in project sofa-rpc by sofastack.
the class SofaRpcMetricsTest method buildResponse.
private SofaResponse buildResponse() {
SofaResponse response = new SofaResponse();
response.setAppResponse("123");
return response;
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class FaultToleranceSubscriberTest method onProviderEvent.
@Test
public void onProviderEvent() {
FaultToleranceConfig config = new FaultToleranceConfig();
config.setRegulationEffective(true);
FaultToleranceConfigManager.putAppConfig(APP_NAME1, config);
ProviderInfo providerInfo1 = ProviderHelper.toProviderInfo("127.0.0.1");
ProviderInfo providerInfo2 = ProviderHelper.toProviderInfo("127.0.0.2");
ProviderInfo providerInfo3 = ProviderHelper.toProviderInfo("127.0.0.3");
ProviderInfo providerInfo4 = ProviderHelper.toProviderInfo("127.0.0.4");
ProviderInfo providerInfo5 = ProviderHelper.toProviderInfo("127.0.0.5");
FaultToleranceSubscriber subscriber = new FaultToleranceSubscriber();
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo1, new SofaRequest(), new SofaResponse(), null));
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo2, new SofaRequest(), new SofaResponse(), null));
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo3, new SofaRequest(), new SofaResponse(), null));
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo4, new SofaRequest(), new SofaResponse(), null));
Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 4);
subscriber.onEvent(new ProviderInfoRemoveEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1")))));
Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
subscriber.onEvent(new ProviderInfoUpdateEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.3"), ProviderHelper.toProviderInfo("127.0.0.4"))), new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))));
Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 2);
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo5, new SofaRequest(), new SofaResponse(), null));
Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
subscriber.onEvent(new ProviderInfoUpdateAllEvent(consumerConfig, Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))), Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1"), ProviderHelper.toProviderInfo("127.0.0.4"))))));
Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 1);
}
Aggregations