blob: 01192bafe3685e2101b9f5f9d79408421bd86e43 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 HIGUCHI0ad9eff2014-10-12 00:07:25 -070016package org.onlab.onos.store.cluster.messaging.impl;
tom5346a952014-09-30 09:02:51 -070017
18import org.junit.After;
19import org.junit.Before;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070020import org.junit.Ignore;
tom5346a952014-09-30 09:02:51 -070021import org.junit.Test;
22import org.onlab.onos.cluster.DefaultControllerNode;
23import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI0ad9eff2014-10-12 00:07:25 -070024import org.onlab.onos.store.cluster.impl.ClusterNodesDelegate;
Madan Jampanic9ed9be2014-10-02 16:13:11 -070025import org.onlab.netty.NettyMessagingService;
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070026import org.onlab.packet.IpAddress;
tom5346a952014-09-30 09:02:51 -070027
28import java.util.concurrent.CountDownLatch;
29import java.util.concurrent.TimeUnit;
30
tome440a632014-09-30 09:04:21 -070031import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertTrue;
tom5346a952014-09-30 09:02:51 -070033
34/**
35 * Tests of the cluster communication manager.
36 */
37public 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 Radoslavov444b5192014-10-28 10:45:19 -070045 private static final IpAddress IP = IpAddress.valueOf("127.0.0.1");
tom5346a952014-09-30 09:02:51 -070046
Madan Jampani3b0dfd52014-10-02 16:48:13 -070047 private ClusterCommunicationManager ccm1;
48 private ClusterCommunicationManager ccm2;
tom5346a952014-09-30 09:02:51 -070049
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 Jampani890bc352014-10-01 22:35:29 -070057 public void setUp() throws Exception {
tom5346a952014-09-30 09:02:51 -070058
Madan Jampani890bc352014-10-01 22:35:29 -070059 NettyMessagingService messagingService = new NettyMessagingService();
60 messagingService.activate();
61
Madan Jampani3b0dfd52014-10-02 16:48:13 -070062 ccm1 = new ClusterCommunicationManager();
tom5346a952014-09-30 09:02:51 -070063 ccm1.activate();
64
Madan Jampani3b0dfd52014-10-02 16:48:13 -070065 ccm2 = new ClusterCommunicationManager();
tom5346a952014-09-30 09:02:51 -070066 ccm2.activate();
67
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070068// ccm1.initialize(node1, cnd1);
69// ccm2.initialize(node2, cnd2);
tom5346a952014-09-30 09:02:51 -070070 }
71
72 @After
73 public void tearDown() {
74 ccm1.deactivate();
75 ccm2.deactivate();
76 }
77
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070078 @Ignore("FIXME: failing randomly?")
tom5346a952014-09-30 09:02:51 -070079 @Test
80 public void connect() throws Exception {
81 cnd1.latch = new CountDownLatch(1);
82 cnd2.latch = new CountDownLatch(1);
83
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070084// ccm1.addNode(node2);
tom5346a952014-09-30 09:02:51 -070085 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
86 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
87 }
88
89 @Test
Madan Jampani890bc352014-10-01 22:35:29 -070090 @Ignore
tom5346a952014-09-30 09:02:51 -070091 public void disconnect() throws Exception {
92 cnd1.latch = new CountDownLatch(1);
93 cnd2.latch = new CountDownLatch(1);
94
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070095// ccm1.addNode(node2);
tom5346a952014-09-30 09:02:51 -070096 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 Radoslavov444b5192014-10-28 10:45:19 -0700122 public DefaultControllerNode nodeDetected(NodeId nodeId, IpAddress ip, int tcpPort) {
tom5346a952014-09-30 09:02:51 -0700123 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 Jampani890bc352014-10-01 22:35:29 -0700143}