Skip to content

Commit b2b93bf

Browse files
committed
Repeat test in case of failure
It involves retention, which is flaky on CI.
1 parent 9bb9ccd commit b2b93bf

File tree

3 files changed

+57
-68
lines changed

3 files changed

+57
-68
lines changed

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

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
1414
package com.rabbitmq.stream.impl;
1515

16+
import static com.rabbitmq.stream.impl.TestUtils.*;
1617
import static com.rabbitmq.stream.impl.TestUtils.ResponseConditions.*;
17-
import static com.rabbitmq.stream.impl.TestUtils.b;
18-
import static com.rabbitmq.stream.impl.TestUtils.latchAssert;
19-
import static com.rabbitmq.stream.impl.TestUtils.streamName;
20-
import static com.rabbitmq.stream.impl.TestUtils.waitAtMost;
2118
import static java.util.concurrent.TimeUnit.SECONDS;
2219
import static org.assertj.core.api.Assertions.assertThat;
2320
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -925,52 +922,44 @@ void streamStatsShouldReturnErrorWhenStreamDoesNotExist() {
925922

926923
@Test
927924
@BrokerVersionAtLeast(BrokerVersion.RABBITMQ_3_11)
928-
void streamStatsFirstOffsetShouldChangeAfterRetentionKickedIn(TestInfo info) {
925+
void streamStatsFirstOffsetShouldChangeAfterRetentionKickedIn(TestInfo info) throws Exception {
929926
// this test is flaky in some CI environments, so we have to retry it
930-
int attemptCount = 0;
931-
int maxAttempts = 3;
932-
while (attemptCount <= maxAttempts) {
933-
attemptCount++;
934-
int messageCount = 1000;
935-
int payloadSize = 1000;
936-
String s = TestUtils.streamName(info);
937-
Client client = cf.get();
938-
try {
939-
assertThat(
940-
client
941-
.create(
942-
s,
943-
new Client.StreamParametersBuilder()
944-
.maxLengthBytes(messageCount * payloadSize / 10)
945-
.maxSegmentSizeBytes(messageCount * payloadSize / 20)
946-
.build())
947-
.isOk())
948-
.isTrue();
949-
950-
StreamStatsResponse response = client.streamStats(s);
951-
assertThat(response.getInfo()).containsEntry("first_chunk_id", -1L);
952-
assertThat(response.getInfo()).containsEntry("committed_chunk_id", -1L);
953-
954-
byte[] payload = new byte[payloadSize];
955-
Function<MessageBuilder, Message> messageCreation = mb -> mb.addData(payload).build();
956-
957-
TestUtils.publishAndWaitForConfirms(cf, messageCreation, messageCount, s);
958-
// publishing again, to make sure new segments trigger retention strategy
959-
TestUtils.publishAndWaitForConfirms(cf, messageCreation, messageCount, s);
960-
response = client.streamStats(s);
961-
assertThat(response.getInfo().get("first_chunk_id")).isPositive();
962-
assertThat(response.getInfo().get("committed_chunk_id")).isPositive();
963-
964-
attemptCount = Integer.MAX_VALUE;
965-
} catch (AssertionError e) {
966-
// if too many attempts, fail the test, otherwise, try again
967-
if (attemptCount > maxAttempts) {
968-
throw e;
969-
}
970-
} finally {
971-
assertThat(client.delete(s).isOk()).isTrue();
972-
}
973-
}
927+
repeatIfFailure(
928+
() -> {
929+
int messageCount = 1000;
930+
int payloadSize = 1000;
931+
String s = TestUtils.streamName(info);
932+
Client client = cf.get();
933+
try {
934+
assertThat(
935+
client
936+
.create(
937+
s,
938+
new Client.StreamParametersBuilder()
939+
.maxLengthBytes(messageCount * payloadSize / 10)
940+
.maxSegmentSizeBytes(messageCount * payloadSize / 20)
941+
.build())
942+
.isOk())
943+
.isTrue();
944+
945+
StreamStatsResponse response = client.streamStats(s);
946+
assertThat(response.getInfo()).containsEntry("first_chunk_id", -1L);
947+
assertThat(response.getInfo()).containsEntry("committed_chunk_id", -1L);
948+
949+
byte[] payload = new byte[payloadSize];
950+
Function<MessageBuilder, Message> messageCreation = mb -> mb.addData(payload).build();
951+
952+
TestUtils.publishAndWaitForConfirms(cf, messageCreation, messageCount, s);
953+
// publishing again, to make sure new segments trigger retention strategy
954+
TestUtils.publishAndWaitForConfirms(cf, messageCreation, messageCount, s);
955+
response = client.streamStats(s);
956+
assertThat(response.getInfo().get("first_chunk_id")).isPositive();
957+
assertThat(response.getInfo().get("committed_chunk_id")).isPositive();
958+
959+
} finally {
960+
assertThat(client.delete(s).isOk()).isTrue();
961+
}
962+
});
974963
}
975964

976965
@Test

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -419,23 +419,4 @@ private void publishAmqp(Connection c, int messageCount, Supplier<String> filter
419419
ch.waitForConfirmsOrDie();
420420
}
421421
}
422-
423-
private static void repeatIfFailure(RunnableWithException test) throws Exception {
424-
int executionCount = 0;
425-
Throwable lastException = null;
426-
while (executionCount < 5) {
427-
try {
428-
test.run();
429-
return;
430-
} catch (Exception | AssertionError e) {
431-
executionCount++;
432-
lastException = e;
433-
}
434-
}
435-
if (lastException instanceof Error) {
436-
throw new RuntimeException(lastException);
437-
} else {
438-
throw (Exception) lastException;
439-
}
440-
}
441422
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,4 +1066,23 @@ static void waitUntilStable(LongSupplier value) {
10661066
}
10671067
}
10681068
}
1069+
1070+
static void repeatIfFailure(RunnableWithException test) throws Exception {
1071+
int executionCount = 0;
1072+
Throwable lastException = null;
1073+
while (executionCount < 5) {
1074+
try {
1075+
test.run();
1076+
return;
1077+
} catch (Exception | AssertionError e) {
1078+
executionCount++;
1079+
lastException = e;
1080+
}
1081+
}
1082+
if (lastException instanceof Error) {
1083+
throw new RuntimeException(lastException);
1084+
} else {
1085+
throw (Exception) lastException;
1086+
}
1087+
}
10691088
}

0 commit comments

Comments
 (0)