You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2024/09/20 19:04:30 UTC

(shardingsphere) branch master updated: Add test cases on GlobalClockRule (#32943)

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

zhangliang 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 32cc8541708 Add test cases on GlobalClockRule (#32943)
32cc8541708 is described below

commit 32cc85417084fbf5e8a60b18394429032d38a0d7
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Sep 21 03:04:24 2024 +0800

    Add test cases on GlobalClockRule (#32943)
    
    * Add test cases on GlobalClockRule
    
    * Add test cases on GlobalClockRuleBuilder
    
    * DefaultGlobalClockRuleConfigurationBuilder
    
    * Refactor OpenGaussGlobalClockTransactionExecutor
    
    * Refactor OpenGaussGlobalClockTransactionExecutor
    
    * Refactor OpenGaussGlobalClockTransactionExecutor
---
 .../OpenGaussGlobalClockTransactionExecutor.java   | 12 +++---
 ...alclock.executor.GlobalClockTransactionExecutor |  2 +-
 .../fixture/FixtureGlobalClockProvider.java        | 38 +++++++++++++++++
 .../globalclock/rule/GlobalClockRuleTest.java      | 39 ++++++++++++++++++
 ...ultGlobalClockRuleConfigurationBuilderTest.java | 47 ++++++++++++++++++++++
 .../rule/builder/GlobalClockRuleBuilderTest.java   | 43 ++++++++++++++++++++
 ...phere.globalclock.provider.GlobalClockProvider} |  2 +-
 7 files changed, 176 insertions(+), 7 deletions(-)

diff --git a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/OpenGaussGlobalClockTransactionExecutor.java b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/type/OpenGaussGlobalClockTransactionExecutor.java
similarity index 81%
rename from kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/OpenGaussGlobalClockTransactionExecutor.java
rename to kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/type/OpenGaussGlobalClockTransactionExecutor.java
index eb8ede747fd..257fc64f763 100644
--- a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/OpenGaussGlobalClockTransactionExecutor.java
+++ b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/type/OpenGaussGlobalClockTransactionExecutor.java
@@ -15,7 +15,9 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.globalclock.executor;
+package org.apache.shardingsphere.globalclock.executor.type;
+
+import org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor;
 
 import java.sql.Connection;
 import java.sql.SQLException;
@@ -29,20 +31,20 @@ public final class OpenGaussGlobalClockTransactionExecutor implements GlobalCloc
     
     @Override
     public void sendSnapshotTimestamp(final Collection<Connection> connections, final long globalTimestamp) throws SQLException {
-        String setSnapshotTimestampSQL = String.format("SELECT %d AS SETSNAPSHOTCSN", globalTimestamp);
+        String sql = String.format("SELECT %d AS SETSNAPSHOTCSN", globalTimestamp);
         for (Connection each : connections) {
             try (Statement statement = each.createStatement()) {
-                statement.execute(setSnapshotTimestampSQL);
+                statement.execute(sql);
             }
         }
     }
     
     @Override
     public void sendCommitTimestamp(final Collection<Connection> connections, final long globalTimestamp) throws SQLException {
-        String setCommitTimestampSQL = String.format("SELECT %d AS SETCOMMITCSN", globalTimestamp);
+        String sql = String.format("SELECT %d AS SETCOMMITCSN", globalTimestamp);
         for (Connection each : connections) {
             try (Statement statement = each.createStatement()) {
-                statement.execute(setCommitTimestampSQL);
+                statement.execute(sql);
             }
         }
     }
diff --git a/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor b/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor
index 64e9e3f8bca..dd53b23033d 100644
--- a/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor
+++ b/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.globalclock.executor.OpenGaussGlobalClockTransactionExecutor
+org.apache.shardingsphere.globalclock.executor.type.OpenGaussGlobalClockTransactionExecutor
diff --git a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/fixture/FixtureGlobalClockProvider.java b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/fixture/FixtureGlobalClockProvider.java
new file mode 100644
index 00000000000..9b0b9a8a012
--- /dev/null
+++ b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/fixture/FixtureGlobalClockProvider.java
@@ -0,0 +1,38 @@
+/*
+ * 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.fixture;
+
+import org.apache.shardingsphere.globalclock.provider.GlobalClockProvider;
+
+public final class FixtureGlobalClockProvider implements GlobalClockProvider {
+    
+    @Override
+    public long getCurrentTimestamp() {
+        return 0;
+    }
+    
+    @Override
+    public long getNextTimestamp() {
+        return 0;
+    }
+    
+    @Override
+    public Object getType() {
+        return "FIXTURE.FIXTURE";
+    }
+}
diff --git a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/GlobalClockRuleTest.java b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/GlobalClockRuleTest.java
new file mode 100644
index 00000000000..2d4ec0ddb70
--- /dev/null
+++ b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/GlobalClockRuleTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.rule;
+
+import org.apache.shardingsphere.globalclock.config.GlobalClockRuleConfiguration;
+import org.junit.jupiter.api.Test;
+
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class GlobalClockRuleTest {
+    
+    @Test
+    void assertGetGlobalClockProviderWhenDisabled() {
+        assertFalse(new GlobalClockRule(new GlobalClockRuleConfiguration("FIXTURE", "FIXTURE", false, new Properties())).getGlobalClockProvider().isPresent());
+    }
+    
+    @Test
+    void assertGetGlobalClockProviderWhenEnabled() {
+        assertTrue(new GlobalClockRule(new GlobalClockRuleConfiguration("FIXTURE", "FIXTURE", true, new Properties())).getGlobalClockProvider().isPresent());
+    }
+}
diff --git a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/builder/DefaultGlobalClockRuleConfigurationBuilderTest.java b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/builder/DefaultGlobalClockRuleConfigurationBuilderTest.java
new file mode 100644
index 00000000000..62f5dcadae1
--- /dev/null
+++ b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/builder/DefaultGlobalClockRuleConfigurationBuilderTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.rule.builder;
+
+import org.apache.shardingsphere.globalclock.config.GlobalClockRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DefaultGlobalClockRuleConfigurationBuilderTest {
+    
+    @SuppressWarnings("rawtypes")
+    @Test
+    void assertBuild() {
+        GlobalClockRuleBuilder builder = new GlobalClockRuleBuilder();
+        Map<GlobalRuleBuilder, DefaultGlobalRuleConfigurationBuilder> builders = OrderedSPILoader.getServices(DefaultGlobalRuleConfigurationBuilder.class, Collections.singleton(builder));
+        GlobalClockRuleConfiguration actual = (GlobalClockRuleConfiguration) builders.get(builder).build();
+        assertThat(actual.getType(), is("TSO"));
+        assertThat(actual.getProvider(), is("local"));
+        assertFalse(actual.isEnabled());
+        assertTrue(actual.getProps().isEmpty());
+    }
+}
diff --git a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/builder/GlobalClockRuleBuilderTest.java b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/builder/GlobalClockRuleBuilderTest.java
new file mode 100644
index 00000000000..b4fa1a01cf0
--- /dev/null
+++ b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/rule/builder/GlobalClockRuleBuilderTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.rule.builder;
+
+import org.apache.shardingsphere.globalclock.config.GlobalClockRuleConfiguration;
+import org.apache.shardingsphere.globalclock.rule.GlobalClockRule;
+import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class GlobalClockRuleBuilderTest {
+    
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    @Test
+    void assertBuild() {
+        GlobalClockRuleConfiguration ruleConfig = new GlobalClockRuleConfiguration("FIXTURE", "FIXTURE", false, new Properties());
+        Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders = OrderedSPILoader.getServices(GlobalRuleBuilder.class, Collections.singleton(ruleConfig));
+        assertThat(builders.get(ruleConfig).build(ruleConfig, Collections.emptyMap(), null), instanceOf(GlobalClockRule.class));
+    }
+}
diff --git a/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor b/kernel/global-clock/core/src/test/resources/META-INF/services/org.apache.shardingsphere.globalclock.provider.GlobalClockProvider
similarity index 90%
copy from kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor
copy to kernel/global-clock/core/src/test/resources/META-INF/services/org.apache.shardingsphere.globalclock.provider.GlobalClockProvider
index 64e9e3f8bca..fe0ae751259 100644
--- a/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.globalclock.executor.GlobalClockTransactionExecutor
+++ b/kernel/global-clock/core/src/test/resources/META-INF/services/org.apache.shardingsphere.globalclock.provider.GlobalClockProvider
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.globalclock.executor.OpenGaussGlobalClockTransactionExecutor
+org.apache.shardingsphere.globalclock.fixture.FixtureGlobalClockProvider