Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Yuta HIGUCHI | 0ad9eff | 2014-10-12 00:07:25 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.cluster.messaging.impl; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 20 | import org.junit.Ignore; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 23 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | 0ad9eff | 2014-10-12 00:07:25 -0700 | [diff] [blame] | 24 | import org.onlab.onos.store.cluster.impl.ClusterNodesDelegate; |
Madan Jampani | c9ed9be | 2014-10-02 16:13:11 -0700 | [diff] [blame] | 25 | import org.onlab.netty.NettyMessagingService; |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.concurrent.CountDownLatch; |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | |
tom | e440a63 | 2014-09-30 09:04:21 -0700 | [diff] [blame] | 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertTrue; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Tests of the cluster communication manager. |
| 36 | */ |
| 37 | public class ClusterCommunicationManagerTest { |
| 38 | |
| 39 | private static final NodeId N1 = new NodeId("n1"); |
| 40 | private static final NodeId N2 = new NodeId("n2"); |
| 41 | |
| 42 | private static final int P1 = 9881; |
| 43 | private static final int P2 = 9882; |
| 44 | |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 45 | private static final IpAddress IP = IpAddress.valueOf("127.0.0.1"); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 46 | |
Madan Jampani | 3b0dfd5 | 2014-10-02 16:48:13 -0700 | [diff] [blame] | 47 | private ClusterCommunicationManager ccm1; |
| 48 | private ClusterCommunicationManager ccm2; |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 49 | |
| 50 | private TestDelegate cnd1 = new TestDelegate(); |
| 51 | private TestDelegate cnd2 = new TestDelegate(); |
| 52 | |
| 53 | private DefaultControllerNode node1 = new DefaultControllerNode(N1, IP, P1); |
| 54 | private DefaultControllerNode node2 = new DefaultControllerNode(N2, IP, P2); |
| 55 | |
| 56 | @Before |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 57 | public void setUp() throws Exception { |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 58 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 59 | NettyMessagingService messagingService = new NettyMessagingService(); |
| 60 | messagingService.activate(); |
| 61 | |
Madan Jampani | 3b0dfd5 | 2014-10-02 16:48:13 -0700 | [diff] [blame] | 62 | ccm1 = new ClusterCommunicationManager(); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 63 | ccm1.activate(); |
| 64 | |
Madan Jampani | 3b0dfd5 | 2014-10-02 16:48:13 -0700 | [diff] [blame] | 65 | ccm2 = new ClusterCommunicationManager(); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 66 | ccm2.activate(); |
| 67 | |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 68 | // ccm1.initialize(node1, cnd1); |
| 69 | // ccm2.initialize(node2, cnd2); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @After |
| 73 | public void tearDown() { |
| 74 | ccm1.deactivate(); |
| 75 | ccm2.deactivate(); |
| 76 | } |
| 77 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 78 | @Ignore("FIXME: failing randomly?") |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 79 | @Test |
| 80 | public void connect() throws Exception { |
| 81 | cnd1.latch = new CountDownLatch(1); |
| 82 | cnd2.latch = new CountDownLatch(1); |
| 83 | |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 84 | // ccm1.addNode(node2); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 85 | validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); |
| 86 | validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); |
| 87 | } |
| 88 | |
| 89 | @Test |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 90 | @Ignore |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 91 | public void disconnect() throws Exception { |
| 92 | cnd1.latch = new CountDownLatch(1); |
| 93 | cnd2.latch = new CountDownLatch(1); |
| 94 | |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 95 | // ccm1.addNode(node2); |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 96 | validateDelegateEvent(cnd1, Op.DETECTED, node2.id()); |
| 97 | validateDelegateEvent(cnd2, Op.DETECTED, node1.id()); |
| 98 | |
| 99 | cnd1.latch = new CountDownLatch(1); |
| 100 | cnd2.latch = new CountDownLatch(1); |
| 101 | ccm1.deactivate(); |
| 102 | // |
| 103 | // validateDelegateEvent(cnd2, Op.VANISHED, node1.id()); |
| 104 | } |
| 105 | |
| 106 | private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId) |
| 107 | throws InterruptedException { |
| 108 | assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS)); |
| 109 | assertEquals("incorrect event", op, delegate.op); |
| 110 | assertEquals("incorrect event node", nodeId, delegate.nodeId); |
| 111 | } |
| 112 | |
| 113 | enum Op { DETECTED, VANISHED, REMOVED }; |
| 114 | |
| 115 | private class TestDelegate implements ClusterNodesDelegate { |
| 116 | |
| 117 | Op op; |
| 118 | CountDownLatch latch; |
| 119 | NodeId nodeId; |
| 120 | |
| 121 | @Override |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 122 | public DefaultControllerNode nodeDetected(NodeId nodeId, IpAddress ip, int tcpPort) { |
tom | 5346a95 | 2014-09-30 09:02:51 -0700 | [diff] [blame] | 123 | latch(nodeId, Op.DETECTED); |
| 124 | return new DefaultControllerNode(nodeId, ip, tcpPort); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void nodeVanished(NodeId nodeId) { |
| 129 | latch(nodeId, Op.VANISHED); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void nodeRemoved(NodeId nodeId) { |
| 134 | latch(nodeId, Op.REMOVED); |
| 135 | } |
| 136 | |
| 137 | private void latch(NodeId nodeId, Op op) { |
| 138 | this.op = op; |
| 139 | this.nodeId = nodeId; |
| 140 | latch.countDown(); |
| 141 | } |
| 142 | } |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 143 | } |