-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix/3362 grpc timestamp access #3366
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull request overview
This PR fixes a bug where the replay service incorrectly accessed HTTPReq.Timestamp from the first test case, even when that test case was a gRPC request, resulting in zero or incorrect timestamps during mock matching. The fix extracts timestamp selection logic into a dedicated helper function that correctly handles both HTTP and gRPC test cases.
- Introduced
getBackdateTimestamphelper function to safely extract timestamps based on test case Kind - Updated both Docker Compose and non-Docker replay paths to use the new helper
- Added comprehensive unit tests covering gRPC-first, HTTP-first, empty, and nil edge cases
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| pkg/service/replay/replay.go | Adds getBackdateTimestamp helper function and updates both replay code paths (lines 747, 838) to use it instead of directly accessing testCases[0].HTTPReq.Timestamp |
| pkg/service/replay/replay_test.go | Adds four unit tests validating the helper function with gRPC-first, HTTP-first, empty, and nil test case scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
pkg/service/replay/replay.go
Outdated
| func getBackdateTimestamp(testCases []*models.TestCase) time.Time { | ||
| var backdate time.Time | ||
| if len(testCases) > 0 && testCases[0] != nil { | ||
| if testCases[0].Kind == models.HTTP { | ||
| backdate = testCases[0].HTTPReq.Timestamp | ||
| } else if testCases[0].Kind == models.GRPC_EXPORT { | ||
| backdate = testCases[0].GrpcReq.Timestamp | ||
| } | ||
| } | ||
| return backdate | ||
| } |
Copilot
AI
Dec 11, 2025
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.
The helper function getBackdateTimestamp is missing documentation. Consider adding a comment that explains its purpose, parameters, and return value. For example: "getBackdateTimestamp extracts the timestamp from the first test case based on its Kind (HTTP or GRPC_EXPORT). Returns zero time if the test cases are empty, nil, or have an unrecognized Kind."
pkg/service/replay/replay.go
Outdated
| if testCases[0].Kind == models.HTTP { | ||
| backdate = testCases[0].HTTPReq.Timestamp | ||
| } else if testCases[0].Kind == models.GRPC_EXPORT { | ||
| backdate = testCases[0].GrpcReq.Timestamp | ||
| } |
Copilot
AI
Dec 11, 2025
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.
The function currently only handles HTTP and GRPC_EXPORT kinds. While these are the only test case kinds currently used in the codebase, consider adding a default case that logs a warning if an unexpected Kind is encountered. This would make the code more defensive and easier to debug if new test case kinds are added in the future.
pkg/service/replay/replay_test.go
Outdated
| t.Errorf("Expected zero time for nil test case, got %v", backdate) | ||
| } | ||
| } | ||
|
|
Copilot
AI
Dec 11, 2025
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.
There's an extra blank line at the end of the file. Remove this trailing newline to maintain consistency with Go code style.
9f2c6d2 to
393886d
Compare
|
@remo-lab please address copilot comments |
AkashKumar7902
left a comment
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.
please address the review comments
| backdate = testCases[0].GrpcReq.Timestamp | ||
| } | ||
| } | ||
| return backdate |
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.
the branching is too much, can we shorten it? also what happens if backdate is returned as it is (means it doesn't go on the if condition of 104), shouldn't we log a warning ?
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.
also improve the function name, it should be short and to the point
|
Pls link the issue corresponding to this pr |
|
HI @AkashKumar7902 ,Thanks for the feedback! I’ve made the requested updates and refined the implementation: Simplified the branching by switching to a switch statement, which removes the nested conditions and makes the logic easier to read. 1)Improved the function name to backdate so it’s shorter and more aligned with Go naming conventions. 2)Converted it into a method on Replayer so we can use the instance logger (r.logger) for warnings. 3)Added warning logs for all edge cases (empty test cases, unknown kind, zero timestamps) to make unexpected flows more visible. 4)Added a doc comment explaining what the method does and how it handles HTTP and gRPC test cases. 5)Updated the tests to use r.backdate() directly and removed the trailing newline that Copilot flagged. Everything compiles cleanly and all tests are passing. Let me know if you’d like anything else improved — happy to adjust! |
…ar first. Signed-off-by: remo-lab <remopanda7@gmail.com>
Signed-off-by: remo-lab <remopanda7@gmail.com>
…ests Signed-off-by: remo-lab <remopanda7@gmail.com>
71723f3 to
9c0d0bd
Compare
|
Hi @AkashKumar7902 ,I have Implemented the suggested changes for this PR.” |
|
Hi @AkashKumar7902, I’ve implemented the suggested changes for this PR. |
|
Hi @Sarthak160 @gouravkrosx , can you please approve this pr? |
|
HI @AkashKumar7902 @gouravkrosx @Sarthak160 could you please approve this pr? |
Describe the changes that are made
This PR fixes an issue where the replay service always accessed HTTPReq.Timestamp from the first test case, even when the first test case was a gRPC request. This resulted in incorrect or zero timestamps being used during mock matching.
To address this cleanly, the timestamp selection logic has been extracted into a dedicated helper function and both replay paths now use this unified function.
What was the problem?
When replaying test cases, the code assumed that the first entry in testCases was always an HTTP test. For gRPC-first scenarios, this caused:
an incorrect zero-value timestamp
inconsistent backdate calculations
potential nil dereferences
incorrect mock matching behavior
This bug is tracked in Issue #3362.
What’s changed?
Extracted getBackdateTimestamp helper (replay.go: 102–112):
A new reusable function now handles selecting the correct timestamp based on the test case kind (HTTP or GRPC_EXPORT).
This eliminates duplicated logic and ensures both replay paths use the same behavior.
Updated both replay paths to use the helper:
Docker Compose path (line ~747)
Non-Docker path (line ~838)
Both now simply call:
backdate := getBackdateTimestamp(testCases)
This guarantees consistent timestamp handling.
Added comprehensive unit tests (replay_test.go)
All tests now call the real getBackdateTimestamp() function — no duplicated logic.
The following cases are covered:
gRPC-first test case (original bug scenario)
HTTP-first test case (regression test)
Empty test case list
Nil test case entry
To run:
go test -v ./pkg/service/replay -run TestGetBackdateTimestamp
i have also provided the screenshot ahter running test below.
Links & References
Why this fix matters:
Ensures correct timestamps are used for both HTTP and gRPC test cases
Eliminates the previous zero-value timestamp bug
Removes duplicated logic and makes replay behavior consistent
Adds reliable test coverage for all edge cases
Improves maintainability and safety for future changes
Files Modified:
pkg/service/replay/replay.go (timestamp logic extracted + call sites updated)
pkg/service/replay/replay_test.go (new tests added)
Closes: #[issue number that will be closed through this PR]
🔗 Related PRs
🐞 Related Issues
📄 Related Documents
What type of PR is this? (check all applicable)
Added e2e test pipeline?
Added comments for hard-to-understand areas?
Added to documentation?
Are there any sample code or steps to test the changes?
Self Review done?
Any relevant screenshots, recordings or logs?
🧠 Semantics for PR Title & Branch Name
Please ensure your PR title and branch name follow the Keploy semantics:
📌 PR Semantics Guide
📌 Branch Semantics Guide
Examples:
fix: patch MongoDB document update bugfeat/#1-login-flow(You may skip mentioning the issue number in the branch name if the change is small and the PR description clearly explains it.)Additional checklist: