Skip to content

Commit e03d125

Browse files
committed
Improve some tests performance
1 parent 4f5250b commit e03d125

File tree

11 files changed

+102
-102
lines changed

11 files changed

+102
-102
lines changed

spring-integration-core/src/test/java/org/springframework/integration/channel/PriorityChannelTests.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
import java.util.concurrent.TimeUnit;
2424
import java.util.concurrent.atomic.AtomicBoolean;
2525

26-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2727

2828
import org.springframework.integration.support.MessageBuilder;
2929
import org.springframework.messaging.Message;
@@ -80,17 +80,19 @@ public void testDefaultComparator() {
8080
assertThat(channel.receive(0).getPayload()).isEqualTo("test:-99");
8181
}
8282

83-
// although this test has no assertions it results in ConcurrentModificationException
84-
// if executed before changes for INT-2508
8583
@Test
86-
public void testPriorityChannelWithConcurrentModification() {
84+
public void testPriorityChannelWithConcurrentModification() throws InterruptedException {
85+
ExecutorService executorService = Executors.newCachedThreadPool();
8786
final PriorityChannel channel = new PriorityChannel();
8887
final Message<String> message = new GenericMessage<>("hello");
8988
for (int i = 0; i < 1000; i++) {
9089
channel.send(message);
91-
new Thread(() -> channel.receive()).start();
92-
new Thread(() -> message.getHeaders().toString()).start();
90+
executorService.execute(channel::receive);
91+
executorService.execute(() -> message.getHeaders().toString());
9392
}
93+
94+
executorService.shutdown();
95+
assertThat(executorService.awaitTermination(10, TimeUnit.SECONDS)).isTrue();
9496
}
9597

9698
@Test
@@ -244,7 +246,7 @@ public void testTimeoutDoesNotElapse() throws InterruptedException {
244246
final AtomicBoolean sentSecondMessage = new AtomicBoolean(false);
245247
final CountDownLatch latch = new CountDownLatch(1);
246248
ExecutorService executor = Executors.newSingleThreadScheduledExecutor();
247-
channel.send(new GenericMessage<String>("test-1"));
249+
channel.send(new GenericMessage<>("test-1"));
248250
executor.execute(() -> {
249251
sentSecondMessage.set(channel.send(new GenericMessage<>("test-2"), 3000));
250252
latch.countDown();
@@ -267,7 +269,7 @@ public void testIndefiniteTimeout() throws InterruptedException {
267269
final PriorityChannel channel = new PriorityChannel(1);
268270
final AtomicBoolean sentSecondMessage = new AtomicBoolean(false);
269271
ExecutorService executor = Executors.newSingleThreadScheduledExecutor();
270-
channel.send(new GenericMessage<String>("test-1"));
272+
channel.send(new GenericMessage<>("test-1"));
271273
executor.execute(() -> sentSecondMessage.set(channel.send(new GenericMessage<>("test-2"), -1)));
272274
assertThat(sentSecondMessage.get()).isFalse();
273275
Thread.sleep(10);
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns:ftp="http://www.springframework.org/schema/integration/ftp"
55
xmlns:int="http://www.springframework.org/schema/integration"
6-
xmlns:context="http://www.springframework.org/schema/context"
76
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
87
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
9-
http://www.springframework.org/schema/integration/ftp https://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
10-
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
8+
http://www.springframework.org/schema/integration/ftp https://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd">
119

1210
<int:message-history/>
13-
11+
1412
<bean id="ftpSessionFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
1513
<property name="host" value="localhost"/>
1614
<property name="port" value="22"/>
@@ -20,16 +18,17 @@
2018
<property name="fileType" value="2"/>
2119
</bean>
2220

23-
<ftp:inbound-channel-adapter id="adapterFtp"
24-
session-factory="ftpSessionFactory"
25-
channel="ftpIn"
26-
auto-create-local-directory="true"
27-
local-directory="file:target/foo"
28-
remote-directory="foo/bar"
29-
delete-remote-files="false">
21+
<ftp:inbound-channel-adapter id="adapterFtp"
22+
session-factory="ftpSessionFactory"
23+
channel="ftpIn"
24+
auto-create-local-directory="true"
25+
local-directory="file:target/foo"
26+
remote-directory="foo/bar"
27+
delete-remote-files="false"
28+
auto-startup="false">
3029
<int:poller fixed-rate="1000"/>
3130
</ftp:inbound-channel-adapter>
32-
31+
3332
<int:channel id="ftpIn">
3433
<int:queue/>
3534
</int:channel>

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpMessageHistoryTests.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,27 +18,31 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
import org.springframework.context.support.ClassPathXmlApplicationContext;
21+
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
23+
import org.springframework.test.annotation.DirtiesContext;
24+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2325

2426
import static org.assertj.core.api.Assertions.assertThat;
2527

2628
/**
2729
* @author Oleg Zhurakousky
2830
* @author Gunnar Hillert
2931
* @author Gary Russell
32+
* @author Artem Bilan
3033
*
3134
*/
35+
@SpringJUnitConfig
36+
@DirtiesContext
3237
public class FtpMessageHistoryTests {
3338

39+
@Autowired
40+
SourcePollingChannelAdapter adapter;
41+
3442
@Test
35-
public void testMessageHistory() throws Exception {
36-
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ftp-message-history-context.xml",
37-
this.getClass());
38-
SourcePollingChannelAdapter adapter = ac.getBean("adapterFtp", SourcePollingChannelAdapter.class);
43+
public void testMessageHistory() {
3944
assertThat(adapter.getComponentName()).isEqualTo("adapterFtp");
4045
assertThat(adapter.getComponentType()).isEqualTo("ftp:inbound-channel-adapter");
41-
ac.close();
4246
}
4347

4448
}

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,31 @@
1515
<property name="clientMode" value="2"/>
1616
<property name="fileType" value="2"/>
1717
</bean>
18-
19-
<ftp:inbound-channel-adapter id="adapterFtp"
18+
19+
<ftp:inbound-channel-adapter id="adapterFtp"
2020
session-factory="ftpSessionFactory"
2121
channel="ftpIn"
2222
filename-pattern="foo"
2323
local-directory="target/foo"
2424
remote-directory="foo/bar"
2525
auto-create-local-directory="true"
26-
delete-remote-files="false">
26+
delete-remote-files="false"
27+
auto-startup="false">
2728
<int:poller fixed-rate="1000"/>
2829
</ftp:inbound-channel-adapter>
29-
30-
31-
<ftp:inbound-channel-adapter id="adapterFtp2"
32-
session-factory="ftpSessionFactory"
30+
31+
32+
<ftp:inbound-channel-adapter id="adapterFtp2"
33+
session-factory="ftpSessionFactory"
3334
channel="ftpIn"
34-
filter="filter"
3535
local-directory="target"
3636
remote-directory="foo/bar"
3737
auto-create-local-directory="true"
38-
delete-remote-files="false">
38+
delete-remote-files="false"
39+
auto-startup="false">
3940
<int:poller fixed-rate="1000"/>
4041
</ftp:inbound-channel-adapter>
41-
42-
<bean id="filter" class="org.mockito.Mockito" factory-method="mock">
43-
<constructor-arg value="org.springframework.integration.file.filters.FileListFilter" type="java.lang.Class"/>
44-
</bean>
45-
42+
4643
<int:channel id="ftpIn">
4744
<int:queue/>
4845
</int:channel>

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-fail-context.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@
1919
<ftp:inbound-channel-adapter id="adapterFtpDontAutoCreate"
2020
channel="ftpIn"
2121
session-factory="ftpSessionFactory"
22-
filter="filter"
2322
local-directory="file:target/bar"
2423
remote-directory="foo/bar"
2524
auto-create-local-directory="false"
26-
delete-remote-files="false">
25+
delete-remote-files="false"
26+
auto-startup="false">
2727
<int:poller fixed-rate="1000"/>
2828
</ftp:inbound-channel-adapter>
2929

30-
<bean id="filter" class="org.mockito.Mockito" factory-method="mock">
31-
<constructor-arg value="org.springframework.integration.file.filters.FileListFilter" type="java.lang.Class"/>
32-
</bean>
33-
3430
<int:channel id="ftpIn">
3531
<int:queue/>
3632
</int:channel>

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,12 @@
2323
import org.junit.jupiter.api.BeforeEach;
2424
import org.junit.jupiter.api.Test;
2525

26-
import org.springframework.beans.BeansException;
2726
import org.springframework.beans.factory.BeanCreationException;
2827
import org.springframework.beans.factory.BeanInitializationException;
2928
import org.springframework.context.support.ClassPathXmlApplicationContext;
3029

3130
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.fail;
31+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3332

3433
/**
3534
* @author Oleg Zhurakousky
@@ -45,32 +44,28 @@ public void prepare() {
4544
}
4645

4746
@Test
48-
public void testLocalFilesAutoCreationTrue() throws Exception {
49-
assertThat(!new File("target/foo").exists()).isTrue();
47+
public void testLocalFilesAutoCreationTrue() {
48+
assertThat(new File("target/foo").exists()).isFalse();
5049
new ClassPathXmlApplicationContext("FtpParserInboundTests-context.xml", this.getClass()).close();
5150
assertThat(new File("target/foo").exists()).isTrue();
52-
assertThat(!new File("target/bar").exists()).isTrue();
51+
assertThat(new File("target/bar").exists()).isFalse();
5352
}
5453

5554
@Test
56-
public void testLocalFilesAutoCreationFalse() throws Exception {
57-
assertThat(!new File("target/bar").exists()).isTrue();
58-
try {
59-
new ClassPathXmlApplicationContext("FtpParserInboundTests-fail-context.xml", this.getClass()).close();
60-
fail("BeansException expected.");
61-
}
62-
catch (BeansException e) {
63-
assertThat(e).isInstanceOf(BeanCreationException.class);
64-
Throwable cause = e.getCause();
65-
assertThat(cause).isInstanceOf(BeanInitializationException.class);
66-
cause = cause.getCause();
67-
assertThat(cause).isInstanceOf(FileNotFoundException.class);
68-
assertThat(cause.getMessage()).isEqualTo("bar");
69-
}
55+
public void testLocalFilesAutoCreationFalse() {
56+
assertThat(new File("target/bar").exists()).isFalse();
57+
58+
assertThatExceptionOfType(BeanCreationException.class)
59+
.isThrownBy(() ->
60+
new ClassPathXmlApplicationContext("FtpParserInboundTests-fail-context.xml", this.getClass()))
61+
.withCauseInstanceOf(BeanInitializationException.class)
62+
.withRootCauseInstanceOf(FileNotFoundException.class)
63+
.withStackTraceContaining("bar");
7064
}
7165

7266
@AfterEach
73-
public void cleanUp() throws Exception {
67+
public void cleanUp() {
7468
new File("target/foo").delete();
7569
}
70+
7671
}

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,6 @@
3636
import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer;
3737
import org.springframework.integration.ip.util.SocketTestUtils;
3838
import org.springframework.integration.ip.util.TestingUtilities;
39-
import org.springframework.integration.test.condition.LogLevels;
4039
import org.springframework.messaging.Message;
4140
import org.springframework.messaging.support.ErrorMessage;
4241

@@ -113,7 +112,7 @@ public void testFragmented() throws Exception {
113112
AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
114113
responses.add(message);
115114
try {
116-
Thread.sleep(10);
115+
Thread.sleep(1);
117116
}
118117
catch (InterruptedException e) {
119118
Thread.currentThread().interrupt();
@@ -241,7 +240,6 @@ public void removeDeadConnection(TcpConnection connection) {
241240
}
242241

243242
@Test
244-
@LogLevels(categories = "org.springframework.integration.ip", level = "DEBUG")
245243
public void testReadStxEtxOverflow() throws Exception {
246244
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
247245
serializer.setMaxMessageSize(1024);

spring-integration-jms/src/test/java/org/springframework/integration/jms/request_reply/AsyncGatewayTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 the original author or authors.
2+
* Copyright 2016-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ public void testWithTimeoutNoReplyRequired() {
110110
template.setReceiveTimeout(10000);
111111
final Message received = template.receive("asyncTest3");
112112
assertThat(received).isNotNull();
113-
org.springframework.messaging.Message<?> error = errors.receive(1000);
113+
org.springframework.messaging.Message<?> error = errors.receive(10);
114114
assertThat(error).isNull();
115115
this.gateway2.stop();
116116
}

spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/AbstractMongoDbMessageGroupStoreTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -478,9 +478,9 @@ protected void testWithAggregatorWithShutdown(String config) {
478478
.setCorrelationId(1)
479479
.build();
480480
input.send(m1);
481-
assertThat(output.receive(1000)).isNull();
481+
assertThat(output.receive(10)).isNull();
482482
input.send(m2);
483-
assertThat(output.receive(1000)).isNull();
483+
assertThat(output.receive(10)).isNull();
484484

485485
for (int i = 3; i < 10; i++) {
486486
input.send(MessageBuilder.withPayload("" + i)

0 commit comments

Comments
 (0)