Search in sources :

Example 1 with MockProtocol

use of org.apache.dubbo.rpc.support.MockProtocol in project dubbo by alibaba.

the class MockClusterInvokerTest method testMockInvokerInvoke_normal.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerInvoke_normal() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName());
    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://localhost/" + IHelloService.class.getName() + "?getSomething.mock=return aa");
    Protocol protocol = new MockProtocol();
    Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl);
    invokers.add(mInvoker1);
    // Configured with mock
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getSomething");
    Result ret = cluster.invoke(invocation);
    Assertions.assertEquals("something", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("sayHello");
    ret = cluster.invoke(invocation);
    Assertions.assertNull(ret.getValue());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 2 with MockProtocol

use of org.apache.dubbo.rpc.support.MockProtocol in project dubbo by alibaba.

the class MockClusterInvokerTest method testMockInvokerInvoke_forcemock_defaultreturn.

@Test
public void testMockInvokerInvoke_forcemock_defaultreturn() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName());
    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://localhost/" + IHelloService.class.getName() + "?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());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 3 with MockProtocol

use of org.apache.dubbo.rpc.support.MockProtocol in project dubbo by alibaba.

the class MockClusterInvokerTest method testMockInvokerInvoke_failmock.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerInvoke_failmock() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).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://localhost/" + IHelloService.class.getName() + "?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);
    // 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());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 4 with MockProtocol

use of org.apache.dubbo.rpc.support.MockProtocol 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());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Aggregations

URL (org.apache.dubbo.common.URL)4 Protocol (org.apache.dubbo.rpc.Protocol)4 Result (org.apache.dubbo.rpc.Result)4 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)4 MockProtocol (org.apache.dubbo.rpc.support.MockProtocol)4 Test (org.junit.jupiter.api.Test)4