From: Michal Rehak Date: Wed, 3 May 2017 07:15:02 +0000 (+0200) Subject: Increase startup and shutdown timeouts for udpHandler test X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=openflowjava.git;a=commitdiff_plain;h=a6c95ee03c6e53fc4e8b59c2a2c4d656d02a63aa Increase startup and shutdown timeouts for udpHandler test Startup timeout: 2s -> 10s Shutdown timeout: inf. -> 10s + minor cosmetic changes (IDE warnings cleanup) Change-Id: I95b604641d7dbec73ab9770640f50a71a310127f Signed-off-by: Michal Rehak --- diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/UdpHandlerTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/UdpHandlerTest.java index ff36181f..72bbe8e0 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/UdpHandlerTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/UdpHandlerTest.java @@ -7,6 +7,7 @@ */ package org.opendaylight.openflowjava.protocol.impl.core.connection; +import com.google.common.util.concurrent.ListenableFuture; import java.io.IOException; import java.net.InetAddress; import java.util.concurrent.ExecutionException; @@ -19,16 +20,20 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer; import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler; - -import com.google.common.util.concurrent.ListenableFuture; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author madamjak * */ public class UdpHandlerTest { - @Mock UdpChannelInitializer udpChannelInitializerMock; - UdpHandler udpHandler; + + private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class); + + @Mock + private UdpChannelInitializer udpChannelInitializerMock; + private UdpHandler udpHandler; /** * Mock init */ @@ -44,12 +49,12 @@ public class UdpHandlerTest { * @throws IOException */ @Test - public void testWithEmptyAddress() throws InterruptedException, ExecutionException, IOException { + public void testWithEmptyAddress() throws Exception { udpHandler = new UdpHandler(null, 0); udpHandler.setChannelInitializer(udpChannelInitializerMock); Assert.assertTrue("Wrong - start server", startupServer(false)); try { - Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue()); + Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS)); } catch (TimeoutException e) { Assert.fail("Wrong - getIsOnlineFuture timed out"); } @@ -64,12 +69,12 @@ public class UdpHandlerTest { * @throws IOException */ @Test - public void testWithEmptyAddressOnEpoll() throws InterruptedException, ExecutionException, IOException { + public void testWithEmptyAddressOnEpoll() throws Exception { udpHandler = new UdpHandler(null, 0); udpHandler.setChannelInitializer(udpChannelInitializerMock); Assert.assertTrue("Wrong - start server", startupServer(true)); try { - Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue()); + Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS)); } catch (TimeoutException e) { Assert.fail("Wrong - getIsOnlineFuture timed out"); } @@ -84,13 +89,13 @@ public class UdpHandlerTest { * @throws IOException */ @Test - public void testWithAddressAndPort() throws InterruptedException, ExecutionException, IOException{ + public void testWithAddressAndPort() throws Exception{ int port = 9874; udpHandler = new UdpHandler(InetAddress.getLocalHost(), port); udpHandler.setChannelInitializer(udpChannelInitializerMock); Assert.assertTrue("Wrong - start server", startupServer(false)); try { - Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue()); + Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS)); } catch (TimeoutException e) { Assert.fail("Wrong - getIsOnlineFuture timed out"); } @@ -105,13 +110,13 @@ public class UdpHandlerTest { * @throws IOException */ @Test - public void testWithAddressAndPortOnEpoll() throws InterruptedException, ExecutionException, IOException{ + public void testWithAddressAndPortOnEpoll() throws Exception { int port = 9874; udpHandler = new UdpHandler(InetAddress.getLocalHost(), port); udpHandler.setChannelInitializer(udpChannelInitializerMock); Assert.assertTrue("Wrong - start server", startupServer(true)); try { - Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue()); + Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS)); } catch (TimeoutException e) { Assert.fail("Wrong - getIsOnlineFuture timed out"); } @@ -119,25 +124,28 @@ public class UdpHandlerTest { shutdownServer(); } - private Boolean startupServer(boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException { + private Boolean startupServer(final boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException { ListenableFuture online = udpHandler.getIsOnlineFuture(); /** * Test EPoll based native transport if isEpollEnabled is true. * Else use Nio based transport. */ udpHandler.initiateEventLoopGroups(null, isEpollEnabled); - (new Thread(udpHandler)).start(); - int retry = 0; - while (online.isDone() != true && retry++ < 20) { - Thread.sleep(100); - } - return online.isDone() ; + (new Thread(udpHandler)).start(); + + boolean startedSuccessfully = false; + try { + startedSuccessfully = online.get(10, TimeUnit.SECONDS); + } catch (TimeoutException e) { + LOG.warn("Timeout while waiting for UDP handler to start", e); + } + + return online.isDone(); } - private void shutdownServer() throws InterruptedException, ExecutionException { + private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException { ListenableFuture shutdownRet = udpHandler.shutdown() ; - while ( shutdownRet.isDone() != true ) - Thread.sleep(100) ; - Assert.assertTrue("Wrong - shutdown failed", shutdownRet.get()); + final Boolean shutdownSucceeded = shutdownRet.get(10, TimeUnit.SECONDS); + Assert.assertTrue("Wrong - shutdown failed", shutdownSucceeded); } }