Showing
1 changed file
with
125 additions
and
0 deletions
core/store/dist/src/test/java/org/onlab/onos/store/cluster/impl/ClusterCommunicationManagerTest.java
0 → 100644
| 1 | +package org.onlab.onos.store.cluster.impl; | ||
| 2 | + | ||
| 3 | +import org.junit.After; | ||
| 4 | +import org.junit.Before; | ||
| 5 | +import org.junit.Test; | ||
| 6 | +import org.onlab.onos.cluster.DefaultControllerNode; | ||
| 7 | +import org.onlab.onos.cluster.NodeId; | ||
| 8 | +import org.onlab.onos.store.cluster.messaging.SerializationService; | ||
| 9 | +import org.onlab.packet.IpPrefix; | ||
| 10 | +import sun.jvm.hotspot.TestDebugger; | ||
| 11 | + | ||
| 12 | +import java.util.concurrent.CountDownLatch; | ||
| 13 | +import java.util.concurrent.TimeUnit; | ||
| 14 | + | ||
| 15 | +import static org.junit.Assert.*; | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * Tests of the cluster communication manager. | ||
| 19 | + */ | ||
| 20 | +public class ClusterCommunicationManagerTest { | ||
| 21 | + | ||
| 22 | + private static final NodeId N1 = new NodeId("n1"); | ||
| 23 | + private static final NodeId N2 = new NodeId("n2"); | ||
| 24 | + | ||
| 25 | + private static final int P1 = 9881; | ||
| 26 | + private static final int P2 = 9882; | ||
| 27 | + | ||
| 28 | + private static final IpPrefix IP = IpPrefix.valueOf("127.0.0.1"); | ||
| 29 | + | ||
| 30 | + private ClusterCommunicationManager ccm1; | ||
| 31 | + private ClusterCommunicationManager ccm2; | ||
| 32 | + | ||
| 33 | + private TestDelegate cnd1 = new TestDelegate(); | ||
| 34 | + private TestDelegate cnd2 = new TestDelegate(); | ||
| 35 | + | ||
| 36 | + private DefaultControllerNode node1 = new DefaultControllerNode(N1, IP, P1); | ||
| 37 | + private DefaultControllerNode node2 = new DefaultControllerNode(N2, IP, P2); | ||
| 38 | + | ||
| 39 | + @Before | ||
| 40 | + public void setUp() { | ||
| 41 | + MessageSerializer messageSerializer = new MessageSerializer(); | ||
| 42 | + messageSerializer.activate(); | ||
| 43 | + | ||
| 44 | + ccm1 = new ClusterCommunicationManager(); | ||
| 45 | + ccm1.serializationService = messageSerializer; | ||
| 46 | + ccm1.activate(); | ||
| 47 | + | ||
| 48 | + ccm2 = new ClusterCommunicationManager(); | ||
| 49 | + ccm2.serializationService = messageSerializer; | ||
| 50 | + ccm2.activate(); | ||
| 51 | + | ||
| 52 | + ccm1.startUp(node1, cnd1); | ||
| 53 | + ccm2.startUp(node2, cnd2); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + @After | ||
| 57 | + public void tearDown() { | ||
| 58 | + ccm1.deactivate(); | ||
| 59 | + ccm2.deactivate(); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + @Test | ||
| 63 | + public void connect() throws Exception { | ||
| 64 | + cnd1.latch = new CountDownLatch(1); | ||
| 65 | + cnd2.latch = new CountDownLatch(1); | ||
| 66 | + | ||
| 67 | + ccm1.addNode(node2); | ||
| 68 | + validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); | ||
| 69 | + validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + @Test | ||
| 73 | + public void disconnect() throws Exception { | ||
| 74 | + cnd1.latch = new CountDownLatch(1); | ||
| 75 | + cnd2.latch = new CountDownLatch(1); | ||
| 76 | + | ||
| 77 | + ccm1.addNode(node2); | ||
| 78 | + validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); | ||
| 79 | + validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); | ||
| 80 | + | ||
| 81 | + cnd1.latch = new CountDownLatch(1); | ||
| 82 | + cnd2.latch = new CountDownLatch(1); | ||
| 83 | + ccm1.deactivate(); | ||
| 84 | +// | ||
| 85 | +// validateDelegateEvent(cnd2, Op.VANISHED, node1.id()); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId) | ||
| 89 | + throws InterruptedException { | ||
| 90 | + assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS)); | ||
| 91 | + assertEquals("incorrect event", op, delegate.op); | ||
| 92 | + assertEquals("incorrect event node", nodeId, delegate.nodeId); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + enum Op { DETECTED, VANISHED, REMOVED }; | ||
| 96 | + | ||
| 97 | + private class TestDelegate implements ClusterNodesDelegate { | ||
| 98 | + | ||
| 99 | + Op op; | ||
| 100 | + CountDownLatch latch; | ||
| 101 | + NodeId nodeId; | ||
| 102 | + | ||
| 103 | + @Override | ||
| 104 | + public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) { | ||
| 105 | + latch(nodeId, Op.DETECTED); | ||
| 106 | + return new DefaultControllerNode(nodeId, ip, tcpPort); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + @Override | ||
| 110 | + public void nodeVanished(NodeId nodeId) { | ||
| 111 | + latch(nodeId, Op.VANISHED); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + @Override | ||
| 115 | + public void nodeRemoved(NodeId nodeId) { | ||
| 116 | + latch(nodeId, Op.REMOVED); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + private void latch(NodeId nodeId, Op op) { | ||
| 120 | + this.op = op; | ||
| 121 | + this.nodeId = nodeId; | ||
| 122 | + latch.countDown(); | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment