From e03d125fdb5d206748b1cbbb1e805b220962046e Mon Sep 17 00:00:00 2001 From: abilan Date: Mon, 10 Apr 2023 14:55:45 -0400 Subject: [PATCH] Improve some tests performance --- .../channel/PriorityChannelTests.java | 20 ++++--- ...xml => FtpMessageHistoryTests-context.xml} | 23 ++++---- .../ftp/FtpMessageHistoryTests.java | 18 +++--- .../ftp/FtpParserInboundTests-context.xml | 25 ++++---- .../FtpParserInboundTests-fail-context.xml | 8 +-- .../ftp/FtpParserInboundTests.java | 37 ++++++------ .../connection/TcpNioConnectionReadTests.java | 6 +- .../jms/request_reply/AsyncGatewayTests.java | 4 +- ...AbstractMongoDbMessageGroupStoreTests.java | 6 +- ...oundChannelAdapterParserTests-context.xml} | 0 ...nsoleInboundChannelAdapterParserTests.java | 57 +++++++++++-------- 11 files changed, 102 insertions(+), 102 deletions(-) rename spring-integration-ftp/src/test/java/org/springframework/integration/ftp/{ftp-message-history-context.xml => FtpMessageHistoryTests-context.xml} (72%) rename spring-integration-stream/src/test/java/org/springframework/integration/stream/config/{consoleInboundChannelAdapterParserTests.xml => ConsoleInboundChannelAdapterParserTests-context.xml} (100%) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/PriorityChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/PriorityChannelTests.java index dc12a5f5ee..ee4eaba22a 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/PriorityChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/PriorityChannelTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; @@ -80,17 +80,19 @@ public class PriorityChannelTests { assertThat(channel.receive(0).getPayload()).isEqualTo("test:-99"); } - // although this test has no assertions it results in ConcurrentModificationException - // if executed before changes for INT-2508 @Test - public void testPriorityChannelWithConcurrentModification() { + public void testPriorityChannelWithConcurrentModification() throws InterruptedException { + ExecutorService executorService = Executors.newCachedThreadPool(); final PriorityChannel channel = new PriorityChannel(); final Message message = new GenericMessage<>("hello"); for (int i = 0; i < 1000; i++) { channel.send(message); - new Thread(() -> channel.receive()).start(); - new Thread(() -> message.getHeaders().toString()).start(); + executorService.execute(channel::receive); + executorService.execute(() -> message.getHeaders().toString()); } + + executorService.shutdown(); + assertThat(executorService.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); } @Test @@ -244,7 +246,7 @@ public class PriorityChannelTests { final AtomicBoolean sentSecondMessage = new AtomicBoolean(false); final CountDownLatch latch = new CountDownLatch(1); ExecutorService executor = Executors.newSingleThreadScheduledExecutor(); - channel.send(new GenericMessage("test-1")); + channel.send(new GenericMessage<>("test-1")); executor.execute(() -> { sentSecondMessage.set(channel.send(new GenericMessage<>("test-2"), 3000)); latch.countDown(); @@ -267,7 +269,7 @@ public class PriorityChannelTests { final PriorityChannel channel = new PriorityChannel(1); final AtomicBoolean sentSecondMessage = new AtomicBoolean(false); ExecutorService executor = Executors.newSingleThreadScheduledExecutor(); - channel.send(new GenericMessage("test-1")); + channel.send(new GenericMessage<>("test-1")); executor.execute(() -> sentSecondMessage.set(channel.send(new GenericMessage<>("test-2"), -1))); assertThat(sentSecondMessage.get()).isFalse(); Thread.sleep(10); diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/ftp-message-history-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests-context.xml similarity index 72% rename from spring-integration-ftp/src/test/java/org/springframework/integration/ftp/ftp-message-history-context.xml rename to spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests-context.xml index f59f9a1f3a..7bf0d12f1e 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/ftp-message-history-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests-context.xml @@ -3,14 +3,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ftp="http://www.springframework.org/schema/integration/ftp" xmlns:int="http://www.springframework.org/schema/integration" - xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd - http://www.springframework.org/schema/integration/ftp https://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd - http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> + http://www.springframework.org/schema/integration/ftp https://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd"> - + @@ -20,16 +18,17 @@ - + - + diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests.java index 9d01f9611c..8b0f93888f 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,10 @@ package org.springframework.integration.ftp; import org.junit.jupiter.api.Test; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.endpoint.SourcePollingChannelAdapter; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; @@ -27,18 +29,20 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Oleg Zhurakousky * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan * */ +@SpringJUnitConfig +@DirtiesContext public class FtpMessageHistoryTests { + @Autowired + SourcePollingChannelAdapter adapter; + @Test - public void testMessageHistory() throws Exception { - ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ftp-message-history-context.xml", - this.getClass()); - SourcePollingChannelAdapter adapter = ac.getBean("adapterFtp", SourcePollingChannelAdapter.class); + public void testMessageHistory() { assertThat(adapter.getComponentName()).isEqualTo("adapterFtp"); assertThat(adapter.getComponentType()).isEqualTo("ftp:inbound-channel-adapter"); - ac.close(); } } diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml index 9d1181b16c..0de5caa7bd 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml @@ -15,34 +15,31 @@ - - + delete-remote-files="false" + auto-startup="false"> - - - + delete-remote-files="false" + auto-startup="false"> - - - - - + diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-fail-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-fail-context.xml index edc1019ffe..bd827d3e6b 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-fail-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-fail-context.xml @@ -19,18 +19,14 @@ + delete-remote-files="false" + auto-startup="false"> - - - - diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests.java index 6000fcdac4..921dc626da 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,13 +23,12 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Oleg Zhurakousky @@ -45,32 +44,28 @@ public class FtpParserInboundTests { } @Test - public void testLocalFilesAutoCreationTrue() throws Exception { - assertThat(!new File("target/foo").exists()).isTrue(); + public void testLocalFilesAutoCreationTrue() { + assertThat(new File("target/foo").exists()).isFalse(); new ClassPathXmlApplicationContext("FtpParserInboundTests-context.xml", this.getClass()).close(); assertThat(new File("target/foo").exists()).isTrue(); - assertThat(!new File("target/bar").exists()).isTrue(); + assertThat(new File("target/bar").exists()).isFalse(); } @Test - public void testLocalFilesAutoCreationFalse() throws Exception { - assertThat(!new File("target/bar").exists()).isTrue(); - try { - new ClassPathXmlApplicationContext("FtpParserInboundTests-fail-context.xml", this.getClass()).close(); - fail("BeansException expected."); - } - catch (BeansException e) { - assertThat(e).isInstanceOf(BeanCreationException.class); - Throwable cause = e.getCause(); - assertThat(cause).isInstanceOf(BeanInitializationException.class); - cause = cause.getCause(); - assertThat(cause).isInstanceOf(FileNotFoundException.class); - assertThat(cause.getMessage()).isEqualTo("bar"); - } + public void testLocalFilesAutoCreationFalse() { + assertThat(new File("target/bar").exists()).isFalse(); + + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> + new ClassPathXmlApplicationContext("FtpParserInboundTests-fail-context.xml", this.getClass())) + .withCauseInstanceOf(BeanInitializationException.class) + .withRootCauseInstanceOf(FileNotFoundException.class) + .withStackTraceContaining("bar"); } @AfterEach - public void cleanUp() throws Exception { + public void cleanUp() { new File("target/foo").delete(); } + } diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java index 346af4f21c..5685d26338 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,7 +36,6 @@ import org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSe import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer; import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.ip.util.TestingUtilities; -import org.springframework.integration.test.condition.LogLevels; import org.springframework.messaging.Message; import org.springframework.messaging.support.ErrorMessage; @@ -113,7 +112,7 @@ public class TcpNioConnectionReadTests { AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> { responses.add(message); try { - Thread.sleep(10); + Thread.sleep(1); } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -241,7 +240,6 @@ public class TcpNioConnectionReadTests { } @Test - @LogLevels(categories = "org.springframework.integration.ip", level = "DEBUG") public void testReadStxEtxOverflow() throws Exception { ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer(); serializer.setMaxMessageSize(1024); diff --git a/spring-integration-jms/src/test/java/org/springframework/integration/jms/request_reply/AsyncGatewayTests.java b/spring-integration-jms/src/test/java/org/springframework/integration/jms/request_reply/AsyncGatewayTests.java index 38e4e26d31..d8493a61ef 100644 --- a/spring-integration-jms/src/test/java/org/springframework/integration/jms/request_reply/AsyncGatewayTests.java +++ b/spring-integration-jms/src/test/java/org/springframework/integration/jms/request_reply/AsyncGatewayTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 the original author or authors. + * Copyright 2016-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -110,7 +110,7 @@ public class AsyncGatewayTests extends ActiveMQMultiContextTests { template.setReceiveTimeout(10000); final Message received = template.receive("asyncTest3"); assertThat(received).isNotNull(); - org.springframework.messaging.Message error = errors.receive(1000); + org.springframework.messaging.Message error = errors.receive(10); assertThat(error).isNull(); this.gateway2.stop(); } diff --git a/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/AbstractMongoDbMessageGroupStoreTests.java b/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/AbstractMongoDbMessageGroupStoreTests.java index a0bea43a87..d8385cc178 100644 --- a/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/AbstractMongoDbMessageGroupStoreTests.java +++ b/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/AbstractMongoDbMessageGroupStoreTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -478,9 +478,9 @@ public abstract class AbstractMongoDbMessageGroupStoreTests implements MongoDbCo .setCorrelationId(1) .build(); input.send(m1); - assertThat(output.receive(1000)).isNull(); + assertThat(output.receive(10)).isNull(); input.send(m2); - assertThat(output.receive(1000)).isNull(); + assertThat(output.receive(10)).isNull(); for (int i = 3; i < 10; i++) { input.send(MessageBuilder.withPayload("" + i) diff --git a/spring-integration-stream/src/test/java/org/springframework/integration/stream/config/consoleInboundChannelAdapterParserTests.xml b/spring-integration-stream/src/test/java/org/springframework/integration/stream/config/ConsoleInboundChannelAdapterParserTests-context.xml similarity index 100% rename from spring-integration-stream/src/test/java/org/springframework/integration/stream/config/consoleInboundChannelAdapterParserTests.xml rename to spring-integration-stream/src/test/java/org/springframework/integration/stream/config/ConsoleInboundChannelAdapterParserTests-context.xml diff --git a/spring-integration-stream/src/test/java/org/springframework/integration/stream/config/ConsoleInboundChannelAdapterParserTests.java b/spring-integration-stream/src/test/java/org/springframework/integration/stream/config/ConsoleInboundChannelAdapterParserTests.java index 3c5e608033..2826dbeeba 100644 --- a/spring-integration-stream/src/test/java/org/springframework/integration/stream/config/ConsoleInboundChannelAdapterParserTests.java +++ b/spring-integration-stream/src/test/java/org/springframework/integration/stream/config/ConsoleInboundChannelAdapterParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,38 +22,56 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.core.MessageSource; import org.springframework.integration.endpoint.SourcePollingChannelAdapter; import org.springframework.integration.support.context.NamedComponent; import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.Message; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Mark Fisher * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan */ +@SpringJUnitConfig +@DirtiesContext public class ConsoleInboundChannelAdapterParserTests { - @Before - public void writeTestInput() { - ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes()); + private static final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes()); + + @Autowired + ApplicationContext context; + + @BeforeAll + public static void setTestInputStream() { System.setIn(stream); } + @BeforeEach + void resetStream() { + stream.reset(); + } + + @Test public void adapterWithDefaultCharset() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "consoleInboundChannelAdapterParserTests.xml", ConsoleInboundChannelAdapterParserTests.class); SourcePollingChannelAdapter adapter = context.getBean("adapterWithDefaultCharset.adapter", SourcePollingChannelAdapter.class); MessageSource source = (MessageSource) new DirectFieldAccessor(adapter).getPropertyValue("source"); @@ -75,13 +93,10 @@ public class ConsoleInboundChannelAdapterParserTests { adapter = context.getBean("pipedAdapterNoCharset.adapter", SourcePollingChannelAdapter.class); source = adapter.getMessageSource(); assertThat(TestUtils.getPropertyValue(source, "blockToDetectEOF", Boolean.class)).isTrue(); - context.close(); } @Test public void adapterWithProvidedCharset() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "consoleInboundChannelAdapterParserTests.xml", ConsoleInboundChannelAdapterParserTests.class); SourcePollingChannelAdapter adapter = context.getBean("adapterWithProvidedCharset.adapter", SourcePollingChannelAdapter.class); MessageSource source = adapter.getMessageSource(); @@ -93,7 +108,7 @@ public class ConsoleInboundChannelAdapterParserTests { Reader reader = (Reader) bufferedReaderAccessor.getPropertyValue("in"); assertThat(reader.getClass()).isEqualTo(InputStreamReader.class); Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding()); - assertThat(readerCharset).isEqualTo(Charset.forName("UTF-8")); + assertThat(readerCharset).isEqualTo(StandardCharsets.UTF_8); Message message = source.receive(); assertThat(message).isNotNull(); assertThat(message.getPayload()).isEqualTo("foo"); @@ -106,22 +121,16 @@ public class ConsoleInboundChannelAdapterParserTests { reader = (Reader) bufferedReaderAccessor.getPropertyValue("in"); assertThat(reader.getClass()).isEqualTo(InputStreamReader.class); readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding()); - assertThat(readerCharset).isEqualTo(Charset.forName("UTF-8")); - context.close(); + assertThat(readerCharset).isEqualTo(StandardCharsets.UTF_8); } @Test public void testConsoleSourceWithInvalidCharset() { - BeanCreationException beanCreationException = null; - try { - new ClassPathXmlApplicationContext("invalidConsoleInboundChannelAdapterParserTests.xml", - ConsoleInboundChannelAdapterParserTests.class).close(); - } - catch (BeanCreationException e) { - beanCreationException = e; - } - Throwable rootCause = beanCreationException.getRootCause(); - assertThat(rootCause.getClass()).isEqualTo(UnsupportedEncodingException.class); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> + new ClassPathXmlApplicationContext("invalidConsoleInboundChannelAdapterParserTests.xml", + ConsoleInboundChannelAdapterParserTests.class)) + .withRootCauseInstanceOf(UnsupportedEncodingException.class); } }