Jordan Halterman | b8cace7 | 2018-07-02 17:39:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.cluster.impl; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | import java.util.concurrent.atomic.AtomicInteger; |
| 20 | |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.cluster.NodeId; |
| 23 | import org.onosproject.cluster.ProxyFactory; |
| 24 | import org.onosproject.store.serializers.KryoNamespaces; |
| 25 | import org.onosproject.store.service.Serializer; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | |
| 29 | /** |
| 30 | * Proxy manager test. |
| 31 | */ |
| 32 | public class ProxyManagerTest { |
| 33 | @Test |
| 34 | public void testProxyManager() throws Exception { |
| 35 | TestClusterCommunicationServiceFactory clusterCommunicatorFactory = |
| 36 | new TestClusterCommunicationServiceFactory(); |
| 37 | |
| 38 | NodeId a = NodeId.nodeId("a"); |
| 39 | NodeId b = NodeId.nodeId("b"); |
| 40 | |
| 41 | Serializer serializer = Serializer.using(KryoNamespaces.BASIC); |
| 42 | |
| 43 | ProxyInterfaceImpl proxyInterface1 = new ProxyInterfaceImpl(); |
| 44 | ProxyManager proxyManager1 = new ProxyManager(); |
| 45 | proxyManager1.clusterCommunicator = clusterCommunicatorFactory.newCommunicationService(a); |
| 46 | proxyManager1.activate(); |
| 47 | proxyManager1.registerProxyService(ProxyInterface.class, proxyInterface1, serializer); |
| 48 | |
| 49 | ProxyInterfaceImpl proxyInterface2 = new ProxyInterfaceImpl(); |
| 50 | ProxyManager proxyManager2 = new ProxyManager(); |
| 51 | proxyManager2.clusterCommunicator = clusterCommunicatorFactory.newCommunicationService(b); |
| 52 | proxyManager2.activate(); |
| 53 | proxyManager2.registerProxyService(ProxyInterface.class, proxyInterface2, serializer); |
| 54 | |
| 55 | ProxyFactory<ProxyInterface> proxyFactory1 = proxyManager1.getProxyFactory(ProxyInterface.class, serializer); |
| 56 | assertEquals("Hello world!", proxyFactory1.getProxyFor(b).sync("Hello world!")); |
| 57 | assertEquals(1, proxyInterface2.syncCalls.get()); |
| 58 | assertEquals("Hello world!", proxyFactory1.getProxyFor(b).async("Hello world!").join()); |
| 59 | assertEquals(1, proxyInterface2.asyncCalls.get()); |
| 60 | |
| 61 | ProxyFactory<ProxyInterface> proxyFactory2 = proxyManager2.getProxyFactory(ProxyInterface.class, serializer); |
| 62 | assertEquals("Hello world!", proxyFactory2.getProxyFor(b).sync("Hello world!")); |
| 63 | assertEquals(2, proxyInterface2.syncCalls.get()); |
| 64 | assertEquals("Hello world!", proxyFactory2.getProxyFor(b).async("Hello world!").join()); |
| 65 | assertEquals(2, proxyInterface2.asyncCalls.get()); |
| 66 | |
| 67 | proxyManager1.deactivate(); |
| 68 | proxyManager2.deactivate(); |
| 69 | } |
| 70 | |
| 71 | interface ProxyInterface { |
| 72 | String sync(String arg); |
| 73 | CompletableFuture<String> async(String arg); |
| 74 | } |
| 75 | |
| 76 | class ProxyInterfaceImpl implements ProxyInterface { |
| 77 | private final AtomicInteger syncCalls = new AtomicInteger(); |
| 78 | private final AtomicInteger asyncCalls = new AtomicInteger(); |
| 79 | |
| 80 | @Override |
| 81 | public String sync(String arg) { |
| 82 | syncCalls.incrementAndGet(); |
| 83 | return arg; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<String> async(String arg) { |
| 88 | asyncCalls.incrementAndGet(); |
| 89 | return CompletableFuture.completedFuture(arg); |
| 90 | } |
| 91 | } |
| 92 | } |