lovepoem closed pull request #1738: [Dubbo-1691] Add unit tests for
MulticastRegister #1691
URL: https://github.com/apache/incubator-dubbo/pull/1738
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/RegistryService.java
b/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/RegistryService.java
index 764c5d5427..2494d1e2d6 100644
---
a/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/RegistryService.java
+++
b/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/RegistryService.java
@@ -54,7 +54,7 @@
void unregister(URL url);
/**
- * Subscrib to eligible registered data and automatically push when the
registered data is changed.
+ * Subscribe to eligible registered data and automatically push when the
registered data is changed.
* <p>
* Subscribing need to support contracts:<br>
* 1. When the URL sets the check=false parameter. When the registration
fails, the exception is not thrown and retried in the background. <br>
diff --git
a/dubbo-registry/dubbo-registry-multicast/src/main/java/com/alibaba/dubbo/registry/multicast/MulticastRegistry.java
b/dubbo-registry/dubbo-registry-multicast/src/main/java/com/alibaba/dubbo/registry/multicast/MulticastRegistry.java
index aa499fc1bc..8112fcf42c 100644
---
a/dubbo-registry/dubbo-registry-multicast/src/main/java/com/alibaba/dubbo/registry/multicast/MulticastRegistry.java
+++
b/dubbo-registry/dubbo-registry-multicast/src/main/java/com/alibaba/dubbo/registry/multicast/MulticastRegistry.java
@@ -144,6 +144,9 @@ private static boolean isMulticastAddress(String ip) {
return false;
}
+ /**
+ * Remove the expired providers, only when "clean" parameter is true.
+ */
private void clean() {
if (admin) {
for (Set<URL> providers : new
HashSet<Set<URL>>(received.values())) {
@@ -298,6 +301,9 @@ public boolean isAvailable() {
}
}
+ /**
+ * Remove the expired providers(if clean is true), leave the multicast
group and close the multicast socket.
+ */
@Override
public void destroy() {
super.destroy();
diff --git
a/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java
b/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java
index 3870d54099..30fca7ef07 100644
---
a/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java
+++
b/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java
@@ -27,12 +27,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertEquals;
public class MulticastRegistryTest {
@@ -44,31 +44,38 @@
private URL consumerUrl = URL.valueOf("subscribe://" +
NetUtils.getLocalHost() + "/" + service + "?arg1=1&arg2=2");
private MulticastRegistry registry = new MulticastRegistry(registryUrl);
- /**
- * @throws java.lang.Exception
- */
@Before
- public void setUp() throws Exception {
+ public void setUp() {
registry.register(serviceUrl);
}
+ /**
+ * Test method for {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#MulticastRegistry(URL)}.
+ */
@Test(expected = IllegalArgumentException.class)
public void testUrlError() {
URL errorUrl = URL.valueOf("multicast://mullticast/");
new MulticastRegistry(errorUrl);
}
+ /**
+ * Test method for {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#MulticastRegistry(URL)}.
+ */
@Test(expected = IllegalStateException.class)
public void testAnyHost() {
URL errorUrl = URL.valueOf("multicast://0.0.0.0/");
new MulticastRegistry(errorUrl);
}
+ /**
+ * Test method for {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#MulticastRegistry(URL)}.
+ */
@Test
public void testGetCustomPort() {
- URL customPortUrl = URL.valueOf("multicast://239.255.255.255:4321/");
+ int port = NetUtils.getAvailablePort();
+ URL customPortUrl = URL.valueOf("multicast://239.255.255.255:" + port);
MulticastRegistry multicastRegistry = new
MulticastRegistry(customPortUrl);
- assertThat(multicastRegistry.getUrl().getPort(), is(4321));
+ assertThat(multicastRegistry.getUrl().getPort(), is(port));
}
/**
@@ -76,20 +83,39 @@ public void testGetCustomPort() {
*/
@Test
public void testRegister() {
- Set<URL> registered = null;
+ Set<URL> registered;
// clear first
registered = registry.getRegistered();
+ registered.clear();
for (int i = 0; i < 2; i++) {
registry.register(serviceUrl);
registered = registry.getRegistered();
assertTrue(registered.contains(serviceUrl));
}
- // confirm only 1 regist success;
+ // confirm only 1 register success
registered = registry.getRegistered();
assertEquals(1, registered.size());
}
+ /**
+ * Test method for {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#unregister(URL)}.
+ */
+ @Test
+ public void testUnregister() {
+ Set<URL> registered;
+
+ // register first
+ registry.register(serviceUrl);
+ registered = registry.getRegistered();
+ assertTrue(registered.contains(serviceUrl));
+
+ // then unregister
+ registered = registry.getRegistered();
+ registry.unregister(serviceUrl);
+ assertFalse(registered.contains(serviceUrl));
+ }
+
/**
* Test method for
* {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#subscribe(URL url,
com.alibaba.dubbo.registry.NotifyListener)}
@@ -97,22 +123,72 @@ public void testRegister() {
*/
@Test
public void testSubscribe() {
- // verify lisener.
- final AtomicReference<URL> args = new AtomicReference<URL>();
+ // verify listener
registry.subscribe(consumerUrl, new NotifyListener() {
+ @Override
+ public void notify(List<URL> urls) {
+ assertEquals(serviceUrl.toFullString(),
urls.get(0).toFullString());
+ Map<URL, Set<NotifyListener>> subscribed =
registry.getSubscribed();
+ assertEquals(consumerUrl,
subscribed.keySet().iterator().next());
+ }
+ });
+ }
+
+ /**
+ * Test method for {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#unsubscribe(URL,
NotifyListener)}
+ */
+ @Test
+ public void testUnsubscribe() {
+ // subscribe first
+ registry.subscribe(consumerUrl, new NotifyListener() {
@Override
public void notify(List<URL> urls) {
- // FIXME assertEquals(MulticastRegistry.this.service, service);
- args.set(urls.get(0));
+ // do nothing
+ }
+ });
+
+ // then unsubscribe
+ registry.unsubscribe(consumerUrl, new NotifyListener() {
+ @Override
+ public void notify(List<URL> urls) {
+ Map<URL, Set<NotifyListener>> subscribed =
registry.getSubscribed();
+ Set<NotifyListener> listeners = subscribed.get(consumerUrl);
+ assertTrue(listeners.isEmpty());
+
+ Map<URL, Set<URL>> received = registry.getReceived();
+ assertTrue(received.get(consumerUrl).isEmpty());
}
});
- assertEquals(serviceUrl.toFullString(), args.get().toFullString());
- Map<URL, Set<NotifyListener>> arg = registry.getSubscribed();
- assertEquals(consumerUrl, arg.keySet().iterator().next());
+ }
+ /**
+ * Test method for {@link MulticastRegistry#isAvailable()}
+ */
+ @Test
+ public void testAvailability() {
+ int port = NetUtils.getAvailablePort();
+ MulticastRegistry registry = new
MulticastRegistry(URL.valueOf("multicast://224.5.6.8:" + port));
+ assertTrue(registry.isAvailable());
+ }
+
+ /**
+ * Test method for {@link MulticastRegistry#destroy()}
+ */
+ @Test
+ public void testDestroy() {
+ MulticastSocket socket = registry.getMutilcastSocket();
+ assertFalse(socket.isClosed());
+
+ // then destroy, the multicast socket will be closed
+ registry.destroy();
+ socket = registry.getMutilcastSocket();
+ assertTrue(socket.isClosed());
}
+ /**
+ * Test method for {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#MulticastRegistry(URL)}
+ */
@Test
public void testDefaultPort() {
MulticastRegistry multicastRegistry = new
MulticastRegistry(URL.valueOf("multicast://224.5.6.7"));
@@ -124,4 +200,19 @@ public void testDefaultPort() {
}
}
+ /**
+ * Test method for {@link
com.alibaba.dubbo.registry.multicast.MulticastRegistry#MulticastRegistry(URL)}
+ */
+ @Test
+ public void testCustomedPort() {
+ int port = NetUtils.getAvailablePort();
+ MulticastRegistry multicastRegistry = new
MulticastRegistry(URL.valueOf("multicast://224.5.6.7:" + port));
+ try {
+ MulticastSocket multicastSocket =
multicastRegistry.getMutilcastSocket();
+ assertEquals(port, multicastSocket.getLocalPort());
+ } finally {
+ multicastRegistry.destroy();
+ }
+ }
+
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]