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;
27 * Unit tests for UdpHandler.
31 public class UdpHandlerTest {
33 private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
36 private UdpChannelInitializer udpChannelInitializerMock;
37 private UdpHandler udpHandler;
43 public void startUp() {
44 MockitoAnnotations.initMocks(this);
48 * Test to create UdpHandler with empty address and zero port.
51 public void testWithEmptyAddress() throws Exception {
52 udpHandler = new UdpHandler(null, 0);
53 udpHandler.setChannelInitializer(udpChannelInitializerMock);
54 Assert.assertTrue("Wrong - start server", startupServer(false));
56 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS));
57 } catch (TimeoutException e) {
58 Assert.fail("Wrong - getIsOnlineFuture timed out");
60 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
65 * Test to create UdpHandler with empty address and zero port on Epoll native transport.
68 public void testWithEmptyAddressOnEpoll() throws Exception {
69 udpHandler = new UdpHandler(null, 0);
70 udpHandler.setChannelInitializer(udpChannelInitializerMock);
71 Assert.assertTrue("Wrong - start server", startupServer(true));
73 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
74 } catch (TimeoutException e) {
75 Assert.fail("Wrong - getIsOnlineFuture timed out");
77 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
82 * Test to create UdpHandler with fill address and given port.
85 public void testWithAddressAndPort() throws Exception {
87 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
88 udpHandler.setChannelInitializer(udpChannelInitializerMock);
89 Assert.assertTrue("Wrong - start server", startupServer(false));
91 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
92 } catch (TimeoutException e) {
93 Assert.fail("Wrong - getIsOnlineFuture timed out");
95 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
100 * Test to create UdpHandler with fill address and given port on Epoll native transport.
103 public void testWithAddressAndPortOnEpoll() throws Exception {
105 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
106 udpHandler.setChannelInitializer(udpChannelInitializerMock);
107 Assert.assertTrue("Wrong - start server", startupServer(true));
109 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
110 } catch (TimeoutException e) {
111 Assert.fail("Wrong - getIsOnlineFuture timed out");
113 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
117 private Boolean startupServer(final boolean isEpollEnabled)
118 throws InterruptedException, IOException, ExecutionException {
119 ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
121 * Test EPoll based native transport if isEpollEnabled is true.
122 * Else use Nio based transport.
124 udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
125 new Thread(udpHandler).start();
128 online.get(10, TimeUnit.SECONDS);
129 } catch (TimeoutException e) {
130 LOG.warn("Timeout while waiting for UDP handler to start", e);
133 return online.isDone();
136 private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException {
137 ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
138 final Boolean shutdownSucceeded = shutdownRet.get(10, TimeUnit.SECONDS);
139 Assert.assertTrue("Wrong - shutdown failed", shutdownSucceeded);