-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Request to enhance some tests performance to avoid random getDeclaredFields() results #2520
Comments
Thanks, this is very interesting! There are a couple of paths forward. One possibility would be to update all the tests so they accept any field ordering. Perhaps with a helper method that takes a Another possibility would be to change the logic so it is deterministic, for example by sorting the field names. We touched on some of the implications in #2309. I would be reluctant to make that change unconditionally because I'm fairly sure there are lots of tests out there that are implicitly depending on the current order, as unspecified and fragile as that is. But perhaps it could be controlled by a system property. Then we could ensure that the system property is set for the tests that depend on a specific order, and update those tests as necessary so that they expected sorted field names. My inclination would be for the first possibility, updating the tests, but I would be interested in others' opinions. |
Personally I think another option would be to simply do nothing. This is not really a 'solution' to this problem, but on the other hand it looks like all recent JDK versions and possibly even different JDK distributions all behave the same way, and make the tests pass. Making the tests more complex and difficult to maintain to account for different field orders is in my opinion not worth it if the tests don't actually fail1. As soon as the tests fail for a specific JDK though this should probably be addressed. However, I assume the OpenJDK maintainers will be careful to not change the order of the fields (despite being unspecified) due to how many projects explicitly or implicitly rely on it. It is nonetheless great that you highlighted this issue @FarmersWrap, and the NonDex tool sounds really interesting! Otherwise, if we do want to adjust the tests, then the idea to use a system property to guarantee the order sounds good to me. But if the system property is intended to be internal, then we should clearly indicate that in the property name to avoid users relying on it. Footnotes
|
Hi eamonnmcmanus and Marcono1234, I tried to fix the tests using the maps. But as Marcono1234 said, the test will become very complicated. I had a demo here And, I also find when the classes are nested or there are so many fields(variables) or formats, for example, naming policies in FieldNamingTest.java. The original code was hard coded. But if we want to find all possible jsons. We need to hardcode every variable as strings in each naming policy. That is ugly. I tried to use a switch and a backtracking function to get all String. I searched online, I think the behavior of getDeclaredFields() is nondeterministic. Since I don't have the access to the JVM implementation. I just cite someone on StackOverflow with "And it might be affected by unexpected things ... like class unload / reload, or JIT recompilation.” I understand that you would not like to sort the fields because it is not necessary at all.(Here is 219sansim's fix by sorting) Do you have any gradle/maven projects that you would recommend me to run nondex and debug flaky tests? I want to test them and fix them as well. Thank you very much! |
Gson version
Java / Android version
Used tools
Description
There are 37 tests within gson has been reported as flaky when ran with the NonDextool. The test failed because of the randomness of the function Class.getDeclaredFields(). As mentioned by eamonnmcmanus in issue:2309, the tests are fragile due to the function. I wish to improve the stability of these tests by adding more options.
Expected behavior
The test should not only have one expected json answer according to the current implementation.
The behavior of gson tests should not rely on the getDeclaredFields() function because according to Class.getDeclaredFields() "The elements in the returned array are not sorted and are not in any particular order.". The test answers can differ.
Actual behavior
Tests pass when getDeclaredFields() returns the same order of Fields as they were declared.
But if the order of getDeclaredFields() results is not the same as it was declared. Tests fail.
Reproduction steps
Full description of Nondex please refer to its doc on github
I printed out the getDeclaredFields() results. The order of the Fields was randomized by Nondex on purpose to detect implementation-dependent flakiness. You can observe the order of the Fields is not always the same.
Failed test(There are 36, this is an example).
Flaky tests found
Flaky tests detected(idoft format):
Here are some possible fixes:FarmersWrap_PR.
Simran_PR.
Thanks
The text was updated successfully, but these errors were encountered: