tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 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; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 8 | import org.onlab.packet.IpPrefix; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 9 | |
| 10 | import java.util.concurrent.CountDownLatch; |
| 11 | import java.util.concurrent.TimeUnit; |
| 12 | |
tom | e440a63 | 2014-09-30 09:04:21 -0700 | [diff] [blame] | 13 | import static org.junit.Assert.assertEquals; |
| 14 | import static org.junit.Assert.assertTrue; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Tests of the cluster communication manager. |
| 18 | */ |
| 19 | public class ClusterCommunicationManagerTest { |
| 20 | |
| 21 | private static final NodeId N1 = new NodeId("n1"); |
| 22 | private static final NodeId N2 = new NodeId("n2"); |
| 23 | |
| 24 | private static final int P1 = 9881; |
| 25 | private static final int P2 = 9882; |
| 26 | |
| 27 | private static final IpPrefix IP = IpPrefix.valueOf("127.0.0.1"); |
| 28 | |
| 29 | private ClusterCommunicationManager ccm1; |
| 30 | private ClusterCommunicationManager ccm2; |
| 31 | |
| 32 | private TestDelegate cnd1 = new TestDelegate(); |
| 33 | private TestDelegate cnd2 = new TestDelegate(); |
| 34 | |
| 35 | private DefaultControllerNode node1 = new DefaultControllerNode(N1, IP, P1); |
| 36 | private DefaultControllerNode node2 = new DefaultControllerNode(N2, IP, P2); |
| 37 | |
| 38 | @Before |
| 39 | public void setUp() { |
| 40 | MessageSerializer messageSerializer = new MessageSerializer(); |
| 41 | messageSerializer.activate(); |
| 42 | |
| 43 | ccm1 = new ClusterCommunicationManager(); |
| 44 | ccm1.serializationService = messageSerializer; |
| 45 | ccm1.activate(); |
| 46 | |
| 47 | ccm2 = new ClusterCommunicationManager(); |
| 48 | ccm2.serializationService = messageSerializer; |
| 49 | ccm2.activate(); |
| 50 | |
| 51 | ccm1.startUp(node1, cnd1); |
| 52 | ccm2.startUp(node2, cnd2); |
| 53 | } |
| 54 | |
| 55 | @After |
| 56 | public void tearDown() { |
| 57 | ccm1.deactivate(); |
| 58 | ccm2.deactivate(); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void connect() throws Exception { |
| 63 | cnd1.latch = new CountDownLatch(1); |
| 64 | cnd2.latch = new CountDownLatch(1); |
| 65 | |
| 66 | ccm1.addNode(node2); |
| 67 | validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); |
| 68 | validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void disconnect() throws Exception { |
| 73 | cnd1.latch = new CountDownLatch(1); |
| 74 | cnd2.latch = new CountDownLatch(1); |
| 75 | |
| 76 | ccm1.addNode(node2); |
| 77 | validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); |
| 78 | validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); |
| 79 | |
| 80 | cnd1.latch = new CountDownLatch(1); |
| 81 | cnd2.latch = new CountDownLatch(1); |
| 82 | ccm1.deactivate(); |
| 83 | // |
| 84 | // validateDelegateEvent(cnd2, Op.VANISHED, node1.id()); |
| 85 | } |
| 86 | |
| 87 | private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId) |
| 88 | throws InterruptedException { |
| 89 | assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS)); |
| 90 | assertEquals("incorrect event", op, delegate.op); |
| 91 | assertEquals("incorrect event node", nodeId, delegate.nodeId); |
| 92 | } |
| 93 | |
| 94 | enum Op { DETECTED, VANISHED, REMOVED }; |
| 95 | |
| 96 | private class TestDelegate implements ClusterNodesDelegate { |
| 97 | |
| 98 | Op op; |
| 99 | CountDownLatch latch; |
| 100 | NodeId nodeId; |
| 101 | |
| 102 | @Override |
| 103 | public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 104 | latch(nodeId, Op.DETECTED); |
| 105 | return new DefaultControllerNode(nodeId, ip, tcpPort); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void nodeVanished(NodeId nodeId) { |
| 110 | latch(nodeId, Op.VANISHED); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void nodeRemoved(NodeId nodeId) { |
| 115 | latch(nodeId, Op.REMOVED); |
| 116 | } |
| 117 | |
| 118 | private void latch(NodeId nodeId, Op op) { |
| 119 | this.op = op; |
| 120 | this.nodeId = nodeId; |
| 121 | latch.countDown(); |
| 122 | } |
| 123 | } |
| 124 | } |