From 5aeac2ebf3f3b33d01a8f28b529773cb74d13821 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Wed, 29 Apr 2015 17:18:35 -0400 Subject: [PATCH] NIFI-557: Added sleep statements to address timing issues --- .../ClusterManagerProtocolSenderImplTest.java | 32 ++++++++++++------- .../standard/TestHandleHttpRequest.java | 20 +++++------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/protocol/impl/ClusterManagerProtocolSenderImplTest.java b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/protocol/impl/ClusterManagerProtocolSenderImplTest.java index 1a3fdb6f38..86179c8659 100644 --- a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/protocol/impl/ClusterManagerProtocolSenderImplTest.java +++ b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/protocol/impl/ClusterManagerProtocolSenderImplTest.java @@ -18,6 +18,7 @@ package org.apache.nifi.cluster.protocol.impl; import java.io.IOException; import java.net.InetAddress; + import org.apache.nifi.cluster.protocol.NodeIdentifier; import org.apache.nifi.cluster.protocol.ProtocolContext; import org.apache.nifi.cluster.protocol.ProtocolException; @@ -31,13 +32,17 @@ import org.apache.nifi.cluster.protocol.message.ProtocolMessage; import org.apache.nifi.io.socket.ServerSocketConfiguration; import org.apache.nifi.io.socket.SocketConfiguration; import org.junit.After; + import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; + import org.junit.Before; import org.junit.Test; + import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; + import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -57,22 +62,27 @@ public class ClusterManagerProtocolSenderImplTest { private ProtocolHandler mockHandler; @Before - public void setup() throws IOException { + public void setup() throws IOException, InterruptedException { address = InetAddress.getLocalHost(); - ServerSocketConfiguration serverSocketConfiguration = new ServerSocketConfiguration(); + final ServerSocketConfiguration serverSocketConfiguration = new ServerSocketConfiguration(); mockHandler = mock(ProtocolHandler.class); - ProtocolContext protocolContext = new JaxbProtocolContext(JaxbProtocolUtils.JAXB_CONTEXT); + final ProtocolContext protocolContext = new JaxbProtocolContext(JaxbProtocolUtils.JAXB_CONTEXT); listener = new SocketProtocolListener(5, 0, serverSocketConfiguration, protocolContext); listener.addHandler(mockHandler); listener.start(); + // Need to be sure that we give the listener plenty of time to startup. Otherwise, we get intermittent + // test failures because the Thread started by listener.start() isn't ready to accept connections + // before we make them. + Thread.sleep(1000L); + port = listener.getPort(); - SocketConfiguration socketConfiguration = new SocketConfiguration(); + final SocketConfiguration socketConfiguration = new SocketConfiguration(); sender = new ClusterManagerProtocolSenderImpl(socketConfiguration, protocolContext); } @@ -88,9 +98,9 @@ public class ClusterManagerProtocolSenderImplTest { when(mockHandler.canHandle(any(ProtocolMessage.class))).thenReturn(Boolean.TRUE); when(mockHandler.handle(any(ProtocolMessage.class))).thenReturn(new FlowResponseMessage()); - FlowRequestMessage request = new FlowRequestMessage(); + final FlowRequestMessage request = new FlowRequestMessage(); request.setNodeId(new NodeIdentifier("id", "api-address", 1, address.getHostAddress(), port)); - FlowResponseMessage response = sender.requestFlow(request); + final FlowResponseMessage response = sender.requestFlow(request); assertNotNull(response); } @@ -99,12 +109,12 @@ public class ClusterManagerProtocolSenderImplTest { when(mockHandler.canHandle(any(ProtocolMessage.class))).thenReturn(Boolean.TRUE); when(mockHandler.handle(any(ProtocolMessage.class))).thenReturn(new PingMessage()); - FlowRequestMessage request = new FlowRequestMessage(); + final FlowRequestMessage request = new FlowRequestMessage(); request.setNodeId(new NodeIdentifier("id", "api-address", 1, address.getHostAddress(), port)); try { sender.requestFlow(request); fail("failed to throw exception"); - } catch (ProtocolException pe) { + } catch (final ProtocolException pe) { } } @@ -118,17 +128,17 @@ public class ClusterManagerProtocolSenderImplTest { when(mockHandler.canHandle(any(ProtocolMessage.class))).thenReturn(Boolean.TRUE); when(mockHandler.handle(any(ProtocolMessage.class))).thenAnswer(new Answer() { @Override - public FlowResponseMessage answer(InvocationOnMock invocation) throws Throwable { + public FlowResponseMessage answer(final InvocationOnMock invocation) throws Throwable { Thread.sleep(time * 3); return new FlowResponseMessage(); } }); - FlowRequestMessage request = new FlowRequestMessage(); + final FlowRequestMessage request = new FlowRequestMessage(); request.setNodeId(new NodeIdentifier("id", "api-address", 1, address.getHostAddress(), port)); try { sender.requestFlow(request); fail("failed to throw exception"); - } catch (ProtocolException pe) { + } catch (final ProtocolException pe) { } } diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestHandleHttpRequest.java b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestHandleHttpRequest.java index 688b9eb1b3..235ec2ddae 100644 --- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestHandleHttpRequest.java +++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestHandleHttpRequest.java @@ -42,8 +42,8 @@ import org.junit.Test; public class TestHandleHttpRequest { - @Test - public void testRequestAddedToService() throws InitializationException, MalformedURLException, IOException { + @Test(timeout=10000) + public void testRequestAddedToService() throws InitializationException, MalformedURLException, IOException, InterruptedException { final TestRunner runner = TestRunners.newTestRunner(HandleHttpRequest.class); runner.setProperty(HandleHttpRequest.PORT, "0"); @@ -79,15 +79,11 @@ public class TestHandleHttpRequest { }); httpThread.start(); - // give processor a bit to handle the http request - try { - Thread.sleep(1000L); - } catch (final InterruptedException ie) { + while ( runner.getFlowFilesForRelationship(HandleHttpRequest.REL_SUCCESS).isEmpty() ) { + // process the request. + runner.run(1, false); } - // process the request. - runner.run(1, false); - runner.assertAllFlowFilesTransferred(HandleHttpRequest.REL_SUCCESS, 1); assertEquals(1, contextMap.size()); @@ -110,18 +106,18 @@ public class TestHandleHttpRequest { private final ConcurrentMap responseMap = new ConcurrentHashMap<>(); @Override - public boolean register(String identifier, HttpServletRequest request, HttpServletResponse response, AsyncContext context) { + public boolean register(final String identifier, final HttpServletRequest request, final HttpServletResponse response, final AsyncContext context) { responseMap.put(identifier, response); return true; } @Override - public HttpServletResponse getResponse(String identifier) { + public HttpServletResponse getResponse(final String identifier) { return responseMap.get(identifier); } @Override - public void complete(String identifier) { + public void complete(final String identifier) { responseMap.remove(identifier); }