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