Skip to content

Commit

Permalink
fix stable problem for unit cases (#2497)
Browse files Browse the repository at this point in the history
* fix stable problem for unit cases

* add cache
  • Loading branch information
jerrick-zhu authored Sep 13, 2018
1 parent ffed96f commit 96eec3d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ jdk:
- oraclejdk9
- oraclejdk8

cache:
directories:
- $HOME/.m2

install: true

script:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,35 @@ public void testParseFromParameter() {

@Test
public void testParseUrl2() {
String address = "127.0.0.1";
String backupAddress1 = "127.0.0.2";
String backupAddress2 = "127.0.0.3";
String address = "192.168.0.1";
String backupAddress1 = "192.168.0.2";
String backupAddress2 = "192.168.0.3";

Map<String, String> parameters = new HashMap<String, String>();
parameters.put("username", "root");
parameters.put("password", "alibaba");
parameters.put("port", "10000");
parameters.put("protocol", "dubbo");
URL url = UrlUtils.parseURL(address + "," + backupAddress1 + "," + backupAddress2, parameters);
assertEquals(localAddress + ":10000", url.getAddress());
assertEquals("192.168.0.1:10000", url.getAddress());
assertEquals("root", url.getUsername());
assertEquals("alibaba", url.getPassword());
assertEquals(10000, url.getPort());
assertEquals("dubbo", url.getProtocol());
assertEquals("127.0.0.2" + "," + "127.0.0.3", url.getParameter("backup"));
assertEquals("192.168.0.2" + "," + "192.168.0.3", url.getParameter("backup"));
}

@Test
public void testParseUrls() {
String addresses = "127.0.0.1|127.0.0.2|127.0.0.3";
String addresses = "192.168.0.1|192.168.0.2|192.168.0.3";
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("username", "root");
parameters.put("password", "alibaba");
parameters.put("port", "10000");
parameters.put("protocol", "dubbo");
List<URL> urls = UrlUtils.parseURLs(addresses, parameters);
assertEquals(localAddress + ":10000", urls.get(0).getAddress());
assertEquals("127.0.0.2" + ":10000", urls.get(1).getAddress());
assertEquals("192.168.0.1" + ":10000", urls.get(0).getAddress());
assertEquals("192.168.0.2" + ":10000", urls.get(1).getAddress());
}

@Test
Expand Down Expand Up @@ -330,7 +330,7 @@ public void testIsItemMatch() throws Exception {

@Test
public void testIsServiceKeyMatch() throws Exception {
URL url = URL.valueOf("test://127.0.0.0");
URL url = URL.valueOf("test://127.0.0.1");
URL pattern = url.addParameter(Constants.GROUP_KEY, "test")
.addParameter(Constants.INTERFACE_KEY, "test")
.addParameter(Constants.VERSION_KEY, "test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testGenericServiceException() {
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
reference.setApplication(new ApplicationConfig("generic-consumer"));
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:29581?generic=true");
reference.setUrl("dubbo://127.0.0.1:29581?generic=true&timeout=3000");
DemoService demoService = reference.get();
try {
// say name
Expand Down Expand Up @@ -113,7 +113,7 @@ public void testGenericReferenceException() {
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setApplication(new ApplicationConfig("generic-consumer"));
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:29581?scope=remote");
reference.setUrl("dubbo://127.0.0.1:29581?scope=remote&timeout=3000");
reference.setGeneric(true);
GenericService genericService = reference.get();
try {
Expand Down Expand Up @@ -147,7 +147,7 @@ public void testGenericSerializationJava() throws Exception {
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setApplication(new ApplicationConfig("generic-consumer"));
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:29581?scope=remote");
reference.setUrl("dubbo://127.0.0.1:29581?scope=remote&timeout=3000");
reference.setGeneric(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA);
GenericService genericService = reference.get();
try {
Expand Down Expand Up @@ -216,7 +216,7 @@ public void testGenericInvokeWithBeanSerialization() throws Exception {
reference = new ReferenceConfig<GenericService>();
reference.setApplication(new ApplicationConfig("bean-consumer"));
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:29581?scope=remote");
reference.setUrl("dubbo://127.0.0.1:29581?scope=remote&timeout=3000");
reference.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
GenericService genericService = reference.get();
User user = new User();
Expand Down Expand Up @@ -270,7 +270,7 @@ public void testGenericImplementationWithBeanSerialization() throws Exception {
ref = new ReferenceConfig<DemoService>();
ref.setApplication(new ApplicationConfig("bean-consumer"));
ref.setInterface(DemoService.class);
ref.setUrl("dubbo://127.0.0.1:29581?scope=remote&generic=bean");
ref.setUrl("dubbo://127.0.0.1:29581?scope=remote&generic=bean&timeout=3000");
DemoService demoService = ref.get();
User user = new User();
user.setName("zhangsan");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ private void testCache(String type) throws Exception {
ServiceConfig<CacheService> service = new ServiceConfig<CacheService>();
service.setApplication(new ApplicationConfig("cache-provider"));
service.setRegistry(new RegistryConfig("N/A"));
service.setProtocol(new ProtocolConfig("dubbo", 29582));
service.setProtocol(new ProtocolConfig("injvm"));
service.setInterface(CacheService.class.getName());
service.setRef(new CacheServiceImpl());
service.export();
try {
ReferenceConfig<CacheService> reference = new ReferenceConfig<CacheService>();
reference.setApplication(new ApplicationConfig("cache-consumer"));
reference.setInterface(CacheService.class);
reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&cache=true");
reference.setUrl("injvm://127.0.0.1?scope=remote&cache=true");

MethodConfig method = new MethodConfig();
method.setName("findCache");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testServiceClass() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/service-class.xml");
ctx.start();
try {
DemoService demoService = refer("dubbo://127.0.0.1:20887");
DemoService demoService = refer("dubbo://127.0.0.1:30887");
String hello = demoService.sayName("hello");
assertEquals("welcome:hello", hello);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public class AbstractRegistryTest {

@Before
public void init() {
URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":2233");
testUrl = URL.valueOf("http://1.2.3.4:9090/registry?check=false&file=N/A&interface=com.test");
mockUrl = new URL("dubbo", "127.0.0.0", 2200);
URL url = URL.valueOf("dubbo://192.168.0.2:2233");
testUrl = URL.valueOf("http://192.168.0.3:9090/registry?check=false&file=N/A&interface=com.test");
mockUrl = new URL("dubbo", "192.168.0.1", 2200);

parametersConsumer.put("application", "demo-consumer");
parametersConsumer.put("category", "consumer");
Expand Down Expand Up @@ -115,7 +115,7 @@ public void testRegisterIfURLNULL() throws Exception {
@Test
public void testUnregister() throws Exception {
//test one unregister
URL url = new URL("dubbo", "127.0.0.0", 2200);
URL url = new URL("dubbo", "192.168.0.1", 2200);
abstractRegistry.getRegistered().add(url);
abstractRegistry.unregister(url);
Assert.assertThat(false, Matchers.equalTo(abstractRegistry.getRegistered().contains(url)));
Expand Down Expand Up @@ -145,7 +145,7 @@ public void testSubscribeAndUnsubscribe() throws Exception {
//test subscribe
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
NotifyListener listener = urls -> notified.set(Boolean.TRUE);
URL url = new URL("dubbo", "127.0.0.0", 2200);
URL url = new URL("dubbo", "192.168.0.1", 2200);
abstractRegistry.subscribe(url, listener);
Set<NotifyListener> subscribeListeners = abstractRegistry.getSubscribed().get(url);
Assert.assertThat(true, Matchers.equalTo(subscribeListeners.contains(listener)));
Expand All @@ -159,7 +159,7 @@ public void testSubscribeAndUnsubscribe() throws Exception {
public void testSubscribeIfUrlNull() throws Exception {
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
NotifyListener listener = urls -> notified.set(Boolean.TRUE);
URL url = new URL("dubbo", "127.0.0.0", 2200);
URL url = new URL("dubbo", "192.168.0.1", 2200);
abstractRegistry.subscribe(null, listener);
Assert.fail("subscribe url == null");
}
Expand All @@ -168,7 +168,7 @@ public void testSubscribeIfUrlNull() throws Exception {
public void testSubscribeIfListenerNull() throws Exception {
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
NotifyListener listener = urls -> notified.set(Boolean.TRUE);
URL url = new URL("dubbo", "127.0.0.0", 2200);
URL url = new URL("dubbo", "192.168.0.1", 2200);
abstractRegistry.subscribe(url, null);
Assert.fail("listener url == null");
}
Expand All @@ -184,7 +184,7 @@ public void testUnsubscribeIfUrlNull() throws Exception {
@Test(expected = IllegalArgumentException.class)
public void testUnsubscribeIfNotifyNull() throws Exception {
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
URL url = new URL("dubbo", "127.0.0.0", 2200);
URL url = new URL("dubbo", "192.168.0.1", 2200);
abstractRegistry.unsubscribe(url, null);
Assert.fail("unsubscribe listener == null");
}
Expand Down Expand Up @@ -293,13 +293,13 @@ public void testRecover2() throws Exception {
public void testNotify() throws Exception {
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
NotifyListener listner1 = urls -> notified.set(Boolean.TRUE);
URL url1 = new URL("dubbo", "127.0.0.0", 2200, parametersConsumer);
URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer);
abstractRegistry.subscribe(url1, listner1);
NotifyListener listner2 = urls -> notified.set(Boolean.TRUE);
URL url2 = new URL("dubbo", "127.0.0.1", 2201, parametersConsumer);
URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer);
abstractRegistry.subscribe(url2, listner2);
NotifyListener listner3 = urls -> notified.set(Boolean.TRUE);
URL url3 = new URL("dubbo", "127.0.0.2", 2202, parametersConsumer);
URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer);
abstractRegistry.subscribe(url3, listner3);
List<URL> urls = new ArrayList<>();
urls.add(url1);
Expand All @@ -319,13 +319,13 @@ public void testNotify() throws Exception {
public void testNotifyList() throws Exception {
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
NotifyListener listner1 = urls -> notified.set(Boolean.TRUE);
URL url1 = new URL("dubbo", "127.0.0.0", 2200, parametersConsumer);
URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer);
abstractRegistry.subscribe(url1, listner1);
NotifyListener listner2 = urls -> notified.set(Boolean.TRUE);
URL url2 = new URL("dubbo", "127.0.0.1", 2201, parametersConsumer);
URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer);
abstractRegistry.subscribe(url2, listner2);
NotifyListener listner3 = urls -> notified.set(Boolean.TRUE);
URL url3 = new URL("dubbo", "127.0.0.2", 2202, parametersConsumer);
URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer);
abstractRegistry.subscribe(url3, listner3);
List<URL> urls = new ArrayList<>();
urls.add(url1);
Expand All @@ -342,13 +342,13 @@ public void testNotifyList() throws Exception {
public void testNotifyIfURLNull() throws Exception {
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
NotifyListener listner1 = urls -> notified.set(Boolean.TRUE);
URL url1 = new URL("dubbo", "127.0.0.0", 2200, parametersConsumer);
URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer);
abstractRegistry.subscribe(url1, listner1);
NotifyListener listner2 = urls -> notified.set(Boolean.TRUE);
URL url2 = new URL("dubbo", "127.0.0.1", 2201, parametersConsumer);
URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer);
abstractRegistry.subscribe(url2, listner2);
NotifyListener listner3 = urls -> notified.set(Boolean.TRUE);
URL url3 = new URL("dubbo", "127.0.0.2", 2202, parametersConsumer);
URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer);
abstractRegistry.subscribe(url3, listner3);
List<URL> urls = new ArrayList<>();
urls.add(url1);
Expand All @@ -362,13 +362,13 @@ public void testNotifyIfURLNull() throws Exception {
public void testNotifyIfNotifyNull() {
final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
NotifyListener listner1 = urls -> notified.set(Boolean.TRUE);
URL url1 = new URL("dubbo", "127.0.0.0", 2200, parametersConsumer);
URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer);
abstractRegistry.subscribe(url1, listner1);
NotifyListener listner2 = urls -> notified.set(Boolean.TRUE);
URL url2 = new URL("dubbo", "127.0.0.1", 2201, parametersConsumer);
URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer);
abstractRegistry.subscribe(url2, listner2);
NotifyListener listner3 = urls -> notified.set(Boolean.TRUE);
URL url3 = new URL("dubbo", "127.0.0.2", 2202, parametersConsumer);
URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer);
abstractRegistry.subscribe(url3, listner3);
List<URL> urls = new ArrayList<>();
urls.add(url1);
Expand Down Expand Up @@ -507,9 +507,9 @@ public void allTest() throws Exception {

private List<URL> getList() {
List<URL> list = new ArrayList<>();
URL url1 = new URL("dubbo", "127.0.0.0", 1000);
URL url2 = new URL("dubbo", "127.0.0.1", 1001);
URL url3 = new URL("dubbo", "127.0.0.2", 1002);
URL url1 = new URL("dubbo", "192.168.0.1", 1000);
URL url2 = new URL("dubbo", "192.168.0.2", 1001);
URL url3 = new URL("dubbo", "192.168.0.3", 1002);
list.add(url1);
list.add(url2);
list.add(url3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void testReconnect() throws RemotingException, InterruptedException {
public void testReconnectWarnLog() throws RemotingException, InterruptedException {
int port = NetUtils.getAvailablePort();
DubboAppender.doStart();
String url = "exchange://127.0.0.2:" + port + "/client.reconnect.test?check=false&client=netty3&"
String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?check=false&client=netty3&"
+ Constants.RECONNECT_KEY + "=" + 1; //1ms reconnect, ensure that there is enough frequency to reconnect
try {
Exchangers.connect(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected ExchangeServer newServer(int port, Replier<?> receiver) throws Remotin
}

protected ExchangeChannel newClient(int port) throws RemotingException {
return Exchangers.connect(URL.valueOf("exchange://localhost:" + port + "?client=netty3"));
return Exchangers.connect(URL.valueOf("exchange://localhost:" + port + "?client=netty3&timeout=3000"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -41,20 +43,20 @@
public class PortTelnetHandlerTest {

private static TelnetHandler port = new PortTelnetHandler();
private Invoker<DemoService> mockInvoker;
private static Invoker<DemoService> mockInvoker;

@SuppressWarnings("unchecked")
@Before
public void before() {
@BeforeClass
public static void before() {
mockInvoker = mock(Invoker.class);
given(mockInvoker.getInterface()).willReturn(DemoService.class);
given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20887/demo"));

DubboProtocol.getDubboProtocol().export(mockInvoker);
}

@After
public void after() {
@AfterClass
public static void after() {
ProtocolUtils.closeAll();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public class WebserviceProtocolTest {
public void testDemoProtocol() throws Exception {
DemoService service = new DemoServiceImpl();
protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?codec=exchange")));
service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?codec=exchange")));
service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?codec=exchange&timeout=3000")));
assertEquals(service.getSize(new String[]{"", "", ""}), 3);
}

@Test
public void testWebserviceProtocol() throws Exception {
DemoService service = new DemoServiceImpl();
protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName())));
service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName())));
service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?timeout=3000")));
assertEquals(service.create(1,"kk").getName(), "kk");
assertEquals(service.getSize(null), -1);
assertEquals(service.getSize(new String[]{"", "", ""}), 3);
Expand Down

0 comments on commit 96eec3d

Please sign in to comment.