This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 c6c0c7b67b8 Use mock to instead of WorkerIdGeneratorFixture (#31321)
c6c0c7b67b8 is described below
commit c6c0c7b67b8d2f5369a466b6fb20a3948840f0d0
Author: Liang Zhang <[email protected]>
AuthorDate: Tue May 21 11:19:15 2024 +0800
Use mock to instead of WorkerIdGeneratorFixture (#31321)
* Use mock to instead of WorkerIdGeneratorFixture
* Use mock to instead of WorkerIdGeneratorFixture
---
.../SnowflakeKeyGenerateAlgorithmTest.java | 23 --------------
.../fixture/WorkerIdGeneratorFixture.java | 36 ----------------------
.../instance/ComputeNodeInstanceContextTest.java | 33 ++++++++++----------
.../instance/fixture/WorkerIdGeneratorFixture.java | 34 --------------------
4 files changed, 16 insertions(+), 110 deletions(-)
diff --git
a/infra/algorithm/key-generator/type/snowflake/src/test/java/org/apache/shardingsphere/infra/algorithm/keygen/snowflake/SnowflakeKeyGenerateAlgorithmTest.java
b/infra/algorithm/key-generator/type/snowflake/src/test/java/org/apache/shardingsphere/infra/algorithm/keygen/snowflake/SnowflakeKeyGenerateAlgorithmTest.java
index 3a9d4287ae5..9f536e57fff 100644
---
a/infra/algorithm/key-generator/type/snowflake/src/test/java/org/apache/shardingsphere/infra/algorithm/keygen/snowflake/SnowflakeKeyGenerateAlgorithmTest.java
+++
b/infra/algorithm/key-generator/type/snowflake/src/test/java/org/apache/shardingsphere/infra/algorithm/keygen/snowflake/SnowflakeKeyGenerateAlgorithmTest.java
@@ -23,16 +23,9 @@ import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmExecute
import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import
org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
import
org.apache.shardingsphere.infra.algorithm.keygen.snowflake.fixture.FixedTimeService;
-import
org.apache.shardingsphere.infra.algorithm.keygen.snowflake.fixture.WorkerIdGeneratorFixture;
-import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import
org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContextAware;
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
-import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.awaitility.Awaitility;
@@ -199,14 +192,6 @@ class SnowflakeKeyGenerateAlgorithmTest {
Plugins.getMemberAccessor().set(SnowflakeKeyGenerateAlgorithm.class.getDeclaredField("sequence"),
algorithm, new AtomicLong(value.longValue()));
}
- @Test
- void assertSetWorkerIdFailureWhenNegative() {
- SnowflakeKeyGenerateAlgorithm algorithm =
(SnowflakeKeyGenerateAlgorithm)
TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE");
- ComputeNodeInstanceContext computeNodeInstanceContext = new
ComputeNodeInstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(-1),
- new ModeConfiguration("Standalone", null),
mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
- assertThrows(IllegalArgumentException.class, () ->
algorithm.setComputeNodeInstanceContext(computeNodeInstanceContext));
- }
-
@Test
void assertSetMaxVibrationOffsetFailureWhenNegative() {
assertThrows(AlgorithmInitializationException.class,
@@ -214,14 +199,6 @@ class SnowflakeKeyGenerateAlgorithmTest {
.generateKeys(mock(AlgorithmSQLContext.class), 1));
}
- @Test
- void assertSetWorkerIdFailureWhenOutOfRange() {
- SnowflakeKeyGenerateAlgorithm algorithm =
(SnowflakeKeyGenerateAlgorithm)
TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE");
- ComputeNodeInstanceContext computeNodeInstanceContext = new
ComputeNodeInstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(Integer.MIN_VALUE),
- new ModeConfiguration("Standalone", null),
mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
- assertThrows(IllegalArgumentException.class, () ->
algorithm.setComputeNodeInstanceContext(computeNodeInstanceContext));
- }
-
@Test
void assertSetMaxVibrationOffsetFailureWhenOutOfRange() {
assertThrows(AlgorithmInitializationException.class,
diff --git
a/infra/algorithm/key-generator/type/snowflake/src/test/java/org/apache/shardingsphere/infra/algorithm/keygen/snowflake/fixture/WorkerIdGeneratorFixture.java
b/infra/algorithm/key-generator/type/snowflake/src/test/java/org/apache/shardingsphere/infra/algorithm/keygen/snowflake/fixture/WorkerIdGeneratorFixture.java
deleted file mode 100644
index 718b654803b..00000000000
---
a/infra/algorithm/key-generator/type/snowflake/src/test/java/org/apache/shardingsphere/infra/algorithm/keygen/snowflake/fixture/WorkerIdGeneratorFixture.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.infra.algorithm.keygen.snowflake.fixture;
-
-import com.google.common.base.Preconditions;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
-
-import java.util.Properties;
-
-@RequiredArgsConstructor
-public final class WorkerIdGeneratorFixture implements WorkerIdGenerator {
-
- private final int fixtureWorkerId;
-
- @Override
- public int generate(final Properties props) {
- Preconditions.checkArgument(fixtureWorkerId >= 0L && fixtureWorkerId
<= MAX_WORKER_ID, "Illegal worker id.");
- return fixtureWorkerId;
- }
-}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java
index aa3ca88ed58..029697f7a47 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java
@@ -18,9 +18,9 @@
package org.apache.shardingsphere.infra.instance;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import
org.apache.shardingsphere.infra.instance.fixture.WorkerIdGeneratorFixture;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
+import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.state.instance.InstanceState;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
@@ -52,8 +52,8 @@ class ComputeNodeInstanceContextTest {
void assertUpdateInstanceStatus() {
InstanceMetaData instanceMetaData = mock(InstanceMetaData.class);
when(instanceMetaData.getId()).thenReturn("foo_instance_id");
- ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(new ComputeNodeInstance(instanceMetaData),
- new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig,
modeContextManager, lockContext, eventBusContext);
+ ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(
+ new ComputeNodeInstance(instanceMetaData),
mock(WorkerIdGenerator.class), modeConfig, modeContextManager, lockContext,
eventBusContext);
InstanceState actual =
context.getInstance().getState().getCurrentState();
assertThat(actual, is(InstanceState.OK));
context.updateStatus(instanceMetaData.getId(),
InstanceState.CIRCUIT_BREAK.name());
@@ -68,24 +68,23 @@ class ComputeNodeInstanceContextTest {
void assertGetWorkerId() {
ComputeNodeInstance computeNodeInstance =
mock(ComputeNodeInstance.class);
when(computeNodeInstance.getWorkerId()).thenReturn(0);
- ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(
- computeNodeInstance, new
WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, modeContextManager,
lockContext, eventBusContext);
+ ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(computeNodeInstance, mock(WorkerIdGenerator.class),
modeConfig, modeContextManager, lockContext, eventBusContext);
assertThat(context.getWorkerId(), is(0));
}
@Test
void assertGenerateWorkerId() {
ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(
- new ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, modeContextManager,
lockContext, eventBusContext);
- assertThat(context.generateWorkerId(new Properties()),
is(Integer.MIN_VALUE));
+ new ComputeNodeInstance(mock(InstanceMetaData.class)),
mock(WorkerIdGenerator.class), modeConfig, modeContextManager, lockContext,
eventBusContext);
+ assertThat(context.generateWorkerId(new Properties()), is(0));
}
@Test
void assertUpdateLabel() {
InstanceMetaData instanceMetaData = mock(InstanceMetaData.class);
when(instanceMetaData.getId()).thenReturn("foo_instance_id");
- ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(new ComputeNodeInstance(instanceMetaData), new
WorkerIdGeneratorFixture(Integer.MIN_VALUE),
- modeConfig, modeContextManager, lockContext, eventBusContext);
+ ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(
+ new ComputeNodeInstance(instanceMetaData),
mock(WorkerIdGenerator.class), modeConfig, modeContextManager, lockContext,
eventBusContext);
Collection<String> expected = Arrays.asList("label_1", "label_2");
context.updateLabel("foo_instance_id", expected);
Collection<String> actual = context.getInstance().getLabels();
@@ -95,31 +94,31 @@ class ComputeNodeInstanceContextTest {
@Test
void assertGetInstance() {
ComputeNodeInstance expected = new
ComputeNodeInstance(mock(InstanceMetaData.class));
- ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(expected, new
WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, modeContextManager,
lockContext, eventBusContext);
+ ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(expected, mock(WorkerIdGenerator.class), modeConfig,
modeContextManager, lockContext, eventBusContext);
ComputeNodeInstance actual = context.getInstance();
assertThat(actual, is(expected));
}
@Test
void assertGetState() {
- ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig,
- modeContextManager, lockContext, eventBusContext);
+ ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(
+ new ComputeNodeInstance(mock(InstanceMetaData.class)),
mock(WorkerIdGenerator.class), modeConfig, modeContextManager, lockContext,
eventBusContext);
assertNotNull(context.getInstance().getState());
}
@Test
void assertGetModeConfiguration() {
- ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig,
- modeContextManager, lockContext, eventBusContext);
+ ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(
+ new ComputeNodeInstance(mock(InstanceMetaData.class)),
mock(WorkerIdGenerator.class), modeConfig, modeContextManager, lockContext,
eventBusContext);
assertThat(context.getModeConfiguration(), is(modeConfig));
}
@Test
void assertIsCluster() {
- ComputeNodeInstanceContext context = new
ComputeNodeInstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig,
- modeContextManager, lockContext, eventBusContext);
+ ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(
+ new ComputeNodeInstance(mock(InstanceMetaData.class)),
mock(WorkerIdGenerator.class), modeConfig, modeContextManager, lockContext,
eventBusContext);
assertFalse(context.isCluster());
- ComputeNodeInstanceContext clusterContext = new
ComputeNodeInstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(Integer.MIN_VALUE),
+ ComputeNodeInstanceContext clusterContext = new
ComputeNodeInstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)),
mock(WorkerIdGenerator.class),
new ModeConfiguration("Cluster", null), modeContextManager,
lockContext, eventBusContext);
assertTrue(clusterContext.isCluster());
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/fixture/WorkerIdGeneratorFixture.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/fixture/WorkerIdGeneratorFixture.java
deleted file mode 100644
index d0a34b75dc9..00000000000
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/fixture/WorkerIdGeneratorFixture.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.infra.instance.fixture;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
-
-import java.util.Properties;
-
-@RequiredArgsConstructor
-public final class WorkerIdGeneratorFixture implements WorkerIdGenerator {
-
- private final int fixtureWorkerId;
-
- @Override
- public int generate(final Properties props) {
- return fixtureWorkerId;
- }
-}