blob: e63fcaa491603ef5a6e0f7afa6efa0655aa2d273 [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;
Yuta HIGUCHIc057c632014-10-06 18:38:14 -070010import org.onlab.onos.store.cluster.messaging.impl.MessageSerializer;
Madan Jampanic9ed9be2014-10-02 16:13:11 -070011import org.onlab.netty.NettyMessagingService;
tom5346a952014-09-30 09:02:51 -070012import org.onlab.packet.IpPrefix;
tom5346a952014-09-30 09:02:51 -070013
14import java.util.concurrent.CountDownLatch;
15import java.util.concurrent.TimeUnit;
16
tome440a632014-09-30 09:04:21 -070017import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertTrue;
tom5346a952014-09-30 09:02:51 -070019
20/**
21 * Tests of the cluster communication manager.
22 */
23public class ClusterCommunicationManagerTest {
24
25 private static final NodeId N1 = new NodeId("n1");
26 private static final NodeId N2 = new NodeId("n2");
27
28 private static final int P1 = 9881;
29 private static final int P2 = 9882;
30
31 private static final IpPrefix IP = IpPrefix.valueOf("127.0.0.1");
32
Madan Jampani3b0dfd52014-10-02 16:48:13 -070033 private ClusterCommunicationManager ccm1;
34 private ClusterCommunicationManager ccm2;
tom5346a952014-09-30 09:02:51 -070035
36 private TestDelegate cnd1 = new TestDelegate();
37 private TestDelegate cnd2 = new TestDelegate();
38
39 private DefaultControllerNode node1 = new DefaultControllerNode(N1, IP, P1);
40 private DefaultControllerNode node2 = new DefaultControllerNode(N2, IP, P2);
41
42 @Before
Madan Jampani890bc352014-10-01 22:35:29 -070043 public void setUp() throws Exception {
tom5346a952014-09-30 09:02:51 -070044 MessageSerializer messageSerializer = new MessageSerializer();
45 messageSerializer.activate();
46
Madan Jampani890bc352014-10-01 22:35:29 -070047 NettyMessagingService messagingService = new NettyMessagingService();
48 messagingService.activate();
49
Madan Jampani3b0dfd52014-10-02 16:48:13 -070050 ccm1 = new ClusterCommunicationManager();
Madan Jampani890bc352014-10-01 22:35:29 -070051// ccm1.serializationService = messageSerializer;
tom5346a952014-09-30 09:02:51 -070052 ccm1.activate();
53
Madan Jampani3b0dfd52014-10-02 16:48:13 -070054 ccm2 = new ClusterCommunicationManager();
Madan Jampani890bc352014-10-01 22:35:29 -070055// ccm2.serializationService = messageSerializer;
tom5346a952014-09-30 09:02:51 -070056 ccm2.activate();
57
Madan Jampani890bc352014-10-01 22:35:29 -070058 ccm1.initialize(node1, cnd1);
59 ccm2.initialize(node2, cnd2);
tom5346a952014-09-30 09:02:51 -070060 }
61
62 @After
63 public void tearDown() {
64 ccm1.deactivate();
65 ccm2.deactivate();
66 }
67
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070068 @Ignore("FIXME: failing randomly?")
tom5346a952014-09-30 09:02:51 -070069 @Test
70 public void connect() throws Exception {
71 cnd1.latch = new CountDownLatch(1);
72 cnd2.latch = new CountDownLatch(1);
73
74 ccm1.addNode(node2);
75 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
76 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
77 }
78
79 @Test
Madan Jampani890bc352014-10-01 22:35:29 -070080 @Ignore
tom5346a952014-09-30 09:02:51 -070081 public void disconnect() throws Exception {
82 cnd1.latch = new CountDownLatch(1);
83 cnd2.latch = new CountDownLatch(1);
84
85 ccm1.addNode(node2);
86 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
87 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
88
89 cnd1.latch = new CountDownLatch(1);
90 cnd2.latch = new CountDownLatch(1);
91 ccm1.deactivate();
92//
93// validateDelegateEvent(cnd2, Op.VANISHED, node1.id());
94 }
95
96 private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId)
97 throws InterruptedException {
98 assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS));
99 assertEquals("incorrect event", op, delegate.op);
100 assertEquals("incorrect event node", nodeId, delegate.nodeId);
101 }
102
103 enum Op { DETECTED, VANISHED, REMOVED };
104
105 private class TestDelegate implements ClusterNodesDelegate {
106
107 Op op;
108 CountDownLatch latch;
109 NodeId nodeId;
110
111 @Override
112 public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) {
113 latch(nodeId, Op.DETECTED);
114 return new DefaultControllerNode(nodeId, ip, tcpPort);
115 }
116
117 @Override
118 public void nodeVanished(NodeId nodeId) {
119 latch(nodeId, Op.VANISHED);
120 }
121
122 @Override
123 public void nodeRemoved(NodeId nodeId) {
124 latch(nodeId, Op.REMOVED);
125 }
126
127 private void latch(NodeId nodeId, Op op) {
128 this.op = op;
129 this.nodeId = nodeId;
130 latch.countDown();
131 }
132 }
Madan Jampani890bc352014-10-01 22:35:29 -0700133}