Skip to content

Commit

Permalink
Add test cases on ReadwriteSplittingStaticDataSourceRuleAttribute (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 7, 2024
1 parent fb7688e commit 2d95386
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public void cleanStorageNodeDataSource(final String groupName) {
deleteStorageNodeDataSources(dataSourceGroupRules.get(groupName));
}

private void deleteStorageNodeDataSources(final ReadwriteSplittingDataSourceGroupRule rule) {
rule.getReadwriteSplittingGroup().getReadDataSources()
.forEach(each -> computeNodeInstanceContext.getEventBusContext().post(new QualifiedDataSourceDeletedEvent(new QualifiedDataSource(databaseName, rule.getName(), each))));
}

@Override
public void cleanStorageNodeDataSources() {
for (Entry<String, ReadwriteSplittingDataSourceGroupRule> entry : dataSourceGroupRules.entrySet()) {
deleteStorageNodeDataSources(entry.getValue());
}
}

private void deleteStorageNodeDataSources(final ReadwriteSplittingDataSourceGroupRule rule) {
rule.getReadwriteSplittingGroup().getReadDataSources()
.forEach(each -> computeNodeInstanceContext.getEventBusContext().post(new QualifiedDataSourceDeletedEvent(new QualifiedDataSource(databaseName, rule.getName(), each))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.readwritesplitting.rule.attribute;

import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource;
import org.apache.shardingsphere.infra.state.datasource.DataSourceState;
import org.apache.shardingsphere.mode.event.deliver.datasource.qualified.QualifiedDataSourceDeletedEvent;
import org.apache.shardingsphere.readwritesplitting.exception.logic.ReadwriteSplittingDataSourceRuleNotFoundException;
import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceGroupRule;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.apache.shardingsphere.test.matcher.ShardingSphereArgumentVerifyMatchers.deepEq;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class ReadwriteSplittingStaticDataSourceRuleAttributeTest {

@Test
void assertUpdateStatusWithNotExistedDataSourceGroupRule() {
ReadwriteSplittingStaticDataSourceRuleAttribute ruleAttribute = new ReadwriteSplittingStaticDataSourceRuleAttribute("foo_db", Collections.emptyMap(), mock(ComputeNodeInstanceContext.class));
assertThrows(ReadwriteSplittingDataSourceRuleNotFoundException.class, () -> ruleAttribute.updateStatus(new QualifiedDataSource("foo_db.foo_group.foo_ds"), DataSourceState.DISABLED));
}

@Test
void assertUpdateStatusWithDisable() {
ReadwriteSplittingDataSourceGroupRule dataSourceGroupRule = mock(ReadwriteSplittingDataSourceGroupRule.class);
ReadwriteSplittingStaticDataSourceRuleAttribute ruleAttribute = new ReadwriteSplittingStaticDataSourceRuleAttribute(
"foo_db", Collections.singletonMap("foo_group", dataSourceGroupRule), mock(ComputeNodeInstanceContext.class));
ruleAttribute.updateStatus(new QualifiedDataSource("foo_db.foo_group.foo_ds"), DataSourceState.DISABLED);
verify(dataSourceGroupRule).disableDataSource("foo_ds");
}

@Test
void assertUpdateStatusWithEnable() {
ReadwriteSplittingDataSourceGroupRule dataSourceGroupRule = mock(ReadwriteSplittingDataSourceGroupRule.class);
ReadwriteSplittingStaticDataSourceRuleAttribute ruleAttribute = new ReadwriteSplittingStaticDataSourceRuleAttribute(
"foo_db", Collections.singletonMap("foo_group", dataSourceGroupRule), mock(ComputeNodeInstanceContext.class));
ruleAttribute.updateStatus(new QualifiedDataSource("foo_db.foo_group.foo_ds"), DataSourceState.ENABLED);
verify(dataSourceGroupRule).enableDataSource("foo_ds");
}

@Test
void assertCleanStorageNodeDataSourceWithNotExistedDataSourceGroupRule() {
ReadwriteSplittingStaticDataSourceRuleAttribute ruleAttribute = new ReadwriteSplittingStaticDataSourceRuleAttribute("foo_db", Collections.emptyMap(), mock(ComputeNodeInstanceContext.class));
assertThrows(ReadwriteSplittingDataSourceRuleNotFoundException.class, () -> ruleAttribute.cleanStorageNodeDataSource("foo_group"));
}

@Test
void assertCleanStorageNodeDataSource() {
ReadwriteSplittingDataSourceGroupRule dataSourceGroupRule = mock(ReadwriteSplittingDataSourceGroupRule.class, RETURNS_DEEP_STUBS);
when(dataSourceGroupRule.getName()).thenReturn("foo_group");
when(dataSourceGroupRule.getReadwriteSplittingGroup().getReadDataSources()).thenReturn(Collections.singletonList("read_ds"));
ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS);
ReadwriteSplittingStaticDataSourceRuleAttribute ruleAttribute = new ReadwriteSplittingStaticDataSourceRuleAttribute(
"foo_db", Collections.singletonMap("foo_group", dataSourceGroupRule), computeNodeInstanceContext);
ruleAttribute.cleanStorageNodeDataSource("foo_group");
verify(computeNodeInstanceContext.getEventBusContext()).post(deepEq(new QualifiedDataSourceDeletedEvent(new QualifiedDataSource("foo_db.foo_group.read_ds"))));
}

@Test
void assertCleanStorageNodeDataSources() {
ReadwriteSplittingDataSourceGroupRule dataSourceGroupRule = mock(ReadwriteSplittingDataSourceGroupRule.class, RETURNS_DEEP_STUBS);
when(dataSourceGroupRule.getName()).thenReturn("foo_group");
when(dataSourceGroupRule.getReadwriteSplittingGroup().getReadDataSources()).thenReturn(Collections.singletonList("read_ds"));
ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS);
ReadwriteSplittingStaticDataSourceRuleAttribute ruleAttribute = new ReadwriteSplittingStaticDataSourceRuleAttribute(
"foo_db", Collections.singletonMap("foo_group", dataSourceGroupRule), computeNodeInstanceContext);
ruleAttribute.cleanStorageNodeDataSources();
verify(computeNodeInstanceContext.getEventBusContext()).post(deepEq(new QualifiedDataSourceDeletedEvent(new QualifiedDataSource("foo_db.foo_group.read_ds"))));
}
}

0 comments on commit 2d95386

Please sign in to comment.