add test cases for injvm rpc protocol (#2041)

This commit is contained in:
kimmking 2018-07-09 14:17:16 +08:00 committed by Xin Wang
parent ef78bbdbf3
commit 137393c7a7
3 changed files with 35 additions and 2 deletions

View File

@ -97,7 +97,7 @@ public class InjvmProtocol extends AbstractProtocol implements Protocol {
// Since injvm protocol is configured explicitly, we don't need to set any extra flag, use normal refer process.
if (Constants.LOCAL_PROTOCOL.toString().equals(url.getProtocol())) {
isJvmRefer = false;
} else if (Constants.SCOPE_LOCAL.equals(scope) || (url.getParameter("injvm", false))) {
} else if (Constants.SCOPE_LOCAL.equals(scope) || (url.getParameter(Constants.LOCAL_PROTOCOL, false))) {
// if it's declared as local reference
// 'scope=local' is equivalent to 'injvm=true', injvm will be deprecated in the future release
isJvmRefer = true;

View File

@ -21,6 +21,7 @@ import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
@ -28,9 +29,11 @@ import org.junit.After;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
@ -38,6 +41,11 @@ import static org.junit.Assert.assertTrue;
*/
public class InjvmProtocolTest {
static{
InjvmProtocol injvm = InjvmProtocol.getInjvmProtocol();
}
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
private List<Exporter<?>> exporters = new ArrayList<Exporter<?>>();
@ -53,11 +61,17 @@ public class InjvmProtocolTest {
@Test
public void testLocalProtocol() throws Exception {
DemoService service = new DemoServiceImpl();
Exporter<?> exporter = protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService").addParameter(Constants.INTERFACE_KEY, DemoService.class.getName())));
Invoker<?> invoker = proxy.getInvoker(service, DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService").addParameter(Constants.INTERFACE_KEY, DemoService.class.getName()));
assertTrue(invoker.isAvailable());
Exporter<?> exporter = protocol.export(invoker);
exporters.add(exporter);
service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService").addParameter(Constants.INTERFACE_KEY, DemoService.class.getName())));
assertEquals(service.getSize(new String[]{"", "", ""}), 3);
service.invoke("injvm://127.0.0.1/TestService", "invoke");
InjvmInvoker injvmInvoker = new InjvmInvoker(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"),null,new HashMap<String, Exporter<?>>());
assertFalse(injvmInvoker.isAvailable());
}
@Test
@ -74,6 +88,19 @@ public class InjvmProtocolTest {
url = url.addParameter(Constants.GROUP_KEY, "*")
.addParameter(Constants.VERSION_KEY, "*");
assertTrue(InjvmProtocol.getInjvmProtocol().isInjvmRefer(url));
url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(Constants.SCOPE_KEY, Constants.SCOPE_LOCAL);
assertTrue(InjvmProtocol.getInjvmProtocol().isInjvmRefer(url));
url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(Constants.LOCAL_PROTOCOL,true);
assertTrue(InjvmProtocol.getInjvmProtocol().isInjvmRefer(url));
url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(Constants.SCOPE_KEY, Constants.SCOPE_REMOTE);
assertFalse(InjvmProtocol.getInjvmProtocol().isInjvmRefer(url));
url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(Constants.GENERIC_KEY, true);
assertFalse(InjvmProtocol.getInjvmProtocol().isInjvmRefer(url));
}
}

View File

@ -36,6 +36,10 @@ public class ProtocolTest {
}
};
static{
InjvmProtocol injvm = InjvmProtocol.getInjvmProtocol();
}
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("javassist");
URL url = URL.valueOf("injvm://localhost:0/org.apache.dubbo.rpc.support.IEcho?interface=org.apache.dubbo.rpc.support.IEcho");
@ -48,6 +52,8 @@ public class ProtocolTest {
Protocol InjvmProtocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("injvm");
assertEquals(0,InjvmProtocol.getDefaultPort());
InjvmProtocol.export(invoker);
Invoker<IEcho> refer = InjvmProtocol.refer(IEcho.class, url);