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 java.io.IOException;
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;
23 import com.google.common.util.concurrent.ListenableFuture;
29 public class UdpHandlerTest {
30 @Mock UdpChannelInitializer udpChannelInitializerMock;
31 UdpHandler udpHandler;
36 public void startUp() {
37 MockitoAnnotations.initMocks(this);
41 * Test to create UdpHandler with empty address and zero port
42 * @throws InterruptedException
43 * @throws ExecutionException
47 public void testWithEmptyAddress() throws InterruptedException, ExecutionException, IOException {
48 udpHandler = new UdpHandler(null, 0);
49 udpHandler.setChannelInitializer(udpChannelInitializerMock);
50 Assert.assertTrue("Wrong - start server", startupServer(false));
52 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
53 } catch (TimeoutException e) {
54 Assert.fail("Wrong - getIsOnlineFuture timed out");
56 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
61 * Test to create UdpHandler with empty address and zero port on Epoll native transport
62 * @throws InterruptedException
63 * @throws ExecutionException
67 public void testWithEmptyAddressOnEpoll() throws InterruptedException, ExecutionException, IOException {
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).booleanValue());
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
82 * @throws InterruptedException
83 * @throws ExecutionException
87 public void testWithAddressAndPort() throws InterruptedException, ExecutionException, IOException{
89 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
90 udpHandler.setChannelInitializer(udpChannelInitializerMock);
91 Assert.assertTrue("Wrong - start server", startupServer(false));
93 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
94 } catch (TimeoutException e) {
95 Assert.fail("Wrong - getIsOnlineFuture timed out");
97 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
102 * Test to create UdpHandler with fill address and given port on Epoll native transport
103 * @throws InterruptedException
104 * @throws ExecutionException
105 * @throws IOException
108 public void testWithAddressAndPortOnEpoll() throws InterruptedException, ExecutionException, IOException{
110 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
111 udpHandler.setChannelInitializer(udpChannelInitializerMock);
112 Assert.assertTrue("Wrong - start server", startupServer(true));
114 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
115 } catch (TimeoutException e) {
116 Assert.fail("Wrong - getIsOnlineFuture timed out");
118 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
122 private Boolean startupServer(boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException {
123 ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
125 * Test EPoll based native transport if isEpollEnabled is true.
126 * Else use Nio based transport.
128 udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
129 (new Thread(udpHandler)).start();
131 while (online.isDone() != true && retry++ < 20) {
134 return online.isDone() ;
137 private void shutdownServer() throws InterruptedException, ExecutionException {
138 ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
139 while ( shutdownRet.isDone() != true )
141 Assert.assertTrue("Wrong - shutdown failed", shutdownRet.get());