You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2024/06/26 08:29:46 UTC

(shardingsphere) branch master updated: Add more test cases on DriverStateContextTest (#31869)

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

sunnianjun 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 e26c1c88131 Add more test cases on DriverStateContextTest (#31869)
e26c1c88131 is described below

commit e26c1c881318a43fa77370ceb79c46a7710a65fd
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Jun 26 16:29:41 2024 +0800

    Add more test cases on DriverStateContextTest (#31869)
    
    * Add shardingsphere-test-it-spi module
    
    * Fix SPIMatchedCheckIT
    
    * Fix SPIMatchedCheckIT
    
    * Fix SPIMatchedCheckIT
    
    * Add more test cases on DriverStateContextTest
---
 .../driver/state/DriverStateContextTest.java       | 59 +++++++---------------
 1 file changed, 17 insertions(+), 42 deletions(-)

diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
index e17a4b25546..2840efce9a5 100644
--- a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
+++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
@@ -18,63 +18,38 @@
 package org.apache.shardingsphere.driver.state;
 
 import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.driver.state.circuit.connection.CircuitBreakerConnection;
 import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
-import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.state.instance.InstanceState;
 import org.apache.shardingsphere.infra.state.instance.InstanceStateContext;
-import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
-import org.apache.shardingsphere.transaction.rule.TransactionRule;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Answers;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import java.sql.Connection;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-@ExtendWith(MockitoExtension.class)
 class DriverStateContextTest {
     
-    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
-    private ContextManager contextManager;
-    
-    @BeforeEach
-    void setUp() {
-        Map<String, ShardingSphereDatabase> databases = mockDatabases();
-        TransactionRule transactionRule = mock(TransactionRule.class);
-        RuleMetaData globalRuleMetaData = new RuleMetaData(Collections.singleton(transactionRule));
-        MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(
-                mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties())));
-        when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
-        when(contextManager.getComputeNodeInstanceContext().getInstance().getState()).thenReturn(new InstanceStateContext());
+    @Test
+    void assertGetConnectionWithOkState() {
+        ContextManager contextManager = mockContextManager(InstanceState.OK);
+        assertThat(DriverStateContext.getConnection(DefaultDatabase.LOGIC_NAME, contextManager), instanceOf(ShardingSphereConnection.class));
     }
     
-    private Map<String, ShardingSphereDatabase> mockDatabases() {
-        ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS);
-        when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
-        return Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database);
+    @Test
+    void assertGetConnectionWithCircuitBreakState() {
+        ContextManager contextManager = mockContextManager(InstanceState.CIRCUIT_BREAK);
+        assertThat(DriverStateContext.getConnection(DefaultDatabase.LOGIC_NAME, contextManager), instanceOf(CircuitBreakerConnection.class));
     }
     
-    @Test
-    void assertGetConnectionWithOkState() {
-        Connection actual = DriverStateContext.getConnection(DefaultDatabase.LOGIC_NAME, contextManager);
-        assertThat(actual, instanceOf(ShardingSphereConnection.class));
+    private ContextManager mockContextManager(final InstanceState instanceState) {
+        ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+        InstanceStateContext instanceStateContext = new InstanceStateContext();
+        instanceStateContext.switchState(instanceState);
+        when(result.getComputeNodeInstanceContext().getInstance().getState()).thenReturn(instanceStateContext);
+        return result;
     }
 }