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.io.IOException;
12 import java.net.InetAddress;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer;
22 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
30 public class UdpHandlerTest {
32 private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
35 private UdpChannelInitializer udpChannelInitializerMock;
36 private UdpHandler udpHandler;
41 public void startUp() {
42 MockitoAnnotations.initMocks(this);
46 * Test to create UdpHandler with empty address and zero port
47 * @throws InterruptedException
48 * @throws ExecutionException
52 public void testWithEmptyAddress() throws Exception {
53 udpHandler = new UdpHandler(null, 0);
54 udpHandler.setChannelInitializer(udpChannelInitializerMock);
55 Assert.assertTrue("Wrong - start server", startupServer(false));
57 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS));
58 } catch (TimeoutException e) {
59 Assert.fail("Wrong - getIsOnlineFuture timed out");
61 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
66 * Test to create UdpHandler with empty address and zero port on Epoll native transport
67 * @throws InterruptedException
68 * @throws ExecutionException
72 public void testWithEmptyAddressOnEpoll() throws Exception {
73 udpHandler = new UdpHandler(null, 0);
74 udpHandler.setChannelInitializer(udpChannelInitializerMock);
75 Assert.assertTrue("Wrong - start server", startupServer(true));
77 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
78 } catch (TimeoutException e) {
79 Assert.fail("Wrong - getIsOnlineFuture timed out");
81 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
86 * Test to create UdpHandler with fill address and given port
87 * @throws InterruptedException
88 * @throws ExecutionException
92 public void testWithAddressAndPort() throws Exception{
94 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
95 udpHandler.setChannelInitializer(udpChannelInitializerMock);
96 Assert.assertTrue("Wrong - start server", startupServer(false));
98 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
99 } catch (TimeoutException e) {
100 Assert.fail("Wrong - getIsOnlineFuture timed out");
102 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
107 * Test to create UdpHandler with fill address and given port on Epoll native transport
108 * @throws InterruptedException
109 * @throws ExecutionException
110 * @throws IOException
113 public void testWithAddressAndPortOnEpoll() throws Exception {
115 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
116 udpHandler.setChannelInitializer(udpChannelInitializerMock);
117 Assert.assertTrue("Wrong - start server", startupServer(true));
119 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
120 } catch (TimeoutException e) {
121 Assert.fail("Wrong - getIsOnlineFuture timed out");
123 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
127 private Boolean startupServer(final boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException {
128 ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
130 * Test EPoll based native transport if isEpollEnabled is true.
131 * Else use Nio based transport.
133 udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
134 (new Thread(udpHandler)).start();
136 boolean startedSuccessfully = false;
138 startedSuccessfully = online.get(10, TimeUnit.SECONDS);
139 } catch (TimeoutException e) {
140 LOG.warn("Timeout while waiting for UDP handler to start", e);
143 return online.isDone();
146 private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException {
147 ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
148 final Boolean shutdownSucceeded = shutdownRet.get(10, TimeUnit.SECONDS);
149 Assert.assertTrue("Wrong - shutdown failed", shutdownSucceeded);