From 99214d673b8292c21366a2b06fb73c4c7078419b Mon Sep 17 00:00:00 2001 From: Kaijie Chen Date: Sat, 14 May 2022 16:30:18 +0800 Subject: [PATCH 1/5] HDDS-6737. Migrate parameterized tests in hdds-server-framework to JUnit5 --- hadoop-hdds/framework/pom.xml | 4 + .../client/TestCertificateClientInit.java | 53 ++--- .../server/http/TestRatisNameRewrite.java | 41 ++-- .../hdds/utils/db/cache/TestTableCache.java | 223 ++++++++++-------- 4 files changed, 161 insertions(+), 160 deletions(-) diff --git a/hadoop-hdds/framework/pom.xml b/hadoop-hdds/framework/pom.xml index e09032c73fdc..f10cd3010cd1 100644 --- a/hadoop-hdds/framework/pom.xml +++ b/hadoop-hdds/framework/pom.xml @@ -141,6 +141,10 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd"> test-jar test + + org.junit.jupiter + junit-jupiter-params + diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java index dc4d7b7d5f28..df4197db3aa6 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java @@ -28,12 +28,10 @@ import org.apache.hadoop.security.ssl.KeyStoreTestUtil; import org.apache.ozone.test.GenericTestUtils; import org.bouncycastle.cert.X509CertificateHolder; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameter; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.nio.file.Files; import java.nio.file.Path; @@ -41,8 +39,8 @@ import java.security.KeyPair; import java.security.cert.X509Certificate; import java.util.Arrays; -import java.util.Collection; import java.util.UUID; +import java.util.stream.Stream; import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_METADATA_DIR_NAME; import static org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse; @@ -50,13 +48,12 @@ import static org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.GETCERT; import static org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.RECOVER; import static org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.SUCCESS; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Test class for {@link DefaultCertificateClient}. */ -@RunWith(Parameterized.class) -@SuppressWarnings("visibilitymodifier") public class TestCertificateClientInit { private KeyPair keyPair; @@ -72,18 +69,8 @@ public class TestCertificateClientInit { private static final String DN_COMPONENT = DNCertificateClient.COMPONENT_NAME; private static final String OM_COMPONENT = OMCertificateClient.COMPONENT_NAME; - @Parameter - public boolean pvtKeyPresent; - @Parameter(1) - public boolean pubKeyPresent; - @Parameter(2) - public boolean certPresent; - @Parameter(3) - public InitResponse expectedResult; - - @Parameterized.Parameters - public static Collection initData() { - return Arrays.asList(new Object[][]{ + private static Stream parameters() { + return Arrays.stream(new Object[][]{ {false, false, false, GETCERT}, {false, false, true, FAILURE}, {false, true, false, FAILURE}, @@ -94,7 +81,7 @@ public static Collection initData() { {true, true, true, SUCCESS}}); } - @Before + @BeforeEach public void setUp() throws Exception { OzoneConfiguration config = new OzoneConfiguration(); final String path = GenericTestUtils @@ -117,7 +104,7 @@ public void setUp() throws Exception { Files.createDirectories(securityConfig.getKeyLocation(OM_COMPONENT)); } - @After + @AfterEach public void tearDown() { dnCertificateClient = null; omCertificateClient = null; @@ -125,8 +112,10 @@ public void tearDown() { } - @Test - public void testInitDatanode() throws Exception { + @ParameterizedTest + @MethodSource("parameters") + public void testInitDatanode(boolean pvtKeyPresent, boolean pubKeyPresent, + boolean certPresent, InitResponse expectedResult) throws Exception { if (pvtKeyPresent) { dnKeyCodec.writePrivateKey(keyPair.getPrivate()); } else { @@ -157,7 +146,7 @@ public void testInitDatanode() throws Exception { } InitResponse response = dnCertificateClient.init(); - assertTrue(response.equals(expectedResult)); + assertEquals(expectedResult, response); if (!response.equals(FAILURE)) { assertTrue(OzoneSecurityUtil.checkIfFileExist( @@ -169,8 +158,10 @@ public void testInitDatanode() throws Exception { } } - @Test - public void testInitOzoneManager() throws Exception { + @ParameterizedTest + @MethodSource("parameters") + public void testInitOzoneManager(boolean pvtKeyPresent, boolean pubKeyPresent, + boolean certPresent, InitResponse expectedResult) throws Exception { if (pvtKeyPresent) { omKeyCodec.writePrivateKey(keyPair.getPrivate()); } else { @@ -202,9 +193,9 @@ public void testInitOzoneManager() throws Exception { InitResponse response = omCertificateClient.init(); if (pvtKeyPresent && pubKeyPresent && !certPresent) { - assertTrue(response.equals(RECOVER)); + assertEquals(RECOVER, response); } else { - assertTrue(response.equals(expectedResult)); + assertEquals(expectedResult, response); } if (!response.equals(FAILURE)) { diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java index f0067fd569a9..25dca29674fb 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java @@ -20,30 +20,20 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Stream; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test Ratis metrics renaming. */ -@RunWith(Parameterized.class) public class TestRatisNameRewrite { - private List names = new ArrayList<>(); - private List values = new ArrayList<>(); - private String originalName; - private String expectedName; - - private List expectedTagNames; - private List expectedTagValues; - - @Parameterized.Parameters - public static List parameters() { - return Arrays.asList( + private static Stream parameters() { + return Stream.of( new Object[] { "ratis.log_appender" + ".851cb00a-af97-455a-b079-d94a77d2a936@group-C14654DE8C2C" @@ -95,23 +85,20 @@ public static List parameters() { ); } - public TestRatisNameRewrite(String originalName, String expectedName, + @ParameterizedTest + @MethodSource("parameters") + public void normalizeRatisMetricName(String originalName, String expectedName, String[] expectedTagNames, String[] expectedTagValues) { - this.originalName = originalName; - this.expectedName = expectedName; - this.expectedTagNames = Arrays.asList(expectedTagNames); - this.expectedTagValues = Arrays.asList(expectedTagValues); - } - @Test - public void normalizeRatisMetricName() { + List names = new ArrayList<>(); + List values = new ArrayList<>(); String cleanName = new RatisNameRewriteSampleBuilder() .normalizeRatisMetric(originalName, names, values); - Assert.assertEquals(expectedName, cleanName); - Assert.assertEquals(expectedTagNames, names); - Assert.assertEquals(expectedTagValues, values); + Assertions.assertEquals(expectedName, cleanName); + Assertions.assertEquals(Arrays.asList(expectedTagNames), names); + Assertions.assertEquals(Arrays.asList(expectedTagValues), values); } } \ No newline at end of file diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java index 0449a79e1d70..1a38aa59e31b 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java @@ -20,56 +20,44 @@ package org.apache.hadoop.hdds.utils.db.cache; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; import com.google.common.base.Optional; import org.apache.ozone.test.GenericTestUtils; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.event.Level; -import static org.junit.Assert.fail; /** * Class tests partial table cache. */ -@RunWith(value = Parameterized.class) public class TestTableCache { private static final Logger LOG = - LoggerFactory.getLogger(TestTableCache.class); + LoggerFactory.getLogger(TestTableCache.class); private TableCache, CacheValue> tableCache; - private final TableCache.CacheType cacheType; - - - @Parameterized.Parameters - public static Collection policy() { - Object[][] params = new Object[][] { - {TableCache.CacheType.FULL_CACHE}, - {TableCache.CacheType.PARTIAL_CACHE} - }; - return Arrays.asList(params); + private static Stream policy() { + return Stream.of( + TableCache.CacheType.FULL_CACHE, + TableCache.CacheType.PARTIAL_CACHE + ); } - public TestTableCache( - TableCache.CacheType cacheType) { + @BeforeAll + public static void setLogLevel() { GenericTestUtils.setLogLevel(FullTableCache.LOG, Level.DEBUG); - this.cacheType = cacheType; } - - @Before - public void create() { + public void createTableCache(TableCache.CacheType cacheType) { if (cacheType == TableCache.CacheType.FULL_CACHE) { tableCache = new FullTableCache<>(); } else { @@ -78,10 +66,11 @@ public void create() { LOG.info("cacheType: {}", cacheType); } + @ParameterizedTest + @MethodSource("policy") + public void testPartialTableCache(TableCache.CacheType cacheType) { - @Test - public void testPartialTableCache() { - + createTableCache(cacheType); for (int i = 0; i < 10; i++) { tableCache.put(new CacheKey<>(Integer.toString(i)), @@ -90,11 +79,11 @@ public void testPartialTableCache() { for (int i = 0; i < 10; i++) { - Assert.assertEquals(Integer.toString(i), + Assertions.assertEquals(Integer.toString(i), tableCache.get(new CacheKey<>(Integer.toString(i))).getCacheValue()); } - ArrayList epochs = new ArrayList(); + ArrayList epochs = new ArrayList<>(); epochs.add(0L); epochs.add(1L); epochs.add(2L); @@ -104,13 +93,17 @@ public void testPartialTableCache() { tableCache.evictCache(epochs); for (int i = 5; i < 10; i++) { - Assert.assertEquals(Integer.toString(i), + Assertions.assertEquals(Integer.toString(i), tableCache.get(new CacheKey<>(Integer.toString(i))).getCacheValue()); } } - @Test - public void testTableCacheWithRenameKey() { + @ParameterizedTest + @MethodSource("policy") + public void testTableCacheWithRenameKey(TableCache.CacheType cacheType) { + + createTableCache(cacheType); + // putting cache with same epoch and different keyNames for (int i = 0; i < 3; i++) { tableCache.put(new CacheKey<>(Integer.toString(i).concat("A")), @@ -121,18 +114,19 @@ public void testTableCacheWithRenameKey() { // Epoch entries should be like (long, (key1, key2, ...)) // (0, (0A, 0B)) (1, (1A, 1B)) (2, (2A, 1B)) - Assert.assertEquals(3, tableCache.getEpochEntries().size()); - Assert.assertEquals(2, tableCache.getEpochEntries().get(0L).size()); + Assertions.assertEquals(3, tableCache.getEpochEntries().size()); + Assertions.assertEquals(2, tableCache.getEpochEntries().get(0L).size()); // Cache should be like (key, (cacheValue, long)) // (0A, (null, 0)) (0B, (0, 0)) // (1A, (null, 1)) (1B, (0, 1)) // (2A, (null, 2)) (2B, (0, 2)) for (int i = 0; i < 3; i++) { - Assert.assertNull(tableCache.get(new CacheKey<>( + Assertions.assertNull(tableCache.get(new CacheKey<>( Integer.toString(i).concat("A"))).getCacheValue()); - Assert.assertEquals(Integer.toString(i), tableCache.get(new CacheKey<>( - Integer.toString(i).concat("B"))).getCacheValue()); + Assertions.assertEquals(Integer.toString(i), + tableCache.get(new CacheKey<>(Integer.toString(i).concat("B"))) + .getCacheValue()); } ArrayList epochs = new ArrayList<>(); @@ -144,24 +138,29 @@ public void testTableCacheWithRenameKey() { tableCache.evictCache(epochs); - Assert.assertEquals(0, tableCache.getEpochEntries().size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); if (cacheType == TableCache.CacheType.PARTIAL_CACHE) { - Assert.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.size()); } else { - Assert.assertEquals(3, tableCache.size()); + Assertions.assertEquals(3, tableCache.size()); } } - @Test - public void testPartialTableCacheWithNotContinousEntries() throws Exception { + @ParameterizedTest + @MethodSource("policy") + public void testPartialTableCacheWithNotContinousEntries( + TableCache.CacheType cacheType) { + + createTableCache(cacheType); + int totalCount = 0; int insertedCount = 3000; int cleanupCount = 0; - ArrayList epochs = new ArrayList(); + ArrayList epochs = new ArrayList<>(); for (long i = 0; i < insertedCount; i += 2) { if (cleanupCount++ < 1000) { epochs.add(i); @@ -171,7 +170,7 @@ public void testPartialTableCacheWithNotContinousEntries() throws Exception { totalCount++; } - Assert.assertEquals(totalCount, tableCache.size()); + Assertions.assertEquals(totalCount, tableCache.size()); tableCache.evictCache(epochs); @@ -179,30 +178,34 @@ public void testPartialTableCacheWithNotContinousEntries() throws Exception { // If cleanup policy is manual entries should have been removed. if (cacheType == TableCache.CacheType.PARTIAL_CACHE) { - Assert.assertEquals(count - epochs.size(), tableCache.size()); + Assertions.assertEquals(count - epochs.size(), tableCache.size()); // Check remaining entries exist or not and deleted entries does not // exist. for (long i = 0; i < insertedCount; i += 2) { if (!epochs.contains(i)) { - Assert.assertEquals(Long.toString(i), + Assertions.assertEquals(Long.toString(i), tableCache.get(new CacheKey<>(Long.toString(i))).getCacheValue()); } else { - Assert.assertEquals(null, + Assertions.assertNull( tableCache.get(new CacheKey<>(Long.toString(i)))); } } } else { for (long i = 0; i < insertedCount; i += 2) { - Assert.assertEquals(Long.toString(i), + Assertions.assertEquals(Long.toString(i), tableCache.get(new CacheKey<>(Long.toString(i))).getCacheValue()); } } } - @Test - public void testPartialTableCacheWithOverrideEntries() throws Exception { + @ParameterizedTest + @MethodSource("policy") + public void testPartialTableCacheWithOverrideEntries( + TableCache.CacheType cacheType) { + + createTableCache(cacheType); tableCache.put(new CacheKey<>(Long.toString(0)), new CacheValue<>(Optional.of(Long.toString(0)), 0)); @@ -223,9 +226,9 @@ public void testPartialTableCacheWithOverrideEntries() throws Exception { - Assert.assertEquals(3, tableCache.size()); + Assertions.assertEquals(3, tableCache.size()); // It will have 2 additional entries because we have 2 override entries. - Assert.assertEquals(3 + 2, + Assertions.assertEquals(3 + 2, tableCache.getEpochEntries().size()); // Now remove @@ -241,9 +244,9 @@ public void testPartialTableCacheWithOverrideEntries() throws Exception { tableCache.evictCache(epochs); - Assert.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.size()); - Assert.assertEquals(0, tableCache.getEpochEntries().size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } // Add a new entry. @@ -255,17 +258,21 @@ public void testPartialTableCacheWithOverrideEntries() throws Exception { if (cacheType == TableCache.CacheType.PARTIAL_CACHE) { tableCache.evictCache(epochs); - Assert.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.size()); // Overrided entries would have been deleted. - Assert.assertEquals(0, tableCache.getEpochEntries().size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } } - @Test - public void testPartialTableCacheWithOverrideAndDelete() throws Exception { + @ParameterizedTest + @MethodSource("policy") + public void testPartialTableCacheWithOverrideAndDelete( + TableCache.CacheType cacheType) { + + createTableCache(cacheType); tableCache.put(new CacheKey<>(Long.toString(0)), new CacheValue<>(Optional.of(Long.toString(0)), 0)); @@ -293,9 +300,9 @@ public void testPartialTableCacheWithOverrideAndDelete() throws Exception { // 0-5, 1-6, 2-2 - Assert.assertEquals(3, tableCache.size()); + Assertions.assertEquals(3, tableCache.size()); // It will have 4 additional entries because we have 4 override entries. - Assert.assertEquals(3 + 4, + Assertions.assertEquals(3 + 4, tableCache.getEpochEntries().size()); // Now remove @@ -313,16 +320,16 @@ public void testPartialTableCacheWithOverrideAndDelete() throws Exception { if (cacheType == TableCache.CacheType.PARTIAL_CACHE) { tableCache.evictCache(epochs); - Assert.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.size()); - Assert.assertEquals(0, tableCache.getEpochEntries().size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } else { tableCache.evictCache(epochs); - Assert.assertEquals(1, tableCache.size()); + Assertions.assertEquals(1, tableCache.size()); // Epoch entries which are overrided also will be cleaned up. - Assert.assertEquals(0, tableCache.getEpochEntries().size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } // Add a new entry, now old override entries will be cleaned up. @@ -335,26 +342,30 @@ public void testPartialTableCacheWithOverrideAndDelete() throws Exception { if (cacheType == TableCache.CacheType.PARTIAL_CACHE) { tableCache.evictCache(epochs); - Assert.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.size()); // Epoch entries which are overrided now would have been deleted. - Assert.assertEquals(0, tableCache.getEpochEntries().size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } else { tableCache.evictCache(epochs); // 2 entries will be in cache, as 2 are not deleted. - Assert.assertEquals(2, tableCache.size()); + Assertions.assertEquals(2, tableCache.size()); // Epoch entries which are not marked for delete will also be cleaned up. // As they are override entries in full cache. - Assert.assertEquals(0, tableCache.getEpochEntries().size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } } - @Test - public void testPartialTableCacheParallel() throws Exception { + @ParameterizedTest + @MethodSource("policy") + public void testPartialTableCacheParallel( + TableCache.CacheType cacheType) throws Exception { + + createTableCache(cacheType); int totalCount = 0; CompletableFuture future = @@ -362,12 +373,12 @@ public void testPartialTableCacheParallel() throws Exception { try { return writeToCache(10, 1, 0); } catch (InterruptedException ex) { - fail("writeToCache got interrupt exception"); + Assertions.fail("writeToCache got interrupt exception"); } return 0; }); int value = future.get(); - Assert.assertEquals(10, value); + Assertions.assertEquals(10, value); totalCount += value; @@ -376,20 +387,20 @@ public void testPartialTableCacheParallel() throws Exception { try { return writeToCache(10, 11, 100); } catch (InterruptedException ex) { - fail("writeToCache got interrupt exception"); + Assertions.fail("writeToCache got interrupt exception"); } return 0; }); // Check we have first 10 entries in cache. for (int i = 1; i <= 10; i++) { - Assert.assertEquals(Integer.toString(i), + Assertions.assertEquals(Integer.toString(i), tableCache.get(new CacheKey<>(Integer.toString(i))).getCacheValue()); } value = future.get(); - Assert.assertEquals(10, value); + Assertions.assertEquals(10, value); totalCount += value; @@ -407,11 +418,10 @@ public void testPartialTableCacheParallel() throws Exception { tableCache.evictCache(epochs); // We should totalCount - deleted entries in cache. - final int tc = totalCount; - Assert.assertEquals(tc - deleted, tableCache.size()); + Assertions.assertEquals(totalCount - deleted, tableCache.size()); // Check if we have remaining entries. for (int i = 6; i <= totalCount; i++) { - Assert.assertEquals(Integer.toString(i), tableCache.get( + Assertions.assertEquals(Integer.toString(i), tableCache.get( new CacheKey<>(Integer.toString(i))).getCacheValue()); } @@ -423,21 +433,24 @@ public void testPartialTableCacheParallel() throws Exception { tableCache.evictCache(epochs); // Cleaned up all entries, so cache size should be zero. - Assert.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.size()); } else { ArrayList epochs = new ArrayList<>(); for (long i = 0; i <= totalCount; i++) { epochs.add(i); } tableCache.evictCache(epochs); - Assert.assertEquals(totalCount, tableCache.size()); + Assertions.assertEquals(totalCount, tableCache.size()); } } - @Test - public void testTableCache() { + @ParameterizedTest + @MethodSource("policy") + public void testTableCache(TableCache.CacheType cacheType) { + + createTableCache(cacheType); // In non-HA epoch entries might be out of order. // Scenario is like create vol, set vol, set vol, delete vol @@ -459,13 +472,17 @@ public void testTableCache() { tableCache.evictCache(epochs); - Assert.assertTrue(tableCache.size() == 0); - Assert.assertTrue(tableCache.getEpochEntries().size() == 0); + Assertions.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } - @Test - public void testTableCacheWithNonConsecutiveEpochList() { + @ParameterizedTest + @MethodSource("policy") + public void testTableCacheWithNonConsecutiveEpochList( + TableCache.CacheType cacheType) { + + createTableCache(cacheType); // In non-HA epoch entries might be out of order. tableCache.put(new CacheKey<>(Long.toString(0)), @@ -488,15 +505,15 @@ public void testTableCacheWithNonConsecutiveEpochList() { tableCache.evictCache(epochs); - Assert.assertTrue(tableCache.size() == 2); - Assert.assertTrue(tableCache.getEpochEntries().size() == 2); + Assertions.assertEquals(2, tableCache.size()); + Assertions.assertEquals(2, tableCache.getEpochEntries().size()); - Assert.assertNotNull(tableCache.get(new CacheKey<>(Long.toString(0)))); - Assert.assertEquals(2, + Assertions.assertNotNull(tableCache.get(new CacheKey<>(Long.toString(0)))); + Assertions.assertEquals(2, tableCache.get(new CacheKey<>(Long.toString(0))).getEpoch()); - Assert.assertNotNull(tableCache.get(new CacheKey<>(Long.toString(1)))); - Assert.assertEquals(4, + Assertions.assertNotNull(tableCache.get(new CacheKey<>(Long.toString(1)))); + Assertions.assertEquals(4, tableCache.get(new CacheKey<>(Long.toString(1))).getEpoch()); // now evict 2,4 @@ -507,19 +524,21 @@ public void testTableCacheWithNonConsecutiveEpochList() { tableCache.evictCache(epochs); if (cacheType == TableCache.CacheType.PARTIAL_CACHE) { - Assert.assertTrue(tableCache.size() == 0); - Assert.assertTrue(tableCache.getEpochEntries().size() == 0); + Assertions.assertEquals(0, tableCache.size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } else { - Assert.assertTrue(tableCache.size() == 2); - Assert.assertTrue(tableCache.getEpochEntries().size() == 0); + Assertions.assertEquals(2, tableCache.size()); + Assertions.assertEquals(0, tableCache.getEpochEntries().size()); // Entries should exist, as the entries are not delete entries - Assert.assertNotNull(tableCache.get(new CacheKey<>(Long.toString(0)))); - Assert.assertEquals(2, + Assertions.assertNotNull( + tableCache.get(new CacheKey<>(Long.toString(0)))); + Assertions.assertEquals(2, tableCache.get(new CacheKey<>(Long.toString(0))).getEpoch()); - Assert.assertNotNull(tableCache.get(new CacheKey<>(Long.toString(1)))); - Assert.assertEquals(4, + Assertions.assertNotNull( + tableCache.get(new CacheKey<>(Long.toString(1)))); + Assertions.assertEquals(4, tableCache.get(new CacheKey<>(Long.toString(1))).getEpoch()); } From bd76d0962e6b51af88ed968f4c5efdf9c366732e Mon Sep 17 00:00:00 2001 From: Kaijie Chen Date: Mon, 16 May 2022 10:25:10 +0800 Subject: [PATCH 2/5] Replace Object[] by Arguments --- .../client/TestCertificateClientInit.java | 24 +++++++++-------- .../server/http/TestRatisNameRewrite.java | 26 ++++++++++--------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java index df4197db3aa6..47b02a9a2e9b 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestCertificateClientInit.java @@ -31,6 +31,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.nio.file.Files; @@ -38,7 +39,6 @@ import java.nio.file.Paths; import java.security.KeyPair; import java.security.cert.X509Certificate; -import java.util.Arrays; import java.util.UUID; import java.util.stream.Stream; @@ -50,6 +50,7 @@ import static org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.SUCCESS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.params.provider.Arguments.arguments; /** * Test class for {@link DefaultCertificateClient}. @@ -69,16 +70,17 @@ public class TestCertificateClientInit { private static final String DN_COMPONENT = DNCertificateClient.COMPONENT_NAME; private static final String OM_COMPONENT = OMCertificateClient.COMPONENT_NAME; - private static Stream parameters() { - return Arrays.stream(new Object[][]{ - {false, false, false, GETCERT}, - {false, false, true, FAILURE}, - {false, true, false, FAILURE}, - {true, false, false, FAILURE}, - {false, true, true, FAILURE}, - {true, true, false, GETCERT}, - {true, false, true, SUCCESS}, - {true, true, true, SUCCESS}}); + private static Stream parameters() { + return Stream.of( + arguments(false, false, false, GETCERT), + arguments(false, false, true, FAILURE), + arguments(false, true, false, FAILURE), + arguments(true, false, false, FAILURE), + arguments(false, true, true, FAILURE), + arguments(true, true, false, GETCERT), + arguments(true, false, true, SUCCESS), + arguments(true, true, true, SUCCESS) + ); } @BeforeEach diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java index 25dca29674fb..774ccf11aa79 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestRatisNameRewrite.java @@ -24,17 +24,20 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import static org.junit.jupiter.params.provider.Arguments.arguments; + /** * Test Ratis metrics renaming. */ public class TestRatisNameRewrite { - private static Stream parameters() { + private static Stream parameters() { return Stream.of( - new Object[] { + arguments( "ratis.log_appender" + ".851cb00a-af97-455a-b079-d94a77d2a936@group-C14654DE8C2C" + ".follower_65f881ea-8794-403d-be77-a030ed79c341_match_index", @@ -43,8 +46,8 @@ private static Stream parameters() { new String[] {"851cb00a-af97-455a-b079-d94a77d2a936", "group-C14654DE8C2C", "65f881ea-8794-403d-be77-a030ed79c341"} - }, - new Object[] { + ), + arguments( "ratis_grpc.log_appender.72caaf3a-fb1c-4da4-9cc0-a2ce21bb8e67@group" + "-72caaf3a-fb1c-4da4-9cc0-a2ce21bb8e67" + ".grpc_log_appender_follower_75fa730a-59f0-4547" @@ -54,15 +57,15 @@ private static Stream parameters() { new String[] {"72caaf3a-fb1c-4da4-9cc0-a2ce21bb8e67", "group-72caaf3a-fb1c-4da4-9cc0-a2ce21bb8e67", "75fa730a-59f0-4547-bd68-216162c263eb"} - }, - new Object[] { + ), + arguments( "ratis_core.ratis_log_worker.72caaf3a-fb1c-4da4-9cc0-a2ce21bb8e67" + ".dataQueueSize", "ratis_core.ratis_log_worker.dataQueueSize", new String[] {"instance"}, new String[] {"72caaf3a-fb1c-4da4-9cc0-a2ce21bb8e67"} - }, - new Object[] { + ), + arguments( "ratis_grpc.log_appender.8e505d6e-12a4-4660-80e3-eb735879db06" + "@group-49616B7F02CE.grpc_log_appender_follower_a4b099a7" + "-511f-4fef-85bf-b9eeddd7c270_latency", @@ -70,8 +73,8 @@ private static Stream parameters() { new String[] {"instance", "group", "follower"}, new String[] {"8e505d6e-12a4-4660-80e3-eb735879db06", "group-49616B7F02CE", "a4b099a7-511f-4fef-85bf-b9eeddd7c270"} - }, - new Object[] { + ), + arguments( "ratis_grpc.log_appender.8e505d6e-12a4-4660-80e3-eb735879db06" + "@group-49616B7F02CE.grpc_log_appender_follower_a4b099a7" + "-511f-4fef-85bf-b9eeddd7c270_success_reply_count", @@ -80,8 +83,7 @@ private static Stream parameters() { new String[] {"instance", "group", "follower"}, new String[] {"8e505d6e-12a4-4660-80e3-eb735879db06", "group-49616B7F02CE", "a4b099a7-511f-4fef-85bf-b9eeddd7c270"} - } - + ) ); } From d593c777ed030256b5f3d900b761bd69356bbf18 Mon Sep 17 00:00:00 2001 From: Kaijie Chen Date: Tue, 17 May 2022 22:28:27 +0800 Subject: [PATCH 3/5] Address comments --- .../apache/hadoop/hdds/utils/db/cache/TestTableCache.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java index 1a38aa59e31b..32790ece5e79 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java @@ -30,8 +30,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.slf4j.event.Level; @@ -40,9 +38,6 @@ */ public class TestTableCache { - private static final Logger LOG = - LoggerFactory.getLogger(TestTableCache.class); - private TableCache, CacheValue> tableCache; private static Stream policy() { @@ -57,13 +52,12 @@ public static void setLogLevel() { GenericTestUtils.setLogLevel(FullTableCache.LOG, Level.DEBUG); } - public void createTableCache(TableCache.CacheType cacheType) { + private void createTableCache(TableCache.CacheType cacheType) { if (cacheType == TableCache.CacheType.FULL_CACHE) { tableCache = new FullTableCache<>(); } else { tableCache = new PartialTableCache<>(); } - LOG.info("cacheType: {}", cacheType); } @ParameterizedTest From 9d82caba6e890f7d32156f0c9bfcef4045dc939c Mon Sep 17 00:00:00 2001 From: Kaijie Chen Date: Wed, 18 May 2022 01:22:46 +0800 Subject: [PATCH 4/5] Replace MethodSource by EnumSource --- .../hdds/utils/db/cache/TestTableCache.java | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java index 32790ece5e79..6d674e281271 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java @@ -22,14 +22,13 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; -import java.util.stream.Stream; import com.google.common.base.Optional; import org.apache.ozone.test.GenericTestUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.EnumSource; import org.slf4j.event.Level; @@ -40,13 +39,6 @@ public class TestTableCache { private TableCache, CacheValue> tableCache; - private static Stream policy() { - return Stream.of( - TableCache.CacheType.FULL_CACHE, - TableCache.CacheType.PARTIAL_CACHE - ); - } - @BeforeAll public static void setLogLevel() { GenericTestUtils.setLogLevel(FullTableCache.LOG, Level.DEBUG); @@ -61,7 +53,7 @@ private void createTableCache(TableCache.CacheType cacheType) { } @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testPartialTableCache(TableCache.CacheType cacheType) { createTableCache(cacheType); @@ -93,7 +85,7 @@ public void testPartialTableCache(TableCache.CacheType cacheType) { } @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testTableCacheWithRenameKey(TableCache.CacheType cacheType) { createTableCache(cacheType); @@ -143,7 +135,7 @@ public void testTableCacheWithRenameKey(TableCache.CacheType cacheType) { } @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testPartialTableCacheWithNotContinousEntries( TableCache.CacheType cacheType) { @@ -195,7 +187,7 @@ public void testPartialTableCacheWithNotContinousEntries( } @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testPartialTableCacheWithOverrideEntries( TableCache.CacheType cacheType) { @@ -262,7 +254,7 @@ public void testPartialTableCacheWithOverrideEntries( } @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testPartialTableCacheWithOverrideAndDelete( TableCache.CacheType cacheType) { @@ -355,7 +347,7 @@ public void testPartialTableCacheWithOverrideAndDelete( } @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testPartialTableCacheParallel( TableCache.CacheType cacheType) throws Exception { @@ -441,7 +433,7 @@ public void testPartialTableCacheParallel( } @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testTableCache(TableCache.CacheType cacheType) { createTableCache(cacheType); @@ -472,7 +464,7 @@ public void testTableCache(TableCache.CacheType cacheType) { @ParameterizedTest - @MethodSource("policy") + @EnumSource(TableCache.CacheType.class) public void testTableCacheWithNonConsecutiveEpochList( TableCache.CacheType cacheType) { From 6ec71175394f71a590efbf8104878e7a9b130655 Mon Sep 17 00:00:00 2001 From: Kaijie Chen Date: Wed, 18 May 2022 01:27:39 +0800 Subject: [PATCH 5/5] Fix typo --- .../hdds/utils/db/cache/TestTableCache.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java index 6d674e281271..8881636d04b4 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/cache/TestTableCache.java @@ -75,7 +75,7 @@ public void testPartialTableCache(TableCache.CacheType cacheType) { epochs.add(2L); epochs.add(3L); epochs.add(4L); - // On a full table cache if some one calls cleanup it is a no-op. + // On a full table cache if someone calls cleanup it is a no-op. tableCache.evictCache(epochs); for (int i = 5; i < 10; i++) { @@ -136,7 +136,7 @@ public void testTableCacheWithRenameKey(TableCache.CacheType cacheType) { @ParameterizedTest @EnumSource(TableCache.CacheType.class) - public void testPartialTableCacheWithNotContinousEntries( + public void testPartialTableCacheWithNotContinuousEntries( TableCache.CacheType cacheType) { createTableCache(cacheType); @@ -246,7 +246,7 @@ public void testPartialTableCacheWithOverrideEntries( Assertions.assertEquals(0, tableCache.size()); - // Overrided entries would have been deleted. + // Overridden entries would have been deleted. Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } @@ -268,13 +268,13 @@ public void testPartialTableCacheWithOverrideAndDelete( new CacheValue<>(Optional.of(Long.toString(2)), 2)); - //Override entries + // Override entries tableCache.put(new CacheKey<>(Long.toString(0)), new CacheValue<>(Optional.of(Long.toString(0)), 3)); tableCache.put(new CacheKey<>(Long.toString(1)), new CacheValue<>(Optional.of(Long.toString(1)), 4)); - // Finally mark them for deleted + // Finally, mark them for deleted tableCache.put(new CacheKey<>(Long.toString(0)), new CacheValue<>(Optional.absent(), 5)); tableCache.put(new CacheKey<>(Long.toString(1)), @@ -314,7 +314,7 @@ public void testPartialTableCacheWithOverrideAndDelete( Assertions.assertEquals(1, tableCache.size()); - // Epoch entries which are overrided also will be cleaned up. + // Epoch entries which are overridden also will be cleaned up. Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } @@ -330,7 +330,7 @@ public void testPartialTableCacheWithOverrideAndDelete( Assertions.assertEquals(0, tableCache.size()); - // Epoch entries which are overrided now would have been deleted. + // Epoch entries which are overridden now would have been deleted. Assertions.assertEquals(0, tableCache.getEpochEntries().size()); } else { tableCache.evictCache(epochs); @@ -393,7 +393,7 @@ public void testPartialTableCacheParallel( if (cacheType == TableCache.CacheType.PARTIAL_CACHE) { int deleted = 5; - // cleanup first 5 entires + // cleanup first 5 entries ArrayList epochs = new ArrayList<>(); epochs.add(1L);