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 com.google.common.util.concurrent.ListenableFuture;
11 import java.net.InetAddress;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer;
21 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Unit tests for UdpHandler.
30 public class UdpHandlerTest {
32 private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
35 private UdpChannelInitializer udpChannelInitializerMock;
36 private UdpHandler udpHandler;
42 public void startUp() {
43 MockitoAnnotations.initMocks(this);
47 * Test to create UdpHandler with empty address and zero port.
50 public void testWithEmptyAddress() throws Exception {
51 udpHandler = new UdpHandler(null, 0, () -> { });
52 udpHandler.setChannelInitializer(udpChannelInitializerMock);
53 Assert.assertTrue("Wrong - start server", startupServer(false));
55 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS));
56 } catch (TimeoutException e) {
57 Assert.fail("Wrong - getIsOnlineFuture timed out");
59 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
64 * Test to create UdpHandler with empty address and zero port on Epoll native transport.
67 public void testWithEmptyAddressOnEpoll() throws Exception {
68 udpHandler = new UdpHandler(null, 0, () -> { });
69 udpHandler.setChannelInitializer(udpChannelInitializerMock);
70 Assert.assertTrue("Wrong - start server", startupServer(true));
72 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
73 } catch (TimeoutException e) {
74 Assert.fail("Wrong - getIsOnlineFuture timed out");
76 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
81 * Test to create UdpHandler with fill address and given port.
84 public void testWithAddressAndPort() throws Exception {
86 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
87 udpHandler.setChannelInitializer(udpChannelInitializerMock);
88 Assert.assertTrue("Wrong - start server", startupServer(false));
90 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
91 } catch (TimeoutException e) {
92 Assert.fail("Wrong - getIsOnlineFuture timed out");
94 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
99 * Test to create UdpHandler with fill address and given port on Epoll native transport.
102 public void testWithAddressAndPortOnEpoll() throws Exception {
104 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
105 udpHandler.setChannelInitializer(udpChannelInitializerMock);
106 Assert.assertTrue("Wrong - start server", startupServer(true));
108 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
109 } catch (TimeoutException e) {
110 Assert.fail("Wrong - getIsOnlineFuture timed out");
112 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
116 private Boolean startupServer(final boolean isEpollEnabled)
117 throws InterruptedException, ExecutionException {
118 ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
120 * Test EPoll based native transport if isEpollEnabled is true.
121 * Else use Nio based transport.
123 udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
124 new Thread(udpHandler).start();
127 online.get(10, TimeUnit.SECONDS);
128 } catch (TimeoutException e) {
129 LOG.warn("Timeout while waiting for UDP handler to start", e);
132 return online.isDone();
135 private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException {
136 ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
137 final Boolean shutdownSucceeded = shutdownRet.get(10, TimeUnit.SECONDS);
138 Assert.assertTrue("Wrong - shutdown failed", shutdownSucceeded);