You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by ch...@apache.org on 2021/08/25 08:57:51 UTC

[pulsar] branch branch-2.8 updated: Make MetadataCacheTest reliable. (#10877)

This is an automated email from the ASF dual-hosted git repository.

chenhang pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-2.8 by this push:
     new 9ea5180  Make MetadataCacheTest reliable. (#10877)
9ea5180 is described below

commit 9ea5180cc1f59b2eb5418f3496e30e49a9af3c6c
Author: Surinder Singh <su...@gmail.com>
AuthorDate: Wed Jun 9 16:58:06 2021 -0700

    Make MetadataCacheTest reliable. (#10877)
    
    Avoid issue due to async update by using Awaitility
    
    Co-authored-by: Surinder Singh <su...@splunk.com>
    (cherry picked from commit 4cf1008a7b5faf48e02cc3a68560ff469d700c73)
---
 pulsar-metadata/pom.xml                            |  8 ++++++-
 .../apache/pulsar/metadata/MetadataCacheTest.java  | 28 ++++++++++++----------
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/pulsar-metadata/pom.xml b/pulsar-metadata/pom.xml
index c61e41e..e1e01ff 100644
--- a/pulsar-metadata/pom.xml
+++ b/pulsar-metadata/pom.xml
@@ -72,6 +72,12 @@
       <groupId>com.github.ben-manes.caffeine</groupId>
       <artifactId>caffeine</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>org.awaitility</groupId>
+      <artifactId>awaitility</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
@@ -95,4 +101,4 @@
       </plugin>
     </plugins>
   </build>
-</project>
\ No newline at end of file
+</project>
diff --git a/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/MetadataCacheTest.java b/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/MetadataCacheTest.java
index 4d83270..2247f6b 100644
--- a/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/MetadataCacheTest.java
+++ b/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/MetadataCacheTest.java
@@ -52,6 +52,7 @@ import org.apache.pulsar.metadata.api.MetadataStoreException.ContentDeserializat
 import org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException;
 import org.apache.pulsar.metadata.api.MetadataStoreFactory;
 import org.apache.pulsar.metadata.api.Stat;
+import org.awaitility.Awaitility;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
@@ -140,25 +141,28 @@ public class MetadataCacheTest extends BaseMetadataStoreTest {
         MyClass value1 = new MyClass(testName, 1);
 
         addCache.create(key1, value1).join();
+
         // all time for changes to propagate to other caches
-        Thread.sleep(100);
-        for (MetadataCache<MyClass> cache: caches) {
-            if (cache == addCache) {
+        Awaitility.await().ignoreExceptions().untilAsserted(() -> {
+            for (MetadataCache<MyClass> cache : caches) {
+                if (cache == addCache) {
+                    assertEquals(cache.getIfCached(key1), Optional.of(value1));
+                }
+                assertEquals(cache.get(key1).join(), Optional.of(value1));
                 assertEquals(cache.getIfCached(key1), Optional.of(value1));
             }
-            assertEquals(cache.get(key1).join(), Optional.of(value1));
-            assertEquals(cache.getIfCached(key1), Optional.of(value1));
-        }
+        });
 
         delCache.delete(key1).join();
 
         // all time for changes to propagate to other caches
-        Thread.sleep(100);
-        // The entry should get removed from all caches
-        for (MetadataCache<MyClass> cache: caches) {
-            assertEquals(cache.getIfCached(key1), Optional.empty());
-            assertEquals(cache.get(key1).join(), Optional.empty());
-        }
+        Awaitility.await().ignoreExceptions().untilAsserted(() -> {
+            // The entry should get removed from all caches
+            for (MetadataCache<MyClass> cache : caches) {
+                assertEquals(cache.getIfCached(key1), Optional.empty());
+                assertEquals(cache.get(key1).join(), Optional.empty());
+            }
+        });
     }
 
     @Test(dataProvider = "impl")