blob: 34e5b3b07d510b101884676a62e4b79923b6cc65 [file] [log] [blame]
Yuta HIGUCHI0ad9eff2014-10-12 00:07:25 -07001package org.onlab.onos.store.cluster.messaging.impl;
tom5346a952014-09-30 09:02:51 -07002
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;
Yuta HIGUCHI0ad9eff2014-10-12 00:07:25 -07009import org.onlab.onos.store.cluster.impl.ClusterNodesDelegate;
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
Madan Jampani890bc352014-10-01 22:35:29 -070044 NettyMessagingService messagingService = new NettyMessagingService();
45 messagingService.activate();
46
Madan Jampani3b0dfd52014-10-02 16:48:13 -070047 ccm1 = new ClusterCommunicationManager();
tom5346a952014-09-30 09:02:51 -070048 ccm1.activate();
49
Madan Jampani3b0dfd52014-10-02 16:48:13 -070050 ccm2 = new ClusterCommunicationManager();
tom5346a952014-09-30 09:02:51 -070051 ccm2.activate();
52
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070053// ccm1.initialize(node1, cnd1);
54// ccm2.initialize(node2, cnd2);
tom5346a952014-09-30 09:02:51 -070055 }
56
57 @After
58 public void tearDown() {
59 ccm1.deactivate();
60 ccm2.deactivate();
61 }
62
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070063 @Ignore("FIXME: failing randomly?")
tom5346a952014-09-30 09:02:51 -070064 @Test
65 public void connect() throws Exception {
66 cnd1.latch = new CountDownLatch(1);
67 cnd2.latch = new CountDownLatch(1);
68
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070069// ccm1.addNode(node2);
tom5346a952014-09-30 09:02:51 -070070 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
71 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
72 }
73
74 @Test
Madan Jampani890bc352014-10-01 22:35:29 -070075 @Ignore
tom5346a952014-09-30 09:02:51 -070076 public void disconnect() throws Exception {
77 cnd1.latch = new CountDownLatch(1);
78 cnd2.latch = new CountDownLatch(1);
79
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070080// ccm1.addNode(node2);
tom5346a952014-09-30 09:02:51 -070081 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
82 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
83
84 cnd1.latch = new CountDownLatch(1);
85 cnd2.latch = new CountDownLatch(1);
86 ccm1.deactivate();
87//
88// validateDelegateEvent(cnd2, Op.VANISHED, node1.id());
89 }
90
91 private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId)
92 throws InterruptedException {
93 assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS));
94 assertEquals("incorrect event", op, delegate.op);
95 assertEquals("incorrect event node", nodeId, delegate.nodeId);
96 }
97
98 enum Op { DETECTED, VANISHED, REMOVED };
99
100 private class TestDelegate implements ClusterNodesDelegate {
101
102 Op op;
103 CountDownLatch latch;
104 NodeId nodeId;
105
106 @Override
107 public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) {
108 latch(nodeId, Op.DETECTED);
109 return new DefaultControllerNode(nodeId, ip, tcpPort);
110 }
111
112 @Override
113 public void nodeVanished(NodeId nodeId) {
114 latch(nodeId, Op.VANISHED);
115 }
116
117 @Override
118 public void nodeRemoved(NodeId nodeId) {
119 latch(nodeId, Op.REMOVED);
120 }
121
122 private void latch(NodeId nodeId, Op op) {
123 this.op = op;
124 this.nodeId = nodeId;
125 latch.countDown();
126 }
127 }
Madan Jampani890bc352014-10-01 22:35:29 -0700128}