Skip to content

Commit c4bbc3b

Browse files
artembilangaryrussell
authored andcommitted
Fix Race Condition in TxSyncQueueChannelTests
https://build.spring.io/browse/INT-MASTER-653 The test-case uses only 1 second to wait for the message in the `QueueChannel`. That isn't enough on slow environment like CI causing failure for the current test and unexpected value in the queue for the subsequent tests * Fix timeouts * Get rid of `CountDownLatch` - the same is done by the wait on queues * Purge queue in between tests. This way we may not have failed subsequent test, thus failure analyze will be much easier * Rework `testRollback()` to wait for good result after one retry over rollback * Remove duplicate bean definition from XML config **Cherry-pick to 4.3.x and 4.2.x** Conflicts: spring-integration-core/src/test/java/org/springframework/integration/channel/TransactionSynchronizationQueueChannelTests.java Resolved.
1 parent 0a7dfa9 commit c4bbc3b

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

spring-integration-core/src/test/java/org/springframework/integration/channel/TransactionSynchronizationQueueChannelTests-context.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
77

8+
<bean id="transactionManager" class="org.springframework.integration.transaction.PseudoTransactionManager" />
9+
10+
<bean id="service"
11+
class="org.springframework.integration.channel.TransactionSynchronizationQueueChannelTests$Service"/>
12+
813
<int:channel id="queueChannel">
914
<int:queue />
1015
</int:channel>
@@ -15,7 +20,6 @@
1520
</int:poller>
1621
</int:service-activator>
1722

18-
<bean id="service" class="org.springframework.integration.channel.TransactionSynchronizationQueueChannelTests$Service"/>
1923

2024
<int:transaction-synchronization-factory id="txSyncFactory">
2125
<int:after-commit channel="good" />
@@ -26,8 +30,6 @@
2630
<int:queue />
2731
</int:channel>
2832

29-
<bean id="transactionManager" class="org.springframework.integration.transaction.PseudoTransactionManager" />
30-
3133
<int:channel id="queueChannel2">
3234
<int:queue />
3335
</int:channel>
@@ -43,8 +45,4 @@
4345
channel="good" />
4446
</int:transaction-synchronization-factory>
4547

46-
<int:channel id="good">
47-
<int:queue />
48-
</int:channel>
49-
5048
</beans>

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@
1717

1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertNull;
2120
import static org.junit.Assert.assertSame;
22-
import static org.junit.Assert.assertTrue;
23-
24-
import java.util.concurrent.CountDownLatch;
25-
import java.util.concurrent.TimeUnit;
2621

22+
import org.junit.Before;
2723
import org.junit.Test;
2824
import org.junit.runner.RunWith;
25+
2926
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.integration.support.MessageBuilder;
3028
import org.springframework.messaging.Message;
31-
import org.springframework.messaging.PollableChannel;
3229
import org.springframework.messaging.support.GenericMessage;
33-
import org.springframework.integration.support.MessageBuilder;
30+
import org.springframework.test.annotation.DirtiesContext;
3431
import org.springframework.test.context.ContextConfiguration;
3532
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3633

@@ -41,66 +38,63 @@
4138
*/
4239
@ContextConfiguration
4340
@RunWith(SpringJUnit4ClassRunner.class)
41+
@DirtiesContext
4442
public class TransactionSynchronizationQueueChannelTests {
4543

4644
@Autowired
47-
private PollableChannel queueChannel;
45+
private QueueChannel queueChannel;
4846

4947
@Autowired
50-
private PollableChannel good;
48+
private QueueChannel good;
5149

5250
@Autowired
53-
private Service service;
51+
private QueueChannel queueChannel2;
5452

55-
@Autowired
56-
private PollableChannel queueChannel2;
53+
@Before
54+
public void setup() {
55+
this.good.purge(null);
56+
this.queueChannel.purge(null);
57+
this.queueChannel2.purge(null);
58+
}
5759

5860
@Test
5961
public void testCommit() throws Exception {
60-
service.latch = new CountDownLatch(1);
61-
GenericMessage<String> sentMessage = new GenericMessage<String>("hello");
62-
queueChannel.send(sentMessage);
63-
assertTrue(service.latch.await(10, TimeUnit.SECONDS));
64-
Message<?> message = good.receive(1000);
62+
GenericMessage<String> sentMessage = new GenericMessage<>("hello");
63+
this.queueChannel.send(sentMessage);
64+
Message<?> message = this.good.receive(10000);
6565
assertNotNull(message);
6666
assertEquals("hello", message.getPayload());
6767
assertSame(message, sentMessage);
6868
}
6969

7070
@Test
7171
public void testRollback() throws Exception {
72-
service.latch = new CountDownLatch(1);
73-
queueChannel.send(new GenericMessage<String>("fail"));
74-
assertTrue(service.latch.await(10, TimeUnit.SECONDS));
75-
Message<?> message = queueChannel.receive(1000);
72+
this.queueChannel.send(new GenericMessage<>("fail"));
73+
Message<?> message = this.good.receive(10000);
7674
assertNotNull(message);
7775
assertEquals("retry:fail", message.getPayload());
78-
assertNull(good.receive(0));
7976
}
8077

8178
@Test
8279
public void testIncludeChannelName() throws Exception {
83-
service.latch = new CountDownLatch(1);
8480
Message<String> sentMessage = MessageBuilder.withPayload("hello")
8581
.setHeader("foo", "bar").build();
8682
queueChannel2.send(sentMessage);
87-
assertTrue(service.latch.await(10, TimeUnit.SECONDS));
88-
Message<?> message = good.receive(1000);
83+
Message<?> message = good.receive(10000);
8984
assertNotNull(message);
9085
assertEquals("hello processed ok from queueChannel2", message.getPayload());
9186
assertNotNull(message.getHeaders().get("foo"));
9287
assertEquals("bar", message.getHeaders().get("foo"));
93-
}
88+
}
9489

9590
public static class Service {
96-
private CountDownLatch latch;
9791

9892
public void handle(String foo) {
99-
latch.countDown();
10093
if (foo.startsWith("fail")) {
10194
throw new RuntimeException("planned failure");
10295
}
10396
}
97+
10498
}
10599

106100
}

0 commit comments

Comments
 (0)