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**
This commit is contained in:
Artem Bilan
2018-04-20 09:40:25 -04:00
parent 1f6f70b4ef
commit e4e4ee092d
2 changed files with 26 additions and 24 deletions

View File

@@ -5,45 +5,42 @@
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
<jdbc:embedded-database id="dataSource" type="DERBY" />
<jdbc:embedded-database id="dataSource" type="H2" />
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="${int.schema.script}" />
<jdbc:script location="org/springframework/integration/jdbc/schema-h2.sql" />
</jdbc:initialize-database>
<int-jdbc:message-store id="messageStore" data-source="dataSource" />
<channel id="input" xmlns="http://www.springframework.org/schema/integration">
<queue ref="queue"/>
</channel>
<int:channel id="output"/>
<int:logging-channel-adapter channel="output"/>
<int:channel id="input">
<int:queue ref="queue"/>
</int:channel>
<bean id="queue" class="org.springframework.integration.store.MessageGroupQueue">
<constructor-arg ref="messageStore" />
<constructor-arg value="JdbcMessageStoreChannelTests" />
</bean>
<service-activator id="service-activator" input-channel="input" output-channel="output" xmlns="http://www.springframework.org/schema/integration">
<int:service-activator id="service-activator" input-channel="input" output-channel="nullChannel">
<beans:bean class="org.springframework.integration.jdbc.store.JdbcMessageStoreChannelTests$Service" />
<poller fixed-rate="2000">
<transactional />
</poller>
</service-activator>
<int:poller fixed-rate="2000">
<int:transactional synchronization-factory="transactionSynchronizationFactory"/>
</int:poller>
</int:service-activator>
<context:property-placeholder location="int-${ENVIRONMENT:derby}.properties"
system-properties-mode="OVERRIDE"
ignore-unresolvable="true"
order="1"/>
<int:transaction-synchronization-factory id="transactionSynchronizationFactory">
<int:after-commit expression="@afterCommitLatch.countDown()"/>
</int:transaction-synchronization-factory>
<bean id="afterCommitLatch" class="java.util.concurrent.CountDownLatch">
<constructor-arg value="1"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.jdbc.store;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.List;
@@ -58,6 +59,9 @@ public class JdbcMessageStoreChannelTests {
@Autowired
private MessageChannel input;
@Autowired
private CountDownLatch afterCommitLatch;
@Autowired
private JdbcMessageStore messageStore;
@@ -65,6 +69,7 @@ public class JdbcMessageStoreChannelTests {
@Qualifier("service-activator")
private AbstractEndpoint serviceActivator;
@Before
public void init() {
Service.reset(1);
@@ -84,9 +89,9 @@ public class JdbcMessageStoreChannelTests {
}
@Test
public void testSendAndActivate() throws Exception {
input.send(new GenericMessage<>("foo"));
Service.await(10000);
public void testSendAndActivate() throws InterruptedException {
this.input.send(new GenericMessage<>("foo"));
assertTrue(this.afterCommitLatch.await(10, TimeUnit.SECONDS));
assertEquals(1, Service.messages.size());
assertEquals(0, messageStore.getMessageGroup("JdbcMessageStoreChannelTests").size());
}