You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/03/23 07:17:41 UTC

[GitHub] [pulsar] Demogorgon314 opened a new pull request #14815: [refactor][test] Move assert equals and retry to a base class

Demogorgon314 opened a new pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815


   ### Motivation
   
   Move assert equals and retry to a base class. 
   
   ### Documentation
   
   Need to update docs? 
   
   - [ ] `doc-required` 
   - [x] `no-need-doc` 
   - [ ] `doc` 
   
   
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
lhotari commented on pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#issuecomment-1077861545


   There was a similar discussion about the desire to replace retryStrategically with Awailibility in https://github.com/apache/pulsar/pull/14373#issuecomment-1055500489 . 


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] codelipenghui merged pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
codelipenghui merged pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815


   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] michaeljmarshall commented on a change in pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on a change in pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#discussion_r834556792



##########
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:
       +1 - I think we should consolidate and only use `Awaitility` for these kinds of tests.




-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari edited a comment on pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
lhotari edited a comment on pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#issuecomment-1077859022


   @Demogorgon314 A better approach would be to remove retryStrategically completely, as I did in #14518 (which didn't get merged since it was obsolete)


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on a change in pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
lhotari commented on a change in pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#discussion_r834550623



##########
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:
       Please don't expand the usage of these methods.
   Awaitability should be used instead of this approach.




-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
lhotari commented on pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#issuecomment-1077859022


   @Demogorgon314 A better approach would be to remove retryStrategically completely, as I did in #14518


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Demogorgon314 commented on pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#issuecomment-1076377273


   /pulsarbot rerun-failure-checks


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Demogorgon314 commented on pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#issuecomment-1076979115


   /pulsarbot rerun-failure-checks


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari edited a comment on pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
lhotari edited a comment on pull request #14815:
URL: https://github.com/apache/pulsar/pull/14815#issuecomment-1077861545


   There was a similar discussion about the desire to replace retryStrategically with Availability in https://github.com/apache/pulsar/pull/14373#issuecomment-1055500489 . 


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Demogorgon314 commented on a change in pull request #14815: [refactor][test] Move assert equals and retry to a base class

Posted by GitBox <gi...@apache.org>.
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org