-
Notifications
You must be signed in to change notification settings - Fork 132
Fixed flaky tests resulting from getDeclaredFields method used in a dependency. This causes the order of the fields returned by preparedOperation to be non-deterministic. #691
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
Conversation
…ic behavior of getDeclaredFields.
…ic behavior of the getDeclaredFields method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few code suggestions from a drive-by reviewer (not a member, so the remarks might not be relevant).
src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java
Show resolved
Hide resolved
for(String sortedField: sortedFieldsList){ | ||
if(sortedFieldsList.get(0)!=sortedField){ | ||
sortedFields.append(", "); | ||
} | ||
sortedFields.append(sortedField); | ||
} | ||
return sortedFields.toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for(String sortedField: sortedFieldsList){ | |
if(sortedFieldsList.get(0)!=sortedField){ | |
sortedFields.append(", "); | |
} | |
sortedFields.append(sortedField); | |
} | |
return sortedFields.toString(); | |
return String.join(", ", sortedFields); |
The order of selected columns depended on the nondeterministic behavior of getDeclaredFields. Original pull request /pull/691
Refactored the assertions towards a custom assertion class, taking care of limited parsing of the generated SQL statements. Original pull request /pull/691
Standardized GitHub references of tests. Original pull request /pull/691
The order of selected columns depended on the nondeterministic behavior of getDeclaredFields. Original pull request spring-projects/spring-data-r2dbc/pull/691
Refactored the assertions towards a custom assertion class, taking care of limited parsing of the generated SQL statements. Original pull request spring-projects/spring-data-r2dbc/pull/691
Standardized GitHub references of tests. Original pull request spring-projects/spring-data-r2dbc/pull/691
Thanks for bringing this up. |
This PR fixes the flaky tests that results when
preparedOperation.get()
is compared to an expected query string, say for example,"SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1"
. The assumption is thatpreparedOperation.get()
returns the field names in the exact orderALL_FIELDS
is declared with. However, this is not the case. To be more specific, the the field names returned bypreparedOperation.get()
can be in any of the n! orders, where n is the number of fields querying for. This non-deterministic behavior is due to thegetDeclaredFields()
method called in one of your dependencies (org.springframework.util.ReflectionUtils
). And as specified by the javaDocs forgetDeclaredFields()
, The elements in the returned array are not sorted and are not in any particular order.My fix is to sort the fields for both the expected and actual query strings prior to checking whether they are equal. I, however, excluded applying this fix to cases where only one field was queried because 1!=1, and so those tests are deterministic.
Declaration: