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 fbd924b7f84 Add test cases for ListenerAssistedSubscriber (#32883)
fbd924b7f84 is described below
commit fbd924b7f8461818f6bd8d3b194425bd0f6f4441
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Sep 15 18:57:39 2024 +0800
Add test cases for ListenerAssistedSubscriber (#32883)
---
.../ListenerAssistedDispatchEventBuilder.java | 20 +++---
.../GlobalRuleConfigurationEventSubscriber.java | 3 +-
.../dispatch/ListenerAssistedSubscriber.java | 4 +-
.../dispatch/ListenerAssistedSubscriberTest.java | 73 ++++++++++++++++++++++
4 files changed, 87 insertions(+), 13 deletions(-)
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/builder/ListenerAssistedDispatchEventBuilder.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/builder/ListenerAssistedDispatchEventBuilder.java
index 0d4d8ad7a33..d65ad5d55e7 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/builder/ListenerAssistedDispatchEventBuilder.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/builder/ListenerAssistedDispatchEventBuilder.java
@@ -17,15 +17,14 @@
package org.apache.shardingsphere.mode.manager.cluster.event.builder;
-import org.apache.shardingsphere.mode.event.dispatch.DispatchEvent;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
-import
org.apache.shardingsphere.mode.event.dispatch.assisted.DropDatabaseListenerAssistedEvent;
+import org.apache.shardingsphere.mode.event.dispatch.DispatchEvent;
import
org.apache.shardingsphere.mode.event.dispatch.assisted.CreateDatabaseListenerAssistedEvent;
-import org.apache.shardingsphere.mode.persist.pojo.ListenerAssistedType;
-import org.apache.shardingsphere.mode.persist.pojo.ListenerAssisted;
+import
org.apache.shardingsphere.mode.event.dispatch.assisted.DropDatabaseListenerAssistedEvent;
import org.apache.shardingsphere.mode.path.ListenerAssistedNodePath;
+import org.apache.shardingsphere.mode.persist.pojo.ListenerAssisted;
import java.util.Arrays;
import java.util.Collection;
@@ -53,12 +52,13 @@ public final class ListenerAssistedDispatchEventBuilder
implements DispatchEvent
if (!databaseName.isPresent()) {
return Optional.empty();
}
- ListenerAssisted data = YamlEngine.unmarshal(event.getValue(),
ListenerAssisted.class);
- if (ListenerAssistedType.CREATE_DATABASE ==
data.getListenerAssistedType()) {
- return Optional.of(new
CreateDatabaseListenerAssistedEvent(databaseName.get()));
+ switch (YamlEngine.unmarshal(event.getValue(),
ListenerAssisted.class).getListenerAssistedType()) {
+ case CREATE_DATABASE:
+ return Optional.of(new
CreateDatabaseListenerAssistedEvent(databaseName.get()));
+ case DROP_DATABASE:
+ return Optional.of(new
DropDatabaseListenerAssistedEvent(databaseName.get()));
+ default:
+ return Optional.empty();
}
- return ListenerAssistedType.DROP_DATABASE ==
data.getListenerAssistedType()
- ? Optional.of(new
DropDatabaseListenerAssistedEvent(databaseName.get()))
- : Optional.empty();
}
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
index d098cb6aba8..098a1cbb88b 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
@@ -32,6 +32,7 @@ import java.util.Optional;
/**
* Global rule configuration event subscriber.
*/
+@SuppressWarnings("unused")
@RequiredArgsConstructor
public final class GlobalRuleConfigurationEventSubscriber implements
EventSubscriber {
@@ -42,7 +43,7 @@ public final class GlobalRuleConfigurationEventSubscriber
implements EventSubscr
*
* @param event global rule alter event
*/
- @SuppressWarnings({"unchecked", "unused"})
+ @SuppressWarnings("unchecked")
@Subscribe
public synchronized void renew(final AlterGlobalRuleConfigurationEvent
event) {
Preconditions.checkArgument(event.getActiveVersion().equals(
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/ListenerAssistedSubscriber.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/ListenerAssistedSubscriber.java
index 2a64143c579..ff38e24f6ed 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/ListenerAssistedSubscriber.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/ListenerAssistedSubscriber.java
@@ -34,6 +34,7 @@ import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositor
/**
* Listener assisted subscriber.
*/
+@SuppressWarnings("unused")
@RequiredArgsConstructor
public final class ListenerAssistedSubscriber implements EventSubscriber {
@@ -75,8 +76,7 @@ public final class ListenerAssistedSubscriber implements
EventSubscriber {
private void refreshShardingSphereStatisticsData() {
if
(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()
&& InstanceType.PROXY ==
contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType())
{
- new ShardingSphereStatisticsRefreshEngine(contextManager,
- new GlobalLockContext(new
GlobalLockPersistService((ClusterPersistRepository)
contextManager.getPersistServiceFacade().getRepository()))).asyncRefresh();
+ new ShardingSphereStatisticsRefreshEngine(contextManager, new
GlobalLockContext(new GlobalLockPersistService(repository))).asyncRefresh();
}
}
}
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/ListenerAssistedSubscriberTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/ListenerAssistedSubscriberTest.java
new file mode 100644
index 00000000000..cf9f782b57b
--- /dev/null
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/ListenerAssistedSubscriberTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.mode.manager.cluster.event.subscriber.dispatch;
+
+import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
+import
org.apache.shardingsphere.mode.event.dispatch.assisted.CreateDatabaseListenerAssistedEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.assisted.DropDatabaseListenerAssistedEvent;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+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 static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ListenerAssistedSubscriberTest {
+
+ private ListenerAssistedSubscriber subscriber;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ContextManager contextManager;
+
+ @Mock
+ private ClusterPersistRepository repository;
+
+ @BeforeEach
+ void setUp() {
+
when(contextManager.getPersistServiceFacade().getRepository()).thenReturn(repository);
+ subscriber = new ListenerAssistedSubscriber(contextManager);
+ }
+
+ @Test
+ void assertRenewWithCreateDatabaseListenerAssistedEvent() {
+
when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true);
+
when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.JDBC);
+ subscriber.renew(new CreateDatabaseListenerAssistedEvent("foo_db"));
+ verify(repository).watch(eq("/metadata/foo_db"), any());
+
verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).addDatabase("foo_db");
+
verify(contextManager.getPersistServiceFacade().getListenerAssistedPersistService()).deleteDatabaseNameListenerAssisted("foo_db");
+ }
+
+ @Test
+ void assertRenewWithDropDatabaseListenerAssistedEvent() {
+
when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true);
+
when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY);
+ subscriber.renew(new DropDatabaseListenerAssistedEvent("foo_db"));
+ verify(repository).removeDataListener("/metadata/foo_db");
+
verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).dropDatabase("foo_db");
+
verify(contextManager.getPersistServiceFacade().getListenerAssistedPersistService()).deleteDatabaseNameListenerAssisted("foo_db");
+ }
+}