Merge "Bug 7182 related: Remove M2E lifecycle mapping"
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / UdpHandlerTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
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;
25
26 /**
27  * @author madamjak
28  *
29  */
30 public class UdpHandlerTest {
31
32     private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
33
34     @Mock
35     private UdpChannelInitializer udpChannelInitializerMock;
36     private UdpHandler udpHandler;
37     /**
38      * Mock init
39      */
40     @Before
41     public void startUp() {
42         MockitoAnnotations.initMocks(this);
43     }
44
45     /**
46      * Test to create UdpHandler with empty address and zero port
47      * @throws InterruptedException
48      * @throws ExecutionException
49      * @throws IOException
50      */
51     @Test
52     public void testWithEmptyAddress() throws Exception {
53         udpHandler = new UdpHandler(null, 0);
54         udpHandler.setChannelInitializer(udpChannelInitializerMock);
55         Assert.assertTrue("Wrong - start server", startupServer(false));
56         try {
57             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS));
58         } catch (TimeoutException e) {
59             Assert.fail("Wrong - getIsOnlineFuture timed out");
60         }
61         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
62         shutdownServer();
63     }
64
65     /**
66      * Test to create UdpHandler with empty address and zero port on Epoll native transport
67      * @throws InterruptedException
68      * @throws ExecutionException
69      * @throws IOException
70      */
71     @Test
72     public void testWithEmptyAddressOnEpoll() throws Exception {
73         udpHandler = new UdpHandler(null, 0);
74         udpHandler.setChannelInitializer(udpChannelInitializerMock);
75         Assert.assertTrue("Wrong - start server", startupServer(true));
76         try {
77             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
78         } catch (TimeoutException e) {
79             Assert.fail("Wrong - getIsOnlineFuture timed out");
80         }
81         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
82         shutdownServer();
83     }
84
85     /**
86      * Test to create UdpHandler with fill address and given port
87      * @throws InterruptedException
88      * @throws ExecutionException
89      * @throws IOException
90      */
91     @Test
92     public void testWithAddressAndPort() throws Exception{
93         int port = 9874;
94         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
95         udpHandler.setChannelInitializer(udpChannelInitializerMock);
96         Assert.assertTrue("Wrong - start server", startupServer(false));
97         try {
98             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
99         } catch (TimeoutException e) {
100             Assert.fail("Wrong - getIsOnlineFuture timed out");
101         }
102         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
103         shutdownServer();
104     }
105
106     /**
107      * Test to create UdpHandler with fill address and given port on Epoll native transport
108      * @throws InterruptedException
109      * @throws ExecutionException
110      * @throws IOException
111      */
112     @Test
113     public void testWithAddressAndPortOnEpoll() throws Exception {
114         int port = 9874;
115         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
116         udpHandler.setChannelInitializer(udpChannelInitializerMock);
117         Assert.assertTrue("Wrong - start server", startupServer(true));
118         try {
119             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
120         } catch (TimeoutException e) {
121             Assert.fail("Wrong - getIsOnlineFuture timed out");
122         }
123         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
124         shutdownServer();
125     }
126
127     private Boolean startupServer(final boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException {
128         ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
129         /**
130          * Test EPoll based native transport if isEpollEnabled is true.
131          * Else use Nio based transport.
132          */
133         udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
134         (new Thread(udpHandler)).start();
135
136         boolean startedSuccessfully = false;
137         try {
138             startedSuccessfully = online.get(10, TimeUnit.SECONDS);
139         } catch (TimeoutException e) {
140             LOG.warn("Timeout while waiting for UDP handler to start", e);
141         }
142
143         return online.isDone();
144     }
145
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);
150     }
151 }