Skip to content

Commit

Permalink
Add more tests on ReadwriteSplittingRuleStatementConverter (#33576)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 6, 2024
1 parent aa79306 commit 3024d95
Showing 1 changed file with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,34 @@
import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.distsql.segment.ReadwriteSplittingRuleSegment;
import org.apache.shardingsphere.readwritesplitting.transaction.TransactionalReadQueryStrategy;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ReadwriteSplittingRuleStatementConverterTest {

@Test
void assertEmptyRuleSegmentConvertResult() {
ReadwriteSplittingRuleConfiguration actualEmptyRuleSegmentConvertResult = ReadwriteSplittingRuleStatementConverter
.convert(Collections.emptyList());
assertTrue(actualEmptyRuleSegmentConvertResult.getDataSourceGroups().isEmpty());
assertTrue(actualEmptyRuleSegmentConvertResult.getLoadBalancers().isEmpty());
void assertConvert() {
ReadwriteSplittingRuleConfiguration actual = ReadwriteSplittingRuleStatementConverter.convert(Collections.singleton(new ReadwriteSplittingRuleSegment(
"foo_name", "write_ds", Arrays.asList("read_ds0", "read_ds1"), TransactionalReadQueryStrategy.FIXED.name(), new AlgorithmSegment("foo_algo", new Properties()))));
assertThat(actual.getDataSourceGroups(), deepEqual(Collections.singletonList(
new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_name", "write_ds", Arrays.asList("read_ds0", "read_ds1"), TransactionalReadQueryStrategy.FIXED, "foo_name_foo_algo"))));
assertThat(actual.getLoadBalancers(), deepEqual(Collections.singletonMap("foo_name_foo_algo", new AlgorithmConfiguration("foo_algo", new Properties()))));
}

@Test
void assertSingleRuleSegmentConvertResult() {
ReadwriteSplittingRuleSegment expectedSingleReadwriteSplittingRuleSegment = createReadwriteSplittingRuleSegment("write_ds", Arrays.asList("read_ds_01", "read_ds_02"),
"static_load_balancer_type", new Properties());
ReadwriteSplittingRuleConfiguration actualSingleRuleSegmentConvertResult = ReadwriteSplittingRuleStatementConverter
.convert(Collections.singleton(expectedSingleReadwriteSplittingRuleSegment));
Collection<ReadwriteSplittingDataSourceGroupRuleConfiguration> actualSingleRuleSegmentConvertResultDataSourceGroups = actualSingleRuleSegmentConvertResult.getDataSourceGroups();
Map<String, AlgorithmConfiguration> actualSingleRuleSegmentConvertResultLoadBalancers = actualSingleRuleSegmentConvertResult.getLoadBalancers();
assertThat(actualSingleRuleSegmentConvertResultDataSourceGroups.size(), is(1));
assertThat(actualSingleRuleSegmentConvertResultLoadBalancers.size(), is(1));
ReadwriteSplittingDataSourceGroupRuleConfiguration actualRuleConfig = actualSingleRuleSegmentConvertResultDataSourceGroups.iterator().next();
assertThat(actualRuleConfig.getName(), is(expectedSingleReadwriteSplittingRuleSegment.getName()));
String expectedLoadBalancerName = String.format("%s_%s", expectedSingleReadwriteSplittingRuleSegment.getName(), expectedSingleReadwriteSplittingRuleSegment.getLoadBalancer().getName());
assertThat(actualRuleConfig.getLoadBalancerName(), is(expectedLoadBalancerName));
assertThat(actualRuleConfig.getWriteDataSourceName(), is(expectedSingleReadwriteSplittingRuleSegment.getWriteDataSource()));
assertThat(actualRuleConfig.getReadDataSourceNames(), is(expectedSingleReadwriteSplittingRuleSegment.getReadDataSources()));
AlgorithmConfiguration actualSphereAlgorithmConfig = actualSingleRuleSegmentConvertResultLoadBalancers.get(expectedLoadBalancerName);
assertThat(actualSphereAlgorithmConfig.getType(), is(expectedSingleReadwriteSplittingRuleSegment.getLoadBalancer().getName()));
assertThat(actualSphereAlgorithmConfig.getProps(), is(expectedSingleReadwriteSplittingRuleSegment.getLoadBalancer().getProps()));
}

private ReadwriteSplittingRuleSegment createReadwriteSplittingRuleSegment(final String writeDataSource, final List<String> readDataSources,
final String loadBalancerTypeName, final Properties props) {
return new ReadwriteSplittingRuleSegment("", writeDataSource, readDataSources, new AlgorithmSegment(loadBalancerTypeName, props));
void assertConvertWithoutTransactionalReadQueryStrategyAndLoadBalancer() {
ReadwriteSplittingRuleConfiguration actual = ReadwriteSplittingRuleStatementConverter.convert(
Collections.singleton(new ReadwriteSplittingRuleSegment("foo_name", "write_ds", Arrays.asList("read_ds0", "read_ds1"), null)));
assertThat(actual.getDataSourceGroups(), deepEqual(Collections.singletonList(
new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_name", "write_ds", Arrays.asList("read_ds0", "read_ds1"), null))));
assertTrue(actual.getLoadBalancers().isEmpty());
}
}

0 comments on commit 3024d95

Please sign in to comment.