From bfebb784290cda550cc482bd2f7e3aa51741d991 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 4 Nov 2015 17:28:20 -0500 Subject: [PATCH] Adjust some sporadic tests https://build.spring.io/browse/INT-B41-JOB1-470 https://build.spring.io/browse/INT-B41-JOB1-471 --- .../configuration/EnableIntegrationTests.java | 29 +++++++ .../ip/tcp/TcpOutboundGatewayTests.java | 81 +++++++++++-------- .../integration/sftp/TestSftpServer.java | 8 +- 3 files changed, 80 insertions(+), 38 deletions(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java index 2abd995bd4..cdca9f8af3 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java @@ -43,8 +43,15 @@ import java.util.concurrent.atomic.AtomicReference; import org.aopalliance.intercept.MethodInterceptor; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; import org.hamcrest.Matchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestName; import org.junit.runner.RunWith; import org.springframework.beans.factory.FactoryBean; @@ -263,6 +270,28 @@ public class EnableIntegrationTests { @Qualifier("enableIntegrationTests.ContextConfiguration2.sendAsyncHandler.serviceActivator") private AbstractEndpoint sendAsyncHandler; + @Rule + public TestName testName = new TestName(); + + private final Log logger = LogFactory.getLog(this.getClass()); + + private final Logger loggerToAdjust = LogManager.getLogger("org.springframework.integration"); + + private Level oldCategory; + + @Before + public void beforeTest() { + this.oldCategory = loggerToAdjust.getEffectiveLevel(); + this.loggerToAdjust.setLevel(Level.TRACE); + this.logger.debug("!!!! Starting the test: " + this.testName.getMethodName() + " !!!!"); + } + + @After + public void afterTest() { + logger.debug("!!!! Finish the test: " + this.testName.getMethodName() + " !!!!"); + this.loggerToAdjust.setLevel(this.oldCategory); + } + @Test public void testAnnotatedServiceActivator() { assertEquals(10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll")); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java index eff34615a9..be0919d4c1 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java @@ -55,21 +55,21 @@ import javax.net.ServerSocketFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.Level; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.mockito.Matchers; +import org.junit.rules.TestName; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.springframework.beans.factory.BeanFactory; import org.springframework.core.serializer.DefaultDeserializer; import org.springframework.core.serializer.DefaultSerializer; import org.springframework.expression.EvaluationContext; +import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.MessageTimeoutException; import org.springframework.integration.channel.QueueChannel; -import org.springframework.integration.expression.ValueExpression; import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory; import org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory; import org.springframework.integration.ip.tcp.connection.FailoverClientConnectionFactory; @@ -91,17 +91,30 @@ import org.springframework.messaging.support.GenericMessage; public class TcpOutboundGatewayTests { @Rule - public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE, - "org.springframework.integration.ip.tcp"); + public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE, "org.springframework.integration"); + + @Rule + public TestName testName = new TestName(); private final Log logger = LogFactory.getLog(this.getClass()); + @Before + public void beforeTest() { + logger.debug("!!!! Starting the test: " + this.testName.getMethodName() + " !!!!"); + } + + @After + public void afterTest() { + logger.debug("!!!! Finish the test: " + this.testName.getMethodName() + " !!!!"); + } + @Test public void testGoodNetSingle() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean done = new AtomicBoolean(); final AtomicReference serverSocket = new AtomicReference(); Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override public void run() { try { @@ -125,6 +138,7 @@ public class TcpOutboundGatewayTests { } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", @@ -169,6 +183,7 @@ public class TcpOutboundGatewayTests { final AtomicBoolean done = new AtomicBoolean(); final AtomicReference serverSocket = new AtomicReference(); Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override public void run() { try { @@ -183,12 +198,14 @@ public class TcpOutboundGatewayTests { ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject("Reply" + (i++)); } - } catch (Exception e) { + } + catch (Exception e) { if (!done.get()) { e.printStackTrace(); } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", @@ -226,6 +243,7 @@ public class TcpOutboundGatewayTests { final AtomicBoolean done = new AtomicBoolean(); final AtomicReference serverSocket = new AtomicReference(); Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override public void run() { try { @@ -248,6 +266,7 @@ public class TcpOutboundGatewayTests { } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", @@ -267,12 +286,14 @@ public class TcpOutboundGatewayTests { Future[] results = (Future[]) new Future[2]; for (int i = 0; i < 2; i++) { final int j = i; - results[j] = (Executors.newSingleThreadExecutor().submit(new Callable(){ + results[j] = (Executors.newSingleThreadExecutor().submit(new Callable() { + @Override public Integer call() throws Exception { gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build()); return 0; } + })); } Set replies = new HashSet(); @@ -280,10 +301,12 @@ public class TcpOutboundGatewayTests { for (int i = 0; i < 2; i++) { try { results[i].get(); - } catch (ExecutionException e) { + } + catch (ExecutionException e) { if (timeouts > 0) { fail("Unexpected " + e.getMessage()); - } else { + } + else { assertNotNull(e.getCause()); assertTrue(e.getCause() instanceof MessageTimeoutException); } @@ -342,7 +365,7 @@ public class TcpOutboundGatewayTests { * @throws Exception */ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFactory ccf, - final ServerSocket server) throws InterruptedException { + final ServerSocket server) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean done = new AtomicBoolean(); /* @@ -389,6 +412,7 @@ public class TcpOutboundGatewayTests { } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); final TcpOutboundGateway gateway = new TcpOutboundGateway(); @@ -398,25 +422,10 @@ public class TcpOutboundGatewayTests { gateway.setRequiresReply(true); gateway.setOutputChannel(replyChannel); - ValueExpression remoteTimeoutExpression = Mockito.spy(new ValueExpression(500L)); + Expression remoteTimeoutExpression = Mockito.mock(Expression.class); - final AtomicBoolean remoteTimeoutUsed = new AtomicBoolean(); - - Mockito.doAnswer(new Answer() { - - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if (remoteTimeoutUsed.getAndSet(true)) { - // increase the timeout after the first send - return 10000L; - } - else { - return 500L; - } - } - - }).when(remoteTimeoutExpression) - .getValue(Mockito.any(EvaluationContext.class), Matchers.any()); + when(remoteTimeoutExpression.getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class), + Mockito.eq(Long.class))).thenReturn(500L, 10000L); gateway.setRemoteTimeoutExpression(remoteTimeoutExpression); @@ -507,6 +516,7 @@ public class TcpOutboundGatewayTests { } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); @@ -591,6 +601,7 @@ public class TcpOutboundGatewayTests { } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); @@ -704,7 +715,7 @@ public class TcpOutboundGatewayTests { } private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConnectionFactory ccf, - final ServerSocket server) throws Exception { + final ServerSocket server) throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean done = new AtomicBoolean(); final AtomicReference lastReceived = new AtomicReference(); @@ -744,9 +755,11 @@ public class TcpOutboundGatewayTests { try { socket.close(); } - catch (IOException e) {} + catch (IOException e) { + } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); final TcpOutboundGateway gateway = new TcpOutboundGateway(); @@ -832,7 +845,7 @@ public class TcpOutboundGatewayTests { } private void testGWPropagatesSocketTimeoutGuts(final int port, AbstractClientConnectionFactory ccf, - final ServerSocket server) throws Exception { + final ServerSocket server) throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean done = new AtomicBoolean(); @@ -856,9 +869,11 @@ public class TcpOutboundGatewayTests { try { socket.close(); } - catch (IOException e) {} + catch (IOException e) { + } } } + }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); final TcpOutboundGateway gateway = new TcpOutboundGateway(); diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/TestSftpServer.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/TestSftpServer.java index 7b65933fe2..8befdcdd8b 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/TestSftpServer.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/TestSftpServer.java @@ -34,10 +34,10 @@ import org.junit.rules.TemporaryFolder; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.integration.sftp.session.DefaultSftpSessionFactory; -import org.springframework.util.SocketUtils; /** * @author Gary Russell + * @author Artem Bilan * @since 4.1 * */ @@ -45,8 +45,6 @@ public class TestSftpServer implements InitializingBean, DisposableBean { private final SshServer server = SshServer.setUpDefaultServer(); - private final int port = SocketUtils.findAvailableTcpPort(); - private final TemporaryFolder sftpFolder; private final TemporaryFolder localFolder; @@ -130,7 +128,7 @@ public class TestSftpServer implements InitializingBean, DisposableBean { } }); - server.setPort(port); + server.setPort(0); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); this.server.setSubsystemFactories(Collections.>singletonList(new SftpSubsystem.Factory())); this.server.setFileSystemFactory(new VirtualFileSystemFactory(sftpRootFolder.getAbsolutePath())); @@ -175,7 +173,7 @@ public class TestSftpServer implements InitializingBean, DisposableBean { public DefaultSftpSessionFactory getSessionFactory() { DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); factory.setHost("localhost"); - factory.setPort(this.port); + factory.setPort(this.server.getPort()); factory.setUser("foo"); factory.setPassword("foo"); factory.setAllowUnknownKeys(true);