Fix stringPayload ordering to match test expectations #2362
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is the purpose of this PR?
This pull request aims to fix a flaky test in the HttpEventPublisher.java by modifying the getStringPayload() method in HttpEventPublisher.java. The previous implementation caused inconsistencies in the serialized JSON string due to the output ordering the HttpEventPublisherTest.java was looking for.
Why the test fails?
The flaky test failed because getStringPayload() did not return the JSON in the format HttpEventPublisherTest.java inside src/test/java/com/google/cloud/teleport/splunk/. This inconsistency triggered test failures when run under NonDex.
Expected results
The test should consistently pass when getStringPayload() produces the same output regardless of runtime order.
Actual results
The test occasionally fails because the payload string produced by getStringPayload() was not ordered properly.
Description of fix
Rewrote the getStringPayload() method to manually and deterministically construct the expected JSON string. This eliminates any GSON-related ordering issues by using StringBuilder to control field layout and ordering explicitly for each SplunkEvent.
Approach Taken
To resolve the flaky behavior in the stringPayloadTest, I first closely examined the test outputs, particularly the discrepancies between the expected and but was values. After multiple test runs and several hours of observation, I identified that the test failure stemmed from non-deterministic ordering in the serialized JSON payload. The output varied across runs, indicating that the issue was related to how the payload string was being generated.
I then navigated to the test implementation at:
src/test/java/com/google/cloud/teleport/splunk/HttpEventPublisherTest
Inside the stringPayloadTest method, I reviewed the expected structure of the JSON string. Using that insight, I investigated the production code in:
src/main/java/com/google/cloud/teleport/splunk/HttpEventPublisher.java
The method in question, getStringPayload(), originally relied on GSON.toJson() to serialize each SplunkEvent. However, GSON does not guarantee field ordering, which explained the flaky test failures under NonDex.
To fix this, I manually reconstructed the JSON string using a StringBuilder to ensure consistent field ordering. I preserved the exact order expected by the test: time, host, source, sourcetype, index, and event. This deterministic construction guarantees the same output across all runs, eliminating the root cause of the flakiness.
Here’s a simplified view of the updated logic:
After implementing this fix, the test passed consistently across multiple NonDex runs, confirming the resolution of the flakiness.