diff --git a/sharding-orchestration/sharding-orchestration-core/src/main/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGenerator.java b/sharding-orchestration/sharding-orchestration-core/src/main/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGenerator.java index d23f5e69d8452697ac94e84bd25f8be29b417763..eaa1134ff16ad04c0fc64781a4d4490185b59d24 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/main/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGenerator.java +++ b/sharding-orchestration/sharding-orchestration-core/src/main/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGenerator.java @@ -72,7 +72,7 @@ public final class LeafSnowflakeKeyGenerator implements ShardingKeyGenerator { private static final String SLANTING_BAR = "/"; - private static TimeService timeService = new TimeService(); + private final TimeService timeService = new TimeService(); @Getter @Setter diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/registry/CuratorZookeeperRegistryCenter.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/registry/CuratorZookeeperRegistryCenter.java index 5c5dd36eb0c4de47e7dc477895598dff9d5c6584..6c8ba91846411e4feffa2510dbbda040f4e12e4e 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/registry/CuratorZookeeperRegistryCenter.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/registry/CuratorZookeeperRegistryCenter.java @@ -277,10 +277,11 @@ public final class CuratorZookeeperRegistryCenter implements RegistryCenter { CloseableUtils.closeQuietly(client); } - /* TODO 等待500ms, cache先关闭再关闭client, 否则会抛异常 - * 因为异步处理, 可能会导致client先关闭而cache还未关闭结束. - * 等待Curator新版本解决这个bug. - * BUG地址:https://issues.apache.org/jira/browse/CURATOR-157 + /* + * TODO waits for 500 ms. Cache closes the client first and then closes the client. Otherwise, an exception will be thrown. + * Because of asynchronous processing, it may lead to client closing first and cache not closing yet. + * Wait for a new version of Curator to fix this bug. + * BUG address: https://issues.apache.org/jira/browse/CURATOR-157 */ private void waitForCacheClose() { try { @@ -311,4 +312,4 @@ public final class CuratorZookeeperRegistryCenter implements RegistryCenter { public void tryRelease() { leafLock.release(); } -} \ No newline at end of file +} diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSegmentKeyGeneratorIT.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSegmentKeyGeneratorIT.java index 847fda26fc1db72bad1193a9d5b375fa5e04f1da..fa2cb96338467154c49e2517ddc026c3a79aaef4 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSegmentKeyGeneratorIT.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSegmentKeyGeneratorIT.java @@ -17,9 +17,9 @@ package org.apache.shardingsphere.integration.test; -import lombok.SneakyThrows; import org.apache.shardingsphere.integration.util.EmbedTestingServer; import org.apache.shardingsphere.orchestration.internal.keygen.LeafSegmentKeyGenerator; +import org.hamcrest.CoreMatchers; import org.junit.BeforeClass; import org.junit.Test; @@ -33,7 +33,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public final class LeafSegmentKeyGeneratorIT { @@ -60,12 +59,11 @@ public final class LeafSegmentKeyGeneratorIT { for (int i = 0; i < 10; i++) { actual.add(leafSegmentKeyGenerator.generateKey()); } - assertThat(actual, is(expected)); + assertThat(actual, CoreMatchers.is(expected)); } @Test - @SneakyThrows - public void assertGenerateKeyWithMultipleThreads() { + public void assertGenerateKeyWithMultipleThreads() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -86,12 +84,11 @@ public final class LeafSegmentKeyGeneratorIT { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDigest() { + public void assertGenerateKeyWithDigest() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -112,12 +109,11 @@ public final class LeafSegmentKeyGeneratorIT { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDefaultStep() { + public void assertGenerateKeyWithDefaultStep() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -137,12 +133,11 @@ public final class LeafSegmentKeyGeneratorIT { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDefaultInitialValue() { + public void assertGenerateKeyWithDefaultInitialValue() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -162,7 +157,7 @@ public final class LeafSegmentKeyGeneratorIT { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } } diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSnowflakeKeyGeneratorIT.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSnowflakeKeyGeneratorIT.java index 75e51fb31e3a2c1b549299dd8acf3492b5f0b464..30869d1ae8e7015d1b72f6aa275930f4ac3453c3 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSnowflakeKeyGeneratorIT.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/test/LeafSnowflakeKeyGeneratorIT.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.integration.test; -import lombok.SneakyThrows; import org.apache.shardingsphere.integration.util.EmbedTestingServer; import org.apache.shardingsphere.orchestration.internal.keygen.LeafSnowflakeKeyGenerator; import org.apache.shardingsphere.orchestration.internal.keygen.TimeService; @@ -25,6 +24,8 @@ import org.apache.shardingsphere.orchestration.internal.keygen.fixture.FixedTime import org.apache.shardingsphere.orchestration.internal.registry.RegistryCenterServiceLoader; import org.apache.shardingsphere.orchestration.reg.api.RegistryCenter; import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration; +import org.apache.shardingsphere.orchestration.util.FieldUtil; +import org.hamcrest.CoreMatchers; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; @@ -40,8 +41,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import static org.apache.shardingsphere.orchestration.util.FieldUtil.setStaticFinalField; -import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; @FixMethodOrder(value = MethodSorters.NAME_ASCENDING) @@ -55,7 +54,6 @@ public final class LeafSnowflakeKeyGeneratorIT { } @Test - @SneakyThrows public void assertGenerateKeyWithSingleThread() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:3181"); @@ -63,17 +61,16 @@ public final class LeafSnowflakeKeyGeneratorIT { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "zookeeper"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1)); List> expected = Arrays.>asList(4198401L, 4198402L, 8392704L, 8392705L, 12587009L, 12587010L, 16781312L, 16781313L, 20975617L, 20975618L); List> actual = new ArrayList<>(); for (int i = 0; i < 10; i++) { actual.add(leafSnowflakeKeyGenerator.generateKey()); } - assertThat(actual, is(expected)); + assertThat(actual, CoreMatchers.is(expected)); } @Test - @SneakyThrows public void assertGenerateKeyWithFixedWorkId() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:3181"); @@ -81,16 +78,15 @@ public final class LeafSnowflakeKeyGeneratorIT { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "zookeeper"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1)); List> expected = Arrays.>asList(4198401L); List> actual = new ArrayList<>(); actual.add(leafSnowflakeKeyGenerator.generateKey()); - assertThat(actual, is(expected)); + assertThat(actual, CoreMatchers.is(expected)); } @Test - @SneakyThrows - public void assertGenerateKeyWithMultipleThreads() { + public void assertGenerateKeyWithMultipleThreads() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -99,7 +95,7 @@ public final class LeafSnowflakeKeyGeneratorIT { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "zookeeper"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1)); Set> actual = new HashSet<>(); int taskNumber = threadNumber << 2; for (int i = 0; i < taskNumber; i++) { @@ -110,12 +106,11 @@ public final class LeafSnowflakeKeyGeneratorIT { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDigest() { + public void assertGenerateKeyWithDigest() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -124,7 +119,7 @@ public final class LeafSnowflakeKeyGeneratorIT { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "zookeeper"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1)); Set> actual = new HashSet<>(); int taskNumber = threadNumber << 2; for (int i = 0; i < taskNumber; i++) { @@ -135,12 +130,11 @@ public final class LeafSnowflakeKeyGeneratorIT { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDefaultMaxTimeDifference() { + public void assertGenerateKeyWithDefaultMaxTimeDifference() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -148,7 +142,7 @@ public final class LeafSnowflakeKeyGeneratorIT { properties.setProperty("serviceId", "testService1"); properties.setProperty("registryCenterType", "zookeeper"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1)); Set> actual = new HashSet<>(); int taskNumber = threadNumber << 2; for (int i = 0; i < taskNumber; i++) { @@ -159,12 +153,10 @@ public final class LeafSnowflakeKeyGeneratorIT { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } - @Test - @SneakyThrows public void generateKeySuccessWithTimeRollback() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:3181"); @@ -177,14 +169,13 @@ public final class LeafSnowflakeKeyGeneratorIT { leafConfiguration.setServerLists("127.0.0.1:3181"); RegistryCenter registryCenter = new RegistryCenterServiceLoader().load(leafConfiguration); TimeService timeService = new FixedTimeService(1); - registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis()+3000)); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",timeService); + registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis() + 3000)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", timeService); leafSnowflakeKeyGenerator.generateKey(); - registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis())); + registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis())); } @Test(expected = IllegalStateException.class) - @SneakyThrows public void generateKeyFailureWithTimeRollback() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:3181"); @@ -197,10 +188,10 @@ public final class LeafSnowflakeKeyGeneratorIT { leafConfiguration.setServerLists("127.0.0.1:3181"); RegistryCenter registryCenter = new RegistryCenterServiceLoader().load(leafConfiguration); TimeService timeService = new FixedTimeService(1); - registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis()+15000)); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",timeService); + registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis() + 15000)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", timeService); leafSnowflakeKeyGenerator.generateKey(); - registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis())); + registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis())); } } diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/util/EmbedTestingServer.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/util/EmbedTestingServer.java index 803563b4a2b9bada6392a6c69e0aaf0d75cb54bb..4ab567597767fc6d966454c0070c7e06bd652d0f 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/util/EmbedTestingServer.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/integration/util/EmbedTestingServer.java @@ -62,4 +62,4 @@ public final class EmbedTestingServer { private static boolean isIgnoredException(final Throwable cause) { return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; } -} \ No newline at end of file +} diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGeneratorTest.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGeneratorTest.java index 2686c2b73dc6ee5ba4ac711b7e7ae6a3f28c9a0c..3c51d019e502b58380d1960650e24c7c1407ca44 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGeneratorTest.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGeneratorTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.orchestration.internal.keygen; -import lombok.SneakyThrows; +import org.hamcrest.CoreMatchers; import org.junit.Test; import java.util.Properties; @@ -30,7 +30,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public final class LeafSegmentKeyGeneratorTest { @@ -39,7 +38,7 @@ public final class LeafSegmentKeyGeneratorTest { @Test public void assertGetProperties() { - assertThat(leafSegmentKeyGenerator.getProperties().entrySet().size(), is(0)); + assertThat(leafSegmentKeyGenerator.getProperties().entrySet().size(), CoreMatchers.is(0)); } @Test @@ -47,7 +46,7 @@ public final class LeafSegmentKeyGeneratorTest { Properties properties = new Properties(); properties.setProperty("key1", "value1"); leafSegmentKeyGenerator.setProperties(properties); - assertThat(leafSegmentKeyGenerator.getProperties().get("key1"), is((Object) "value1")); + assertThat(leafSegmentKeyGenerator.getProperties().get("key1"), CoreMatchers.is((Object) "value1")); } @Test @@ -65,12 +64,11 @@ public final class LeafSegmentKeyGeneratorTest { for (int i = 0; i < 10; i++) { actual.add(leafSegmentKeyGenerator.generateKey()); } - assertThat(actual, is(expected)); + assertThat(actual, CoreMatchers.is(expected)); } @Test - @SneakyThrows - public void assertGenerateKeyWithMultipleThreads() { + public void assertGenerateKeyWithMultipleThreads() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -91,12 +89,11 @@ public final class LeafSegmentKeyGeneratorTest { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDigest() { + public void assertGenerateKeyWithDigest() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -117,12 +114,11 @@ public final class LeafSegmentKeyGeneratorTest { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDefaultStep() { + public void assertGenerateKeyWithDefaultStep() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -142,12 +138,11 @@ public final class LeafSegmentKeyGeneratorTest { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDefaultInitialValue() { + public void assertGenerateKeyWithDefaultInitialValue() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -167,7 +162,7 @@ public final class LeafSegmentKeyGeneratorTest { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test(expected = IllegalArgumentException.class) diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGeneratorTest.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGeneratorTest.java index 3581f12ae3870a8acefd7b4234e0833807922aba..f52ea2f31b5b975964e3bd653222dca3d05187f3 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGeneratorTest.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSnowflakeKeyGeneratorTest.java @@ -17,9 +17,10 @@ package org.apache.shardingsphere.orchestration.internal.keygen; -import lombok.SneakyThrows; import org.apache.shardingsphere.orchestration.internal.keygen.fixture.FixedTimeService; import org.apache.shardingsphere.orchestration.internal.keygen.fixture.PreviousTimeService; +import org.apache.shardingsphere.orchestration.util.FieldUtil; +import org.hamcrest.CoreMatchers; import org.junit.Test; import java.util.ArrayList; @@ -32,8 +33,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import static org.apache.shardingsphere.orchestration.util.FieldUtil.setStaticFinalField; -import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public final class LeafSnowflakeKeyGeneratorTest { @@ -42,7 +41,7 @@ public final class LeafSnowflakeKeyGeneratorTest { @Test public void assertGetProperties() { - assertThat(leafSnowflakeKeyGenerator.getProperties().entrySet().size(), is(0)); + assertThat(leafSnowflakeKeyGenerator.getProperties().entrySet().size(), CoreMatchers.is(0)); } @Test @@ -50,11 +49,10 @@ public final class LeafSnowflakeKeyGeneratorTest { Properties properties = new Properties(); properties.setProperty("key1", "value1"); leafSnowflakeKeyGenerator.setProperties(properties); - assertThat(leafSnowflakeKeyGenerator.getProperties().get("key1"), is((Object) "value1")); + assertThat(leafSnowflakeKeyGenerator.getProperties().get("key1"), CoreMatchers.is((Object) "value1")); } @Test - @SneakyThrows public void assertGenerateKeyWithSingleThread() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:2181"); @@ -62,17 +60,16 @@ public final class LeafSnowflakeKeyGeneratorTest { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "ForthTestRegistryCenter"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1)); List> expected = Arrays.>asList(4198401L, 4198402L, 8392704L, 8392705L, 12587009L, 12587010L, 16781312L, 16781313L, 20975617L, 20975618L); List> actual = new ArrayList<>(); for (int i = 0; i < 10; i++) { actual.add(leafSnowflakeKeyGenerator.generateKey()); } - assertThat(actual, is(expected)); + assertThat(actual, CoreMatchers.is(expected)); } @Test - @SneakyThrows public void assertGenerateKeyWithFixedWorkId() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:2181"); @@ -80,15 +77,14 @@ public final class LeafSnowflakeKeyGeneratorTest { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "ForthTestRegistryCenter"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1)); List> expected = Arrays.>asList(4198401L); List> actual = new ArrayList<>(); actual.add(leafSnowflakeKeyGenerator.generateKey()); - assertThat(actual, is(expected)); + assertThat(actual, CoreMatchers.is(expected)); } @Test(expected = IllegalStateException.class) - @SneakyThrows public void generateKeyFailureWithTimeRollback() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:2181"); @@ -96,12 +92,11 @@ public final class LeafSnowflakeKeyGeneratorTest { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "FifthTestRegistryCenter"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new PreviousTimeService(15000)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new PreviousTimeService(15000)); leafSnowflakeKeyGenerator.generateKey(); } @Test - @SneakyThrows public void generateKeySuccessWithTimeRollback() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:2181"); @@ -109,13 +104,12 @@ public final class LeafSnowflakeKeyGeneratorTest { properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "FifthTestRegistryCenter"); leafSnowflakeKeyGenerator.setProperties(properties); - setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new PreviousTimeService(3000)); + FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new PreviousTimeService(3000)); leafSnowflakeKeyGenerator.generateKey(); } @Test - @SneakyThrows - public void assertGenerateKeyWithMultipleThreads() { + public void assertGenerateKeyWithMultipleThreads() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -134,12 +128,11 @@ public final class LeafSnowflakeKeyGeneratorTest { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDigest() { + public void assertGenerateKeyWithDigest() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -158,12 +151,11 @@ public final class LeafSnowflakeKeyGeneratorTest { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test - @SneakyThrows - public void assertGenerateKeyWithDefaultMaxTimeDifference() { + public void assertGenerateKeyWithDefaultMaxTimeDifference() throws Exception { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); Properties properties = new Properties(); @@ -181,7 +173,7 @@ public final class LeafSnowflakeKeyGeneratorTest { } }).get()); } - assertThat(actual.size(), is(taskNumber)); + assertThat(actual.size(), CoreMatchers.is(taskNumber)); } @Test(expected = IllegalArgumentException.class) @@ -226,7 +218,6 @@ public final class LeafSnowflakeKeyGeneratorTest { leafSnowflakeKeyGenerator.generateKey(); } - @Test(expected = IllegalArgumentException.class) public void assertSetServiceIdFailureWithSlantingBar() { Properties properties = new Properties(); diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/fixture/PreviousTimeService.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/fixture/PreviousTimeService.java index 3e3dad94cb6eafa79cbeadc7a544a72377767f26..05084f2812ad1e1de43dbd38f34820cf9081ca23 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/fixture/PreviousTimeService.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/fixture/PreviousTimeService.java @@ -27,7 +27,7 @@ public final class PreviousTimeService extends TimeService { @Override public long getCurrentMillis() { - long result = System.currentTimeMillis()-timeDifference; + long result = System.currentTimeMillis() - timeDifference; return result; } } diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/registry/fixture/FifthTestRegistryCenter.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/registry/fixture/FifthTestRegistryCenter.java index 16709d660e8dd599624ffe299fb4ff3f10852893..c3b6cd8b3ee242470db0ae75fb755b31a3c00b6c 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/registry/fixture/FifthTestRegistryCenter.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/registry/fixture/FifthTestRegistryCenter.java @@ -40,18 +40,11 @@ public final class FifthTestRegistryCenter implements RegistryCenter { private ReentrantLock lock = new ReentrantLock(); - private static final String PARENT_NODE = "/leaf_snowflake"; - - private static final String TIME_NODE = "/time"; - - private static final String CURRENT_MAX_WORK_ID_NODE = "/current-max-work-id"; - - private static final String WORK_ID_NODE = "/work-id"; @Override public void init(final RegistryCenterConfiguration config) { - keys.put("/leaf_snowflake/specialService/time",String.valueOf(System.currentTimeMillis())); - keys.put("/leaf_snowflake/specialService/work-id","1"); - keys.put("/leaf_snowflake/current-max-work-id","1"); + keys.put("/leaf_snowflake/specialService/time", String.valueOf(System.currentTimeMillis())); + keys.put("/leaf_snowflake/specialService/work-id", "1"); + keys.put("/leaf_snowflake/current-max-work-id", "1"); } @Override diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/util/FieldUtil.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/util/FieldUtil.java index 47588c10a3815cee2ed6c3fd79bf597b68969133..535bb7cf8334d44bd7b3622de7ca2cd31b6590d4 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/util/FieldUtil.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/util/FieldUtil.java @@ -65,16 +65,15 @@ public final class FieldUtil { * @param target target to be settled * @param fieldName field name to be settled * @param fieldValue field value to be settled - * @return field value */ @SneakyThrows - public static void setStaticFinalField(final Object target, final String fieldName, final Object fieldValue){ + public static void setStaticFinalField(final Object target, final String fieldName, final Object fieldValue) { Field timeServiceField = target.getClass().getDeclaredField(fieldName); timeServiceField.setAccessible(true); Field modifiers = timeServiceField.getClass().getDeclaredField("modifiers"); modifiers.setAccessible(true); modifiers.setInt(timeServiceField, timeServiceField.getModifiers() & ~Modifier.FINAL); - timeServiceField.set(target,fieldValue); + timeServiceField.set(target, fieldValue); modifiers.setInt(timeServiceField, timeServiceField.getModifiers() & ~Modifier.FINAL); } }