From c433f8418bf400e8eb348f9a14f10bcad51dea4e Mon Sep 17 00:00:00 2001 From: xloya <982052490@qq.com> Date: Mon, 1 Apr 2024 17:33:42 +0800 Subject: [PATCH] [#2745] fix(test): Extend sleep time in the `testInternalCache` unit test to fix flaky test (#2744) ### What changes were proposed in this pull request? `Caffeine Cache`'s use of `RemoveListener` is not stable and may cause a single test to fail in some cases. Therefore, the waiting time in the single test is extended to ensure that the `Cache` is cleaned normally. ![image](https://github.com/datastrato/gravitino/assets/26177232/7ecb8a6b-28a0-4146-91bd-0e633f9fe53b) ### Why are the changes needed? Fix: #2745 --------- Co-authored-by: xiaojiebao --- clients/filesystem-hadoop3/build.gradle.kts | 1 + .../TestGravitinoVirtualFileSystem.java | 30 ++++++++++++++----- gradle/libs.versions.toml | 2 ++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/clients/filesystem-hadoop3/build.gradle.kts b/clients/filesystem-hadoop3/build.gradle.kts index a6ad7ad365d..d90bc5d151b 100644 --- a/clients/filesystem-hadoop3/build.gradle.kts +++ b/clients/filesystem-hadoop3/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { implementation(project(":clients:client-java-runtime", configuration = "shadow")) implementation(libs.caffeine) + testImplementation(libs.awaitility) testImplementation(libs.hadoop3.common) testImplementation(libs.junit.jupiter.api) testImplementation(libs.junit.jupiter.params) diff --git a/clients/filesystem-hadoop3/src/test/java/com/datastrato/gravitino/filesystem/hadoop3/TestGravitinoVirtualFileSystem.java b/clients/filesystem-hadoop3/src/test/java/com/datastrato/gravitino/filesystem/hadoop3/TestGravitinoVirtualFileSystem.java index 6c016f42bf2..85a6b8363d8 100644 --- a/clients/filesystem-hadoop3/src/test/java/com/datastrato/gravitino/filesystem/hadoop3/TestGravitinoVirtualFileSystem.java +++ b/clients/filesystem-hadoop3/src/test/java/com/datastrato/gravitino/filesystem/hadoop3/TestGravitinoVirtualFileSystem.java @@ -20,10 +20,12 @@ import java.util.Comparator; import java.util.List; import java.util.Objects; +import java.util.concurrent.TimeUnit; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; +import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -151,7 +153,7 @@ public void testFSCache() throws IOException { } @Test - public void testInternalCache() throws IOException, InterruptedException { + public void testInternalCache() throws IOException { Configuration configuration = new Configuration(conf); configuration.set( GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_FILESET_CACHE_MAX_CAPACITY_KEY, "1"); @@ -187,15 +189,27 @@ public void testInternalCache() throws IOException, InterruptedException { localPath2.toString()); FileSystemTestUtils.mkdirs(filesetPath2, fs); - Thread.sleep(1000); - assertNull( - ((GravitinoVirtualFileSystem) fs) - .getFilesetCache() - .getIfPresent(NameIdentifier.of(metalakeName, catalogName, schemaName, "fileset1"))); + Awaitility.await() + .atMost(5, TimeUnit.SECONDS) + .pollInterval(1, TimeUnit.SECONDS) + .untilAsserted( + () -> + assertNull( + ((GravitinoVirtualFileSystem) fs) + .getFilesetCache() + .getIfPresent( + NameIdentifier.of( + metalakeName, catalogName, schemaName, "fileset1")))); // expired by time - Thread.sleep(1000); - assertEquals(0, ((GravitinoVirtualFileSystem) fs).getFilesetCache().asMap().size()); + Awaitility.await() + .atMost(5, TimeUnit.SECONDS) + .pollInterval(1, TimeUnit.SECONDS) + .untilAsserted( + () -> + assertEquals( + 0, ((GravitinoVirtualFileSystem) fs).getFilesetCache().asMap().size())); + assertNull( ((GravitinoVirtualFileSystem) fs) .getFilesetCache() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 39a9459ba57..e5dbf1a46fe 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -52,6 +52,7 @@ h2db = "1.4.200" kyuubi = "1.8.0" kafka = "3.4.0" curator = "2.12.0" +awaitility = "4.2.1" protobuf-plugin = "0.9.2" spotless-plugin = '6.11.0' @@ -162,6 +163,7 @@ selenium = { group = "org.seleniumhq.selenium", name = "selenium-java", version. rauschig = { group = "org.rauschig", name = "jarchivelib", version.ref = "rauschig" } mybatis = { group = "org.mybatis", name = "mybatis", version.ref = "mybatis"} h2db = { group = "com.h2database", name = "h2", version.ref = "h2db"} +awaitility = { group = "org.awaitility", name = "awaitility", version.ref = "awaitility" } [bundles] log4j = ["slf4j-api", "log4j-slf4j2-impl", "log4j-api", "log4j-core", "log4j-12-api"]