Skip to content

Commit 0a24832

Browse files
committed
Harden test
It fails sometimes on CI.
1 parent 63e6883 commit 0a24832

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/test/java/com/rabbitmq/stream/impl/ClientTest.java

+37-15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.concurrent.atomic.AtomicLong;
5050
import java.util.concurrent.atomic.AtomicReference;
5151
import java.util.function.Function;
52+
import java.util.function.LongConsumer;
5253
import java.util.stream.Collectors;
5354
import java.util.stream.IntStream;
5455
import org.junit.jupiter.api.Test;
@@ -512,30 +513,51 @@ void publishAndConsume(boolean directBuffer) throws Exception {
512513
client.subscribe(b(1), stream, OffsetSpecification.first(), credit);
513514

514515
CountDownLatch confirmedLatch = new CountDownLatch(publishCount);
516+
Set<Long> sent = ConcurrentHashMap.newKeySet(publishCount);
517+
Client publisher =
518+
cf.get(
519+
new Client.ClientParameters()
520+
.byteBufAllocator(allocator)
521+
.publishConfirmListener(
522+
(publisherId, correlationId) -> {
523+
sent.remove(correlationId);
524+
confirmedLatch.countDown();
525+
}));
526+
publisher.declarePublisher(b(1), null, stream);
527+
LongConsumer publish =
528+
messageId -> {
529+
sent.add(messageId);
530+
publisher.publish(
531+
b(1),
532+
Collections.singletonList(
533+
publisher
534+
.messageBuilder()
535+
.addData(("message" + messageId).getBytes(StandardCharsets.UTF_8))
536+
.build()),
537+
msg -> messageId);
538+
};
515539
new Thread(
516540
() -> {
517-
Client publisher =
518-
cf.get(
519-
new Client.ClientParameters()
520-
.byteBufAllocator(allocator)
521-
.publishConfirmListener(
522-
(publisherId, correlationId) -> confirmedLatch.countDown()));
523541
int messageId = 0;
524-
publisher.declarePublisher(b(1), null, stream);
525542
while (messageId < publishCount) {
526543
messageId++;
527-
publisher.publish(
528-
b(1),
529-
Collections.singletonList(
530-
publisher
531-
.messageBuilder()
532-
.addData(("message" + messageId).getBytes(StandardCharsets.UTF_8))
533-
.build()));
544+
publish.accept(messageId);
534545
}
535546
})
536547
.start();
537548

538-
assertThat(confirmedLatch.await(15, SECONDS)).isTrue();
549+
int attempt = 0;
550+
while (attempt < 3) {
551+
boolean allConfirmed = confirmedLatch.await(15, SECONDS);
552+
if (allConfirmed) {
553+
break;
554+
} else {
555+
attempt++;
556+
for (Long messageIdNotConfirmed : sent) {
557+
publish.accept(messageIdNotConfirmed);
558+
}
559+
}
560+
}
539561
assertThat(consumedLatch.await(15, SECONDS)).isTrue();
540562
client.unsubscribe(b(1));
541563
}

0 commit comments

Comments
 (0)