You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "psxjoy (via GitHub)" <gi...@apache.org> on 2025/01/08 14:01:33 UTC
[PR] test: expand unit test coverage for the [cmmon] module. [incubator-seata]
psxjoy opened a new pull request, #7098:
URL: https://github.com/apache/incubator-seata/pull/7098
<!-- Please make sure you have read and understood the contributing guidelines -->
- [x] I have registered the PR [changes](../changes).
- [x] I have optimized the code using seata-codestyle.xml.
- [x] I have checked all codes using seata-checkstyle.xml.
### Ⅰ. Describe what this PR did
Add unit test cases to complete the unit testing objectives for the `seata-common` module.
增加单元测试用例,完成`commons`模块的单元测试目标
| |Instructions Cov.|Branches Cov.|Missed|Cxty|Missed|Lines|Missed|Missed|
|----|----|----|----|----|----|----|----|----|
|Before|73%|66%|431|1,183|673|2,438|182|16|
|***After***|***81%***|***73%***|319|1,138|451|2,438|102|9|
### Ⅱ. Does this pull request fix one issue?
<!-- If that, add "fixes #xxx" below in the next line, for example, fixes #97. -->
fixes #187
### Ⅲ. Why don't you add test cases (unit test/integration test)?
Not applicable.
不适用
### Ⅳ. Describe how to verify it
Run `mvn clean test`
### Ⅴ. Special notes for reviews
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] test: expand unit test coverage for the [cmmon] module. [incubator-seata]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #7098:
URL: https://github.com/apache/incubator-seata/pull/7098#issuecomment-2577791163
## [Codecov](https://app.codecov.io/gh/apache/incubator-seata/pull/7098?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 53.17%. Comparing base [(`5b31862`)](https://app.codecov.io/gh/apache/incubator-seata/commit/5b31862a1301d02e885c8f50ca26131202f1721d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`4167ae6`)](https://app.codecov.io/gh/apache/incubator-seata/commit/4167ae63b986f2949176a6ff28c83d9adac25c6a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/incubator-seata/pull/7098?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## 2.x #7098 +/- ##
============================================
+ Coverage 52.66% 53.17% +0.51%
- Complexity 6688 6795 +107
============================================
Files 1132 1132
Lines 40286 40286
Branches 4724 4722 -2
============================================
+ Hits 21215 21423 +208
+ Misses 17039 16819 -220
- Partials 2032 2044 +12
```
[see 17 files with indirect coverage changes](https://app.codecov.io/gh/apache/incubator-seata/pull/7098/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] test: expand unit test coverage for the [cmmon] module. [incubator-seata]
Posted by "xingfudeshi (via GitHub)" <gi...@apache.org>.
xingfudeshi commented on code in PR #7098:
URL: https://github.com/apache/incubator-seata/pull/7098#discussion_r1908362547
##########
common/src/test/java/org/apache/seata/common/metadata/namingserver/InstanceTest.java:
##########
@@ -52,4 +60,135 @@ void toJsonString() throws JsonProcessingException {
instance.setTransaction(new Node.Endpoint("2.2.2.2", 999));
assertEquals(instance.toJsonString(objectMapper), objectMapper.writeValueAsString(instance));
}
+
+ @Test public void testGetInstance() {
+ Instance anotherInstance = Instance.getInstance();
+ instance = Instance.getInstance();
+ assertEquals(instance, anotherInstance);
+ }
+
+ @Test public void testJsonSerializationShouldSerialize() {
+ instance = Instance.getInstance();
+ instance.setNamespace("testNamespace");
+ instance.setClusterName("testCluster");
+ instance.setUnit("testUnit");
+ instance.getControl().setPort(1234);
+ instance.getTransaction().setPort(4321);
+ instance.setWeight(0.5);
+ instance.setHealthy(false);
+ instance.setTerm(1);
+ instance.setTimestamp(System.currentTimeMillis());
+ instance.addMetadata("key1", "value1");
+
+ String jsonString = instance.toJsonString(objectMapper);
+ Instance deserializedInstance = null;
+ try {
+ deserializedInstance = objectMapper.readValue(jsonString, Instance.class);
+ } catch (Exception e) {
+ fail("Exception during JSON deserialization: " + e.getMessage());
+ }
+
+ assertNotNull(deserializedInstance);
+ assertEquals(instance.getNamespace(), deserializedInstance.getNamespace());
+ assertEquals(instance.getClusterName(), deserializedInstance.getClusterName());
+ assertEquals(instance.getUnit(), deserializedInstance.getUnit());
+ assertEquals(instance.getControl().getPort(), deserializedInstance.getControl().getPort());
+ assertEquals(instance.getTransaction().getPort(), deserializedInstance.getTransaction().getPort());
+ assertEquals(instance.getWeight(), deserializedInstance.getWeight(), 0.0);
+ assertEquals(instance.isHealthy(), deserializedInstance.isHealthy());
+ assertEquals(instance.getTerm(), deserializedInstance.getTerm());
+ assertEquals(instance.getTimestamp(), deserializedInstance.getTimestamp());
+ assertEquals(instance.getMetadata(), deserializedInstance.getMetadata());
+ }
+
+ @Test public void testToMapShouldReturnCorrectMap() {
+ instance = Instance.getInstance();
+ instance.setNamespace("testNamespace");
+ instance.setClusterName("testCluster");
+ instance.setUnit("testUnit");
+ instance.setControl(new Node.Endpoint("127.0.0.1", 1234));
+ instance.setTransaction(new Node.Endpoint("127.0.0.1", 4321));
+
+ instance.setWeight(0.5);
+ instance.setHealthy(false);
+ instance.setTerm(1);
+ instance.setTimestamp(System.currentTimeMillis());
+ instance.addMetadata("key1", "value1");
+
+ Map<String, String> resultMap = new HashMap<>();
+ resultMap.put("namespace", instance.getNamespace());
+ resultMap.put("clusterName", instance.getClusterName());
+ resultMap.put("unit", instance.getUnit());
+ resultMap.put("control", instance.getControl().toString());
+ resultMap.put("transaction", instance.getTransaction().toString());
+ resultMap.put("weight", String.valueOf(instance.getWeight()));
+ resultMap.put("healthy", String.valueOf(instance.isHealthy()));
+ resultMap.put("term", String.valueOf(instance.getTerm()));
+ resultMap.put("timestamp", String.valueOf(instance.getTimestamp()));
+ resultMap.put("metadata", mapToJsonString(instance.getMetadata()));
+
+ assertEquals("testNamespace", resultMap.get("namespace"));
+ assertEquals("testCluster", resultMap.get("clusterName"));
+ assertEquals("testUnit", resultMap.get("unit"));
+ assertTrue(resultMap.get("control").contains("1234"));
+ assertTrue(resultMap.get("transaction").contains("4321"));
+ assertEquals("0.5", resultMap.get("weight"));
+ assertEquals("false", resultMap.get("healthy"));
+ assertEquals("1", resultMap.get("term"));
+ assertTrue(resultMap.get("timestamp").matches("\\d+"));
+ assertTrue(resultMap.get("metadata").contains("key1"));
+ assertTrue(resultMap.get("metadata").contains("value1"));
+ }
+
+ @Test void testSameInstance() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertEquals(instanceA, instanceA);
+ }
+
+ @Test void testNull() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertNotEquals(instanceA, null);
+ }
+
+ @Test void testDifferentClass() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertNotEquals(instanceA, "NotAnInstance");
+ }
+
+ @Test void testSameFields() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ instanceB = Instance.getInstance();
+ instanceB.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceB.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertEquals(instanceA, instanceB);
+ }
+
+ @Test void testDifferentControlPort() {
Review Comment:
Please place the annotation above the method
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] test: expand unit test coverage for the [cmmon] module. [incubator-seata]
Posted by "xingfudeshi (via GitHub)" <gi...@apache.org>.
xingfudeshi merged PR #7098:
URL: https://github.com/apache/incubator-seata/pull/7098
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] test: expand unit test coverage for the [cmmon] module. [incubator-seata]
Posted by "psxjoy (via GitHub)" <gi...@apache.org>.
psxjoy commented on code in PR #7098:
URL: https://github.com/apache/incubator-seata/pull/7098#discussion_r1908652371
##########
common/src/test/java/org/apache/seata/common/metadata/namingserver/InstanceTest.java:
##########
@@ -52,4 +60,135 @@ void toJsonString() throws JsonProcessingException {
instance.setTransaction(new Node.Endpoint("2.2.2.2", 999));
assertEquals(instance.toJsonString(objectMapper), objectMapper.writeValueAsString(instance));
}
+
+ @Test public void testGetInstance() {
+ Instance anotherInstance = Instance.getInstance();
+ instance = Instance.getInstance();
+ assertEquals(instance, anotherInstance);
+ }
+
+ @Test public void testJsonSerializationShouldSerialize() {
+ instance = Instance.getInstance();
+ instance.setNamespace("testNamespace");
+ instance.setClusterName("testCluster");
+ instance.setUnit("testUnit");
+ instance.getControl().setPort(1234);
+ instance.getTransaction().setPort(4321);
+ instance.setWeight(0.5);
+ instance.setHealthy(false);
+ instance.setTerm(1);
+ instance.setTimestamp(System.currentTimeMillis());
+ instance.addMetadata("key1", "value1");
+
+ String jsonString = instance.toJsonString(objectMapper);
+ Instance deserializedInstance = null;
+ try {
+ deserializedInstance = objectMapper.readValue(jsonString, Instance.class);
+ } catch (Exception e) {
+ fail("Exception during JSON deserialization: " + e.getMessage());
+ }
+
+ assertNotNull(deserializedInstance);
+ assertEquals(instance.getNamespace(), deserializedInstance.getNamespace());
+ assertEquals(instance.getClusterName(), deserializedInstance.getClusterName());
+ assertEquals(instance.getUnit(), deserializedInstance.getUnit());
+ assertEquals(instance.getControl().getPort(), deserializedInstance.getControl().getPort());
+ assertEquals(instance.getTransaction().getPort(), deserializedInstance.getTransaction().getPort());
+ assertEquals(instance.getWeight(), deserializedInstance.getWeight(), 0.0);
+ assertEquals(instance.isHealthy(), deserializedInstance.isHealthy());
+ assertEquals(instance.getTerm(), deserializedInstance.getTerm());
+ assertEquals(instance.getTimestamp(), deserializedInstance.getTimestamp());
+ assertEquals(instance.getMetadata(), deserializedInstance.getMetadata());
+ }
+
+ @Test public void testToMapShouldReturnCorrectMap() {
+ instance = Instance.getInstance();
+ instance.setNamespace("testNamespace");
+ instance.setClusterName("testCluster");
+ instance.setUnit("testUnit");
+ instance.setControl(new Node.Endpoint("127.0.0.1", 1234));
+ instance.setTransaction(new Node.Endpoint("127.0.0.1", 4321));
+
+ instance.setWeight(0.5);
+ instance.setHealthy(false);
+ instance.setTerm(1);
+ instance.setTimestamp(System.currentTimeMillis());
+ instance.addMetadata("key1", "value1");
+
+ Map<String, String> resultMap = new HashMap<>();
+ resultMap.put("namespace", instance.getNamespace());
+ resultMap.put("clusterName", instance.getClusterName());
+ resultMap.put("unit", instance.getUnit());
+ resultMap.put("control", instance.getControl().toString());
+ resultMap.put("transaction", instance.getTransaction().toString());
+ resultMap.put("weight", String.valueOf(instance.getWeight()));
+ resultMap.put("healthy", String.valueOf(instance.isHealthy()));
+ resultMap.put("term", String.valueOf(instance.getTerm()));
+ resultMap.put("timestamp", String.valueOf(instance.getTimestamp()));
+ resultMap.put("metadata", mapToJsonString(instance.getMetadata()));
+
+ assertEquals("testNamespace", resultMap.get("namespace"));
+ assertEquals("testCluster", resultMap.get("clusterName"));
+ assertEquals("testUnit", resultMap.get("unit"));
+ assertTrue(resultMap.get("control").contains("1234"));
+ assertTrue(resultMap.get("transaction").contains("4321"));
+ assertEquals("0.5", resultMap.get("weight"));
+ assertEquals("false", resultMap.get("healthy"));
+ assertEquals("1", resultMap.get("term"));
+ assertTrue(resultMap.get("timestamp").matches("\\d+"));
+ assertTrue(resultMap.get("metadata").contains("key1"));
+ assertTrue(resultMap.get("metadata").contains("value1"));
+ }
+
+ @Test void testSameInstance() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertEquals(instanceA, instanceA);
+ }
+
+ @Test void testNull() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertNotEquals(instanceA, null);
+ }
+
+ @Test void testDifferentClass() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertNotEquals(instanceA, "NotAnInstance");
+ }
+
+ @Test void testSameFields() {
+ instanceA = Instance.getInstance();
+ instanceA.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceA.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ instanceB = Instance.getInstance();
+ instanceB.setControl(new Node.Endpoint("127.0.0.1", 8080));
+ instanceB.setTransaction(new Node.Endpoint("127.0.0.1", 9090));
+ assertEquals(instanceA, instanceB);
+ }
+
+ @Test void testDifferentControlPort() {
Review Comment:
> Please place the annotation above the method
I’ve updated this section of the code,PTAL.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org