nacos Flaky test in Common Module caused by Json Ordering Permutations

guz6ccqo  于 2022-11-13  发布在  Nacos
关注(0)|答案(3)|浏览(137)

Describe the bug
The test file

common/
com.alibaba.nacos.common.utils.JacksonUtilsTest.

has flakiness in the test testToJsonBytes

Expected behavior
The assertion should be passed whatever the order of the JSON string is.
We use the NonDex. as a test tool and we expected it passed.

Actually behavior
Fails while using Nondex to run the test with the following error -

[ERROR] Failures: 
[ERROR]   JacksonUtilsTest.testToJsonBytes:460 expected:<{"code":0,"data":{"[string":"你好,中国!","integer":999]}}> but was:<{"code":0,"data":{"[integer":999,"string":"你好,中国!"]}}>
[INFO] 
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

How to Reproduce
The flakiness was found with NonDex.
After installing NonDex and run the following code.

cd nacos
mvn install -pl common -am -DskipTests
mvn -pl common edu.illinois:nondex-maven-plugin:1.1.2:nondex -Dtest=com.alibaba.nacos.common.utils.JacksonUtilsTest#testToJsonBytes

Desktop (please complete the following information):

  • OS: Windows
  • Version: 10
  • Maven Version: 3.8.1
  • Nacos Module - common
  • SDK: Original
mcvgt66p

mcvgt66p1#

Can you provide a fix solution way?

gwo2fgha

gwo2fgha2#

Yes, the proposed solution is as follows: ( #9505 is my pull request for the same.)
There are two points where non-determinism is introduced. Proposed fixes are attached herewith.

  1. Using HashMap introduces non-determinism because HashMap doesn't guarantee the order of elements.
    Fix: Use a LinkedHashMap instead of a HashMap to guarantee the preservation of the order of insertion.
  2. Conversion of the ResultRet object into a JSON string without mentioning the order of the elements in the ResultSet.
    Fix: Annotate the class ResultSet with @JsonPropertyOrder(alphabetic = true) to ensure the order of properties while converting the object into JSON.

Can you please review the same and approve?

lymnna71

lymnna713#

The second way is un-reasonable, the problem is test case problem, so the solution should not change the original codes.

相关问题