use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class MockClusterInvokerTest method testMockInvokerInvoke_forcemock.
/**
* Test if mock policy works fine: force-mock
*/
@Test
public void testMockInvokerInvoke_forcemock() {
URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName());
url = url.addParameter(MOCK_KEY, "force:return null").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName()));
URL mockUrl = URL.valueOf("mock://localhost/" + IHelloService.class.getName() + "?getSomething.mock=return aa&getSomething3xx.mock=return xx").addParameters(url.getParameters());
Protocol protocol = new MockProtocol();
Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl);
Invoker<IHelloService> cluster = getClusterInvokerMock(url, mInvoker1);
// Configured with mock
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("getSomething");
Result ret = cluster.invoke(invocation);
Assertions.assertEquals("aa", ret.getValue());
// If no mock was configured, return null directly
invocation = new RpcInvocation();
invocation.setMethodName("getSomething2");
ret = cluster.invoke(invocation);
Assertions.assertNull(ret.getValue());
// If no mock was configured, return null directly
invocation = new RpcInvocation();
invocation.setMethodName("sayHello");
ret = cluster.invoke(invocation);
Assertions.assertNull(ret.getValue());
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class MockProviderRpcExceptionTest method testMockInvokerProviderRpcException.
/**
* Test if mock policy works fine: ProviderRpcException
*/
@Test
public void testMockInvokerProviderRpcException() {
URL url = URL.valueOf("remote://1.2.3.4/" + IHelloRpcService.class.getName());
url = url.addParameter(MOCK_KEY, "true").addParameter("invoke_return_error", "true").addParameter(REFER_KEY, "path%3dorg.apache.dubbo.rpc.cluster.support.wrapper.MockProviderRpcExceptionTest%24IHelloRpcService");
Invoker<IHelloRpcService> cluster = getClusterInvoker(url);
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("getSomething4");
Result ret = cluster.invoke(invocation);
Assertions.assertEquals("something4mock", ret.getValue());
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class ForkingClusterInvokerTest method testInvokeNoException.
@Test()
public void testInvokeNoException() {
resetInvokerToNoException();
ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<ForkingClusterInvokerTest>(dic);
Result ret = invoker.invoke(invocation);
Assertions.assertSame(result, ret);
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class MetricsFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (exported.compareAndSet(false, true)) {
this.protocolName = invoker.getUrl().getParameter(METRICS_PROTOCOL) == null ? DEFAULT_PROTOCOL : invoker.getUrl().getParameter(METRICS_PROTOCOL);
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocolName);
this.port = invoker.getUrl().getParameter(METRICS_PORT) == null ? protocol.getDefaultPort() : Integer.parseInt(invoker.getUrl().getParameter(METRICS_PORT));
Invoker<MetricsService> metricsInvoker = initMetricsInvoker();
try {
protocol.export(metricsInvoker);
} catch (RuntimeException e) {
logger.error("Metrics Service need to be configured" + " when multiple processes are running on a host" + e.getMessage());
}
}
RpcContext context = RpcContext.getContext();
boolean isProvider = context.isProviderSide();
long start = System.currentTimeMillis();
try {
// proceed invocation chain
Result result = invoker.invoke(invocation);
long duration = System.currentTimeMillis() - start;
reportMetrics(invoker, invocation, duration, "success", isProvider);
return result;
} catch (RpcException e) {
long duration = System.currentTimeMillis() - start;
String result = "error";
if (e.isTimeout()) {
result = "timeoutError";
}
if (e.isBiz()) {
result = "bisError";
}
if (e.isNetwork()) {
result = "networkError";
}
if (e.isSerialization()) {
result = "serializationError";
}
reportMetrics(invoker, invocation, duration, result, isProvider);
throw e;
}
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class ProviderAuthFilterTest method testAuthFailedWhenParameterError.
@Test
void testAuthFailedWhenParameterError() {
String service = "org.apache.dubbo.DemoService";
String method = "test";
Object[] originalParams = new Object[] { "dubbo1", "dubbo2" };
long currentTimeMillis = System.currentTimeMillis();
URL url = URL.valueOf("dubbo://10.10.10.10:2181").setServiceInterface(service).addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test-provider").addParameter(Constants.PARAMETER_SIGNATURE_ENABLE_KEY, true).addParameter(Constants.SERVICE_AUTH, true);
Invoker invoker = mock(Invoker.class);
Invocation invocation = mock(Invocation.class);
when(invocation.getObjectAttachment(Constants.AK_KEY)).thenReturn("ak");
when(invocation.getObjectAttachment(CommonConstants.CONSUMER)).thenReturn("test-consumer");
when(invocation.getObjectAttachment(Constants.REQUEST_TIMESTAMP_KEY)).thenReturn(currentTimeMillis);
when(invocation.getMethodName()).thenReturn(method);
Object[] fakeParams = new Object[] { "dubbo1", "dubbo3" };
when(invocation.getArguments()).thenReturn(fakeParams);
when(invoker.getUrl()).thenReturn(url);
String requestString = String.format(Constants.SIGNATURE_STRING_FORMAT, url.getColonSeparatedKey(), invocation.getMethodName(), "sk", currentTimeMillis);
String sign = SignatureUtils.sign(originalParams, requestString, "sk");
when(invocation.getObjectAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(sign);
ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
Result result = providerAuthFilter.invoke(invoker, invocation);
assertTrue(result.hasException());
assertTrue(result.getException() instanceof RpcAuthenticationException);
}
Aggregations