2 * Copyright 2014 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.cluster.messaging.impl;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.onosproject.cluster.DefaultControllerNode;
23 import org.onosproject.cluster.NodeId;
24 import org.onosproject.store.cluster.impl.ClusterNodesDelegate;
25 import org.onlab.packet.IpAddress;
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.TimeUnit;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertTrue;
34 * Tests of the cluster communication manager.
36 public class ClusterCommunicationManagerTest {
38 private static final NodeId N1 = new NodeId("n1");
39 private static final NodeId N2 = new NodeId("n2");
41 private static final int P1 = 9881;
42 private static final int P2 = 9882;
44 private static final IpAddress IP = IpAddress.valueOf("127.0.0.1");
46 private ClusterCommunicationManager ccm1;
47 private ClusterCommunicationManager ccm2;
49 private TestDelegate cnd1 = new TestDelegate();
50 private TestDelegate cnd2 = new TestDelegate();
52 private DefaultControllerNode node1 = new DefaultControllerNode(N1, IP, P1);
53 private DefaultControllerNode node2 = new DefaultControllerNode(N2, IP, P2);
56 public void setUp() throws Exception {
58 NettyMessagingManager messagingService = new NettyMessagingManager();
59 messagingService.activate();
61 ccm1 = new ClusterCommunicationManager();
64 ccm2 = new ClusterCommunicationManager();
67 // ccm1.initialize(node1, cnd1);
68 // ccm2.initialize(node2, cnd2);
72 public void tearDown() {
77 @Ignore("FIXME: failing randomly?")
79 public void connect() throws Exception {
80 cnd1.latch = new CountDownLatch(1);
81 cnd2.latch = new CountDownLatch(1);
83 // ccm1.addNode(node2);
84 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
85 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
90 public void disconnect() throws Exception {
91 cnd1.latch = new CountDownLatch(1);
92 cnd2.latch = new CountDownLatch(1);
94 // ccm1.addNode(node2);
95 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
96 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
98 cnd1.latch = new CountDownLatch(1);
99 cnd2.latch = new CountDownLatch(1);
102 // validateDelegateEvent(cnd2, Op.VANISHED, node1.id());
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);
112 enum Op { DETECTED, VANISHED, REMOVED };
114 private class TestDelegate implements ClusterNodesDelegate {
117 CountDownLatch latch;
121 public DefaultControllerNode nodeDetected(NodeId nodeId, IpAddress ip, int tcpPort) {
122 latch(nodeId, Op.DETECTED);
123 return new DefaultControllerNode(nodeId, ip, tcpPort);
127 public void nodeVanished(NodeId nodeId) {
128 latch(nodeId, Op.VANISHED);
132 public void nodeRemoved(NodeId nodeId) {
133 latch(nodeId, Op.REMOVED);
136 private void latch(NodeId nodeId, Op op) {
138 this.nodeId = nodeId;