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