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

fixed flaky test. The issue is the test method was directly comparing… #3

Open
wants to merge 4 commits into
base: develop-6.2.x
Choose a base branch
from

Conversation

saivivek116
Copy link
Owner

@saivivek116 saivivek116 commented Aug 6, 2023

What is the purpose of this PR

  • This PR fixes the error resulting from flaky test: org.broadleafcommerce.common.sitemap.service.CustomUrlSiteMapGeneratorTest.testCustomUrlSiteMapGenerator
  • The mentioned tests may fail or pass without changes made to the source code due to inconsistency in order of attributes inside xml tags in files when formatted to strings.

Why the tests fail

The issue is the test method was directly comparing the string versions of XML files, which included differences in attribute order.

Reproduce the test failure

  • Run the tests with NonDex maven plugin. The commands to recreate the flaky test failures are:

  • mvn -pl common edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.broadleafcommerce.common.sitemap.service.CustomUrlSiteMapGeneratorTest#testCustomUrlSiteMapGenerator

Expected results

  • Test should run successfully when run with NonDex

Actual Result

  • We get the following failure for test mvn -pl common edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.broadleafcommerce.common.sitemap.service.CustomUrlSiteMapGeneratorTest#testCustomUrlSiteMapGenerator
    [ERROR] testCustomUrlSiteMapGenerator(org.broadleafcommerce.common.sitemap.service.CustomUrlSiteMapGeneratorTest) Time elapsed: 0.825 s <<< FAILURE!
    java.lang.AssertionError
    at org.broadleafcommerce.common.sitemap.service.CustomUrlSiteMapGeneratorTest.testCustomUrlSiteMapGenerator(CustomUrlSiteMapGeneratorTest.java:61)

Fix

  • Created a document builder using DocumentBuilderFactory and Various features of the DocumentBuilderFactory are set to ensure secure and efficient XML processing. Converted the contents of both the files to strings. Two Document objects (doc1 and doc2) are created by parsing the respective XML strings using the DocumentBuilder obtainesd from the DocumentBuilderFactory. Both documents are normalized using the normalizeDocument() method. This helps ensure consistent comparison by removing any extraneous whitespace or unnecessary structure differences. And compared the both docs using isEqulaNode() method.

… the string versions of XML files, which included differences in attribute order.

Fixed by Enhanced the XML comparison algorithm to address flakiness. The updated method now correctly compares XML files for equivalence, disregarding variations in attribute order.
Copy link

@winglam winglam left a comment

Choose a reason for hiding this comment

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

Beyond the problems that I pointed out in the code changes, could you also improve the PR description to better match the following (i.e., same headers and the respective content for your PR under each header)?
wildfly/wildfly-core#5366

@@ -147,7 +153,7 @@ protected String convertFileToString(File file) throws IOException {
if (line.contains("</lastmod>")) {
continue;
}
line = line.replaceAll("\\s+", "");
line = line.replaceAll("\\s+", " ");
Copy link

Choose a reason for hiding this comment

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

Is this change necessary to fix the flaky test?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes it is required, XML files are compared based on their structure.

common/pom.xml Outdated
@@ -44,6 +44,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>edu.illinois</groupId>
<artifactId>nondex-maven-plugin</artifactId>
Copy link

Choose a reason for hiding this comment

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

Please remove the change to add NonDex in your pull request. Developers are unlikely to add NonDex to their list of dependencies.

common/pom.xml Outdated
@@ -144,6 +149,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
Copy link

Choose a reason for hiding this comment

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

Could you accomplish the fix without adding a new dependency to the project?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I have fixed the issue without using the dependency. Pls review

@saivivek116
Copy link
Owner Author

Hey Professor @winglam
Just a quick follow-up on my PR review request. Looking forward to your feedback. Thanks!

Copy link

@winglam winglam left a comment

Choose a reason for hiding this comment

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

I'm not sure if using the DocumentBuilder is really the most succinct way to fix this test as doing so may make this method/test much more expensive.

More importantly, it seems the changes still contain many extraneous lines/changes. Please review each changed line of the pull request and confirm whether you actually need to make the change or not and whether that line can be improved or not.

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
Copy link

Choose a reason for hiding this comment

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

Please sort your imports and do NOT use *, instead import only what you need.

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
Copy link

Choose a reason for hiding this comment

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

Why do you need this import?

doc2.normalizeDocument();
Assert.assertTrue(doc1.isEqualNode(doc2));
} catch (Exception e) {
throw new IOException();
Copy link

Choose a reason for hiding this comment

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

What you are doing here will mask what the actual exception is. Instead of catching all Exceptions and converting them to IOException, please just throw the actual exception in the method signature.

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
Copy link

Choose a reason for hiding this comment

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

Do you need this import?

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