beiwei30 closed pull request #1958: [Dubbo-1694] add test for p2p and zookeeper
URL: https://github.com/apache/incubator-dubbo/pull/1958
 
 
   

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-remoting/dubbo-remoting-p2p/pom.xml 
b/dubbo-remoting/dubbo-remoting-p2p/pom.xml
index bc0501ca44..eb5f2d7b21 100644
--- a/dubbo-remoting/dubbo-remoting-p2p/pom.xml
+++ b/dubbo-remoting/dubbo-remoting-p2p/pom.xml
@@ -14,7 +14,8 @@
   See the License for the specific language governing permissions and
   limitations under the License.
   -->
-<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>com.alibaba</groupId>
@@ -34,5 +35,11 @@
             <artifactId>dubbo-remoting-api</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>dubbo-remoting-netty</artifactId>
+            <version>${project.parent.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/PeerMain.java
 
b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/PeerMain.java
deleted file mode 100644
index fc3bc28108..0000000000
--- 
a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/PeerMain.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.dubbo.remoting.p2p;
-
-import org.apache.dubbo.remoting.Channel;
-import org.apache.dubbo.remoting.RemotingException;
-import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter;
-
-import java.util.Collection;
-
-/**
- * PeerMain
- */
-public class PeerMain {
-
-    public static void main(String[] args) throws Throwable {
-        String groupURL = "multicast://224.5.6.7:9911"; // Group address, 
supporting two groups of multicast and file, extensible
-        final String peerURL = "dubbo://0.0.0.0:" + (((int) (Math.random() * 
10000)) + 20000); // The native server address for cross networking
-
-        // Join the group and get the peer reference
-        Peer peer = Networkers.join(groupURL, peerURL, new 
ChannelHandlerAdapter() {
-            @Override
-            public void received(Channel channel, Object message) throws 
RemotingException {
-                System.out.println("Received: " + message + " in " + peerURL);
-            }
-        });
-
-        // Sending messages to other peers in the network
-        for (int i = 0; i < Integer.MAX_VALUE; i++) {
-            Collection<Channel> channels = peer.getChannels(); // Access to 
channels with all other peers, this list changes dynamically
-            if (channels != null && channels.size() > 0) {
-                for (Channel channel : channels) {
-                    channel.send("(" + i + ") " + peerURL); // Sending 
messages to a specified peer
-                }
-            }
-            Thread.sleep(1000);
-        }
-
-        // leave the network
-        peer.leave();
-    }
-
-}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeNetworkerTest.java
 
b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeNetworkerTest.java
new file mode 100644
index 0000000000..1366ec0d07
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeNetworkerTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.p2p.exchange.support;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.Channel;
+import org.apache.dubbo.remoting.RemotingException;
+import org.apache.dubbo.remoting.exchange.ExchangeChannel;
+import org.apache.dubbo.remoting.exchange.ExchangeHandler;
+import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
+import org.apache.dubbo.remoting.p2p.Group;
+import org.apache.dubbo.remoting.p2p.Networkers;
+import org.apache.dubbo.remoting.p2p.Peer;
+import org.junit.Test;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class MulticastExchangeNetworkerTest {
+    @Test
+    public void testJoin() throws RemotingException, InterruptedException {
+        final String groupURL = "multicast://224.5.6.7:1234";
+
+        MulticastExchangeNetworker multicastExchangeNetworker = new 
MulticastExchangeNetworker();
+
+        final CountDownLatch countDownLatch = new CountDownLatch(1);
+        Peer peer1 = multicastExchangeNetworker.lookup(URL.valueOf(groupURL))
+                .join(URL.valueOf("dubbo://0.0.0.0:" + 
NetUtils.getAvailablePort()), new ExchangeHandlerAdapter() {
+                    @Override
+                    public CompletableFuture<Object> reply(ExchangeChannel 
channel, Object msg) throws RemotingException {
+                        countDownLatch.countDown();
+                        return super.reply(channel, msg);
+                    }
+                });
+        Peer peer2 = multicastExchangeNetworker.lookup(URL.valueOf(groupURL))
+                .join(URL.valueOf("dubbo://0.0.0.0:" + 
NetUtils.getAvailablePort()), mock(ExchangeHandler.class));
+
+        while (true) {
+            for (Channel channel : peer1.getChannels()) {
+                channel.send("hello multicast exchange network!");
+            }
+            TimeUnit.MILLISECONDS.sleep(50);
+
+            long count = countDownLatch.getCount();
+            if (count > 0) {
+                break;
+            }
+        }
+
+        Group lookup = Networkers.lookup(groupURL);
+        assertThat(lookup, not(nullValue()));
+
+        assertThat(peer1, instanceOf(ExchangeServerPeer.class));
+
+        peer1.close();
+        peer2.close();
+    }
+}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/FileNetworkerTest.java
 
b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/FileNetworkerTest.java
new file mode 100644
index 0000000000..980718e886
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/FileNetworkerTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.p2p.support;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.Channel;
+import org.apache.dubbo.remoting.RemotingException;
+import org.apache.dubbo.remoting.p2p.Group;
+import org.apache.dubbo.remoting.p2p.Peer;
+import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.mockito.Mockito.mock;
+
+public class FileNetworkerTest {
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    @Before
+    public void setUp() throws Exception {
+        folder.create();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        folder.delete();
+    }
+
+    @Test
+    public void testJoin() throws RemotingException, InterruptedException, 
IOException {
+        final String groupURL = "file://" + folder.newFile();
+
+        FileNetworker networker = new FileNetworker();
+        Group group = networker.lookup(URL.valueOf(groupURL));
+
+        final CountDownLatch countDownLatch = new CountDownLatch(1);
+        Peer peer1 = group.join(URL.valueOf("dubbo://0.0.0.0:" + 
NetUtils.getAvailablePort()), new ChannelHandlerAdapter() {
+            @Override
+            public void received(Channel channel, Object message) {
+                countDownLatch.countDown();
+            }
+        });
+        Peer peer2 = group.join(URL.valueOf("dubbo://0.0.0.0:" + 
NetUtils.getAvailablePort()),
+                mock(ChannelHandlerAdapter.class));
+
+        while (true) {
+            long count = countDownLatch.getCount();
+            if (count > 0) {
+                break;
+            }
+            for (Channel channel : peer1.getChannels()) {
+                channel.send(0, false);
+                channel.send("hello world!");
+            }
+            TimeUnit.MILLISECONDS.sleep(50);
+        }
+
+
+        peer2.close();
+        peer1.close();
+    }
+}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/MulticastNetworkerTest.java
 
b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/MulticastNetworkerTest.java
new file mode 100644
index 0000000000..eefc27a01a
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/MulticastNetworkerTest.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.p2p.support;
+
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.Channel;
+import org.apache.dubbo.remoting.RemotingException;
+import org.apache.dubbo.remoting.p2p.Group;
+import org.apache.dubbo.remoting.p2p.Networkers;
+import org.apache.dubbo.remoting.p2p.Peer;
+import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter;
+import org.junit.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class MulticastNetworkerTest {
+
+    @Test
+    public void testJoin() throws RemotingException, InterruptedException {
+        final String groupURL = "multicast://224.5.6.7:1234";
+        final String peerURL = "dubbo://0.0.0.0:" + 
NetUtils.getAvailablePort();
+
+        final CountDownLatch countDownLatch = new CountDownLatch(1);
+        Peer peer1 = Networkers.join(groupURL, peerURL, new 
ChannelHandlerAdapter() {
+            @Override
+            public void received(Channel channel, Object message) {
+                countDownLatch.countDown();
+            }
+        });
+        Peer peer2 = Networkers.join(groupURL, "dubbo://0.0.0.0:" + 
NetUtils.getAvailablePort(),
+                mock(ChannelHandlerAdapter.class));
+
+        while (true) {
+            long count = countDownLatch.getCount();
+            if (count > 0) {
+                break;
+            }
+            for (Channel channel : peer1.getChannels()) {
+                channel.send("hello world!");
+            }
+            TimeUnit.MILLISECONDS.sleep(50);
+        }
+
+        Group lookup = Networkers.lookup(groupURL);
+        assertThat(lookup, not(nullValue()));
+
+        peer2.close();
+        peer1.close();
+    }
+}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
index c0dbee20cf..39ab70b645 100644
--- 
a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
+++ 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
@@ -15,41 +15,107 @@
  * limitations under the License.
  */
 package org.apache.dubbo.remoting.zookeeper.curator;
-
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.zookeeper.ChildListener;
+import org.apache.curator.framework.api.CuratorWatcher;
 import org.apache.curator.test.TestingServer;
+import org.apache.zookeeper.WatchedEvent;
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+
 import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
 
-@Ignore
 public class CuratorZookeeperClientTest {
     private TestingServer zkServer;
-    private int zkServerPort;
+    private CuratorZookeeperClient curatorClient;
 
     @Before
     public void setUp() throws Exception {
-        zkServerPort = NetUtils.getAvailablePort();
-        zkServer = new TestingServer(this.zkServerPort, true);
+        int zkServerPort = NetUtils.getAvailablePort();
+        zkServer = new TestingServer(zkServerPort, true);
+        curatorClient = new 
CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" +
+                zkServerPort + "/org.apache.dubbo.registry.RegistryService"));
     }
 
     @Test
     public void testCheckExists() {
-        CuratorZookeeperClient curatorClient = new 
CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" + this.zkServerPort 
+ "/org.apache.dubbo.registry.RegistryService"));
         String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
         curatorClient.create(path, false);
