ff36181f6d98d8f15e1417361803be9dc2f8a719
[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 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;
22
23 import com.google.common.util.concurrent.ListenableFuture;
24
25 /**
26  * @author madamjak
27  *
28  */
29 public class UdpHandlerTest {
30     @Mock UdpChannelInitializer udpChannelInitializerMock;
31     UdpHandler udpHandler;
32     /**
33      * Mock init
34      */
35     @Before
36     public void startUp() {
37         MockitoAnnotations.initMocks(this);
38     }
39
40     /**
41      * Test to create UdpHandler with empty address and zero port
42      * @throws InterruptedException
43      * @throws ExecutionException
44      * @throws IOException
45      */
46     @Test
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));
51         try {
52             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
53         } catch (TimeoutException e) {
54             Assert.fail("Wrong - getIsOnlineFuture timed out");
55         }
56         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
57         shutdownServer();
58     }
59
60     /**
61      * Test to create UdpHandler with empty address and zero port on Epoll native transport
62      * @throws InterruptedException
63      * @throws ExecutionException
64      * @throws IOException
65      */
66     @Test
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));
71         try {
72             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
73         } catch (TimeoutException e) {
74             Assert.fail("Wrong - getIsOnlineFuture timed out");
75         }
76         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
77         shutdownServer();
78     }
79
80     /**
81      * Test to create UdpHandler with fill address and given port
82      * @throws InterruptedException
83      * @throws ExecutionException
84      * @throws IOException
85      */
86     @Test
87     public void testWithAddressAndPort() throws InterruptedException, ExecutionException, IOException{
88         int port = 9874;
89         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
90         udpHandler.setChannelInitializer(udpChannelInitializerMock);
91         Assert.assertTrue("Wrong - start server", startupServer(false));
92         try {
93             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
94         } catch (TimeoutException e) {
95             Assert.fail("Wrong - getIsOnlineFuture timed out");
96         }
97         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
98         shutdownServer();
99     }
100
101     /**
102      * Test to create UdpHandler with fill address and given port on Epoll native transport
103      * @throws InterruptedException
104      * @throws ExecutionException
105      * @throws IOException
106      */
107     @Test
108     public void testWithAddressAndPortOnEpoll() throws InterruptedException, ExecutionException, IOException{
109         int port = 9874;
110         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
111         udpHandler.setChannelInitializer(udpChannelInitializerMock);
112         Assert.assertTrue("Wrong - start server", startupServer(true));
113         try {
114             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
115         } catch (TimeoutException e) {
116             Assert.fail("Wrong - getIsOnlineFuture timed out");
117         }
118         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
119         shutdownServer();
120     }
121
122     private Boolean startupServer(boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException {
123         ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
124         /**
125          * Test EPoll based native transport if isEpollEnabled is true.
126          * Else use Nio based transport.
127          */
128         udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
129             (new Thread(udpHandler)).start();
130             int retry = 0;
131             while (online.isDone() != true && retry++ < 20) {
132                 Thread.sleep(100);
133             }
134         return online.isDone() ;
135     }
136
137     private void shutdownServer() throws InterruptedException, ExecutionException {
138         ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
139         while ( shutdownRet.isDone() != true )
140             Thread.sleep(100) ;
141         Assert.assertTrue("Wrong - shutdown failed", shutdownRet.get());
142     }
143 }