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