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

refactor(api!): replace legacy event asserts with current one #6070

Merged

Conversation

akaladarshi
Copy link
Contributor

@akaladarshi akaladarshi commented Mar 28, 2024

Description

  • Replace AssertEventsLegacy with AssertEvents.
  • Refactor

closes: #5551


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against the correct branch (see CONTRIBUTING.md).
  • Linked to GitHub issue with discussion and accepted design, OR link to spec that describes this work.
  • Code follows the module structure standards and Go style guide.
  • Wrote unit and integration tests.
  • Updated relevant documentation (docs/).
  • Added relevant godoc comments.
  • Provide a conventional commit message to follow the repository standards.
  • Include a descriptive changelog entry when appropriate. This may be left to the discretion of the PR reviewers. (e.g. chores should be omitted from changelog)
  • Re-reviewed Files changed in the GitHub PR explorer.
  • Review SonarCloud Report in the comment section below once CI passes.

Summary by CodeRabbit

  • Refactor
    • Updated event verification and creation logic across various test modules to enhance consistency and reliability.
  • Documentation
    • Updated migration guide and changelog to reflect the latest changes in event handling and function renaming.
  • Tests
    • Improved test case implementations for better error checking and clarity in event handling.

Copy link
Contributor

coderabbitai bot commented Mar 28, 2024

Walkthrough

This set of changes primarily focuses on refactoring and cleaning up event handling in test suites across various modules. It involves updating event creation and assertion methods to streamline testing and improve code clarity. Significant changes include the replacement of legacy event assertion functions with a unified approach and the adoption of a more consistent event handling mechanism across different testing scenarios.

Changes

File Path Change Summary
modules/.../events_test.go Added abci import, refactored event creation to use abci.Event.
modules/.../msg_server_test.go Updated event verification logic to use sdk.Events and ToABCIEvents.
modules/core/02-client/keeper/events_test.go Added require import, modified type assertions with error checking.
modules/core/04-channel/keeper/... Added abci import, changed event return types and creation in expEvents.
testing/events.go, docs/docs/05-migrations/13-v8-to-v9.md, CHANGELOG.md Consolidated event assertion functions, updated documentation and changelog.

Assessment against linked issues

