Skip to content

Commit 4322538

Browse files
committed
Fix a race condition problem at TransactionalContainerTests.testBatchListenerRecoverAfterRollbackProcessorCrash.
1 parent 3449f8c commit 4322538

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

spring-kafka/src/test/java/org/springframework/kafka/listener/TransactionalContainerTests.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
@EmbeddedKafka(topics = { TransactionalContainerTests.topic1, TransactionalContainerTests.topic2,
111111
TransactionalContainerTests.topic3, TransactionalContainerTests.topic3DLT, TransactionalContainerTests.topic4,
112112
TransactionalContainerTests.topic5, TransactionalContainerTests.topic6, TransactionalContainerTests.topic7,
113-
TransactionalContainerTests.topic8, TransactionalContainerTests.topic8DLT },
113+
TransactionalContainerTests.topic8, TransactionalContainerTests.topic8DLT, TransactionalContainerTests.topic9},
114114
brokerProperties = { "transaction.state.log.replication.factor=1", "transaction.state.log.min.isr=1" })
115115
public class TransactionalContainerTests {
116116

@@ -936,7 +936,8 @@ public void testRollbackProcessorCrash() throws Exception {
936936

937937
@Test
938938
public void testBatchListenerRecoverAfterRollbackProcessorCrash() throws Exception {
939-
Map<String, Object> props = KafkaTestUtils.consumerProps("testBatchListenerRollbackNoRetries", "false", embeddedKafka);
939+
String group = "testBatchListenerRollbackNoRetries";
940+
Map<String, Object> props = KafkaTestUtils.consumerProps(group, "false", embeddedKafka);
940941
props.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, "read_committed");
941942
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 2);
942943
DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props);
@@ -949,23 +950,23 @@ public void testBatchListenerRecoverAfterRollbackProcessorCrash() throws Excepti
949950
pf.setTransactionIdPrefix("batchListener.noRetries.");
950951
final KafkaTemplate<Object, Object> template = new KafkaTemplate<>(pf);
951952
final CountDownLatch latch = new CountDownLatch(1);
952-
AtomicReference<String> data = new AtomicReference<>();
953+
AtomicReference<List<ConsumerRecord<Integer, String>>> data = new AtomicReference<>();
953954
containerProps.setMessageListener((BatchMessageListener<Integer, String>) recordList -> {
954955
for (ConsumerRecord<Integer, String> record : recordList) {
955-
data.set(record.value());
956956
if (record.offset() == 0) {
957957
throw new BatchListenerFailedException("fail for no retry", record);
958958
}
959-
latch.countDown();
960959
}
960+
data.set(recordList);
961+
latch.countDown();
961962
});
962963

963964
KafkaTransactionManager<Object, Object> tm = new KafkaTransactionManager<>(pf);
964965
containerProps.setKafkaAwareTransactionManager(tm);
965966
KafkaMessageListenerContainer<Integer, String> container =
966967
new KafkaMessageListenerContainer<>(cf, containerProps);
967968
container.setBeanName("testBatchListenerRollbackNoRetries");
968-
final KafkaOperations<Object, Object> dlTemplate = spy(new KafkaTemplate<>(pf));
969+
final KafkaOperations<Object, Object> dlTemplate = new KafkaTemplate<>(pf);
969970
AtomicBoolean recovererShouldFail = new AtomicBoolean(true);
970971
DeadLetterPublishingRecoverer recoverer = new DeadLetterPublishingRecoverer(dlTemplate) {
971972
@Override
@@ -977,7 +978,7 @@ public void accept(ConsumerRecord<?, ?> record, Consumer<?, ?> consumer, Excepti
977978

978979
};
979980
DefaultAfterRollbackProcessor<Object, Object> afterRollbackProcessor =
980-
spy(new DefaultAfterRollbackProcessor<>(recoverer, new FixedBackOff(0L, 0L), dlTemplate, true));
981+
new DefaultAfterRollbackProcessor<>(recoverer, new FixedBackOff(0L, 0L), dlTemplate, true);
981982
container.setAfterRollbackProcessor(afterRollbackProcessor);
982983
final CountDownLatch stopLatch = new CountDownLatch(1);
983984
container.setApplicationEventPublisher(e -> {
@@ -997,8 +998,16 @@ public void accept(ConsumerRecord<?, ?> record, Consumer<?, ?> consumer, Excepti
997998
template.sendDefault(0, 0, "qux");
998999
return null;
9991000
});
1001+
10001002
assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue();
1001-
assertThat(data.get()).isEqualTo("qux");
1003+
assertThat(data.get()).isNotNull();
1004+
ConsumerRecord<Integer, String> crBaz = data.get().get(0);
1005+
ConsumerRecord<Integer, String> crQux = data.get().get(1);
1006+
assertThat(crBaz.offset()).isEqualTo(2L);
1007+
assertThat(crBaz.value()).isEqualTo("baz");
1008+
assertThat(crQux.offset()).isEqualTo(3L);
1009+
assertThat(crQux.value()).isEqualTo("qux");
1010+
10021011
container.stop();
10031012
pf.destroy();
10041013
assertThat(stopLatch.await(10, TimeUnit.SECONDS)).isTrue();

0 commit comments

Comments
 (0)