Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test cases for RaftServerFactory #6009

Merged
merged 13 commits into from
Nov 10, 2023
1 change: 1 addition & 0 deletions changes/en-us/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ The version is updated as follows:
- [[#6001](https://github.com/seata/seata/pull/6001)] add test cases for RaftMsgExecute under branch package
- [[#5996](https://github.com/seata/seata/pull/5996)] add test cases for RaftMsgExecute under global package
- [[#6003](https://github.com/seata/seata/pull/6003)] add test cases for RaftMsgExecute under lock package
- [[#6009](https://github.com/seata/seata/pull/6009)] add test cases for RaftServerFactory


### Contributors:
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#6001](https://github.com/seata/seata/pull/6001)] 添加 RaftMsgExecute 模块 branch 包下的单元测试用例
- [[#5996](https://github.com/seata/seata/pull/5996)] 添加 RaftMsgExecute 模块 global 包下的单元测试用例
- [[#6003](https://github.com/seata/seata/pull/6003)] 添加 RaftMsgExecute 模块 lock 包下的单元测试用例
- [[#6009](https://github.com/seata/seata/pull/6009)] 添加RaftServerFactory的单元测试用例


### Contributors:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ public static CliClientService getCliClientServiceInstance() {
public void init() {
String initConfStr = CONFIG.getConfig(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR);
StoreConfig.SessionMode storeMode = StoreConfig.getSessionMode();
if (storeMode.equals(StoreConfig.SessionMode.RAFT)) {
for (RegistryService<?> instance : MultiRegistryFactory.getInstances()) {
if (!(instance instanceof FileRegistryServiceImpl)) {
throw new IllegalArgumentException("Raft store mode not support other Registration Center");
}
}
raftMode = true;
}
raftMode = storeMode.equals(StoreConfig.SessionMode.RAFT);
if (StringUtils.isBlank(initConfStr)) {
if (raftMode) {
throw new IllegalArgumentException(
"Raft store mode must config: " + ConfigurationKeys.SERVER_RAFT_SERVER_ADDR);
}
return;
} else {
if (raftMode) {
for (RegistryService<?> instance : MultiRegistryFactory.getInstances()) {
if (!(instance instanceof FileRegistryServiceImpl)) {
throw new IllegalArgumentException("Raft store mode not support other Registration Center");
}
}
}
LOGGER.warn("raft mode and raft cluster is an experimental feature");
}
final Configuration initConf = new Configuration();
Expand Down Expand Up @@ -161,7 +161,7 @@ public void start() {
public void destroy() {
this.close();
rpcServer = null;
RAFT_SERVER_MAP.clear();
raftMode = false;
}

@Override
Expand All @@ -171,6 +171,7 @@ public void close() {
LOGGER.info("closed seata server raft cluster, group: {} ", group);
});
Optional.ofNullable(rpcServer).ifPresent(RpcServer::shutdown);
RAFT_SERVER_MAP.clear();
}

public RaftServer getRaftServer(String group) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import io.seata.common.metadata.Node;
import io.seata.common.store.StoreMode;
import io.seata.common.util.StringUtils;
import io.seata.config.ConfigurationFactory;
import io.seata.common.ConfigurationKeys;
import io.seata.server.cluster.raft.context.SeataClusterContext;
import io.seata.server.cluster.raft.snapshot.metadata.LeaderMetadataSnapshotFile;
import io.seata.server.cluster.raft.snapshot.session.SessionSnapshotFile;
Expand Down Expand Up @@ -114,7 +112,7 @@ public boolean isLeader() {

public RaftStateMachine(String group) {
this.group = group;
mode = ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.STORE_MODE);
mode = StoreConfig.getSessionMode().getName();
EXECUTES.put(REFRESH_CLUSTER_METADATA, syncMsg -> {
refreshClusterMetadata(syncMsg);
return null;
Expand Down
83 changes: 83 additions & 0 deletions server/src/test/java/io/seata/server/raft/RaftServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.server.raft;

import io.seata.common.ConfigurationKeys;
import io.seata.common.XID;
import io.seata.config.Configuration;
import io.seata.config.ConfigurationCache;
import io.seata.config.ConfigurationFactory;
import io.seata.server.cluster.raft.RaftServerFactory;
import io.seata.server.lock.LockerManagerFactory;
import io.seata.server.session.SessionHolder;
import io.seata.server.store.StoreConfig;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

@SpringBootTest
public class RaftServerTest {

@BeforeAll
public static void setUp(ApplicationContext context) {
LockerManagerFactory.destroy();
SessionHolder.destroy();
}

@AfterEach
public void destroy() {
System.setProperty("server.raftPort", "0");
System.setProperty(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR, "");
ConfigurationCache.clear();
StoreConfig.setStartupParameter("file", "file", "file");
LockerManagerFactory.destroy();
SessionHolder.destroy();
}

@Test
public void initRaftServerStart() {
System.setProperty("server.raftPort", "9091");
System.setProperty(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR,
XID.getIpAddress() + ":9091" + "," + XID.getIpAddress() + ":9092" + "," + XID.getIpAddress() + ":9093");
StoreConfig.setStartupParameter("raft", "raft", "raft");
Assertions.assertDoesNotThrow(() -> RaftServerFactory.getInstance().init());
Assertions.assertNotNull(RaftServerFactory.getInstance().getRaftServer("default"));
Assertions.assertNotNull(RaftServerFactory.groups());
Assertions.assertNotNull(RaftServerFactory.getCliServiceInstance());
Assertions.assertNotNull(RaftServerFactory.getCliClientServiceInstance());
Assertions.assertEquals(RaftServerFactory.getInstance().isLeader("default"), false);
RaftServerFactory.getInstance().start();
}

@Test
public void initRaftServerFail() {
StoreConfig.setStartupParameter("raft", "raft", "raft");
Assertions.assertThrows(IllegalArgumentException.class, () -> RaftServerFactory.getInstance().init());
}

@Test
public void initRaftServerFailByRaftPortNull() {
System.setProperty(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR,
XID.getIpAddress() + ":9091" + "," + XID.getIpAddress() + ":9092" + "," + XID.getIpAddress() + ":9093");
StoreConfig.setStartupParameter("raft", "raft", "raft");
Assertions.assertThrows(IllegalArgumentException.class, () -> RaftServerFactory.getInstance().init());
}

}
Loading