MockClusterInvoker implements ClusterInvoker<T> { @Override public Result invoke(Invocation invocation) throws RpcException { Result result = null; String value = getUrl().getMethodParameter(invocation.getMethodName(), MOCK_KEY, Boolean.FALSE.toString()).trim(); if (value.length() == 0 || "false".equalsIgnoreCase(value)) { result = this.invoker.invoke(invocation); } else if (value.startsWith("force")) { if (logger.isWarnEnabled()) { logger.warn("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + getUrl()); } result = doMockInvoke(invocation, null); } else { try { result = this.invoker.invoke(invocation); if(result.getException() != null && result.getException() instanceof RpcException){ RpcException rpcException= (RpcException)result.getException(); if(rpcException.isBiz()){ throw rpcException; }else { result = doMockInvoke(invocation, rpcException); } } } catch (RpcException e) { if (e.isBiz()) { throw e; } if (logger.isWarnEnabled()) { logger.warn("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + getUrl(), e); } result = doMockInvoke(invocation, e); } } return result; } MockClusterInvoker(Directory<T> directory, Invoker<T> invoker); @Override URL getUrl(); URL getRegistryUrl(); @Override Directory<T> getDirectory(); @Override boolean isDestroyed(); @Override boolean isAvailable(); @Override void destroy(); @Override Class<T> getInterface(); @Override Result invoke(Invocation invocation); @Override String toString();  }
@Test public void testMockInvokerInvoke_normal() { URL url = URL.valueOf("remote: url = url.addParameter(MOCK_KEY, "fail") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())); Invoker<IHelloService> cluster = getClusterInvoker(url); URL mockUrl = URL.valueOf("mock: + "?getSomething.mock=return aa"); Protocol protocol = new MockProtocol(); Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl); invokers.add(mInvoker1); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("something", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); }
@Test public void testMockInvokerInvoke_failmock() { URL url = URL.valueOf("remote: .addParameter(MOCK_KEY, "fail:return null") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); URL mockUrl = URL.valueOf("mock: + "?getSomething.mock=return aa").addParameters(url.getParameters()); Protocol protocol = new MockProtocol(); Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl); Invoker<IHelloService> cluster = getClusterInvokerMock(url, mInvoker1); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("aa", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); }
@Test public void testMockInvokerInvoke_forcemock() { URL url = URL.valueOf("remote: url = url.addParameter(MOCK_KEY, "force:return null") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())); URL mockUrl = URL.valueOf("mock: + "?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); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("aa", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); }
@Test public void testMockInvokerInvoke_forcemock_defaultreturn() { URL url = URL.valueOf("remote: url = url.addParameter(MOCK_KEY, "force") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())); Invoker<IHelloService> cluster = getClusterInvoker(url); URL mockUrl = URL.valueOf("mock: + "?getSomething.mock=return aa&getSomething3xx.mock=return xx&sayHello.mock=return ") .addParameters(url.getParameters()); Protocol protocol = new MockProtocol(); Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl); invokers.add(mInvoker1); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); Result ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_someMethods() { URL url = URL.valueOf("remote: .addParameter("getSomething.mock", "fail:return x") .addParameter("getSomething2.mock", "force:return y") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("something", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assertions.assertEquals("something3", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithOutDefault() { URL url = URL.valueOf("remote: .addParameter("getSomething.mock", "fail:return x") .addParameter("getSomething2.mock", "force:return y") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); try { ret = cluster.invoke(invocation); Assertions.fail(); } catch (RpcException e) { } }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithDefault() { URL url = URL.valueOf("remote: .addParameter("mock", "fail:return null") .addParameter("getSomething.mock", "fail:return x") .addParameter("getSomething2.mock", "force:return y") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertNull(ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithFailDefault() { URL url = URL.valueOf("remote: .addParameter("mock", "fail:return z") .addParameter("getSomething.mock", "fail:return x") .addParameter("getSomething2.mock", "force:return y") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assertions.assertEquals("z", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertEquals("z", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithForceDefault() { URL url = URL.valueOf("remote: .addParameter("mock", "force:return z") .addParameter("getSomething.mock", "fail:return x") .addParameter("getSomething2.mock", "force:return y") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assertions.assertEquals("z", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertEquals("z", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_Default() { URL url = URL.valueOf("remote: .addParameter("mock", "fail:return x") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_return() { URL url = URL.valueOf("remote: .addParameter("getSomething.mock", "return x") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); try { ret = cluster.invoke(invocation); Assertions.fail("fail invoke"); } catch (RpcException e) { } }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock() { URL url = URL.valueOf("remote: .addParameter("mock", "true") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("somethingmock", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock2() { URL url = URL.valueOf("remote: .addParameter("mock", "fail") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("somethingmock", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock3() { URL url = URL.valueOf("remote: .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("mock", "force"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertEquals("somethingmock", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_check_String() { URL url = URL.valueOf("remote: .addParameter("getSomething.mock", "force:return 1688") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assertions.assertTrue(ret.getValue() instanceof String, "result type must be String but was : " + ret.getValue().getClass()); Assertions.assertEquals("1688", (String) ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_check_int() { URL url = URL.valueOf("remote: .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("getInt1.mock", "force:return 1688") .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getInt1"); Result ret = cluster.invoke(invocation); Assertions.assertTrue(ret.getValue() instanceof Integer, "result type must be integer but was : " + ret.getValue().getClass()); Assertions.assertEquals(new Integer(1688), (Integer) ret.getValue()); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListString_empty() { URL url = URL.valueOf("remote: .addParameter("getListString.mock", "force:return empty") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getListString"); Result ret = cluster.invoke(invocation); Assertions.assertEquals(0, ((List<String>) ret.getValue()).size()); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListString() { URL url = URL.valueOf("remote: .addParameter("getListString.mock", "force:return [\"hi\",\"hi2\"]") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getListString"); Result ret = cluster.invoke(invocation); List<String> rl = (List<String>) ret.getValue(); Assertions.assertEquals(2, rl.size()); Assertions.assertEquals("hi", rl.get(0)); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListPojo_empty() { URL url = URL.valueOf("remote: .addParameter("getUsers.mock", "force:return empty") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getUsers"); Result ret = cluster.invoke(invocation); Assertions.assertEquals(0, ((List<User>) ret.getValue()).size()); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListPojo() { URL url = URL.valueOf("remote: .addParameter("getUsers.mock", "force:return [{id:1, name:\"hi1\"}, {id:2, name:\"hi2\"}]") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getUsers"); Result ret = cluster.invoke(invocation); List<User> rl = (List<User>) ret.getValue(); System.out.println(rl); Assertions.assertEquals(2, rl.size()); Assertions.assertEquals("hi1", ((User) rl.get(0)).getName()); }
@Test public void testMockInvokerFromOverride_Invoke_check_ListPojo_error() { URL url = URL.valueOf("remote: .addParameter("getUsers.mock", "force:return [{id:x, name:\"hi1\"}]") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getUsers"); try { cluster.invoke(invocation); } catch (RpcException e) { } }
@Test public void testMockInvokerFromOverride_Invoke_force_throw() { URL url = URL.valueOf("remote: .addParameter("getBoolean2.mock", "force:throw ") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); Assertions.fail(); } catch (RpcException e) { Assertions.assertFalse(e.isBiz(), "not custem exception"); } }
@Test public void testMockInvokerFromOverride_Invoke_force_throwCustemException() throws Throwable { URL url = URL.valueOf("remote: .addParameter("getBoolean2.mock", "force:throw org.apache.dubbo.rpc.cluster.support.wrapper.MyMockException") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation).recreate(); Assertions.fail(); } catch (MyMockException e) { } }
@Test public void testMockInvokerFromOverride_Invoke_force_throwCustemExceptionNotFound() { URL url = URL.valueOf("remote: .addParameter("getBoolean2.mock", "force:throw java.lang.RuntimeException2") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); Assertions.fail(); } catch (Exception e) { Assertions.assertTrue(e.getCause() instanceof IllegalStateException); } }
@Test public void testMockInvokerFromOverride_Invoke_mock_false() { URL url = URL.valueOf("remote: .addParameter("mock", "false") .addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())) .addParameter("invoke_return_error", "true"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); Assertions.fail(); } catch (RpcException e) { Assertions.assertTrue(e.isTimeout()); } }