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 9f1db1b2f93 Remove ProxyContextRestorer on front-opengauss module
(#24310)
9f1db1b2f93 is described below
commit 9f1db1b2f936a0a8ee5361f55a05a2c2957fbf55
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 23 11:40:29 2023 +0800
Remove ProxyContextRestorer on front-opengauss module (#24310)
* Remove ProxyContextRestorer on front-opengauss module
* Remove ProxyContextRestorer on front-opengauss module
---
.../frontend/state/impl/OKProxyStateTest.java | 6 +-
.../frontend/opengauss/ProxyContextRestorer.java | 38 ---------
.../OpenGaussAuthenticationEngineTest.java | 30 +++++---
.../bind/OpenGaussComBatchBindExecutorTest.java | 90 ++++++++++++----------
4 files changed, 76 insertions(+), 88 deletions(-)
diff --git
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java
b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java
index 7bee61f5927..e4947001838 100644
---
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java
+++
b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java
@@ -98,7 +98,8 @@ public final class OKProxyStateTest {
public void assertExecuteWithProxyBackendExecutorSuitableForOLTP() {
ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
when(contextManager.getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
-
when(contextManager.getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLTP);
+
when(contextManager.getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(
+
ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLTP);
EventExecutor eventExecutor = mock(EventExecutor.class);
when(context.executor()).thenReturn(eventExecutor);
try (MockedStatic<ProxyContext> proxyContext =
mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
@@ -112,7 +113,8 @@ public final class OKProxyStateTest {
public void
assertExecuteWithProxyBackendExecutorSuitableForOLAPAndRequiredSameThreadForConnection()
{
ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
when(contextManager.getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
-
when(contextManager.getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLAP);
+
when(contextManager.getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(
+
ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLAP);
when(frontendEngine.getFrontendContext().isRequiredSameThreadForConnection(null)).thenReturn(true);
ExecutorService executorService = registerMockExecutorService(1);
try (MockedStatic<ProxyContext> proxyContext =
mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
diff --git
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/ProxyContextRestorer.java
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/ProxyContextRestorer.java
deleted file mode 100644
index 019fbec1f69..00000000000
---
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/ProxyContextRestorer.java
+++ /dev/null
@@ -1,38 +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.proxy.frontend.opengauss;
-
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.junit.After;
-import org.junit.Before;
-
-public abstract class ProxyContextRestorer {
-
- private ContextManager currentContextManager;
-
- @Before
- public void recordCurrentContextManager() {
- currentContextManager = ProxyContext.getInstance().getContextManager();
- }
-
- @After
- public void restorePreviousContextManager() {
- ProxyContext.init(currentContextManager);
- }
-}
diff --git
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
index 31975db91ac..15fd23f0cec 100644
---
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
+++
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
@@ -40,12 +40,12 @@ import
org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationResult;
-import org.apache.shardingsphere.proxy.frontend.opengauss.ProxyContextRestorer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
+import org.mockito.MockedStatic;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.MockitoJUnitRunner;
@@ -56,10 +56,11 @@ import java.util.Properties;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
-public final class OpenGaussAuthenticationEngineTest extends
ProxyContextRestorer {
+public final class OpenGaussAuthenticationEngineTest {
private final String username = "root";
@@ -71,11 +72,6 @@ public final class OpenGaussAuthenticationEngineTest extends
ProxyContextRestore
@SuppressWarnings("unchecked")
@Before
public void setup() {
- MetaDataContexts metaDataContexts = new
MetaDataContexts(mock(MetaDataPersistService.class),
- new ShardingSphereMetaData(Collections.emptyMap(),
buildGlobalRuleMetaData(new ShardingSphereUser(username, password, "")),
mock(ConfigurationProperties.class)));
- ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
-
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
- ProxyContext.init(contextManager);
when(channelHandlerContext.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
}
@@ -101,7 +97,11 @@ public final class OpenGaussAuthenticationEngineTest
extends ProxyContextRestore
payload.writeInt4(196608);
payload.writeStringNul("client_encoding");
payload.writeStringNul("UTF8");
- new
OpenGaussAuthenticationEngine().authenticate(channelHandlerContext, payload);
+ ContextManager contextManager = mockContextManager();
+ try (MockedStatic<ProxyContext> proxyContext =
mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() ->
ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ new
OpenGaussAuthenticationEngine().authenticate(channelHandlerContext, payload);
+ }
}
@Test(expected = ProtocolViolationException.class)
@@ -111,7 +111,19 @@ public final class OpenGaussAuthenticationEngineTest
extends ProxyContextRestore
PostgreSQLPacketPayload payload = new
PostgreSQLPacketPayload(createByteBuf(8, 16), StandardCharsets.UTF_8);
payload.writeInt1('F');
payload.writeInt8(0);
- authenticationEngine.authenticate(channelHandlerContext, payload);
+ ContextManager contextManager = mockContextManager();
+ try (MockedStatic<ProxyContext> proxyContext =
mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() ->
ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ authenticationEngine.authenticate(channelHandlerContext, payload);
+ }
+ }
+
+ private ContextManager mockContextManager() {
+ MetaDataContexts metaDataContexts = new
MetaDataContexts(mock(MetaDataPersistService.class),
+ new ShardingSphereMetaData(Collections.emptyMap(),
buildGlobalRuleMetaData(new ShardingSphereUser(username, password, "")),
mock(ConfigurationProperties.class)));
+ ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+ when(result.getMetaDataContexts()).thenReturn(metaDataContexts);
+ return result;
}
@SneakyThrows(ReflectiveOperationException.class)
diff --git
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
index c723c6a1287..5446694916d 100644
---
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
+++
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
@@ -41,14 +41,13 @@ import
org.apache.shardingsphere.proxy.backend.connector.jdbc.statement.JDBCBack
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import
org.apache.shardingsphere.proxy.backend.session.ServerPreparedStatementRegistry;
-import org.apache.shardingsphere.proxy.frontend.opengauss.ProxyContextRestorer;
import
org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLServerPreparedStatement;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
import
org.apache.shardingsphere.sqltranslator.rule.builder.DefaultSQLTranslatorRuleConfigurationBuilder;
-import org.junit.Before;
import org.junit.Test;
+import org.mockito.MockedStatic;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -67,39 +66,46 @@ import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
-public final class OpenGaussComBatchBindExecutorTest extends
ProxyContextRestorer {
+public final class OpenGaussComBatchBindExecutorTest {
private static final ShardingSphereSQLParserEngine SQL_PARSER_ENGINE = new
ShardingSphereSQLParserEngine("openGauss", new CacheOption(2000, 65535L), new
CacheOption(128, 1024L), false);
- @Before
- public void setup() {
- ProxyContext.init(mock(ContextManager.class, RETURNS_DEEP_STUBS));
- }
-
@SuppressWarnings("rawtypes")
@Test
public void assertExecute() throws SQLException {
-
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)).thenReturn(0);
-
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)).thenReturn(1);
-
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)).thenReturn(false);
- ShardingSphereRuleMetaData globalRuleMetaData = new
ShardingSphereRuleMetaData(Arrays.asList(new SQLTranslatorRule(new
DefaultSQLTranslatorRuleConfigurationBuilder().build()),
- new LoggingRule(new
DefaultLoggingRuleConfigurationBuilder().build())));
-
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
-
when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("ds_0"));
-
when(database.getResourceMetaData().getStorageTypes()).thenReturn(Collections.singletonMap("ds_0",
new OpenGaussDatabaseType()));
- when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
-
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase("db")).thenReturn(database);
String statement = "S_1";
- OpenGaussComBatchBindPacket packet =
mock(OpenGaussComBatchBindPacket.class);
- when(packet.getStatementId()).thenReturn("S_1");
-
when(packet.readParameterSets(anyList())).thenReturn(Collections.singletonList(Collections.singletonList(0)));
- ConnectionSession connectionSession = mock(ConnectionSession.class);
- when(connectionSession.getConnectionContext()).thenReturn(new
ConnectionContext());
- when(connectionSession.getDatabaseName()).thenReturn("db");
- when(connectionSession.getConnectionId()).thenReturn(1);
+ String sql = "insert into bmsql (id) values (?)";
+ SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
+ SQLStatementContext sqlStatementContext =
mock(InsertStatementContext.class);
+ when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement);
+ ConnectionSession connectionSession = mockConnectionSession();
+
connectionSession.getServerPreparedStatementRegistry().addPreparedStatement(statement,
new PostgreSQLServerPreparedStatement(sql, sqlStatementContext,
Collections.emptyList()));
+ OpenGaussComBatchBindExecutor executor = new
OpenGaussComBatchBindExecutor(mockComBatchBindPacket(), connectionSession);
+ ContextManager contextManager = mockContextManager();
+ try (MockedStatic<ProxyContext> proxyContext =
mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() ->
ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ Iterator<DatabasePacket<?>> actualPacketsIterator =
executor.execute().iterator();
+ assertThat(actualPacketsIterator.next(),
is(PostgreSQLBindCompletePacket.getInstance()));
+ assertThat(actualPacketsIterator.next(),
instanceOf(PostgreSQLCommandCompletePacket.class));
+ assertFalse(actualPacketsIterator.hasNext());
+ }
+ }
+
+ private static OpenGaussComBatchBindPacket mockComBatchBindPacket() {
+ OpenGaussComBatchBindPacket result =
mock(OpenGaussComBatchBindPacket.class);
+ when(result.getStatementId()).thenReturn("S_1");
+
when(result.readParameterSets(anyList())).thenReturn(Collections.singletonList(Collections.singletonList(0)));
+ return result;
+ }
+
+ private static ConnectionSession mockConnectionSession() throws
SQLException {
+ ConnectionSession result = mock(ConnectionSession.class);
+ when(result.getConnectionContext()).thenReturn(new
ConnectionContext());
+ when(result.getDatabaseName()).thenReturn("db");
+ when(result.getConnectionId()).thenReturn(1);
BackendConnection backendConnection = mock(BackendConnection.class);
Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
when(connection.getMetaData().getURL()).thenReturn("jdbc:opengauss://127.0.0.1/db");
@@ -109,18 +115,24 @@ public final class OpenGaussComBatchBindExecutorTest
extends ProxyContextRestore
JDBCBackendStatement backendStatement =
mock(JDBCBackendStatement.class);
when(backendStatement.createStorageResource(any(ExecutionUnit.class),
any(Connection.class), any(ConnectionMode.class), any(StatementOption.class),
nullable(DatabaseType.class)))
.thenReturn(preparedStatement);
-
when(connectionSession.getStatementManager()).thenReturn(backendStatement);
-
when(connectionSession.getBackendConnection()).thenReturn(backendConnection);
-
when(connectionSession.getServerPreparedStatementRegistry()).thenReturn(new
ServerPreparedStatementRegistry());
- String sql = "insert into bmsql (id) values (?)";
- SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
- SQLStatementContext sqlStatementContext =
mock(InsertStatementContext.class);
- when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement);
-
connectionSession.getServerPreparedStatementRegistry().addPreparedStatement(statement,
new PostgreSQLServerPreparedStatement(sql, sqlStatementContext,
Collections.emptyList()));
- OpenGaussComBatchBindExecutor executor = new
OpenGaussComBatchBindExecutor(packet, connectionSession);
- Iterator<DatabasePacket<?>> actualPacketsIterator =
executor.execute().iterator();
- assertThat(actualPacketsIterator.next(),
is(PostgreSQLBindCompletePacket.getInstance()));
- assertThat(actualPacketsIterator.next(),
instanceOf(PostgreSQLCommandCompletePacket.class));
- assertFalse(actualPacketsIterator.hasNext());
+ when(result.getStatementManager()).thenReturn(backendStatement);
+ when(result.getBackendConnection()).thenReturn(backendConnection);
+ when(result.getServerPreparedStatementRegistry()).thenReturn(new
ServerPreparedStatementRegistry());
+ return result;
+ }
+
+ private static ContextManager mockContextManager() {
+ ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+
when(result.getMetaDataContexts().getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)).thenReturn(0);
+
when(result.getMetaDataContexts().getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)).thenReturn(1);
+
when(result.getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)).thenReturn(false);
+
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Arrays.asList(
+ new SQLTranslatorRule(new
DefaultSQLTranslatorRuleConfigurationBuilder().build()), new LoggingRule(new
DefaultLoggingRuleConfigurationBuilder().build()))));
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
+
when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("ds_0"));
+
when(database.getResourceMetaData().getStorageTypes()).thenReturn(Collections.singletonMap("ds_0",
new OpenGaussDatabaseType()));
+ when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
+
when(result.getMetaDataContexts().getMetaData().getDatabase("db")).thenReturn(database);
+ return result;
}
}