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