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 tests in LogMessageTest #1500

Merged
merged 2 commits into from
Oct 19, 2023
Merged

Conversation

bbelide2
Copy link
Contributor

Change Assert condition to check on both map orders in a flaky test in LogMessageTest to make it non-flaky.

Flaky test

cn.hippo4j.common.toolkit.logtracing.LogMessageTest.testKvShouldPutAllKeyAndValuePairs	

Problem

Test testKvShouldPutAllKeyAndValuePairs in LogMessageTest is detected as flaky with the NonDex tool. The test failed with the following error:

Error:
Failed tests:   testKvShouldPutAllKeyAndValuePairs(cn.hippo4j.common.toolkit.logtracing.LogMessageTest): expected: <key1=value1||key2=value2> but was: <key2=value2||key1=value1>

Root cause

In this test, 2 key-value pairs are set to the kvs field (ConcurrentHashMap) of logMessage object. Then, logMessage.toString() is called and the response is compared with hard-coded strings. logMessage.toString() converts the given map to string form along with some other processing. But, ConcurrentHashMap/HashMap may not maintain the order of elements. Therefore, when NonDex tests are run, the order of elements are shuffled and the output is reversed and incorrect thus making the test flaky.

Key-values are inserted this way:

logMessage.kv("key1", "value1");
logMessage.kv("key2", "value2");

Output is compared this way:

assertEquals("key1=value1||key2=value2", logMessage.toString());

Fix

Usually, switching from HashMap to LinkedHashMap will make tests non-flaky because LinkedHashMap will preserve order of elements. But, we cannot change a ConcurrentHashMap to LinkedHashMap because LinkedHashMap is not thread-safe like ConcurrentHashMap. Therefore, I updated the assert statement to check the output for both the possible orders of elements.

This fix will not affect the code since the change is only made in tests.

How this has been tested?

Java: openjdk version "11.0.20.1"
Maven: Apache Maven 3.6.3

  1. Module build - Successful
    Command used -
mvn install -pl infra/common -am -DskipTests
  1. Regular test - Successful
    Command used -
mvn -pl infra/common test -Dtest=cn.hippo4j.common.toolkit.logtracing.LogMessageTest#testKvShouldPutAllKeyAndValuePairs
  1. NonDex test - Failed
    Command used -
mvn -pl infra/common edu.illinois:nondex-maven-plugin:2.1.1:nondex -DnondexRuns=10 -Dtest=cn.hippo4j.common.toolkit.logtracing.LogMessageTest#testKvShouldPutAllKeyAndValuePairs

NonDex tests passed after the fix.

@codecov
Copy link

codecov bot commented Oct 17, 2023

Codecov Report

Attention: 18 lines in your changes are missing coverage. Please review.

Comparison is base (cf854b3) 34.34% compared to head (d889b02) 34.25%.
Report is 1 commits behind head on develop.

❗ Current head d889b02 differs from pull request most recent head 5a743d9. Consider uploading reports for the commit 5a743d9 to get more accurate results

Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #1500      +/-   ##
=============================================
- Coverage      34.34%   34.25%   -0.10%     
  Complexity       848      848              
=============================================
  Files            262      262              
  Lines           5936     5953      +17     
  Branches         554      560       +6     
=============================================
  Hits            2039     2039              
- Misses          3704     3721      +17     
  Partials         193      193              
Files Coverage Δ
.../hippo4j/core/enable/BeforeCheckConfiguration.java 0.00% <0.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@magestacks magestacks merged commit ee2a81f into opengoofy:develop Oct 19, 2023
3 checks passed
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