Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/
package org.apache.pulsar.metadata;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import io.etcd.jetcd.launcher.EtcdCluster;
import io.etcd.jetcd.launcher.EtcdClusterFactory;
import java.io.File;
import java.net.URI;
import java.util.UUID;
import java.util.concurrent.CompletionException;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.pulsar.tests.TestRetrySupport;
Expand Down Expand Up @@ -103,4 +105,35 @@ static void assertException(CompletionException e, Class<?> clazz) {
static void assertException(Throwable t, Class<?> clazz) {
assertTrue(clazz.isInstance(t), String.format("Exception %s is not of type %s", t.getClass(), clazz));
}

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry) throws Exception {
assertEqualsAndRetry(actual, expected, expectedAndRetry, 5, 100);
}

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry,
int retryCount,
long intSleepTimeInMillis) throws Exception {
assertTrue(retryStrategically((__) -> {
if (actual.get().equals(expectedAndRetry)) {
return false;
}
assertEquals(actual.get(), expected);
return true;
}, retryCount, intSleepTimeInMillis));
}

public static boolean retryStrategically(Predicate<Void> predicate, int retryCount, long intSleepTimeInMillis)
throws Exception {
for (int i = 0; i < retryCount; i++) {
if (predicate.test(null)) {
return true;
}
Thread.sleep(intSleepTimeInMillis + (intSleepTimeInMillis * i));
}
return false;
}
}
Comment on lines +108 to 139

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't expand the usage of these methods.
Awaitability should be used instead of this approach.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 - I think we should consolidate and only use Awaitility for these kinds of tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use Awaitility, then we need to use Fail-Fast Conditions feature.

But we need to upgrade the awaitility dependency to 4.1.0 or higher first. (Current Pulsar are using 4.0.3)

Example:

Awaitility.await().failFast(() -> {
    Optional<Map<String, String>> cachedValue = objCache.getIfCached(key1);

    // Need ensure objCache.getIfCached(key1) don't return a wrong value.
    // Only retry when objCache.getIfCached(key1) return Optional.empty() or Optional.of(v)
    return cachedValue.isPresent() && !Optional.of(v).equals(cachedValue);
}).untilAsserted(() -> assertEquals(objCache.getIfCached(key1), Optional.of(v)));

@lhotari @michaeljmarshall Do you have a better idea when using the current version of awaitility? Thanks!

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.pulsar.metadata;

import static org.apache.pulsar.metadata.MetadataCacheTest.assertEqualsAndRetry;
import static org.testng.Assert.assertEquals;
import java.util.EnumSet;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import lombok.Cleanup;
Expand Down Expand Up @@ -587,35 +586,4 @@ public CustomClass deserialize(String path, byte[] content, Stat stat) throws IO
assertEquals(res.getValue().b, 2);
assertEquals(res.getValue().path, key1);
}

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry) throws Exception {
assertEqualsAndRetry(actual, expected, expectedAndRetry, 5, 100);
}

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry,
int retryCount,
long intSleepTimeInMillis) throws Exception {
assertTrue(retryStrategically((__) -> {
if (actual.get().equals(expectedAndRetry)) {
return false;
}
assertEquals(actual.get(), expected);
return true;
}, retryCount, intSleepTimeInMillis));
}

public static boolean retryStrategically(Predicate<Void> predicate, int retryCount, long intSleepTimeInMillis)
throws Exception {
for (int i = 0; i < retryCount; i++) {
if (predicate.test(null)) {
return true;
}
Thread.sleep(intSleepTimeInMillis + (intSleepTimeInMillis * i));
}
return false;
}
}