Skip to content
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

Fix flaky test in CustomTypeAdaptersTest #2523

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

219sansim
Copy link

@219sansim 219sansim commented Nov 3, 2023

Purpose

The test testCustomNestedSerializers in CustomTypeAdaptersTest is detected flaky because the gson.toJson() gives a non-deterministic output.
Related issue: #2520

Description

After modifying the tests to check if the gson.toJson() output to is one of the possible json string values, the test passes, instead of checking against only 1 string.

Reason:
The gson.toJson() internally calls com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields which makes a call to getDeclaredFields


And according to this stackoverlow discussion, getDeclaredFields in Java 8 does not return elements in a consistent order.

Reproduction of error

The test runs fail on 4/5 runs of the NonDex tool

Command to reproduce the failure:

mvn -pl gson edu.illinois:nondex-maven-plugin:2.1.1:debug -Dtest=com.google.gson.functional.CustomTypeAdaptersTest#testCustomNestedSerializers

Error Message:

Running com.google.gson.functional.CustomTypeAdaptersTest
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.689 s <<< FAILURE! -- in com.google.gson.functional.CustomTypeAdaptersTest
[ERROR] com.google.gson.functional.CustomTypeAdaptersTest.testCustomNestedSerializers -- Time elapsed: 0.660 s <<< FAILURE!
value of: toJson(...)
expected: {"bag":6,"value":10}
but was : {"value":10,"bag":6}
        at com.google.gson.functional.CustomTypeAdaptersTest.testCustomNestedSerializers(CustomTypeAdaptersTest.java:128)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   CustomTypeAdaptersTest.testCustomNestedSerializers:128 value of: toJson(...)
expected: {"bag":6,"value":10}
but was : {"value":10,"bag":6}
[INFO] 
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] 

Comment on lines +128 to +129
String targetStr = gson.toJson(target);
assertThat(targetStr.equals("{\"bag\":6,\"value\":10}") || targetStr.equals("{\"value\":10,\"bag\":6}")).isTrue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, instead of using .equals()s, you can you the .isAnyOf(obj1, obj2) method.
According with the .isAnyOf() documentation:

Fails unless the subject is equal to any of the given elements.

The assert could be something like:

Suggested change
String targetStr = gson.toJson(target);
assertThat(targetStr.equals("{\"bag\":6,\"value\":10}") || targetStr.equals("{\"value\":10,\"bag\":6}")).isTrue();
assertThat(gson.toJson(target)).isAnyOf("{\"bag\":6,\"value\":10}", "{\"value\":10,\"bag\":6}");

Note: I am not a maintainer of the project, please wait for a maintainer's review

@219sansim 219sansim marked this pull request as draft November 7, 2023 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants