Skip to content

Commit c014954

Browse files
committed
Fix race in test
CorrelationData must have an id when correlating returns and confirms. With no id; there is a race to deliver the confirm and return.
1 parent 3c569fb commit c014954

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests3.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public void testRepublishOnNackThreadNoExchange() throws Exception {
6565
template.setConfirmCallback((cd, a, c) -> {
6666
confirmLatch.countDown();
6767
if (confirmLatch.getCount() == 1) {
68-
template.convertAndSend(QUEUE1, ((MyCD) cd).payload);
68+
template.convertAndSend(QUEUE1, cd.getId());
6969
}
7070
});
71-
template.convertAndSend("bad.exchange", "junk", "foo", new MyCD("foo"));
71+
template.convertAndSend("bad.exchange", "junk", "foo", new CorrelationData("foo"));
7272
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
7373
assertThat(template.receive(QUEUE1, 10_000)).isNotNull();
7474
}
@@ -102,11 +102,13 @@ public void testDeferredChannelCacheNack() throws Exception {
102102
channel2.close();
103103
conn.close();
104104
assertThat(TestUtils.getPropertyValue(cf, "cachedChannelsNonTransactional", List.class).size()).isEqualTo(2);
105-
template.convertAndSend("", QUEUE2 + "junk", "foo", new MyCD("foo"));
105+
CorrelationData correlationData = new CorrelationData("foo");
106+
template.convertAndSend("", QUEUE2 + "junk", "foo", correlationData);
106107
assertThat(returnLatch.await(10, TimeUnit.SECONDS)).isTrue();
107108
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
108109
assertThat(cacheCount.get()).isEqualTo(1);
109110
assertThat(returnCalledFirst.get()).isTrue();
111+
assertThat(correlationData.getReturnedMessage()).isNotNull();
110112
cf.destroy();
111113
}
112114

@@ -130,7 +132,7 @@ public void testDeferredChannelCacheAck() throws Exception {
130132
channel2.close();
131133
conn.close();
132134
assertThat(TestUtils.getPropertyValue(cf, "cachedChannelsNonTransactional", List.class).size()).isEqualTo(2);
133-
template.convertAndSend("", QUEUE2, "foo", new MyCD("foo"));
135+
template.convertAndSend("", QUEUE2, "foo", new CorrelationData("foo"));
134136
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
135137
assertThat(cacheCount.get()).isEqualTo(1);
136138
cf.destroy();
@@ -147,22 +149,11 @@ public void testTwoSendsAndReceivesDRTMLC() throws Exception {
147149
template.setConfirmCallback((cd, a, c) -> {
148150
confirmLatch.countDown();
149151
});
150-
template.convertSendAndReceive("", QUEUE3, "foo", new MyCD("foo"));
151-
template.convertSendAndReceive("", QUEUE3, "foo", new MyCD("foo")); // listener not registered
152+
template.convertSendAndReceive("", QUEUE3, "foo", new CorrelationData("foo"));
153+
template.convertSendAndReceive("", QUEUE3, "foo", new CorrelationData("foo")); // listener not registered
152154
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
153155
assertThat(template.receive(QUEUE3, 10_000)).isNotNull();
154156
assertThat(template.receive(QUEUE3, 10_000)).isNotNull();
155157
}
156158

157-
158-
private static class MyCD extends CorrelationData {
159-
160-
final String payload;
161-
162-
MyCD(String payload) {
163-
this.payload = payload;
164-
}
165-
166-
}
167-
168159
}

0 commit comments

Comments
 (0)