Skip to content

Commit 19ba1ec

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 19ba1ec

File tree

10 files changed

+868
-0
lines changed

10 files changed

+868
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
21+
import java.time.Clock;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
25+
public class ClusterChangeEventTest {
26+
27+
@Test
28+
public void testConstructorWithFullParameters() {
29+
Object source = new Object();
30+
String group = "test-group";
31+
long term = 123L;
32+
boolean leader = true;
33+
34+
ClusterChangeEvent event = new ClusterChangeEvent(source, group, term, leader);
35+
36+
assertEquals(source, event.getSource());
37+
assertEquals(group, event.getGroup());
38+
assertEquals(term, event.getTerm());
39+
assertTrue(event.isLeader());
40+
}
41+
42+
@Test
43+
public void testConstructorWithSourceAndGroup() {
44+
Object source = new Object();
45+
String group = "test-group-2";
46+
47+
ClusterChangeEvent event = new ClusterChangeEvent(source, group);
48+
49+
assertEquals(source, event.getSource());
50+
assertEquals(group, event.getGroup());
51+
assertEquals(0L, event.getTerm());
52+
assertFalse(event.isLeader());
53+
}
54+
55+
@Test
56+
public void testConstructorWithClock() {
57+
Object source = new Object();
58+
Clock clock = Clock.systemUTC();
59+
60+
ClusterChangeEvent event = new ClusterChangeEvent(source, clock);
61+
62+
assertEquals(source, event.getSource());
63+
assertNull(event.getGroup());
64+
assertEquals(0L, event.getTerm());
65+
assertFalse(event.isLeader());
66+
}
67+
68+
@Test
69+
public void testSetAndGetGroup() {
70+
ClusterChangeEvent event = new ClusterChangeEvent(new Object(), "initial-group");
71+
event.setGroup("updated-group");
72+
assertEquals("updated-group", event.getGroup());
73+
}
74+
75+
@Test
76+
public void testSetAndGetTerm() {
77+
ClusterChangeEvent event = new ClusterChangeEvent(new Object(), Clock.systemUTC());
78+
event.setTerm(999L);
79+
assertEquals(999L, event.getTerm());
80+
}
81+
82+
@Test
83+
public void testSetAndGetLeader() {
84+
ClusterChangeEvent event = new ClusterChangeEvent(new Object(), "group");
85+
event.setLeader(true);
86+
assertTrue(event.isLeader());
87+
88+
event.setLeader(false);
89+
assertFalse(event.isLeader());
90+
}
91+
92+
@Test
93+
public void testLeaderStatusChange() {
94+
Object source = new Object();
95+
ClusterChangeEvent event = new ClusterChangeEvent(source, "group", 1L, false);
96+
assertFalse(event.isLeader());
97+
98+
event.setLeader(true);
99+
assertTrue(event.isLeader());
100+
}
101+
}
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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
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 RaftBaseMsgTest {
30+
31+
@Test
32+
public void testDefaultValues() {
33+
RaftBaseMsg msg = new RaftBaseMsg();
34+
assertEquals(DEFAULT_SEATA_GROUP, msg.getGroup());
35+
assertNull(msg.getMsgType());
36+
}
37+
38+
@Test
39+
public void testSetAndGetMsgType() {
40+
RaftBaseMsg msg = new RaftBaseMsg();
41+
msg.setMsgType(RaftSyncMsgType.ADD_GLOBAL_SESSION);
42+
assertEquals(RaftSyncMsgType.ADD_GLOBAL_SESSION, msg.getMsgType());
43+
}
44+
45+
@Test
46+
public void testSetAndGetGroup() {
47+
RaftBaseMsg msg = new RaftBaseMsg();
48+
msg.setGroup("test-group");
49+
assertEquals("test-group", msg.getGroup());
50+
}
51+
52+
@Test
53+
public void testSerialization() throws Exception {
54+
RaftBaseMsg original = new RaftBaseMsg();
55+
original.setMsgType(RaftSyncMsgType.ADD_BRANCH_SESSION);
56+
original.setGroup("custom-group");
57+
58+
// Serialize
59+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
60+
ObjectOutputStream oos = new ObjectOutputStream(bos);
61+
oos.writeObject(original);
62+
oos.flush();
63+
byte[] serialized = bos.toByteArray();
64+
65+
// Deserialize
66+
ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
67+
ObjectInputStream ois = new ObjectInputStream(bis);
68+
RaftBaseMsg deserialized = (RaftBaseMsg) ois.readObject();
69+
70+
// Verify
71+
assertEquals(original.getMsgType(), deserialized.getMsgType());
72+
assertEquals(original.getGroup(), deserialized.getGroup());
73+
}
74+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.ObjectInputStream;
25+
import java.io.ObjectOutputStream;
26+
27+
import static org.apache.seata.common.DefaultValues.DEFAULT_SEATA_GROUP;
28+
import static org.junit.jupiter.api.Assertions.*;
29+
30+
public class RaftBranchSessionSyncMsgTest {
31+
32+
@Test
33+
public void testDefaultConstructor() {
34+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg();
35+
assertNotNull(msg);
36+
assertEquals(DEFAULT_SEATA_GROUP, msg.getGroup());
37+
assertNull(msg.getBranchSession());
38+
assertNull(msg.getMsgType());
39+
}
40+
41+
@Test
42+
public void testConstructorWithParameters() {
43+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:123", 456L);
44+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg(
45+
RaftSyncMsgType.ADD_BRANCH_SESSION, dto);
46+
47+
assertEquals(RaftSyncMsgType.ADD_BRANCH_SESSION, msg.getMsgType());
48+
assertEquals(dto, msg.getBranchSession());
49+
}
50+
51+
@Test
52+
public void testSetAndGetBranchSession() {
53+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg();
54+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:789", 101L);
55+
msg.setBranchSession(dto);
56+
57+
assertEquals(dto, msg.getBranchSession());
58+
}
59+
60+
@Test
61+
public void testSetAndGetGroup() {
62+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg();
63+
msg.setGroup("custom-group");
64+
assertEquals("custom-group", msg.getGroup());
65+
}
66+
67+
@Test
68+
public void testToString() {
69+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:123", 456L);
70+
RaftBranchSessionSyncMsg msg = new RaftBranchSessionSyncMsg(
71+
RaftSyncMsgType.UPDATE_BRANCH_SESSION_STATUS, dto);
72+
73+
String str = msg.toString();
74+
assertNotNull(str);
75+
assertFalse(str.isEmpty());
76+
}
77+
78+
@Test
79+
public void testSerialization() throws Exception {
80+
BranchTransactionDTO dto = new BranchTransactionDTO("xid:123", 456L);
81+
dto.setLockKey("table:1,2,3");
82+
RaftBranchSessionSyncMsg original = new RaftBranchSessionSyncMsg(
83+
RaftSyncMsgType.ADD_BRANCH_SESSION, dto);
84+
original.setGroup("test-group");
85+
86+
// Serialize
87+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
88+
ObjectOutputStream oos = new ObjectOutputStream(bos);
89+
oos.writeObject(original);
90+
oos.flush();
91+
byte[] serialized = bos.toByteArray();
92+
93+
// Deserialize
94+
ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
95+
ObjectInputStream ois = new ObjectInputStream(bis);
96+
RaftBranchSessionSyncMsg deserialized = (RaftBranchSessionSyncMsg) ois.readObject();
97+
98+
// Verify
99+
assertEquals(original.getMsgType(), deserialized.getMsgType());
100+
assertEquals(original.getGroup(), deserialized.getGroup());
101+
assertEquals(original.getBranchSession().getXid(), deserialized.getBranchSession().getXid());
102+
assertEquals(original.getBranchSession().getBranchId(), deserialized.getBranchSession().getBranchId());
103+
}
104+
}

0 commit comments

Comments
 (0)