Skip to content

Commit e1339d8

Browse files
committed
Fix JdbcMessageStoreChannelTests race condition spring-projects#2
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
1 parent 74e7439 commit e1339d8

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,26 @@
2222
<int:queue ref="queue"/>
2323
</int:channel>
2424

25-
<int:channel id="output">
26-
<int:queue/>
27-
</int:channel>
28-
2925
<bean id="queue" class="org.springframework.integration.store.MessageGroupQueue">
3026
<constructor-arg ref="messageStore" />
3127
<constructor-arg value="JdbcMessageStoreChannelTests" />
3228
</bean>
3329

34-
<int:service-activator id="service-activator" input-channel="input" output-channel="output">
30+
<int:service-activator id="service-activator" input-channel="input" output-channel="nullChannel">
3531
<beans:bean class="org.springframework.integration.jdbc.store.JdbcMessageStoreChannelTests$Service" />
3632
<int:poller fixed-rate="2000">
37-
<int:transactional />
33+
<int:transactional synchronization-factory="transactionSynchronizationFactory"/>
3834
</int:poller>
3935
</int:service-activator>
4036

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>
44+
4145
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
4246
<property name="dataSource" ref="dataSource" />
4347
</bean>

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
2122
import static org.junit.Assert.fail;
2223

2324
import java.util.List;
@@ -61,7 +62,7 @@ public class JdbcMessageStoreChannelTests {
6162
private MessageChannel input;
6263

6364
@Autowired
64-
private PollableChannel output;
65+
private CountDownLatch afterCommitLatch;
6566

6667
@Autowired
6768
private JdbcMessageStore messageStore;
@@ -70,6 +71,8 @@ public class JdbcMessageStoreChannelTests {
7071
@Qualifier("service-activator")
7172
private AbstractEndpoint serviceActivator;
7273

74+
75+
7376
@Before
7477
public void init() {
7578
Service.reset(1);
@@ -89,9 +92,9 @@ public void clear() {
8992
}
9093

9194
@Test
92-
public void testSendAndActivate() {
95+
public void testSendAndActivate() throws InterruptedException {
9396
this.input.send(new GenericMessage<>("foo"));
94-
assertNotNull(output.receive(10000));
97+
assertTrue(this.afterCommitLatch.await(10, TimeUnit.SECONDS));
9598
assertEquals(1, Service.messages.size());
9699
assertEquals(0, messageStore.getMessageGroup("JdbcMessageStoreChannelTests").size());
97100
}

0 commit comments

Comments
 (0)