This is an automated email from the ASF dual-hosted git repository.
chengzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 6760535b95f Add test cases on LocalTSOProvider (#32951)
6760535b95f is described below
commit 6760535b95fc5c51b92508311908db34f772b281
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Sep 21 21:47:27 2024 +0800
Add test cases on LocalTSOProvider (#32951)
* Add test cases on LocalTSOProvider
* Add test cases on LocalTSOProvider
---
.../tso/provider/local/LocalTSOProviderTest.java | 49 ++++++++++++++++++++++
.../type/tso/provider/redis/RedisTSOProvider.java | 20 ++++-----
2 files changed, 57 insertions(+), 12 deletions(-)
diff --git
a/kernel/global-clock/type/tso/provider/local/src/test/java/org/apache/shardingsphere/globalclock/type/tso/provider/local/LocalTSOProviderTest.java
b/kernel/global-clock/type/tso/provider/local/src/test/java/org/apache/shardingsphere/globalclock/type/tso/provider/local/LocalTSOProviderTest.java
new file mode 100644
index 00000000000..5008bbc1412
--- /dev/null
+++
b/kernel/global-clock/type/tso/provider/local/src/test/java/org/apache/shardingsphere/globalclock/type/tso/provider/local/LocalTSOProviderTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.globalclock.type.tso.provider.local;
+
+import org.apache.shardingsphere.globalclock.provider.GlobalClockProvider;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class LocalTSOProviderTest {
+
+ private GlobalClockProvider tsoProvider;
+
+ @BeforeEach
+ void setUp() {
+ tsoProvider = TypedSPILoader.getService(GlobalClockProvider.class,
"TSO.local");
+ }
+
+ @Test
+ void assertGetCurrentTimestamp() {
+ assertThat(tsoProvider.getCurrentTimestamp(), is(0L));
+ assertThat(tsoProvider.getNextTimestamp(), is(1L));
+ assertThat(tsoProvider.getCurrentTimestamp(), is(1L));
+ }
+
+ @Test
+ void assertGetInstanceByDefault() {
+ assertThat(TypedSPILoader.getService(GlobalClockProvider.class, null),
instanceOf(LocalTSOProvider.class));
+ }
+}
diff --git
a/kernel/global-clock/type/tso/provider/redis/src/main/java/org/apache/shardingsphere/globalclock/type/tso/provider/redis/RedisTSOProvider.java
b/kernel/global-clock/type/tso/provider/redis/src/main/java/org/apache/shardingsphere/globalclock/type/tso/provider/redis/RedisTSOProvider.java
index a06a8ec52f5..71fe5cd3224 100644
---
a/kernel/global-clock/type/tso/provider/redis/src/main/java/org/apache/shardingsphere/globalclock/type/tso/provider/redis/RedisTSOProvider.java
+++
b/kernel/global-clock/type/tso/provider/redis/src/main/java/org/apache/shardingsphere/globalclock/type/tso/provider/redis/RedisTSOProvider.java
@@ -41,25 +41,25 @@ public final class RedisTSOProvider implements TSOProvider {
private JedisPool jedisPool;
- private Properties props;
-
@Override
public void init(final Properties props) {
- this.props = props;
if (initialized.compareAndSet(false, true)) {
- createJedisPool();
+ createJedisPool(props);
checkJedisPool();
initCSN();
}
}
- private void createJedisPool() {
+ private void createJedisPool(final Properties props) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.parseInt(getValue(props,
RedisTSOPropertyKey.MAX_IDLE)));
config.setMaxTotal(Integer.parseInt(getValue(props,
RedisTSOPropertyKey.MAX_TOTAL)));
- jedisPool = new JedisPool(config, getValue(props,
RedisTSOPropertyKey.HOST),
- Integer.parseInt(getValue(props, RedisTSOPropertyKey.PORT)),
Integer.parseInt(getValue(props, RedisTSOPropertyKey.TIMEOUT_INTERVAL)),
- getValue(props, RedisTSOPropertyKey.PASSWORD));
+ jedisPool = new JedisPool(config, getValue(props,
RedisTSOPropertyKey.HOST), Integer.parseInt(getValue(props,
RedisTSOPropertyKey.PORT)),
+ Integer.parseInt(getValue(props,
RedisTSOPropertyKey.TIMEOUT_INTERVAL)), getValue(props,
RedisTSOPropertyKey.PASSWORD));
+ }
+
+ private String getValue(final Properties props, final RedisTSOPropertyKey
propertyKey) {
+ return props.containsKey(propertyKey.getKey()) ?
props.getProperty(propertyKey.getKey()) : propertyKey.getDefaultValue();
}
private void checkJedisPool() {
@@ -77,10 +77,6 @@ public final class RedisTSOProvider implements TSOProvider {
}
}
- private String getValue(final Properties props, final RedisTSOPropertyKey
propertyKey) {
- return props.containsKey(propertyKey.getKey()) ?
props.getProperty(propertyKey.getKey()) : propertyKey.getDefaultValue();
- }
-
@Override
public long getCurrentTimestamp() {
try (Jedis jedis = jedisPool.getResource()) {