Refactor ShutdownProvider.shutdown()
[openflowplugin.git] / openflowjava / 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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.InetAddress;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer;
24 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Unit tests for UdpHandler.
30  *
31  * @author madamjak
32  */
33 @RunWith(MockitoJUnitRunner.class)
34 public class UdpHandlerTest {
35
36     private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
37
38     @Mock
39     private UdpChannelInitializer udpChannelInitializerMock;
40     private UdpHandler udpHandler;
41
42     /**
43      * Test to create UdpHandler with empty address and zero port.
44      */
45     @Test
46     public void testWithEmptyAddress() throws Exception {
47         udpHandler = new UdpHandler(null, 0, () -> { });
48         udpHandler.setChannelInitializer(udpChannelInitializerMock);
49         assertTrue("Wrong - start server", startupServer(false));
50         udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS);
51         assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
52         shutdownServer();
53     }
54
55     /**
56      * Test to create UdpHandler with empty address and zero port on Epoll native transport.
57      */
58     @Test
59     public void testWithEmptyAddressOnEpoll() throws Exception {
60         udpHandler = new UdpHandler(null, 0, () -> { });
61         udpHandler.setChannelInitializer(udpChannelInitializerMock);
62         assertTrue("Wrong - start server", startupServer(true));
63         udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS);
64         assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
65         shutdownServer();
66     }
67
68     /**
69      * Test to create UdpHandler with fill address and given port.
70      */
71     @Test
72     public void testWithAddressAndPort() throws Exception {
73         int port = 9874;
74         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
75         udpHandler.setChannelInitializer(udpChannelInitializerMock);
76         assertTrue("Wrong - start server", startupServer(false));
77         udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS);
78         assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
79         shutdownServer();
80     }
81
82     /**
83      * Test to create UdpHandler with fill address and given port on Epoll native transport.
84      */
85     @Test
86     public void testWithAddressAndPortOnEpoll() throws Exception {
87         int port = 9874;
88         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
89         udpHandler.setChannelInitializer(udpChannelInitializerMock);
90         assertTrue("Wrong - start server", startupServer(true));
91         udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS);
92         assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
93         shutdownServer();
94     }
95
96     private Boolean startupServer(final boolean isEpollEnabled)
97             throws InterruptedException, ExecutionException {
98         final var online = udpHandler.getIsOnlineFuture();
99         /**
100          * Test EPoll based native transport if isEpollEnabled is true.
101          * Else use Nio based transport.
102          */
103         udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
104         new Thread(udpHandler).start();
105
106         try {
107             online.get(10, TimeUnit.SECONDS);
108         } catch (TimeoutException e) {
109             LOG.warn("Timeout while waiting for UDP handler to start", e);
110         }
111
112         return online.isDone();
113     }
114
115     private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException {
116         final var shutdownRet = udpHandler.shutdown() ;
117         assertNull("Wrong - shutdown failed", shutdownRet.get(10, TimeUnit.SECONDS));
118     }
119 }