Skip to content

Commit 7a78333

Browse files
committed
fix failed test
1 parent 1852043 commit 7a78333

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/ProducerSendWhileDeletionTest.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.apache.kafka.server.config.ServerLogConfigs;
3838
import org.apache.kafka.storage.internals.checkpoint.OffsetCheckpointFile;
3939
import org.apache.kafka.storage.internals.log.UnifiedLog;
40-
import org.apache.kafka.test.MockProducerInterceptor;
4140

4241
import java.io.File;
4342
import java.nio.charset.StandardCharsets;
@@ -49,6 +48,8 @@
4948
import java.util.Optional;
5049
import java.util.concurrent.CompletableFuture;
5150
import java.util.concurrent.CopyOnWriteArrayList;
51+
52+
import java.util.concurrent.atomic.AtomicInteger;
5253
import java.util.stream.IntStream;
5354

5455
import static org.apache.kafka.clients.producer.ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG;
@@ -165,38 +166,37 @@ public void testSendWithRecreatedTopic() throws Exception {
165166

166167
@ClusterTest
167168
public void testSendWhileTopicGetRecreated() {
168-
int maxNumRecreatTopicAttempts = 20;
169+
int maxNumRecreatTopicAttempts = 10;
169170
List<Uuid> topicIds = new CopyOnWriteArrayList<>();
170-
var f = CompletableFuture.runAsync(() -> {
171+
var recreateTopicFuture = CompletableFuture.runAsync(() -> {
171172
for (int i = 1; i <= maxNumRecreatTopicAttempts; i++) {
172173
Uuid topicId = recreateTopic();
173174
if (topicId != Uuid.ZERO_UUID) {
174175
topicIds.add(topicId);
175176
}
176177
}
177178
});
178-
Map<String, Object> configs = new HashMap<>();
179-
configs.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, MockProducerInterceptor.class.getName());
180-
configs.put(MockProducerInterceptor.APPEND_STRING_PROP, "");
181-
configs.putIfAbsent(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
182-
configs.putIfAbsent(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
183-
184-
var fs = IntStream.range(0, 3).mapToObj(ignored -> CompletableFuture.runAsync(() -> {
185-
try (var producer = cluster.producer(configs)) {
179+
180+
AtomicInteger numSuccess = new AtomicInteger(0);
181+
var producerFutures = IntStream.range(0, 2).mapToObj(producerIndex -> CompletableFuture.runAsync(() -> {
182+
try (var producer = cluster.producer()) {
186183
for (int i = 1; i <= numRecords; i++) {
187-
producer.send(new ProducerRecord<>(topic, "value"));
184+
var resp = producer.send(new ProducerRecord<>(topic, null, ("value" + i).getBytes()),
185+
(metadata, exception) -> {
186+
if (metadata != null) {
187+
numSuccess.incrementAndGet();
188+
}
189+
}).get();
190+
assertEquals(resp.topic(), topic);
188191
}
192+
} catch (Exception e) {
193+
// ignore
189194
}
190195
})).toList();
191-
f.join();
192-
fs.forEach(CompletableFuture::join);
193-
// Test will recreate topic successfully multiple times, however few recreation might fail.
194-
assertTrue(Math.abs(maxNumRecreatTopicAttempts - topicIds.size()) <= 3);
195-
assertEquals(30, MockProducerInterceptor.ON_ACKNOWLEDGEMENT_COUNT.intValue());
196-
// Producer will encounter some metadata errors during topic recreation as the topic id wouldn't be accurate
197-
assertTrue(MockProducerInterceptor.ON_ERROR_COUNT.intValue() != 0);
198-
// Producer succeed to send data with some records without crashing
199-
assertTrue(MockProducerInterceptor.ON_SUCCESS_COUNT.intValue() != 0);
196+
recreateTopicFuture.join();
197+
producerFutures.forEach(CompletableFuture::join);
198+
assertTrue(Math.abs(maxNumRecreatTopicAttempts - topicIds.size()) <= 5);
199+
assertEquals(20, numSuccess.intValue());
200200
}
201201

202202
@ClusterTest

0 commit comments

Comments
 (0)