-        Assert.assertThat(curatorClient.checkExists(path), is(true));
-        Assert.assertThat(curatorClient.checkExists(path + "/noneexits"), 
is(false));
+        assertThat(curatorClient.checkExists(path), is(true));
+        assertThat(curatorClient.checkExists(path + "/noneexits"), is(false));
+    }
+
+    @Test
+    public void testChildrenPath() {
+        String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
+        curatorClient.create(path, false);
+        curatorClient.create(path + "/provider1", false);
+        curatorClient.create(path + "/provider2", false);
+
+        List<String> children = curatorClient.getChildren(path);
+        assertThat(children.size(), is(2));
     }
 
+    @Test
+    public void testChildrenListener() throws InterruptedException {
+        String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
+        curatorClient.create(path, false);
+        final CountDownLatch countDownLatch = new CountDownLatch(1);
+        curatorClient.addTargetChildListener(path, new CuratorWatcher() {
+            @Override
+            public void process(WatchedEvent watchedEvent) throws Exception {
+                countDownLatch.countDown();
+            }
+        });
+        curatorClient.createPersistent(path + "/provider1");
+        countDownLatch.await();
+    }
+
+
+    @Test(expected = IllegalStateException.class)
+    public void testWithInvalidServer() {
+        curatorClient = new 
CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:1/service"));
+        curatorClient.create("/testPath", true);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testWithStoppedServer() throws IOException {
+        curatorClient.create("/testPath", true);
+        zkServer.stop();
+        curatorClient.delete("/testPath");
+    }
+
+    @Test
+    public void testRemoveChildrenListener() {
+        ChildListener childListener = mock(ChildListener.class);
+        curatorClient.addChildListener("/children", childListener);
+        curatorClient.removeChildListener("/children", childListener);
+    }
+
+    @Test
+    public void testCreateExistingPath() {
+        curatorClient.create("/pathOne", false);
+        curatorClient.create("/pathOne", false);
+    }
+
+    @Test
+    public void testConnectedStatus() {
+        curatorClient.createEphemeral("/testPath");
+        boolean connected = curatorClient.isConnected();
+        assertThat(connected, is(true));
+    }
 
     @After
     public void tearDown() throws Exception {
+        curatorClient.close();
         zkServer.stop();
     }
-}
+}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporterTest.java
 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporterTest.java
