Skip to content

Commit d70ff63

Browse files
committed
test: add unit tests for server cluster module
Add unit tests for cluster module components: - Message classes: RaftBaseMsg, RaftBranchSessionSyncMsg, RaftGlobalSessionSyncMsg, RaftClusterMetadataMsg, RaftVGroupSyncMsg - DTO classes: BranchTransactionDTO, GlobalTransactionDTO, RaftClusterMetadata - Serializer: JacksonSerializer - Event: ClusterChangeEvent Total: 10 new test files covering basic cluster module components
1 parent dbcd5af commit d70ff63

File tree

10 files changed

+859
-0
lines changed

10 files changed

+859
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.server.cluster.listener;
18+
19+
import org.junit.jupiter.api.Test;
20+
import java.time.Clock;
21+
22+
import static org.junit.jupiter.api.Assertions.*;
23+
24+
public class ClusterChangeEventTest {
25+
26+
@Test
27+
public void testConstructorWithFullParameters() {
28+
Object source = new Object();
29+
String group = "test-group";
30+
long term = 123L;
31+
boolean leader = true;
32+
33+
ClusterChangeEvent event = new ClusterChangeEvent(source, group, term, leader);
34+
35+
assertEquals(source, event.getSource());
36+
assertEquals(group, event.getGroup());
37+
assertEquals(term, event.getTerm());
38+
assertTrue(event.isLeader());
39+
}
40+
41+
@Test
42+
public void testConstructorWithSourceAndGroup() {
43+
Object source = new Object();
44+
String group = "test-group-2";
45+
46+
ClusterChangeEvent event = new ClusterChangeEvent(source, group);
47+
48+
assertEquals(source, event.getSource());
49+
assertEquals(group, event.getGroup());
50+
assertEquals(0L, event.getTerm());
51+
assertFalse(event.isLeader());
52+
}
53+
54+
@Test
55+
public void testConstructorWithClock() {
56+
Object source = new Object();
57+
Clock clock = Clock.systemUTC();
58+
59+
ClusterChangeEvent event = new ClusterChangeEvent(source, clock);
60+
61+
assertEquals(source, event.getSource());
62+
assertNull(event.getGroup());
63+
assertEquals(0L, event.getTerm());
64+
assertFalse(event.isLeader());
65+
}
66+
67+
@Test
68+
public void testSetAndGetGroup() {
69+
ClusterChangeEvent event = new ClusterChangeEvent(new Object(), "initial-group");
70+
event.setGroup("updated-group");
71+
assertEquals("updated-group", event.getGroup());
72+
}
73+
74+
@Test
75+
public void testSetAndGetTerm() {
76+
ClusterChangeEvent event = new ClusterChangeEvent(new Object(), Clock.systemUTC());
77+
event.setTerm(999L);
78+
assertEquals(999L, event.getTerm());
79+
}
80+
81+
@Test
82+
public void testSetAndGetLeader() {
83+
ClusterChangeEvent event = new ClusterChangeEvent(new Object(), "group");
84+
event.setLeader(true);
85+
assertTrue(event.isLeader());
86+
87+
event.setLeader(false);
88+
assertFalse(event.isLeader());
89+
}
90+
91+
@Test
92+
public void testLeaderStatusChange() {
93+
Object source = new Object();
94+
ClusterChangeEvent event = new ClusterChangeEvent(source, "group", 1L, false);
95+
assertFalse(event.isLeader());
96+
97+
event.setLeader(true);
98+
assertTrue(event.isLeader());
99+
}
100+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.server.cluster.raft.serializer;
18+
19+
import org.apache.seata.server.cluster.raft.sync.msg.RaftBaseMsg;
20+
import org.apache.seata.server.cluster.raft.sync.msg.RaftSyncMsgType;
21+
import org.apache.seata.server.cluster.raft.sync.msg.dto.BranchTransactionDTO;
22+
import org.apache.seata.server.cluster.raft.sync.msg.dto.GlobalTransactionDTO;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
public class JacksonSerializerTest {
28+
29+
private final JacksonSerializer serializer = new JacksonSerializer();
30+
31+
@Test
32+
public void testSerializeAndDeserializeSimpleObject() {
33+
RaftBaseMsg original = new RaftBaseMsg();
34+
original.setMsgType(RaftSyncMsgType.ADD_GLOBAL_SESSION);
35+
original.setGroup("test-group");
36+
37+
byte[] bytes = serializer.serialize(original);
38+
assertNotNull(bytes);
39+
assertTrue(bytes.length > 0);
40+
41+
RaftBaseMsg deserialized = serializer.deserialize(bytes);
42+
assertNotNull(deserialized);
43+
assertEquals(original.getMsgType(), deserialized.getMsgType());
44+
assertEquals(original.getGroup(), deserialized.getGroup());
45+
}
46+
47+
@Test
48+
public void testSerializeAndDeserializeBranchTransactionDTO() {
49+
BranchTransactionDTO original = new BranchTransactionDTO("xid:123", 456L);
50+
original.setLockKey("table:1,2,3");
51+
52+
byte[] bytes = serializer.serialize(original);
53+
assertNotNull(bytes);
54+
55+
BranchTransactionDTO deserialized = serializer.deserialize(bytes);
56+
assertNotNull(deserialized);
57+
assertEquals(original.getXid(), deserialized.getXid());
58+
assertEquals(original.getBranchId(), deserialized.getBranchId());
59+
assertEquals(original.getLockKey(), deserialized.getLockKey());
60+
}
61+
62+
@Test
63+
public void testSerializeAndDeserializeGlobalTransactionDTO() {
64+
GlobalTransactionDTO original = new GlobalTransactionDTO("xid:789");
65+
66+
byte[] bytes = serializer.serialize(original);
67+
assertNotNull(bytes);
68+
69+
GlobalTransactionDTO deserialized = serializer.deserialize(bytes);
70+
assertNotNull(deserialized);
71+
assertEquals(original.getXid(), deserialized.getXid());
72+
}
73+
74+
@Test
75+
public void testSerializeNull() {
76+
assertThrows(RuntimeException.class, () -> {
77+
serializer.serialize(null);
78+
});
79+
}
80+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.server.cluster.raft.sync.msg;
18+
19+
import org.junit.jupiter.api.Test;
20+
import java.io.ByteArrayInputStream;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.ObjectInputStream;
23+
import java.io.ObjectOutputStream;
24+
25+
import static org.apache.seata.common.DefaultValues.DEFAULT_SEATA_GROUP;
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
public class RaftBaseMsgTest {
29+
30+
@Test
31+
public void testDefaultValues() {
32+
RaftBaseMsg msg = new RaftBaseMsg();
33+
assertEquals(DEFAULT_SEATA_GROUP, msg.getGroup());
34+
assertNull(msg.getMsgType());
35+
}
36+
37+
@Test
38+
public void testSetAndGetMsgType() {
39+
RaftBaseMsg msg = new RaftBaseMsg();
40+
msg.setMsgType(RaftSyncMsgType.ADD_GLOBAL_SESSION);
41+
assertEquals(RaftSyncMsgType.ADD_GLOBAL_SESSION, msg.getMsgType());
42+
}
43+
44+
@Test
45+
public void testSetAndGetGroup() {
46+
RaftBaseMsg msg = new RaftBaseMsg();
47+
msg.setGroup("test-group");
48+
assertEquals("test-group", msg.getGroup());
49+
}
50+
51+
@Test
52+
public void testSerialization() throws Exception {
53+
RaftBaseMsg original = new RaftBaseMsg();
54+
original.setMsgType(RaftSyncMsgType.ADD_BRANCH_SESSION);
55+
original.setGroup("custom-group");
56+
57+
// Serialize
58+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
59+
ObjectOutputStream oos = new ObjectOutputStream(bos);
60+
oos.writeObject(original);
61+
oos.flush();
62+
byte[] serialized = bos.toByteArray();
63+
64+
// Deserialize
65+
ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
66+
ObjectInputStream ois = new ObjectInputStream(bis);
67+
RaftBaseMsg deserialized = (RaftBaseMsg) ois.readObject();
68+
69+
// Verify
70+
assertEquals(original.getMsgType(), deserialized.getMsgType());
71+
assertEquals(original.getGroup(), deserialized.getGroup());
72+
}
73+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.server.cluster.raft.sync.msg;
18+
19+
import org.apache.seata.server.cluster.raft.sync.msg.dto.BranchTransactionDTO;
20+
import org.junit.jupiter.api.Test;
21+
import java.io.ByteArrayInputStream;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.ObjectInputStream;
24+
import java.io.ObjectOutputStream;
25+
26+
import static org.apache.seata.common.DefaultValues.DEFAULT_SEATA_GROUP;
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
public class RaftBranchSessionSyncMsgTest {
30+
31+
@Test
32+
public void testDefaultConstructor() {
33+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg();
34+
assertNotNull(msg);
35+
assertEquals(DEFAULT_SEATA_GROUP, msg.getGroup());
36+
assertNull(msg.getBranchSession());
37+
assertNull(msg.getMsgType());
38+
}
39+
40+
@Test
41+
public void testConstructorWithParameters() {
42+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:123", 456L);
43+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg(
44+
RaftSyncMsgType.ADD_BRANCH_SESSION, dto);
45+
46+
assertEquals(RaftSyncMsgType.ADD_BRANCH_SESSION, msg.getMsgType());
47+
assertEquals(dto, msg.getBranchSession());
48+
}
49+
50+
@Test
51+
public void testSetAndGetBranchSession() {
52+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg();
53+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:789", 101L);
54+
msg.setBranchSession(dto);
55+
56+
assertEquals(dto, msg.getBranchSession());
57+
}
58+
59+
@Test
60+
public void testSetAndGetGroup() {
61+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg();
62+
msg.setGroup("custom-group");
63+
assertEquals("custom-group", msg.getGroup());
64+
}
65+
66+
@Test
67+
public void testToString() {
68+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:123", 456L);
69+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg(
70+
RaftSyncMsgType.UPDATE_BRANCH_SESSION_STATUS, dto);
71+
72+
String str = msg.toString();
73+
assertNotNull(str);
74+
assertFalse(str.isEmpty());
75+
}
76+
77+
@Test
78+
public void testSerialization() throws Exception {
79+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:123", 456L);
80+
dto.setLockKey("table:1,2,3");
81+
RaftBranchSessionSyncMsg original = new RaftBranchSessionSyncMsg(
82+
RaftSyncMsgType.ADD_BRANCH_SESSION, dto);
83+
original.setGroup("test-group");
84+
85+
// Serialize
86+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
87+
ObjectOutputStream oos = new ObjectOutputStream(bos);
88+
oos.writeObject(original);
89+
oos.flush();
90+
byte[] serialized = bos.toByteArray();
91+
92+
// Deserialize
93+
ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
94+
ObjectInputStream ois = new ObjectInputStream(bis);
95+
RaftBranchSessionSyncMsg deserialized = (RaftBranchSessionSyncMsg) ois.readObject();
96+
97+
// Verify
98+
assertEquals(original.getMsgType(), deserialized.getMsgType());
99+
assertEquals(original.getGroup(), deserialized.getGroup());
100+
assertEquals(original.getBranchSession().getXid(), deserialized.getBranchSession().getXid());
101+
assertEquals(original.getBranchSession().getBranchId(), deserialized.getBranchSession().getBranchId());
102+
}
103+
}

0 commit comments

Comments
 (0)