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 cc275b3450a Add more test cases on ConnectionLimitContextTest (#37954)
cc275b3450a is described below
commit cc275b3450aaa559b5b931312f093c7ee94c4075
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Feb 4 14:15:51 2026 +0800
Add more test cases on ConnectionLimitContextTest (#37954)
* Add more test cases on ConnectionLimitContextTest
* Add more test cases on ConnectionLimitContextTest
---
.../connection/ConnectionLimitContextTest.java | 74 ++++++++++++++++++----
1 file changed, 60 insertions(+), 14 deletions(-)
diff --git
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java
b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java
index 00647c24acb..27b3ea33282 100644
---
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java
+++
b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java
@@ -17,14 +17,26 @@
package org.apache.shardingsphere.proxy.frontend.connection;
+import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.internal.configuration.plugins.Plugins;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Stream;
+
+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;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -35,31 +47,65 @@ import static org.mockito.Mockito.when;
@StaticMockSettings(ProxyContext.class)
class ConnectionLimitContextTest {
+ @BeforeEach
+ @AfterEach
+ void resetActiveConnections() {
+ setActiveConnections(0);
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("connectionAllowedArguments")
+ void assertConnectionAllowed(final String name, final int maxConnections,
final int initialActiveConnections, final boolean expectedAllowed, final int
expectedActiveAfterCall) {
+ mockMaxConnections(maxConnections);
+ setActiveConnections(initialActiveConnections);
+ assertThat(ConnectionLimitContext.getInstance().connectionAllowed(),
is(expectedAllowed));
+ assertThat(getActiveConnections(), is(expectedActiveAfterCall));
+ }
+
@Test
- void assertConnectionsLimited() {
- ContextManager contextManager = mockContextManager(2);
-
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
- assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
- assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
- assertFalse(ConnectionLimitContext.getInstance().connectionAllowed());
- ConnectionLimitContext.getInstance().connectionInactive();
- ConnectionLimitContext.getInstance().connectionInactive();
- assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
- ConnectionLimitContext.getInstance().connectionInactive();
- ConnectionLimitContext.getInstance().connectionInactive();
+ void assertConnectionInactiveDecrementsCounter() {
+ setActiveConnections(1);
ConnectionLimitContext.getInstance().connectionInactive();
+ assertThat(getActiveConnections(), is(0));
}
@Test
- void assertConnectionsUnlimited() {
- ContextManager contextManager = mockContextManager(0);
-
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ void assertLimitsMaxConnectionsEnabled() {
+ mockMaxConnections(1);
+
assertTrue(ConnectionLimitContext.getInstance().limitsMaxConnections());
+ }
+
+ @Test
+ void assertLimitsMaxConnectionsDisabled() {
+ mockMaxConnections(0);
assertFalse(ConnectionLimitContext.getInstance().limitsMaxConnections());
}
+ private void mockMaxConnections(final int maxConnections) {
+ ContextManager contextManager = mockContextManager(maxConnections);
+
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ }
+
private ContextManager mockContextManager(final int maxConnections) {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_MAX_CONNECTIONS)).thenReturn(maxConnections);
return result;
}
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private void setActiveConnections(final int value) {
+ ((AtomicInteger)
Plugins.getMemberAccessor().get(ConnectionLimitContext.class.getDeclaredField("activeConnections"),
ConnectionLimitContext.getInstance())).set(value);
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private int getActiveConnections() {
+ return ((AtomicInteger)
Plugins.getMemberAccessor().get(ConnectionLimitContext.class.getDeclaredField("activeConnections"),
ConnectionLimitContext.getInstance())).get();
+ }
+
+ private static Stream<Arguments> connectionAllowedArguments() {
+ return Stream.of(
+ Arguments.of("allowed", 2, 1, true, 2),
+ Arguments.of("rejected", 2, 2, false, 3),
+ Arguments.of("unlimited", 0, 0, true, 1));
+ }
}