new file mode 100644
index 0000000000..2c9dbb9a40
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporterTest.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.zookeeper.curator;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.zookeeper.ZookeeperClient;
+import org.apache.curator.test.TestingServer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.core.IsNull.nullValue;
+
+public class CuratorZookeeperTransporterTest {
+    private TestingServer zkServer;
+    private ZookeeperClient zookeeperClient;
+
+    @Before
+    public void setUp() throws Exception {
+        int zkServerPort = NetUtils.getAvailablePort();
+        zkServer = new TestingServer(zkServerPort, true);
+        zookeeperClient = new 
CuratorZookeeperTransporter().connect(URL.valueOf("zookeeper://127.0.0.1:" +
+                zkServerPort + "/service"));
+    }
+
+    @Test
+    public void testZookeeperClient() {
+        assertThat(zookeeperClient, not(nullValue()));
+        zookeeperClient.close();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        zkServer.stop();
+    }
+}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapperTest.java
 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapperTest.java
new file mode 100644
index 0000000000..9105b842fa
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapperTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.zookeeper.zkclient;
+
+import org.apache.dubbo.common.utils.NetUtils;
+import org.I0Itec.zkclient.IZkChildListener;
+import org.apache.curator.test.TestingServer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class ZkClientWrapperTest {
+    private TestingServer zkServer;
+    private ZkClientWrapper zkClientWrapper;
+
+    @Before
+    public void setUp() throws Exception {
+        int zkServerPort = NetUtils.getAvailablePort();
+        zkServer = new TestingServer(zkServerPort, true);
+        zkClientWrapper = new ZkClientWrapper("127.0.0.1:" + zkServerPort, 
10000);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        zkServer.stop();
+    }
+
+    @Test
+    public void testConnectedStatus() {
+        boolean connected = zkClientWrapper.isConnected();
+        assertThat(connected, is(false));
+        zkClientWrapper.start();
+
+        IZkChildListener listener = mock(IZkChildListener.class);
+        zkClientWrapper.subscribeChildChanges("/path", listener);
+        zkClientWrapper.unsubscribeChildChanges("/path", listener);
+    }
+}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClientTest.java
 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClientTest.java
