blob: bba12f2b6b89cc5ec324f333065db5f95172e23b [file] [log] [blame]
tom5346a952014-09-30 09:02:51 -07001package org.onlab.onos.store.cluster.impl;
2
3import org.junit.After;
4import org.junit.Before;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -07005import org.junit.Ignore;
tom5346a952014-09-30 09:02:51 -07006import org.junit.Test;
7import org.onlab.onos.cluster.DefaultControllerNode;
8import org.onlab.onos.cluster.NodeId;
Madan Jampani3b0dfd52014-10-02 16:48:13 -07009import org.onlab.onos.store.cluster.messaging.impl.ClusterCommunicationManager;
Madan Jampanic9ed9be2014-10-02 16:13:11 -070010import org.onlab.netty.NettyMessagingService;
tom5346a952014-09-30 09:02:51 -070011import org.onlab.packet.IpPrefix;
tom5346a952014-09-30 09:02:51 -070012
13import java.util.concurrent.CountDownLatch;
14import java.util.concurrent.TimeUnit;
15
tome440a632014-09-30 09:04:21 -070016import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.assertTrue;
tom5346a952014-09-30 09:02:51 -070018
19/**
20 * Tests of the cluster communication manager.
21 */
22public 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 Jampani3b0dfd52014-10-02 16:48:13 -070032 private ClusterCommunicationManager ccm1;
33 private ClusterCommunicationManager ccm2;
tom5346a952014-09-30 09:02:51 -070034
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 Jampani890bc352014-10-01 22:35:29 -070042 public void setUp() throws Exception {
tom5346a952014-09-30 09:02:51 -070043 MessageSerializer messageSerializer = new MessageSerializer();
44 messageSerializer.activate();
45
Madan Jampani890bc352014-10-01 22:35:29 -070046 NettyMessagingService messagingService = new NettyMessagingService();
47 messagingService.activate();
48
Madan Jampani3b0dfd52014-10-02 16:48:13 -070049 ccm1 = new ClusterCommunicationManager();
Madan Jampani890bc352014-10-01 22:35:29 -070050// ccm1.serializationService = messageSerializer;
tom5346a952014-09-30 09:02:51 -070051 ccm1.activate();
52
Madan Jampani3b0dfd52014-10-02 16:48:13 -070053 ccm2 = new ClusterCommunicationManager();
Madan Jampani890bc352014-10-01 22:35:29 -070054// ccm2.serializationService = messageSerializer;
tom5346a952014-09-30 09:02:51 -070055 ccm2.activate();
56
Madan Jampani890bc352014-10-01 22:35:29 -070057 ccm1.initialize(node1, cnd1);
58 ccm2.initialize(node2, cnd2);
tom5346a952014-09-30 09:02:51 -070059 }
60
61 @After
62 public void tearDown() {
63 ccm1.deactivate();
64 ccm2.deactivate();
65 }
66
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070067 @Ignore("FIXME: failing randomly?")
tom5346a952014-09-30 09:02:51 -070068 @Test
69 public void connect() throws Exception {
70 cnd1.latch = new CountDownLatch(1);
71 cnd2.latch = new CountDownLatch(1);
72
73 ccm1.addNode(node2);
74 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
75 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
76 }
77
78 @Test
Madan Jampani890bc352014-10-01 22:35:29 -070079 @Ignore
tom5346a952014-09-30 09:02:51 -070080 public void disconnect() throws Exception {
81 cnd1.latch = new CountDownLatch(1);
82 cnd2.latch = new CountDownLatch(1);
83
84 ccm1.addNode(node2);
85 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
86 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
87
88 cnd1.latch = new CountDownLatch(1);
89 cnd2.latch = new CountDownLatch(1);
90 ccm1.deactivate();
91//
92// validateDelegateEvent(cnd2, Op.VANISHED, node1.id());
93 }
94
95 private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId)
96 throws InterruptedException {
97 assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS));
98 assertEquals("incorrect event", op, delegate.op);
99 assertEquals("incorrect event node", nodeId, delegate.nodeId);
100 }
101
102 enum Op { DETECTED, VANISHED, REMOVED };
103
104 private class TestDelegate implements ClusterNodesDelegate {
105
106 Op op;
107 CountDownLatch latch;
108 NodeId nodeId;
109
110 @Override
111 public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) {
112 latch(nodeId, Op.DETECTED);
113 return new DefaultControllerNode(nodeId, ip, tcpPort);
114 }
115
116 @Override
117 public void nodeVanished(NodeId nodeId) {
118 latch(nodeId, Op.VANISHED);
119 }
120
121 @Override
122 public void nodeRemoved(NodeId nodeId) {
123 latch(nodeId, Op.REMOVED);
124 }
125
126 private void latch(NodeId nodeId, Op op) {
127 this.op = op;
128 this.nodeId = nodeId;
129 latch.countDown();
130 }
131 }
Madan Jampani890bc352014-10-01 22:35:29 -0700132}