Improve some tests performance

This commit is contained in:
abilan
2023-04-10 14:55:45 -04:00
parent 4f5250b470
commit e03d125fdb
11 changed files with 102 additions and 102 deletions

View File

@@ -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<String> 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<String>("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<String>("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);

View File

@@ -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">
<int:message-history/>
<bean id="ftpSessionFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="localhost"/>
<property name="port" value="22"/>
@@ -20,16 +18,17 @@
<property name="fileType" value="2"/>
</bean>
<ftp:inbound-channel-adapter id="adapterFtp"
session-factory="ftpSessionFactory"
channel="ftpIn"
auto-create-local-directory="true"
local-directory="file:target/foo"
remote-directory="foo/bar"
delete-remote-files="false">
<ftp:inbound-channel-adapter id="adapterFtp"
session-factory="ftpSessionFactory"
channel="ftpIn"
auto-create-local-directory="true"
local-directory="file:target/foo"
remote-directory="foo/bar"
delete-remote-files="false"
auto-startup="false">
<int:poller fixed-rate="1000"/>
</ftp:inbound-channel-adapter>
<int:channel id="ftpIn">
<int:queue/>
</int:channel>

View File

@@ -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();
}
}

View File

@@ -15,34 +15,31 @@
<property name="clientMode" value="2"/>
<property name="fileType" value="2"/>
</bean>
<ftp:inbound-channel-adapter id="adapterFtp"
<ftp:inbound-channel-adapter id="adapterFtp"
session-factory="ftpSessionFactory"
channel="ftpIn"
filename-pattern="foo"
local-directory="target/foo"
remote-directory="foo/bar"
auto-create-local-directory="true"
delete-remote-files="false">
delete-remote-files="false"
auto-startup="false">
<int:poller fixed-rate="1000"/>
</ftp:inbound-channel-adapter>
<ftp:inbound-channel-adapter id="adapterFtp2"
session-factory="ftpSessionFactory"
<ftp:inbound-channel-adapter id="adapterFtp2"
session-factory="ftpSessionFactory"
channel="ftpIn"
filter="filter"
local-directory="target"
remote-directory="foo/bar"
auto-create-local-directory="true"
delete-remote-files="false">
delete-remote-files="false"
auto-startup="false">
<int:poller fixed-rate="1000"/>
</ftp:inbound-channel-adapter>
<bean id="filter" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.file.filters.FileListFilter" type="java.lang.Class"/>
</bean>
<int:channel id="ftpIn">
<int:queue/>
</int:channel>

View File

@@ -19,18 +19,14 @@
<ftp:inbound-channel-adapter id="adapterFtpDontAutoCreate"
channel="ftpIn"
session-factory="ftpSessionFactory"
filter="filter"
local-directory="file:target/bar"
remote-directory="foo/bar"
auto-create-local-directory="false"
delete-remote-files="false">
delete-remote-files="false"
auto-startup="false">
<int:poller fixed-rate="1000"/>
</ftp:inbound-channel-adapter>
<bean id="filter" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.file.filters.FileListFilter" type="java.lang.Class"/>
</bean>
<int:channel id="ftpIn">
<int:queue/>
</int:channel>

View File

@@ -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();
}
}

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -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)

View File

@@ -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);
}
}