Skip to content

Commit 0a7dfa9

Browse files
committed
Fix race condition in the AMQP ChannelTests
https://build.spring.io/browse/INT-AT42SIO-502 There is a small time window when we remove the current consumer from the local store, but there is no a new one yet. So, we have to check the `Set`(`Map`) size before calling its `iterator` **Cherry-pick to 4.3.x** Conflicts: spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java
1 parent f166bd1 commit 0a7dfa9

File tree

1 file changed

+15
-9
lines changed
  • spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel

1 file changed

+15
-9
lines changed

spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.Collection;
2727
import java.util.Map;
28+
import java.util.Set;
2829
import java.util.concurrent.CyclicBarrier;
2930
import java.util.concurrent.TimeUnit;
3031

@@ -110,19 +111,24 @@ public void handleMessage(Message<?> message) throws MessagingException {
110111
assertEquals(0, TestUtils.getPropertyValue(factory, "connectionListener.delegates", Collection.class).size());
111112
}
112113

114+
@SuppressWarnings("unchecked")
113115
private void waitForNewConsumer(PublishSubscribeAmqpChannel channel, BlockingQueueConsumer consumer)
114116
throws Exception {
115-
BlockingQueueConsumer newConsumer = (BlockingQueueConsumer) TestUtils
116-
.getPropertyValue(channel, "container.consumers", Map.class).keySet().iterator().next();
117+
118+
final Object consumersMonitor = TestUtils.getPropertyValue(channel, "container.consumersMonitor");
117119
int n = 0;
118-
boolean newConsumerIsConsuming = newConsumer != consumer && TestUtils.getPropertyValue(newConsumer,
119-
"consumerTags", Map.class).size() > 0;
120-
while (n++ < 100 && !newConsumerIsConsuming) {
120+
while (n++ < 100) {
121+
Set<BlockingQueueConsumer> consumers = TestUtils.getPropertyValue(channel, "container.consumers", Map.class).keySet();
122+
synchronized (consumersMonitor) {
123+
if (!consumers.isEmpty()) {
124+
BlockingQueueConsumer newConsumer = consumers.iterator().next();
125+
if (newConsumer != consumer && TestUtils.getPropertyValue(newConsumer,
126+
"consumerTags", Map.class).size() > 0) {
127+
break;
128+
}
129+
}
130+
}
121131
Thread.sleep(100);
122-
newConsumer = (BlockingQueueConsumer) TestUtils
123-
.getPropertyValue(channel, "container.consumers", Map.class).keySet().iterator().next();
124-
newConsumerIsConsuming = newConsumer != consumer && TestUtils.getPropertyValue(newConsumer,
125-
"consumerTags", Map.class).size() > 0;
126132
}
127133
assertTrue("Failed to restart consumer", n < 100);
128134
}

0 commit comments

Comments
 (0)