Objective Addressed Explanation
Clean up event testing functions by consolidating and updating methods (#5551)
Implement a function to replicate ABCI handler flow in tests (#3997) The changes focus on event testing, not on mimicking ABCI application flows.
Enable verification of event occurrences in E2E tests (#1765) The changes are related to unit tests, not E2E tests.
Refactor tests using the testing package for improved robustness in client module (#4013) The refactoring in this PR does not explicitly address the robustness of the client module.

Possibly related issues


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between eecfa5c and f29b72f.
Files selected for processing (6)
  • modules/apps/callbacks/types/events_test.go (8 hunks)
  • modules/apps/transfer/keeper/msg_server_test.go (1 hunks)
  • modules/core/04-channel/keeper/packet_test.go (4 hunks)
  • modules/core/04-channel/keeper/timeout_test.go (4 hunks)
  • modules/core/keeper/msg_server_test.go (19 hunks)
  • testing/events.go (2 hunks)
Files not summarized due to errors (1)
  • modules/core/keeper/msg_server_test.go: Error: Message exceeds token limit
Path instructions used (6)
modules/apps/transfer/keeper/msg_server_test.go (2)

**/*.go
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


**/*_test.go
Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request

testing/events.go (1)

**/*.go
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

modules/apps/callbacks/types/events_test.go (2)

**/*.go
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


**/*_test.go
Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request

modules/core/04-channel/keeper/timeout_test.go (2)

**/*.go
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


**/*_test.go
Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request

modules/core/04-channel/keeper/packet_test.go (2)

**/*.go
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


**/*_test.go
Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request

modules/core/keeper/msg_server_test.go (2)

**/*.go
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


**/*_test.go
Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request

Additional comments (15)
modules/apps/transfer/keeper/msg_server_test.go (1)
  • 109-125: The updated event verification logic using sdk.Events and ToABCIEvents method for expected and actual events comparison is a good practice. It aligns with the PR's objective to standardize event assertion methods. However, ensure that the AssertEvents function is robust enough to handle the comparison effectively, especially considering the transition from sdk.Event to abci.Event.
testing/events.go (1)
  • 14-19: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-1]

Removing the EventsMap type and the AssertEventsLegacy function, as mentioned in the AI-generated summary, is a positive step towards simplifying the event handling logic. This change should make the event assertion more straightforward and maintainable. Ensure that all references to these removed entities are also updated across the test suite to avoid any broken tests.

modules/apps/callbacks/types/events_test.go (2)
  • 4-4: Adding the import of the abci package to use abci.Event in tests is necessary for the refactor. This aligns with the PR's objective to standardize event assertion methods by using abci.Event for event creation. Good job on keeping the imports organized.
  • 35-50: Refactoring the test cases to use abci.Event instead of sdk.Event for event creation and using ToABCIEvents for expected events is a significant improvement. This change ensures consistency with the new event assertion methodology. However, ensure that all tests are updated accordingly and that the AssertEvents function is capable of accurately comparing abci.Event instances.
modules/core/04-channel/keeper/timeout_test.go (3)
  • 6-6: The import path for abci has been changed to "github.com/cometbft/cometbft/abci/types". Ensure that this new import path is correct and that the CometBFT dependency is intended to replace the previous ABCI import. This change should be verified for compatibility with the rest of the IBC-go project.
  • 251-251: The signature of the expEvents function has been modified to return []abci.Event instead of a map. This change aligns with the PR's objective to standardize event assertion methods. However, ensure that all usages of expEvents throughout the test suite have been updated accordingly to handle the new return type.
  • 337-351: The implementation of expEvents now utilizes sdk.Events and sdk.NewEvent for event creation, converting them to []abci.Event using ToABCIEvents(). This change is part of the refactor to standardize event assertion methods. Ensure that the events created here accurately reflect the expected events for the test cases, particularly in the context of channel flushing completion.
modules/core/04-channel/keeper/packet_test.go (2)
  • 5-5: The addition of the abci package import aligns with the changes made to the event assertion methodology, as the abci.Event type is now used for event creation and comparison. This change is consistent with the PR's objective to standardize event assertion methods.
  • 1326-1326: The call to ibctesting.AssertEvents within the TestAcknowledgePacket test case is a good practice for verifying that the expected events are emitted during the test execution. This approach enhances the test's robustness by ensuring that not only the main functionality is correct but also the side effects (events) are as expected. It's a crucial part of testing in event-driven systems like blockchain applications.
modules/core/keeper/msg_server_test.go (6)
  • 939-955: The event assertions in the test case "success" for TestChannelUpgradeInit are well-structured and check for the correct attributes. However, it's important to ensure that the event types and attributes are consistent with the expected behavior of the channel upgrade initialization process.
  • 1085-1101: In TestChannelUpgradeTry, the event assertions are correctly checking for the expected attributes following a successful channel upgrade try. This is crucial for ensuring that the channel upgrade process is correctly emitting events that reflect the state changes.
  • 1274-1289: The event assertions in TestChannelUpgradeAck for the case "success, no pending in-flight packets" accurately reflect the expected state changes and event emissions. It's important that these checks are in place to validate the correctness of the channel upgrade acknowledgment process.
  • 1878-1894: In TestChannelUpgradeOpen, the event assertions for the "success" case correctly validate the expected events and attributes following a successful channel upgrade open. This ensures that the channel state transitions and event emissions are as expected.
  • 2073-2097: The event assertions in TestChannelUpgradeCancel for the case "success: keeper is not authority, valid error receipt so channel changed to match error receipt seq" are comprehensive and ensure that the channel upgrade cancellation process emits the correct events. This is crucial for verifying that the channel state is correctly reverted in response to the cancellation.
  • 2459-2487: In TestChannelUpgradeTimeout, the event assertions for the "success" case are thorough and validate the expected events following a successful channel upgrade timeout. This is important for ensuring that the channel upgrade timeout process correctly handles the timeout condition and emits the appropriate events.

Comment on lines 955 to 975
suite.Require().True(found)
suite.Require().Equal(uint64(1), nextSequenceAck, "sequence incremented for UNORDERED channel")
},
expEvents: func(path *ibctesting.Path) map[string]map[string]string {
return ibctesting.EventsMap{
types.EventTypeChannelFlushComplete: {
types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID,
types.AttributeKeyChannelID: path.EndpointA.ChannelID,
types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID,
types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID,
types.AttributeKeyChannelState: path.EndpointA.GetChannel().State.String(),
},
sdk.EventTypeMessage: {
sdk.AttributeKeyModule: types.AttributeValueCategory,
},
}
expEvents: func(path *ibctesting.Path) []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeChannelFlushComplete,
sdk.NewAttribute(types.AttributeKeyPortID, path.EndpointA.ChannelConfig.PortID),
sdk.NewAttribute(types.AttributeKeyChannelID, path.EndpointA.ChannelID),
sdk.NewAttribute(types.AttributeCounterpartyPortID, path.EndpointB.ChannelConfig.PortID),
sdk.NewAttribute(types.AttributeCounterpartyChannelID, path.EndpointB.ChannelID),
sdk.NewAttribute(types.AttributeKeyChannelState, path.EndpointA.GetChannel().State.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
}.ToABCIEvents()
},
},
{
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [851-972]

The expEvents function has been modified to return []abci.Event instead of the previous map[string]map[string]string type. This change is part of the refactor to use abci.Event for event creation and comparison, which is a more direct and type-safe approach to handling events in tests. The conversion of sdk.Events to abci.Events using .ToABCIEvents() at the end of the function is a necessary step to match the expected return type.

However, there's a minor issue with the usage of sdk.Events within the expEvents function. The sdk package is not explicitly imported in the provided code snippet, which could lead to a compilation error if it's not already imported elsewhere in the file. It's important to ensure that all necessary packages are imported to avoid compilation issues.

+ import sdk "github.com/cosmos/cosmos-sdk/types"

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Status

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between f29b72f and dd04e90.
Files selected for processing (5)
  • modules/apps/callbacks/types/events_test.go (7 hunks)
  • modules/apps/transfer/keeper/msg_server_test.go (1 hunks)
  • modules/core/02-client/keeper/events_test.go (3 hunks)
  • modules/core/04-channel/keeper/packet_test.go (4 hunks)
  • modules/core/04-channel/keeper/timeout_test.go (4 hunks)
Files skipped from review as they are similar to previous changes (4)
  • modules/apps/callbacks/types/events_test.go
  • modules/apps/transfer/keeper/msg_server_test.go
  • modules/core/04-channel/keeper/packet_test.go
  • modules/core/04-channel/keeper/timeout_test.go
Additional Context Used
Path-based Instructions (1)
modules/core/02-client/keeper/events_test.go (2)

**/*.go:
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


**/*_test.go:
Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request

Additional comments not posted (3)
modules/core/02-client/keeper/events_test.go (3)

4-4: The addition of the github.com/stretchr/testify/require import is appropriate for the introduced error checking in type assertions within the test functions.


23-24: The addition of error checking for the type assertion in TestMsgCreateClientEvents using require.True is a good practice, ensuring the test fails early if the type assertion is incorrect.


63-64: The addition of error checking for the type assertion in TestMsgUpdateClientEvents using require.True follows best practices, enhancing the test's reliability by ensuring it fails early if the type assertion fails.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Status

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between dd04e90 and 5181f1b.
Files selected for processing (2)
  • modules/core/04-channel/keeper/packet_test.go (4 hunks)
  • modules/core/04-channel/keeper/timeout_test.go (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • modules/core/04-channel/keeper/packet_test.go
  • modules/core/04-channel/keeper/timeout_test.go

@akaladarshi
Copy link
Contributor Author

While running make lint locally getting lot of errors.
Is this expected behaviour?.

@DimitrisJim
Copy link
Contributor

DimitrisJim commented Apr 3, 2024

While running make lint locally getting lot of errors.
Is this expected behaviour?.

hm, could you maybe post some of the errors? I can't say I've noticed it.

edit: ah, can repro. I'll give it a peek and see what linter is on about again. Appears to me the local linters don't match the ones used in the workflows. New rules might have been added.

@DimitrisJim
Copy link
Contributor

Can use 1.54.2 for now, checked your PR and no linter errors pop-up:

go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2

Need to bump golangci-lint and get the errors fixed there. That's for another PR though.

@crodriguezvega crodriguezvega changed the title refactor(tests): replace legacy event asserts with current one refactor(api!): replace legacy event asserts with current one Apr 11, 2024
Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Thank you, @akaladarshi. The changes look good to me!

@@ -167,36 +165,6 @@ func ParseProposalIDFromEvents(events []abci.Event) (uint64, error) {
return 0, fmt.Errorf("proposalID event attribute not found")
}

// AssertEventsLegacy asserts that expected events are present in the actual events.
// Expected map needs to be a subset of actual events to pass.
func AssertEventsLegacy(
Copy link
Contributor

Choose a reason for hiding this comment

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

I added migration docs, since this removal is API breaking.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Actionable comments outside the diff hunks (3)
docs/docs/05-migrations/13-v8-to-v9.md (3)

Line range hint 10-10: Consider rephrasing to improve clarity and flow: "This guide provides instructions for migrating to a new version of ibc-go."

- This guide provides instructions for migrating to a new version of ibc-go.
+ This guide outlines the steps required to migrate to the latest version of ibc-go.

Line range hint 19-19: The note about semantic versioning is crucial. Consider highlighting it or making it bold to ensure it catches the reader's attention.

- **Note:** ibc-go supports golang semantic versioning and therefore all imports must be updated on major version releases.
+ **Note:** ibc-go adheres to semantic versioning. Ensure all imports are updated when migrating to a new major version.

[!TIP]
Codebase Verification

The verification script has identified that the deprecated function coordinator.Setup is still being used in the file modules/core/04-channel/keeper/upgrade_test.go. This confirms that despite the deprecation notice, some parts of the codebase continue to use the deprecated functions, which could lead to maintenance issues in the future as these functions are planned to be removed in version 10.

Given this finding, the review comment is accurate in highlighting the need to ensure that deprecated functions are not used elsewhere in the codebase to prevent future maintenance issues.

Analysis chain

Line range hint 29-42: The documentation of API removals and deprecations is clear. However, ensure that the deprecated functions are not used elsewhere in the codebase to prevent future maintenance issues.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Check for usage of deprecated functions in the codebase.
rg --type go "coordinator.Setup|coordinator.SetupClients|coordinator.SetupConnections|coordinator.CreateConnections|coordinator.CreateChannels"

Length of output: 224

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Actionable comments outside the diff hunks (2)
docs/docs/05-migrations/13-v8-to-v9.md (2)

Line range hint 10-10: Possible spelling mistake in "ibc-go". It should be "IBC-Go" to maintain consistency with official naming conventions.

- ibc-go
+ IBC-Go

Line range hint 29-30: Ensure there is a space after each period in the listed removals under the "API removals" section to improve readability.

- The `exported.ChannelI` and `exported.CounterpartyChannelI` interfaces have been removed. Please use the concrete types.The `exported.ConnectionI` and `exported.CounterpartyConnectionI` interfaces have been removed. Please use the concrete types.
+ The `exported.ChannelI` and `exported.CounterpartyChannelI` interfaces have been removed. Please use the concrete types. The `exported.ConnectionI` and `exported.CounterpartyConnectionI` interfaces have been removed. Please use the concrete types.

@akaladarshi
Copy link
Contributor Author

Hey @crodriguezvega.
Are we waiting for some other tasks to be done before merging this PR?

@DimitrisJim
Copy link
Contributor

Are we waiting for some other tasks to be done before merging this PR?

Nope! Just reviews, I'll be reviewing it today, sorry for the delays! 🙏

Copy link
Contributor

@DimitrisJim DimitrisJim left a comment

Choose a reason for hiding this comment

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

This lgtm, thanks! I do wonder why we're sticking with the abci event but that's for another discussion.

suite.Require().True(hasEvent, "event: %s was not found in events", eventName)
}
}

// AssertEvents asserts that expected events are present in the actual events.
func AssertEvents(
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why we use the abci.Event here. I think this could be refactored to use sdk.Event (which is simply an alias for the abci.Event) and we can drop all the ToABCIEvent calls? Might be a thing to consider for a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, we can look into this in a followup!

@crodriguezvega crodriguezvega merged commit 3341773 into cosmos:main Apr 16, 2024
79 checks passed
@akaladarshi akaladarshi deleted the akaldarshi/event-testing-cleanup branch April 17, 2024 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Clean up event testing functions
4 participants