Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.messaging.impl; |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 17 | |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Jonathan Hart | 6f4d586 | 2016-08-17 09:18:01 -0700 | [diff] [blame] | 19 | import com.google.common.util.concurrent.MoreExecutors; |
| 20 | import com.google.common.util.concurrent.Uninterruptibles; |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
Jonathan Hart | 6f4d586 | 2016-08-17 09:18:01 -0700 | [diff] [blame] | 23 | import org.junit.Ignore; |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpAddress; |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterMetadata; |
| 27 | import org.onosproject.cluster.ClusterMetadataEventListener; |
| 28 | import org.onosproject.cluster.ClusterMetadataService; |
| 29 | import org.onosproject.cluster.ControllerNode; |
| 30 | import org.onosproject.cluster.NodeId; |
Madan Jampani | 0583387 | 2016-07-12 23:01:39 -0700 | [diff] [blame] | 31 | import org.onosproject.core.HybridLogicalClockService; |
| 32 | import org.onosproject.core.HybridLogicalTime; |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 33 | import org.onosproject.net.provider.ProviderId; |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 34 | import org.onosproject.store.cluster.messaging.Endpoint; |
| 35 | |
Jonathan Hart | 6f4d586 | 2016-08-17 09:18:01 -0700 | [diff] [blame] | 36 | import java.util.Arrays; |
| 37 | import java.util.UUID; |
| 38 | import java.util.concurrent.CompletableFuture; |
| 39 | import java.util.concurrent.CountDownLatch; |
| 40 | import java.util.concurrent.ExecutorService; |
| 41 | import java.util.concurrent.Executors; |
| 42 | import java.util.concurrent.atomic.AtomicBoolean; |
| 43 | import java.util.concurrent.atomic.AtomicLong; |
| 44 | import java.util.concurrent.atomic.AtomicReference; |
| 45 | import java.util.function.BiFunction; |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 46 | |
Jonathan Hart | 6f4d586 | 2016-08-17 09:18:01 -0700 | [diff] [blame] | 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertNotNull; |
| 49 | import static org.junit.Assert.assertNull; |
| 50 | import static org.junit.Assert.assertTrue; |
| 51 | import static org.junit.Assert.fail; |
HIGUCHI Yuta | 97f7e47 | 2016-01-22 10:03:48 -0800 | [diff] [blame] | 52 | import static org.onlab.junit.TestTools.findAvailablePort; |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Unit tests for NettyMessaging. |
| 56 | */ |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 57 | public class NettyMessagingManagerTest { |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 58 | |
Madan Jampani | 0583387 | 2016-07-12 23:01:39 -0700 | [diff] [blame] | 59 | HybridLogicalClockService testClockService = new HybridLogicalClockService() { |
| 60 | AtomicLong counter = new AtomicLong(); |
| 61 | @Override |
| 62 | public HybridLogicalTime timeNow() { |
| 63 | return new HybridLogicalTime(counter.incrementAndGet(), 0); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void recordEventTime(HybridLogicalTime time) { |
| 68 | } |
| 69 | }; |
| 70 | |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 71 | NettyMessagingManager netty1; |
| 72 | NettyMessagingManager netty2; |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 73 | |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 74 | private static final String DUMMY_NAME = "node"; |
| 75 | private static final String IP_STRING = "127.0.0.1"; |
| 76 | |
| 77 | Endpoint ep1 = new Endpoint(IpAddress.valueOf(IP_STRING), 5001); |
| 78 | Endpoint ep2 = new Endpoint(IpAddress.valueOf(IP_STRING), 5002); |
| 79 | Endpoint invalidEndPoint = new Endpoint(IpAddress.valueOf(IP_STRING), 5003); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 80 | |
| 81 | @Before |
| 82 | public void setUp() throws Exception { |
HIGUCHI Yuta | 97f7e47 | 2016-01-22 10:03:48 -0800 | [diff] [blame] | 83 | ep1 = new Endpoint(IpAddress.valueOf("127.0.0.1"), findAvailablePort(5001)); |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 84 | netty1 = new NettyMessagingManager(); |
| 85 | netty1.clusterMetadataService = dummyMetadataService(DUMMY_NAME, IP_STRING, ep1); |
Madan Jampani | 0583387 | 2016-07-12 23:01:39 -0700 | [diff] [blame] | 86 | netty1.clockService = testClockService; |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 87 | netty1.activate(); |
HIGUCHI Yuta | 97f7e47 | 2016-01-22 10:03:48 -0800 | [diff] [blame] | 88 | |
| 89 | ep2 = new Endpoint(IpAddress.valueOf("127.0.0.1"), findAvailablePort(5003)); |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 90 | netty2 = new NettyMessagingManager(); |
| 91 | netty2.clusterMetadataService = dummyMetadataService(DUMMY_NAME, IP_STRING, ep2); |
Madan Jampani | 0583387 | 2016-07-12 23:01:39 -0700 | [diff] [blame] | 92 | netty2.clockService = testClockService; |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 93 | netty2.activate(); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Returns a random String to be used as a test subject. |
| 98 | * @return string |
| 99 | */ |
| 100 | private String nextSubject() { |
| 101 | return UUID.randomUUID().toString(); |
| 102 | } |
| 103 | |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 104 | @After |
| 105 | public void tearDown() throws Exception { |
| 106 | if (netty1 != null) { |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 107 | netty1.deactivate(); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (netty2 != null) { |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 111 | netty2.deactivate(); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testSendAsync() { |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 117 | String subject = nextSubject(); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 118 | CountDownLatch latch1 = new CountDownLatch(1); |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 119 | CompletableFuture<Void> response = netty1.sendAsync(ep2, subject, "hello world".getBytes()); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 120 | response.whenComplete((r, e) -> { |
| 121 | assertNull(e); |
| 122 | latch1.countDown(); |
| 123 | }); |
| 124 | Uninterruptibles.awaitUninterruptibly(latch1); |
| 125 | |
| 126 | CountDownLatch latch2 = new CountDownLatch(1); |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 127 | response = netty1.sendAsync(invalidEndPoint, subject, "hello world".getBytes()); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 128 | response.whenComplete((r, e) -> { |
| 129 | assertNotNull(e); |
| 130 | latch2.countDown(); |
| 131 | }); |
| 132 | Uninterruptibles.awaitUninterruptibly(latch2); |
| 133 | } |
| 134 | |
| 135 | @Test |
Brian O'Connor | 6329880 | 2016-09-29 18:21:40 -0700 | [diff] [blame^] | 136 | @Ignore // FIXME disabled on 9/29/16 due to random failures |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 137 | public void testSendAndReceive() { |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 138 | String subject = nextSubject(); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 139 | AtomicBoolean handlerInvoked = new AtomicBoolean(false); |
| 140 | AtomicReference<byte[]> request = new AtomicReference<>(); |
| 141 | AtomicReference<Endpoint> sender = new AtomicReference<>(); |
| 142 | |
| 143 | BiFunction<Endpoint, byte[], byte[]> handler = (ep, data) -> { |
| 144 | handlerInvoked.set(true); |
| 145 | sender.set(ep); |
| 146 | request.set(data); |
| 147 | return "hello there".getBytes(); |
| 148 | }; |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 149 | netty2.registerHandler(subject, handler, MoreExecutors.directExecutor()); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 150 | |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 151 | CompletableFuture<byte[]> response = netty1.sendAndReceive(ep2, subject, "hello world".getBytes()); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 152 | assertTrue(Arrays.equals("hello there".getBytes(), response.join())); |
| 153 | assertTrue(handlerInvoked.get()); |
| 154 | assertTrue(Arrays.equals(request.get(), "hello world".getBytes())); |
| 155 | assertEquals(ep1, sender.get()); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Supplies executors when registering a handler and calling sendAndReceive and verifies the request handling |
| 160 | * and response completion occurs on the expected thread. |
| 161 | */ |
| 162 | @Test |
Jonathan Hart | 6f4d586 | 2016-08-17 09:18:01 -0700 | [diff] [blame] | 163 | @Ignore |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 164 | public void testSendAndReceiveWithExecutor() { |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 165 | String subject = nextSubject(); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 166 | ExecutorService completionExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "completion-thread")); |
| 167 | ExecutorService handlerExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "handler-thread")); |
| 168 | AtomicReference<String> handlerThreadName = new AtomicReference<>(); |
| 169 | AtomicReference<String> completionThreadName = new AtomicReference<>(); |
| 170 | |
HIGUCHI Yuta | f093cf2 | 2016-05-25 22:25:18 -0700 | [diff] [blame] | 171 | final CountDownLatch latch = new CountDownLatch(1); |
| 172 | |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 173 | BiFunction<Endpoint, byte[], byte[]> handler = (ep, data) -> { |
| 174 | handlerThreadName.set(Thread.currentThread().getName()); |
HIGUCHI Yuta | f093cf2 | 2016-05-25 22:25:18 -0700 | [diff] [blame] | 175 | try { |
| 176 | latch.await(); |
| 177 | } catch (InterruptedException e1) { |
| 178 | Thread.currentThread().interrupt(); |
| 179 | fail("InterruptedException"); |
| 180 | } |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 181 | return "hello there".getBytes(); |
| 182 | }; |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 183 | netty2.registerHandler(subject, handler, handlerExecutor); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 184 | |
| 185 | CompletableFuture<byte[]> response = netty1.sendAndReceive(ep2, |
Madan Jampani | 0a70f73 | 2016-07-20 11:30:59 -0700 | [diff] [blame] | 186 | subject, |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 187 | "hello world".getBytes(), |
| 188 | completionExecutor); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 189 | response.whenComplete((r, e) -> { |
| 190 | completionThreadName.set(Thread.currentThread().getName()); |
| 191 | }); |
HIGUCHI Yuta | f093cf2 | 2016-05-25 22:25:18 -0700 | [diff] [blame] | 192 | latch.countDown(); |
Madan Jampani | 3961b75 | 2016-01-13 13:30:58 -0800 | [diff] [blame] | 193 | |
| 194 | // Verify that the message was request handling and response completion happens on the correct thread. |
| 195 | assertTrue(Arrays.equals("hello there".getBytes(), response.join())); |
| 196 | assertEquals("completion-thread", completionThreadName.get()); |
| 197 | assertEquals("handler-thread", handlerThreadName.get()); |
| 198 | } |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 199 | |
| 200 | private ClusterMetadataService dummyMetadataService(String name, String ipAddress, Endpoint ep) { |
| 201 | return new ClusterMetadataService() { |
| 202 | @Override |
| 203 | public ClusterMetadata getClusterMetadata() { |
| 204 | return new ClusterMetadata(new ProviderId(DUMMY_NAME, DUMMY_NAME), |
| 205 | name, Sets.newHashSet(), Sets.newHashSet()); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public ControllerNode getLocalNode() { |
| 210 | return new ControllerNode() { |
| 211 | @Override |
| 212 | public NodeId id() { |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public IpAddress ip() { |
| 218 | return IpAddress.valueOf(ipAddress); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public int tcpPort() { |
| 223 | return ep.port(); |
| 224 | } |
| 225 | }; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void addListener(ClusterMetadataEventListener listener) {} |
| 230 | |
| 231 | @Override |
| 232 | public void removeListener(ClusterMetadataEventListener listener) {} |
| 233 | }; |
| 234 | } |
Jonathan Hart | 6f4d586 | 2016-08-17 09:18:01 -0700 | [diff] [blame] | 235 | } |