Skip to content

Commit

Permalink
Fix flaky tests in PortalRegistryTest.java and ConsulRegistryTest.java (
Browse files Browse the repository at this point in the history
#857)

* Add unregistry for subAndUnsubService() method

* Update PortalRegistryTest.java

* Add unregistry & unavailable code
  • Loading branch information
KellyShao committed Dec 10, 2020
1 parent d816fd1 commit e1e8486
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,25 @@ public void subAndUnsubService() throws Exception {
NotifyListener notifyListener = createNewNotifyListener(serviceUrl);
NotifyListener notifylistener2 = createNewNotifyListener(serviceUrl);

// subscribe
registry.doSubscribe(clientUrl, notifyListener);
registry.doSubscribe(clientUrl2, notifylistener2);
Assert.assertTrue(containsNotifyListener(serviceUrl, clientUrl, notifyListener));
Assert.assertTrue(containsNotifyListener(serviceUrl, clientUrl2, notifylistener2));

// register
registry.doRegister(serviceUrl);
registry.doRegister(serviceUrl2);
registry.doAvailable(null);
Thread.sleep(sleepTime);

// unregister
registry.doUnavailable(null);
Thread.sleep(sleepTime);
registry.doUnregister(serviceUrl);
registry.doUnregister(serviceUrl2);

// unsubscrib
registry.doUnsubscribe(clientUrl, notifyListener);
Assert.assertFalse(containsNotifyListener(serviceUrl, clientUrl, notifyListener));
Assert.assertTrue(containsNotifyListener(serviceUrl, clientUrl2, notifylistener2));
Expand All @@ -131,11 +140,16 @@ public void discoverService() throws Exception {
Thread.sleep(sleepTime);
urls = registry.discover(serviceUrl);
Assert.assertTrue(urls.contains(serviceUrl));

// unavailable
registry.doUnavailable(null);
Thread.sleep(sleepTime);
Assert.assertFalse(client.isWorking(serviceid));
}

private Boolean containsNotifyListener(URL serviceUrl, URL clientUrl, NotifyListener listener) {
String service = ConsulUtils.getUrlClusterInfo(serviceUrl);
return registry.getNotifyListeners().get(service).get(clientUrl) == listener;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,23 @@ public void doRegisterAndAvailable() throws Exception {
Assert.assertFalse(client.isRegistered(service2));
}

@Ignore
@Test
public void subAndUnsubService() throws Exception {
//registry.doSubscribe(clientUrl, null);
//registry.doSubscribe(clientUrl2, null);

// registry
registry.doRegister(serviceUrl);
registry.doRegister(serviceUrl2);
registry.doAvailable(null);
Thread.sleep(sleepTime);

// unregistry
registry.doUnavailable(null);
Thread.sleep(sleepTime);
registry.doUnregister(serviceUrl);
registry.doUnregister(serviceUrl2);

//registry.doUnsubscribe(clientUrl, null);
//registry.doUnsubscribe(clientUrl2, null);
Expand All @@ -117,6 +125,11 @@ public void discoverService() throws Exception {
e.printStackTrace();
}
Assert.assertTrue(urls.contains(serviceUrl));

// unavailable
registry.doUnavailable(null);
Thread.sleep(sleepTime);
Assert.assertFalse(client.isWorking(serviceid));
}

@Test
Expand Down Expand Up @@ -159,4 +172,4 @@ public void testStringSplit() {

}
}
}
}

0 comments on commit e1e8486

Please sign in to comment.