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