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

suxiaogang223 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 0b83024dde3 [test](fe) Synchronize cache refresh assertions with 
completion (#71272)
0b83024dde3 is described below

commit 0b83024dde3f1c5224a750611d72f72803eb470c
Author: Socrates <[email protected]>
AuthorDate: Thu Dec 3 14:55:41 2026 +0800

    [test](fe) Synchronize cache refresh assertions with completion (#71272)
    
    ### Background
    
    Two FE unit tests assert the outcome of asynchronous refresh work.
    Their assertions previously ran after fixed sleeps.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: None
    PR Type: Test
    
    Problem Summary:
    
    Make refresh tests independent of timing on busy runners while retaining
    coverage of scheduled catalog refresh and async cache refresh semantics.
    
    ### Details
    
    - **Failure:** RefreshCatalogTest sees four databases before the next
      refresh, and CacheFactoryTest sees only the initial load.
    - **Cause:** A fixed delay does not establish that the scheduled or
      asynchronous work completed.
    - **Stability evidence:** Wait for the catalog to expose db3, and
    control
      async load completion using futures. Distinct values verify that reads
    retain the old entry during refresh and publish the new entry after it.
    
    ### Testing
    
    - git diff --check passed.
    - On Fedora, the Doris FE UT runner passed all eight tests in
      CacheFactoryTest and RefreshCatalogTest.
    
    ### Release note
    
    None
    
    ### Check List (For Author)
    
    - Behavior changed: No
    - Does this need documentation: No
    - Pre-submission status: Ready
    
    ### AI Disclosure
    
    - Tool: OpenAI Codex (GPT-6)
    - Generated: test changes and PR description
    - Human review: Not yet performed.
---
 .../org/apache/doris/common/CacheFactoryTest.java  | 66 ++++++++++------------
 .../doris/datasource/RefreshCatalogTest.java       | 21 +++----
 2 files changed, 42 insertions(+), 45 deletions(-)

diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/common/CacheFactoryTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/common/CacheFactoryTest.java
index aea1f440431..fb28489f1ae 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/common/CacheFactoryTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/common/CacheFactoryTest.java
@@ -23,13 +23,16 @@ import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
 import com.github.benmanes.caffeine.cache.LoadingCache;
 import com.google.common.collect.Lists;
 import com.google.common.testing.FakeTicker;
+import com.google.common.util.concurrent.MoreExecutors;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import java.util.ArrayDeque;
 import java.util.List;
 import java.util.Optional;
 import java.util.OptionalLong;
+import java.util.Queue;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -211,45 +214,38 @@ public class CacheFactoryTest {
     }
 
     @Test
-    public void testAsyncLoadingCacheWithExpireAfterWrite() throws 
InterruptedException, ExecutionException {
+    public void testAsyncLoadingCacheWithExpireAfterWrite() throws Exception {
         FakeTicker ticker = new FakeTicker();
-        AtomicLong counter = new AtomicLong(0);
         CacheFactory cacheFactory = new CacheFactory(
-                OptionalLong.of(60L),
-                OptionalLong.of(10),
-                1000,
-                false,
-                ticker::read);
-        CacheValueLoader loader = new CacheValueLoader(counter);
-        AsyncLoadingCache<Integer, Optional<CacheValue>> loadingCache = 
cacheFactory.buildAsyncCache(loader, executor);
-        CompletableFuture<Optional<CacheValue>> futureValue = 
loadingCache.get(1);
-        Assertions.assertFalse(futureValue.isDone());
-        Assertions.assertEquals("value1", futureValue.get().get().getValue());
-        Assertions.assertEquals(1, counter.get());
-        futureValue = loadingCache.get(1);
-        Assertions.assertTrue(futureValue.isDone());
-        Assertions.assertEquals("value1", futureValue.get().get().getValue());
-        Assertions.assertEquals(1, counter.get());
-        // advance 11 seconds to pass the refreshAfterWrite
+                OptionalLong.of(60L), OptionalLong.of(10L), 1000, false, 
ticker::read);
+        Queue<CompletableFuture<Optional<CacheValue>>> loads = new 
ArrayDeque<>();
+        AsyncLoadingCache<Integer, Optional<CacheValue>> loadingCache = 
cacheFactory.buildAsyncCache(
+                (key, unusedExecutor) -> {
+                    CompletableFuture<Optional<CacheValue>> loaded = new 
CompletableFuture<>();
+                    loads.add(loaded);
+                    return loaded;
+                }, MoreExecutors.newDirectExecutorService());
+
+        CompletableFuture<Optional<CacheValue>> initial = loadingCache.get(1);
+        Assertions.assertFalse(initial.isDone());
+        loads.remove().complete(Optional.of(new CacheValue("initial")));
+        Assertions.assertEquals("initial", initial.get(5, 
TimeUnit.SECONDS).get().getValue());
+        Assertions.assertEquals("initial", 
loadingCache.get(1).join().get().getValue());
+        Assertions.assertTrue(loads.isEmpty());
+
         ticker.advance(11, TimeUnit.SECONDS);
-        // trigger refresh
-        futureValue = loadingCache.get(1);
-        // refresh in background, so still get value1
-        Assertions.assertTrue(futureValue.isDone());
-        Assertions.assertEquals("value1", futureValue.get().get().getValue());
-        // sleep longer to wait for refresh
-        Thread.sleep(2500);
-        futureValue = loadingCache.get(1);
-        Assertions.assertEquals("value1", futureValue.get().get().getValue());
-        // refreshed, so counter +1
-        Assertions.assertEquals(2, counter.get());
-        // advance 61 seconds to pass the expireAfterAccess
+        Assertions.assertEquals("initial", 
loadingCache.get(1).join().get().getValue());
+        Assertions.assertEquals("initial", 
loadingCache.get(1).join().get().getValue());
+        Assertions.assertEquals(1, loads.size());
+        loads.remove().complete(Optional.of(new CacheValue("refreshed")));
+        Assertions.assertEquals("refreshed", 
loadingCache.get(1).join().get().getValue());
+
         ticker.advance(61, TimeUnit.SECONDS);
-        futureValue = loadingCache.get(1);
-        Assertions.assertFalse(futureValue.isDone());
-        Assertions.assertEquals("value1", futureValue.get().get().getValue());
-        // expired, so counter +1
-        Assertions.assertEquals(3, counter.get());
+        CompletableFuture<Optional<CacheValue>> reloaded = loadingCache.get(1);
+        Assertions.assertFalse(reloaded.isDone());
+        loads.remove().complete(Optional.of(new CacheValue("reloaded")));
+        Assertions.assertEquals("reloaded", reloaded.get(5, 
TimeUnit.SECONDS).get().getValue());
+        Assertions.assertTrue(loads.isEmpty());
     }
 
     @Test
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/datasource/RefreshCatalogTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/datasource/RefreshCatalogTest.java
index 306ea1284da..a39bc2827c7 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/datasource/RefreshCatalogTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/datasource/RefreshCatalogTest.java
@@ -41,11 +41,13 @@ import org.apache.doris.utframe.TestWithFeService;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 public class RefreshCatalogTest extends TestWithFeService {
     private static Auth auth;
@@ -108,13 +110,12 @@ public class RefreshCatalogTest extends TestWithFeService 
{
         List<String> dbNames1 = test1.getDbNames();
         // there are test1.db1 , test1.db2, information_schema, mysql
         Assertions.assertEquals(4, dbNames1.size());
-        // 1.simulate ExternalCatalog adds a new table
         RefreshCatalogProvider.addData();
-        // 2.wait for the refresh time of the catalog
-        Thread.sleep(5000);
-        // there are test1.db1 , test1.db2 , test1.db3, information_schema, 
mysql
-        List<String> dbNames2 = test1.getDbNames();
-        Assertions.assertEquals(5, dbNames2.size());
+        Awaitility.await().pollInSameThread().atMost(30, 
TimeUnit.SECONDS).untilAsserted(() -> {
+            List<String> dbNames = test1.getDbNames();
+            Assertions.assertTrue(dbNames.contains("db3"), dbNames.toString());
+            Assertions.assertEquals(5, dbNames.size());
+        });
         ExternalInfoSchemaDatabase infoDb = (ExternalInfoSchemaDatabase) 
test1.getDb(InfoSchemaDb.DATABASE_NAME).get();
         Assertions.assertEquals(SchemaTable.TABLE_MAP.size(), 
infoDb.getTables().size());
         TestExternalDatabase testDb = (TestExternalDatabase) 
test1.getDb("db1").get();
@@ -125,12 +126,12 @@ public class RefreshCatalogTest extends TestWithFeService 
{
         String json = GsonUtils.GSON.toJson(env.getCatalogMgr());
         System.out.println(json);
         CatalogMgr mgr2 = GsonUtils.GSON.fromJson(json, CatalogMgr.class);
-        test1 = mgr2.getCatalog("test1");
-        infoDb = (ExternalInfoSchemaDatabase) 
test1.getDb(InfoSchemaDb.DATABASE_NAME).get();
+        CatalogIf replayedCatalog = mgr2.getCatalog("test1");
+        infoDb = (ExternalInfoSchemaDatabase) 
replayedCatalog.getDb(InfoSchemaDb.DATABASE_NAME).get();
         Assertions.assertEquals(SchemaTable.TABLE_MAP.size(), 
infoDb.getTables().size());
-        testDb = (TestExternalDatabase) test1.getDb("db1").get();
+        testDb = (TestExternalDatabase) replayedCatalog.getDb("db1").get();
         Assertions.assertEquals(2, testDb.getTables().size());
-        mysqlDb = (ExternalMysqlDatabase) 
test1.getDb(MysqlDb.DATABASE_NAME).get();
+        mysqlDb = (ExternalMysqlDatabase) 
replayedCatalog.getDb(MysqlDb.DATABASE_NAME).get();
         Assertions.assertEquals(2, mysqlDb.getTables().size());
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to