Yuta HIGUCHI | 0ad9eff | 2014-10-12 00:07:25 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.cluster.messaging.impl; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 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; |
Yuta HIGUCHI | 0ad9eff | 2014-10-12 00:07:25 -0700 | [diff] [blame] | 9 | import org.onlab.onos.store.cluster.impl.ClusterNodesDelegate; |
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 | 3b0dfd5 | 2014-10-02 16:48:13 -0700 | [diff] [blame] | 32 | private ClusterCommunicationManager ccm1; |
| 33 | private ClusterCommunicationManager 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 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 44 | NettyMessagingService messagingService = new NettyMessagingService(); |
| 45 | messagingService.activate(); |
| 46 | |
Madan Jampani | 3b0dfd5 | 2014-10-02 16:48:13 -0700 | [diff] [blame] | 47 | ccm1 = new ClusterCommunicationManager(); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 48 | ccm1.activate(); |
| 49 | |
Madan Jampani | 3b0dfd5 | 2014-10-02 16:48:13 -0700 | [diff] [blame] | 50 | ccm2 = new ClusterCommunicationManager(); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 51 | ccm2.activate(); |
| 52 | |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 53 | // ccm1.initialize(node1, cnd1); |
| 54 | // ccm2.initialize(node2, cnd2); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @After |
| 58 | public void tearDown() { |
| 59 | ccm1.deactivate(); |
| 60 | ccm2.deactivate(); |
| 61 | } |
| 62 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 63 | @Ignore("FIXME: failing randomly?") |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 64 | @Test |
| 65 | public void connect() throws Exception { |
| 66 | cnd1.latch = new CountDownLatch(1); |
| 67 | cnd2.latch = new CountDownLatch(1); |
| 68 | |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 69 | // ccm1.addNode(node2); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 70 | validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); |
| 71 | validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); |
| 72 | } |
| 73 | |
| 74 | @Test |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 75 | @Ignore |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 76 | public void disconnect() throws Exception { |
| 77 | cnd1.latch = new CountDownLatch(1); |
| 78 | cnd2.latch = new CountDownLatch(1); |
| 79 | |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 80 | // ccm1.addNode(node2); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 81 | validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); |
| 82 | validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); |
| 83 | |
| 84 | cnd1.latch = new CountDownLatch(1); |
| 85 | cnd2.latch = new CountDownLatch(1); |
| 86 | ccm1.deactivate(); |
| 87 | // |
| 88 | // validateDelegateEvent(cnd2, Op.VANISHED, node1.id()); |
| 89 | } |
| 90 | |
| 91 | private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId) |
| 92 | throws InterruptedException { |
| 93 | assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS)); |
| 94 | assertEquals("incorrect event", op, delegate.op); |
| 95 | assertEquals("incorrect event node", nodeId, delegate.nodeId); |
| 96 | } |
| 97 | |
| 98 | enum Op { DETECTED, VANISHED, REMOVED }; |
| 99 | |
| 100 | private class TestDelegate implements ClusterNodesDelegate { |
| 101 | |
| 102 | Op op; |
| 103 | CountDownLatch latch; |
| 104 | NodeId nodeId; |
| 105 | |
| 106 | @Override |
| 107 | public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 108 | latch(nodeId, Op.DETECTED); |
| 109 | return new DefaultControllerNode(nodeId, ip, tcpPort); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void nodeVanished(NodeId nodeId) { |
| 114 | latch(nodeId, Op.VANISHED); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void nodeRemoved(NodeId nodeId) { |
| 119 | latch(nodeId, Op.REMOVED); |
| 120 | } |
| 121 | |
| 122 | private void latch(NodeId nodeId, Op op) { |
| 123 | this.op = op; |
| 124 | this.nodeId = nodeId; |
| 125 | latch.countDown(); |
| 126 | } |
| 127 | } |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 128 | } |