changeset 3:c81cdb1b037d

More exploration tests
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 23 Sep 2016 19:19:18 +0200
parents 9a19a94a5e74
children 6106d0f7f81a
files src/test/java/net/borgac/clusterrpc/DependenciesTest.java src/test/java/net/borgac/clusterrpc/JeroMQExploratoryTest.java src/test/java/net/borgac/clusterrpc/JeroMQGeneral.java src/test/java/net/borgac/clusterrpc/MiscExplorationTest.java src/test/java/net/borgac/clusterrpc/TestDependencies.java
diffstat 5 files changed, 223 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/net/borgac/clusterrpc/DependenciesTest.java	Fri Sep 23 19:19:18 2016 +0200
@@ -0,0 +1,53 @@
+package net.borgac.clusterrpc;
+
+import com.google.protobuf.Empty;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.zeromq.ZMQ;
+
+/**
+ *
+ * @author lbo
+ */
+public class DependenciesTest {
+
+    public DependenciesTest() {
+    }
+
+    @BeforeClass
+    public static void setUpClass() {
+    }
+
+    @AfterClass
+    public static void tearDownClass() {
+    }
+
+    @Before
+    public void setUp() {
+    }
+
+    @After
+    public void tearDown() {
+    }
+
+    // TODO add test methods here.
+    // The methods must be annotated with annotation @Test. For example:
+    //
+    // @Test
+    // public void hello() {}
+    @Test
+    public void testProtobufLib() {
+        Empty e = Empty.newBuilder().build();
+        Assert.assertNotNull(e);
+    }
+
+    @Test
+    public void testJeroMQLib() {
+        int reqType = ZMQ.REQ;
+        Assert.assertEquals(ZMQ.REQ, reqType);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/net/borgac/clusterrpc/JeroMQExploratoryTest.java	Fri Sep 23 19:19:18 2016 +0200
@@ -0,0 +1,124 @@
+package net.borgac.clusterrpc;
+
+import java.util.LinkedList;
+import java.util.List;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.zeromq.ZMQ;
+
+/**
+ * A unit test to explore how JeroMQ works.
+ *
+ * @author lbo
+ */
+public class JeroMQExploratoryTest {
+
+    private static final String address = "inproc://jero-exploration";
+    private static final String routerAddress = "inproc://jero-exploration-router";
+    private final ZMQ.Context context;
+    private ZMQ.Socket server, client;
+    private ZMQ.Socket router, routerClient;
+
+    public JeroMQExploratoryTest() {
+        this.context = ZMQ.context(1);
+    }
+
+    @BeforeClass
+    public static void setUpClass() {
+    }
+
+    @AfterClass
+    public static void tearDownClass() {
+    }
+
+    @Before
+    public void setUp() {
+        server = context.socket(ZMQ.REP);
+        server.bind(address);
+        client = context.socket(ZMQ.REQ);
+        client.connect(address);
+
+        router = context.socket(ZMQ.ROUTER);
+        router.bind(routerAddress);
+        routerClient = context.socket(ZMQ.REQ);
+        routerClient.connect(routerAddress);
+    }
+
+    @After
+    public void tearDown() {
+        server.close();
+        client.close();
+
+        router.close();
+        routerClient.close();
+    }
+
+    @Test
+    public void testReqRep() {
+
+        client.send("Hello World");
+        String msg = server.recvStr();
+
+        Assert.assertEquals("Hello World", msg);
+    }
+
+    @Test
+    public void testConcurrentReqRep() throws InterruptedException {
+        Runnable serverThreadCode = () -> {
+            String fromClient = server.recvStr();
+            Assert.assertEquals("from client", fromClient);
+
+            server.send("from server");
+        };
+
+        Thread serverThread = new Thread(serverThreadCode);
+        serverThread.start();
+
+        Thread.sleep(500);
+
+        client.send("from client");
+        String fromServer = client.recvStr();
+
+        Assert.assertEquals("from server", fromServer);
+
+        serverThread.join();
+    }
+
+    @Test
+    public void testRouter() throws InterruptedException {
+        routerClient.disconnect(routerAddress);
+        routerClient.setIdentity("id1".getBytes());
+        routerClient.connect(routerAddress);
+
+        routerClient.send("Hello");
+
+        String id = new String(router.recv());
+        List<String> parts = new LinkedList<>();
+
+        while (router.hasReceiveMore()) {
+            parts.add(router.recvStr());
+        }
+
+        Assert.assertEquals(2, parts.size());
+        Assert.assertEquals("id1", id);
+        Assert.assertEquals("", parts.get(0));
+        Assert.assertEquals("Hello", parts.get(1));
+
+        router.sendMore("id1");
+        router.sendMore("");
+        router.send("Hello back");
+
+        String reply = routerClient.recvStr();
+
+        Assert.assertEquals("Hello back", reply);
+    }
+
+    @Test
+    public void testEncryption() {
+        // No encryption D:
+    }
+}
--- a/src/test/java/net/borgac/clusterrpc/JeroMQGeneral.java	Fri Sep 23 16:50:09 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-package net.borgac.clusterrpc;
-
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.zeromq.ZMQ;
-
-/**
- * A unit test to explore how JeroMQ works.
- *
- * @author lbo
- */
-public class JeroMQGeneral {
-
-    private static final String address = "inproc://jero-exploration";
-    private final ZMQ.Context context;
-
-    public JeroMQGeneral() {
-        this.context = ZMQ.context(1);
-    }
-
-    @BeforeClass
-    public static void setUpClass() {
-    }
-
-    @AfterClass
-    public static void tearDownClass() {
-    }
-
-    @Before
-    public void setUp() {
-    }
-
-    @After
-    public void tearDown() {
-    }
-
-    @Test
-    public void testReqRep() {
-        ZMQ.Socket server = context.socket(ZMQ.REP);
-        ZMQ.Socket client = context.socket(ZMQ.REQ);
-
-        server.bind(address);
-        client.connect(address);
-
-        client.send("Hello World");
-        String msg = server.recvStr();
-
-        Assert.assertEquals("HelloWorld", msg);
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/net/borgac/clusterrpc/MiscExplorationTest.java	Fri Sep 23 19:19:18 2016 +0200
@@ -0,0 +1,46 @@
+package net.borgac.clusterrpc;
+
+import java.net.InetSocketAddress;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ * @author lbo
+ */
+public class MiscExplorationTest {
+
+    public MiscExplorationTest() {
+    }
+
+    @BeforeClass
+    public static void setUpClass() {
+    }
+
+    @AfterClass
+    public static void tearDownClass() {
+    }
+
+    @Before
+    public void setUp() {
+    }
+
+    @After
+    public void tearDown() {
+    }
+
+    // TODO add test methods here.
+    // The methods must be annotated with annotation @Test. For example:
+    //
+    // @Test
+    // public void hello() {}
+    @Test
+    public void testInetAddress() {
+        InetSocketAddress addr = new InetSocketAddress("borgac.net", 80);
+        Assert.assertEquals("borgac.net:80", addr.toString());
+    }
+}
--- a/src/test/java/net/borgac/clusterrpc/TestDependencies.java	Fri Sep 23 16:50:09 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-package net.borgac.clusterrpc;
-
-import com.google.protobuf.Empty;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- *
- * @author lbo
- */
-public class TestDependencies {
-
-    public TestDependencies() {
-    }
-
-    @BeforeClass
-    public static void setUpClass() {
-    }
-
-    @AfterClass
-    public static void tearDownClass() {
-    }
-
-    @Before
-    public void setUp() {
-    }
-
-    @After
-    public void tearDown() {
-    }
-
-    // TODO add test methods here.
-    // The methods must be annotated with annotation @Test. For example:
-    //
-    // @Test
-    // public void hello() {}
-    @Test
-    public void testProtobufLib() {
-        Empty e = Empty.newBuilder().build();
-        Assert.assertNotNull(e);
-    }
-}