Demogorgon314 commented on a change in pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#discussion_r834877211



##########
File path: 
pulsar-metadata/src/test/java/org/apache/pulsar/metadata/BaseMetadataStoreTest.java
##########
@@ -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;
+    }
 }

Review comment:
       If we use `Awaitility`, then we need to use [Fail-Fast 
Conditions](https://github.com/awaitility/awaitility/wiki/Usage#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: 
   ```java
   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!
   
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to