Skip to content

Commit aca3212

Browse files
committed
Fix JdbcMessageStoreChannelTests race condition
The message in the `QueueChannel` appears for consuming a bit earlier than TX is committed * Introduce `afterCommitLatch` into the test verify the state when TX is really committed and data is removed from DB **Cherry-pick to 5.0.x and 4.3.x** (cherry picked from commit e4e4ee0)
1 parent 87a8008 commit aca3212

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests-context.xml

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,42 @@
55
xmlns:int="http://www.springframework.org/schema/integration"
66
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
77
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
8-
xmlns:context="http://www.springframework.org/schema/context"
98
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
109
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
1110
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
12-
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
13-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
11+
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
1412

15-
<jdbc:embedded-database id="dataSource" type="DERBY" />
13+
<jdbc:embedded-database id="dataSource" type="H2" />
1614

1715
<jdbc:initialize-database data-source="dataSource">
18-
<jdbc:script location="${int.schema.script}" />
16+
<jdbc:script location="org/springframework/integration/jdbc/schema-h2.sql" />
1917
</jdbc:initialize-database>
2018

2119
<int-jdbc:message-store id="messageStore" data-source="dataSource" />
2220

23-
<channel id="input" xmlns="http://www.springframework.org/schema/integration">
24-
<queue ref="queue"/>
25-
</channel>
26-
27-
<int:channel id="output"/>
28-
29-
<int:logging-channel-adapter channel="output"/>
21+
<int:channel id="input">
22+
<int:queue ref="queue"/>
23+
</int:channel>
3024

3125
<bean id="queue" class="org.springframework.integration.store.MessageGroupQueue">
3226
<constructor-arg ref="messageStore" />
3327
<constructor-arg value="JdbcMessageStoreChannelTests" />
3428
</bean>
3529

36-
<service-activator id="service-activator" input-channel="input" output-channel="output" xmlns="http://www.springframework.org/schema/integration">
30+
<int:service-activator id="service-activator" input-channel="input" output-channel="nullChannel">
3731
<beans:bean class="org.springframework.integration.jdbc.store.JdbcMessageStoreChannelTests$Service" />
38-
<poller fixed-rate="2000">
39-
<transactional />
40-
</poller>
41-
</service-activator>
42-
43-
<context:property-placeholder location="int-${ENVIRONMENT:derby}.properties"
44-
system-properties-mode="OVERRIDE"
45-
ignore-unresolvable="true"
46-
order="1"/>
32+
<int:poller fixed-rate="2000">
33+
<int:transactional synchronization-factory="transactionSynchronizationFactory"/>
34+
</int:poller>
35+
</int:service-activator>
36+
37+
<int:transaction-synchronization-factory id="transactionSynchronizationFactory">
38+
<int:after-commit expression="@afterCommitLatch.countDown()"/>
39+
</int:transaction-synchronization-factory>
40+
41+
<bean id="afterCommitLatch" class="java.util.concurrent.CountDownLatch">
42+
<constructor-arg value="1"/>
43+
</bean>
4744

4845
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
4946
<property name="dataSource" ref="dataSource" />

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.jdbc.store;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
2021
import static org.junit.Assert.fail;
2122

2223
import java.util.List;
@@ -58,13 +59,17 @@ public class JdbcMessageStoreChannelTests {
5859
@Autowired
5960
private MessageChannel input;
6061

62+
@Autowired
63+
private CountDownLatch afterCommitLatch;
64+
6165
@Autowired
6266
private JdbcMessageStore messageStore;
6367

6468
@Autowired
6569
@Qualifier("service-activator")
6670
private AbstractEndpoint serviceActivator;
6771

72+
6873
@Before
6974
public void init() {
7075
Service.reset(1);
@@ -84,9 +89,9 @@ public void clear() {
8489
}
8590

8691
@Test
87-
public void testSendAndActivate() throws Exception {
88-
input.send(new GenericMessage<>("foo"));
89-
Service.await(10000);
92+
public void testSendAndActivate() throws InterruptedException {
93+
this.input.send(new GenericMessage<>("foo"));
94+
assertTrue(this.afterCommitLatch.await(10, TimeUnit.SECONDS));
9095
assertEquals(1, Service.messages.size());
9196
assertEquals(0, messageStore.getMessageGroup("JdbcMessageStoreChannelTests").size());
9297
}

0 commit comments

Comments
 (0)