Skip to content

Request to enhance some tests performance to avoid random getDeclaredFields() results #2520

@FarmersWrap

Description

@FarmersWrap

Gson version

2.10.1

Java / Android version

openjdk version "11.0.20.1"

Used tools

Apache Maven 3.6.3
Ubuntu 20.04.6 LTS
Linux version: 5.4.0-163-generic

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

  1. Download NonDex
  2. Execute following commands
cd gson
mvn install -pl gson -am -DskipTests
mvn -pl gson test -Dtest=com.google.gson.functional.StreamingTypeAdaptersTest#testNullSafe
mvn -pl gson edu.illinois:nondex-maven-plugin:2.1.1:nondex -DnondexRuns=10 -Dtest=com.google.gson.functional.StreamingTypeAdaptersTest#testNullSafe

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.

[�[1;34mINFO�[m] Using auto detected provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[�[1;34mINFO�[m] 
[�[1;34mINFO�[m] -------------------------------------------------------
[�[1;34mINFO�[m]  T E S T S
[�[1;34mINFO�[m] -------------------------------------------------------
[�[1;34mINFO�[m] Running com.google.gson.functional.�[1mStreamingTypeAdaptersTest�[m
double com.google.gson.functional.StreamingTypeAdaptersTest$Truck.horsePower
java.util.List com.google.gson.functional.StreamingTypeAdaptersTest$Truck.passengers
java.lang.String com.google.gson.functional.StreamingTypeAdaptersTest$Person.name
int com.google.gson.functional.StreamingTypeAdaptersTest$Person.age
double com.google.gson.functional.StreamingTypeAdaptersTest$Truck.horsePower
java.util.List com.google.gson.functional.StreamingTypeAdaptersTest$Truck.passengers
double com.google.gson.functional.StreamingTypeAdaptersTest$Truck.horsePower
java.util.List com.google.gson.functional.StreamingTypeAdaptersTest$Truck.passengers
[�[1;34mINFO�[m] �[1;32mTests run: �[0;1;32m1�[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.148 s -- in com.google.gson.functional.�[1mStreamingTypeAdaptersTest�[m
[�[1;34mINFO�[m] 
[�[1;34mINFO�[m] Results:
[�[1;34mINFO�[m] 
[�[1;34mINFO�[m] �[1;32mTests run: 1, Failures: 0, Errors: 0, Skipped: 0�[m
[�[1;34mINFO�[m] 
INFO: Adding excluded groups to newly created one
INFO: Adding NonDex argLine to existing argLine specified by the project
CONFIG: nondexFilter=.*
nondexMode=FULL
nondexSeed=1181842
nondexStart=0
nondexEnd=9223372036854775807
nondexPrintstack=false
nondexDir=/home/yu129/gson/gson/.nondex
nondexJarDir=/home/yu129/gson/gson/.nondex
nondexExecid=7bDkRyFRDJglqUt8BYXyKlsd6HNdgRY9duqp6ZWyD8=
nondexLogging=CONFIG
test=
[�[1;34mINFO�[m] Using auto detected provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[�[1;34mINFO�[m] 
[�[1;34mINFO�[m] -------------------------------------------------------
[�[1;34mINFO�[m]  T E S T S
[�[1;34mINFO�[m] -------------------------------------------------------
[�[1;34mINFO�[m] Running com.google.gson.functional.�[1mStreamingTypeAdaptersTest�[m
double com.google.gson.functional.StreamingTypeAdaptersTest$Truck.horsePower
java.util.List com.google.gson.functional.StreamingTypeAdaptersTest$Truck.passengers
java.lang.String com.google.gson.functional.StreamingTypeAdaptersTest$Person.name
int com.google.gson.functional.StreamingTypeAdaptersTest$Person.age
double com.google.gson.functional.StreamingTypeAdaptersTest$Truck.horsePower
java.util.List com.google.gson.functional.StreamingTypeAdaptersTest$Truck.passengers
java.util.List com.google.gson.functional.StreamingTypeAdaptersTest$Truck.passengers
double com.google.gson.functional.StreamingTypeAdaptersTest$Truck.horsePower
[�[1;34mINFO�[m] �[1;32mTests run: �[0;1;32m1�[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.139 s -- in com.google.gson.functional.�[1mStreamingTypeAdaptersTest�[m
[�[1;34mINFO�[m] 
[�[1;34mINFO�[m] Results:
[�[1;34mINFO�[m] 
[�[1;34mINFO�[m] �[1;32mTests run: 1, Failures: 0, Errors: 0, Skipped: 0�[m
[�[1;34mINFO�[m] 
INFO: Adding excluded groups to newly created one
INFO: Adding NonDex argLine to existing argLine specified by the project
CONFIG: nondexFilter=.*
nondexMode=FULL
nondexSeed=1223286
nondexStart=0
nondexEnd=9223372036854775807
nondexPrintstack=false
nondexDir=/home/yu129/gson/gson/.nondex
nondexJarDir=/home/yu129/gson/gson/.nondex
nondexExecid=eoE0yaR5WJj8fVYEdEVYfmhwnpWM0Gh2QuyNp9vuw=
nondexLogging=CONFIG

Failed test(There are 36, this is an example).

INFO: Adding excluded groups to newly created one
INFO: Adding NonDex argLine to existing argLine specified by the project
CONFIG: nondexFilter=.*
nondexMode=FULL
nondexSeed=1181842
nondexStart=0
nondexEnd=9223372036854775807
nondexPrintstack=false
nondexDir=/home/yu129/gson/gson/.nondex
nondexJarDir=/home/yu129/gson/gson/.nondex
nondexExecid=32QrqKafssXCuolEzFuT5jQnxBJ7+XGF3cCy2AZ8yvE=
nondexLogging=CONFIG
test=
[�[1;34mINFO�[m] Using auto detected provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[�[1;34mINFO�[m] 
[�[1;34mINFO�[m] -------------------------------------------------------
[�[1;34mINFO�[m]  T E S T S
[�[1;34mINFO�[m] -------------------------------------------------------
[�[1;34mINFO�[m] Running com.google.gson.functional.�[1mStreamingTypeAdaptersTest�[m
[�[1;31mERROR�[m] �[1;31mTests �[0;1mrun: �[0;1m1�[m, �[1;31mFailures: �[0;1;31m1�[m, Errors: 0, Skipped: 0, Time elapsed: 0.162 s�[1;31m <<< FAILURE!�[m -- in com.google.gson.functional.�[1mStreamingTypeAdaptersTest�[m
[�[1;31mERROR�[m] com.google.gson.functional.StreamingTypeAdaptersTest.testNullSafe -- Time elapsed: 0.156 s <<< FAILURE!
expected: {"horsePower":1.0,"passengers":[null,"jesse,30"]}
but was : {"passengers":[null,"jesse,30"],"horsePower":1.0}
	at com.google.gson.functional.StreamingTypeAdaptersTest.testNullSafe(StreamingTypeAdaptersTest.java:204)

Flaky tests found

Flaky tests detected(idoft format):

https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.StreamingTypeAdaptersTest.testNullSafe,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.StreamingTypeAdaptersTest.testSerializeWithCustomTypeAdapter,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.StreamingTypeAdaptersTest.testSerialize,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.StreamingTypeAdaptersTest.testSerializeNullField,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ReflectionAccessFilterTest.testBlockInaccessibleJava,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ParameterizedTypesTest.testVariableTypeFieldsAndGenericArraysSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ParameterizedTypesTest.testTypesWithMultipleParametersSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ParameterizedTypesTest.testVariableTypeFieldsAndGenericArraysDeserialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.JsonAdapterSerializerDeserializerTest.testJsonAdapterNullSafe,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.CollectionTest.testWildcardCollectionField,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.CollectionTest.testObjectCollectionSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.CollectionTest.testPriorityQueue,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.CollectionTest.testCollectionOfBagOfPrimitivesSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.NamingPolicyTest.testGsonDuplicateNameUsingSerializedNameFieldNamingPolicySerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.InheritanceTest.testSubInterfacesOfCollectionSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.InheritanceTest.testSubClassSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.TypeVariableTest.testTypeVariablesViaTypeParameter,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ExposeFieldsTest.testExposeAnnotationSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ExposeFieldsTest.testArrayWithOneNullExposeFieldObjectSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ObjectTest.testSingletonLists,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ObjectTest.testArrayOfArraysSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ObjectTest.testArrayOfObjectsSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ObjectTest.testNestedSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ObjectTest.testNullFieldsSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ObjectTest.testBagOfPrimitiveWrappersSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.ObjectTest.testBagOfPrimitivesSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.FieldNamingTest.testUpperCamelCase,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.FieldNamingTest.testLowerCaseWithDashes,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.FieldNamingTest.testLowerCaseWithUnderscores,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.FieldNamingTest.testUpperCamelCaseWithSpaces,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.FieldNamingTest.testIdentity,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.FieldNamingTest.testUpperCaseWithUnderscores,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.CustomTypeAdaptersTest.testCustomNestedSerializers,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.VersioningTest.testVersionedGsonWithUnversionedClassesSerialization,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.MapTest.testInterfaceTypeMapWithSerializer,ID,,,
https://github.com/google/gson,e685705b2bf3ae174958612a185bd231c0e0c5d9,gson,com.google.gson.functional.JsonAdapterAnnotationOnFieldsTest.testExcludeDeserializePrecedence,ID,,,

Here are some possible fixes:FarmersWrap_PR.
Simran_PR.
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions