Skip to content

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

Closed
wants to merge 3 commits into from

Conversation

Auzel
Copy link
Contributor

@Auzel Auzel commented Nov 30, 2021

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 that preparedOperation.get() returns the field names in the exact order ALL_FIELDS is declared with. However, this is not the case. To be more specific, the the field names returned by preparedOperation.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 the getDeclaredFields() method called in one of your dependencies (org.springframework.util.ReflectionUtils). And as specified by the javaDocs for getDeclaredFields(), 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:

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@Auzel Auzel changed the title Fixed flaky test resulting from getDeclaredFields method used in a dependency used 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. Nov 30, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 30, 2021
Copy link

@nt-gt nt-gt left a 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).

Comment on lines +734 to +740
for(String sortedField: sortedFieldsList){
if(sortedFieldsList.get(0)!=sortedField){
sortedFields.append(", ");
}
sortedFields.append(sortedField);
}
return sortedFields.toString();
Copy link

Choose a reason for hiding this comment

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

Suggested change
for(String sortedField: sortedFieldsList){
if(sortedFieldsList.get(0)!=sortedField){
sortedFields.append(", ");
}
sortedFields.append(sortedField);
}
return sortedFields.toString();
return String.join(", ", sortedFields);

schauder pushed a commit that referenced this pull request Feb 23, 2022
The order of selected columns depended on the nondeterministic behavior of getDeclaredFields.

Original pull request /pull/691
schauder added a commit that referenced this pull request Feb 23, 2022
Refactored the assertions towards a custom assertion class, taking care of limited parsing of the generated SQL statements.

Original pull request /pull/691
schauder added a commit that referenced this pull request Feb 23, 2022
Standardized GitHub references of tests.

Original pull request /pull/691
schauder pushed a commit to spring-projects/spring-data-relational that referenced this pull request Feb 23, 2022
The order of selected columns depended on the nondeterministic behavior of getDeclaredFields.

Original pull request spring-projects/spring-data-r2dbc/pull/691
schauder added a commit to spring-projects/spring-data-relational that referenced this pull request Feb 23, 2022
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
schauder added a commit to spring-projects/spring-data-relational that referenced this pull request Feb 23, 2022
Standardized GitHub references of tests.

Original pull request spring-projects/spring-data-r2dbc/pull/691
@schauder
Copy link
Contributor

Thanks for bringing this up.
I polished and merged this and also ported it to 3.0.x which lives in spring/projects/spring-data-relational now.

@schauder schauder closed this Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants