use of com.alipay.sofa.rpc.core.request.SofaRequest 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.request.SofaRequest in project Sentinel by alibaba.
the class SofaRpcUtilsTest method testGetApplicationName.
@Test
public void testGetApplicationName() {
SofaRequest request = new SofaRequest();
String applicationName = SofaRpcUtils.getApplicationName(request);
assertEquals("", applicationName);
request.addRequestProp("app", "test-app");
applicationName = SofaRpcUtils.getApplicationName(request);
assertEquals("test-app", applicationName);
}
use of com.alipay.sofa.rpc.core.request.SofaRequest in project Sentinel by alibaba.
the class SofaRpcUtilsTest method testGetMethodArguments.
@Test
public void testGetMethodArguments() {
SofaRequest request = new SofaRequest();
request.setMethodArgs(new Object[] { "Sentinel", 2020 });
Object[] arguments = SofaRpcUtils.getMethodArguments(request);
assertEquals(arguments.length, 2);
assertEquals("Sentinel", arguments[0]);
assertEquals(2020, arguments[1]);
}
use of com.alipay.sofa.rpc.core.request.SofaRequest 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.request.SofaRequest in project httpx by servicex-sh.
the class SofaRpcInvocation method content.
public byte[] content() throws Exception {
SofaRequest request = new SofaRequest();
request.setTargetServiceUniqueName(this.targetServiceUniqueName);
request.setMethodName(this.methodName);
request.setMethodArgSigs(this.methodArgSigs);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Hessian2Output out = new HessianSerializerOutput(bos);
out.writeObject(request);
if (stubArguments != null) {
for (Object argument : stubArguments) {
out.writeObject(argument);
}
}
out.flush();
return bos.toByteArray();
}
Aggregations