new file mode 100644
index 0000000000..01eb787d08
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClientTest.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.zookeeper.zkclient;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.zookeeper.StateListener;
+import org.I0Itec.zkclient.IZkChildListener;
+import org.apache.curator.test.TestingServer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class ZkclientZookeeperClientTest {
+    private TestingServer zkServer;
+    private ZkclientZookeeperClient zkclientZookeeperClient;
+
+    @Before
+    public void setUp() throws Exception {
+        int zkServerPort = NetUtils.getAvailablePort();
+        zkServer = new TestingServer(zkServerPort, true);
+        zkclientZookeeperClient = new 
ZkclientZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" +
+                zkServerPort + "/org.apache.dubbo.registry.RegistryService"));
+    }
+
+    @Test
+    public void testCheckExists() {
+        String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
+        zkclientZookeeperClient.create(path, false);
+        assertThat(zkclientZookeeperClient.checkExists(path), is(true));
+        assertThat(zkclientZookeeperClient.checkExists(path + "/noneexits"), 
is(false));
+    }
+
+    @Test
+    public void testDeletePath() {
+        String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
+        zkclientZookeeperClient.create(path, false);
+        assertThat(zkclientZookeeperClient.checkExists(path), is(true));
+
+        zkclientZookeeperClient.delete(path);
+        assertThat(zkclientZookeeperClient.checkExists(path), is(false));
+    }
+
+    @Test
+    public void testConnectState() throws Exception {
+        assertThat(zkclientZookeeperClient.isConnected(), is(true));
+        final CountDownLatch stopLatch = new CountDownLatch(1);
+        zkclientZookeeperClient.addStateListener(new StateListener() {
+            @Override
+            public void stateChanged(int connected) {
+                stopLatch.countDown();
+            }
+        });
+        zkServer.stop();
+        stopLatch.await();
+        assertThat(zkclientZookeeperClient.isConnected(), is(false));
+    }
+
+    @Test
+    public void testChildrenListener() throws InterruptedException {
+        String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
+        zkclientZookeeperClient.create(path, false);
+        final CountDownLatch countDownLatch = new CountDownLatch(1);
+        zkclientZookeeperClient.addTargetChildListener(path, new 
IZkChildListener() {
+            @Override
+            public void handleChildChange(String s, List<String> list) throws 
Exception {
+                countDownLatch.countDown();
+            }
+        });
+        zkclientZookeeperClient.createPersistent(path + "/provider1");
+        countDownLatch.await();
+    }
+
+    @Test
+    public void testGetChildren() throws IOException {
+        String path = 
"/dubbo/org.apache.dubbo.demo.DemoService/parentProviders";
+        zkclientZookeeperClient.create(path, false);
+        for (int i = 0; i < 5; i++) {
+            zkclientZookeeperClient.createEphemeral(path + "/server" + i);
+        }
+        List<String> zookeeperClientChildren = 
zkclientZookeeperClient.getChildren(path);
+        assertThat(zookeeperClientChildren, hasSize(5));
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        zkclientZookeeperClient.close();
+        zkServer.stop();
+    }
+}
\ No newline at end of file
diff --git 
a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporterTest.java
 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporterTest.java
new file mode 100644
index 0000000000..7d2755bc71
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporterTest.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.zookeeper.zkclient;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.zookeeper.ZookeeperClient;
+import org.apache.curator.test.TestingServer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.core.IsNull.nullValue;
+
+public class ZkclientZookeeperTransporterTest {
+    private TestingServer zkServer;
+    private ZookeeperClient zookeeperClient;
+
+    @Before
+    public void setUp() throws Exception {
+        int zkServerPort = NetUtils.getAvailablePort();
+        zkServer = new TestingServer(zkServerPort, true);
+        zookeeperClient = new 
ZkclientZookeeperTransporter().connect(URL.valueOf("zookeeper://127.0.0.1:" +
+                zkServerPort + "/service"));
+    }
+
+    @Test
+    public void testZookeeperClient() {
+        assertThat(zookeeperClient, not(nullValue()));
+        zookeeperClient.close();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        zkServer.stop();
+    }
+}
\ No newline at end of file


 

----------------------------------------------------------------
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]

Reply via email to