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

test: introduce tests for OnTimeoutPacket and OnAcknowledgementPacket #1169

Merged
merged 2 commits into from
Aug 22, 2023

Conversation

insumity
Copy link
Contributor

@insumity insumity commented Jul 31, 2023

Description

Closes: #362

As described in this comment we cannot test those methods in ibc_module_test.go so we test in relay_test.go instead.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • Included the correct type prefix in the PR title
  • Targeted the correct branch (see PR Targeting)
  • Provided a link to the relevant issue or specification
  • Reviewed "Files changed" and left comments if necessary
  • Confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • Confirmed the correct type prefix in the PR title
  • Confirmed all author checklist items have been addressed
  • Confirmed that this PR does not change production code

@insumity insumity force-pushed the insumity/improve-UBT-tests branch 2 times, most recently from e121be8 to 6933b85 Compare August 15, 2023 10:38
@insumity insumity changed the title Add test for OnTimeoutPacket. test: introduce tests for OnTimeoutPacket and OnAcknowledgementPacket Aug 15, 2023
@insumity insumity marked this pull request as ready for review August 15, 2023 10:49
@insumity insumity requested a review from a team as a code owner August 15, 2023 10:49
Copy link
Contributor

@shaspitz shaspitz left a comment

Choose a reason for hiding this comment

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

I like the granularity of the individual tests, these are easy to follow 👍

I've left some comments regarding intention about what we're actually testing.

In the past there's been some tests in this repo that make it seem like a method is functioning as expected, but in fact the method had a bug that was hidden by vaguely intentioned and overly generic tests (example: just checking that a method doesn't return an error, when there's more complexity to be tested under the hood). Imo it's better to have no test coverage over vaguely intentioned test coverage.

This is where I'm coming from on these seemingly nitpicky comments :)

x/ccv/provider/keeper/relay_test.go Show resolved Hide resolved

// StopConsumerChain receives a `closeChan` parameter. If the `closeChan` argument is true, then
// we register additional expected mock calls that capture the fact that the underlying channel is closed.
if closeChan {
Copy link
Contributor

@shaspitz shaspitz Aug 15, 2023

Choose a reason for hiding this comment

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

Note that when closeChan is false, we're not mocking anything for StopConsumerChain, as the name (SetupForStoppingConsumerChain) suggests. This func becomes a more generic setup func which mocks and calls CreateConsumerClient and SetConsumerChain.

Maybe we refactor to split things out and/or make naming more accurate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I rewrote the godoc of this function to clearly state this.

Maybe we refactor to split things out and/or make naming more accurate

I agree. It would be nice to have 2 functions here: i) SetupForStoppingConsumerChain that just sets up the consumer chain for stopping, ii) TestChannelIsClosed that asserts that the underlying channel is closed when we call StopConsumerChain with closeChan being true. Then the idea would be that in a test we do something like the following:

SetupForStoppingConsumerChain(...)
StopConsumerChain(...) // e.g., we call to stop the consumer chain through `OnTimeoutPacket`
TestChannelIsClosed(...)
TestProviderStateIsCleanedAfterConsumerChainIsStopped(...) // tests that the provider's chain is closed and hence is used as a proxy to verify that the chain was closed 

where TestChannelIsClosed would need to set the expectations from GetMocksForStopConsumerChain, so something like:

func TestChannelIsClosed(...) {
    ....
    mocks.MockChannelKeeper.EXPECT().ChanCloseInit(...).Times(1)
}

The issue with the above approach is that expectations are set after the StopConsumerChain call and hence when StopConsumerChain is called we would fail with something akin to Unexpected call to ChanCloseInit([1]).
To solve the issue, this PR introduces the SetupForStoppingConsumerChainChannel function and does

SetupForStoppingConsumerChain(...)
SetupForStoppingConsumerChainChannel(...)
StopConsumerChain(...)
TestProviderStateIsCleanedAfterConsumerChainIsStopped(...)

The above approach might not be that beautiful since we do testing before (i.e., test that the channel would be closed) and after (i.e., that the producer's chain is cleaned) the StopConsumerChain. Nevertheless, this approachs feels the nicest way to circumvent the mock expectations issue I mentioned above.

x/ccv/provider/keeper/relay_test.go Outdated Show resolved Hide resolved
x/ccv/provider/keeper/relay_test.go Show resolved Hide resolved
Copy link
Contributor

@p-offtermatt p-offtermatt left a comment

Choose a reason for hiding this comment

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

Thanks for the work!
I agree with Shawns comments, it's hard to tell whether the tests actually test meaningful behaviour. I'd recommend asserting stronger properties in the tests.

It may or may not be helpful to go back to the spec and see what the spec claims about the functionality here, e.g. see https://github.com/cosmos/ibc/blob/jk/ccvTLA/spec/app/ics-028-cross-chain-validation/methods.md#consumer-chain-removal

x/ccv/provider/keeper/relay_test.go Show resolved Hide resolved
@github-actions github-actions bot added C:Testing Assigned automatically by the PR labeler C:x/provider Assigned automatically by the PR labeler labels Aug 18, 2023
@insumity insumity force-pushed the insumity/improve-UBT-tests branch 2 times, most recently from 07f4e4d to 9456868 Compare August 18, 2023 09:33
@@ -569,48 +572,12 @@ func TestStopConsumerChain(t *testing.T) {
require.NoError(t, err)
}

testProviderStateIsCleaned(t, ctx, providerKeeper, "chainID", "channelID")
testkeeper.TestProviderStateIsCleanedAfterConsumerChainIsStopped(t, ctx, providerKeeper, "chainID", "channelID")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because this is used in relay_test.go as well now, the function is moved to testutil/keeper/unit_test_helpers.go.

Copy link
Contributor

@shaspitz shaspitz left a comment

Choose a reason for hiding this comment

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

LGTM thanks!

Copy link
Contributor

@p-offtermatt p-offtermatt left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the work!

@insumity insumity merged commit aa0063d into main Aug 22, 2023
12 of 13 checks passed
@insumity insumity deleted the insumity/improve-UBT-tests branch August 22, 2023 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:Testing Assigned automatically by the PR labeler C:x/provider Assigned automatically by the PR labeler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve UTs for ibc_module.go
3 participants