2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import java.net.InetAddress;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer;
24 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Unit tests for UdpHandler.
33 @RunWith(MockitoJUnitRunner.class)
34 public class UdpHandlerTest {
36 private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
39 private UdpChannelInitializer udpChannelInitializerMock;
40 private UdpHandler udpHandler;
43 * Test to create UdpHandler with empty address and zero port.
46 public void testWithEmptyAddress() throws Exception {
47 udpHandler = new UdpHandler(null, 0, () -> { });
48 udpHandler.setChannelInitializer(udpChannelInitializerMock);
49 assertTrue("Wrong - start server", startupServer(false));
50 udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS);
51 assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
56 * Test to create UdpHandler with empty address and zero port on Epoll native transport.
59 public void testWithEmptyAddressOnEpoll() throws Exception {
60 udpHandler = new UdpHandler(null, 0, () -> { });
61 udpHandler.setChannelInitializer(udpChannelInitializerMock);
62 assertTrue("Wrong - start server", startupServer(true));
63 udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS);
64 assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
69 * Test to create UdpHandler with fill address and given port.
72 public void testWithAddressAndPort() throws Exception {
74 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
75 udpHandler.setChannelInitializer(udpChannelInitializerMock);
76 assertTrue("Wrong - start server", startupServer(false));
77 udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS);
78 assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
83 * Test to create UdpHandler with fill address and given port on Epoll native transport.
86 public void testWithAddressAndPortOnEpoll() throws Exception {
88 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
89 udpHandler.setChannelInitializer(udpChannelInitializerMock);
90 assertTrue("Wrong - start server", startupServer(true));
91 udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS);
92 assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
96 private Boolean startupServer(final boolean isEpollEnabled)
97 throws InterruptedException, ExecutionException {
98 final var online = udpHandler.getIsOnlineFuture();
100 * Test EPoll based native transport if isEpollEnabled is true.
101 * Else use Nio based transport.
103 udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
104 new Thread(udpHandler).start();
107 online.get(10, TimeUnit.SECONDS);
108 } catch (TimeoutException e) {
109 LOG.warn("Timeout while waiting for UDP handler to start", e);
112 return online.isDone();
115 private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException {
116 final var shutdownRet = udpHandler.shutdown() ;
117 assertNull("Wrong - shutdown failed", shutdownRet.get(10, TimeUnit.SECONDS));