-
Notifications
You must be signed in to change notification settings - Fork 579
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: add ParsePacketsFromEvents #4975
test: add ParsePacketsFromEvents #4975
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.
thanks @tbruyelle 🙏
testing/events.go
Outdated
@@ -120,6 +120,22 @@ func ParsePacketFromEvents(events []abci.Event) (channeltypes.Packet, error) { | |||
return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found") | |||
} | |||
|
|||
// ParsePacketsFromEvents parses events emitted from a MsgRecvPacket and returns | |||
// all the packets found. | |||
func ParsePacketsFromEvents(events []abci.Event) ([]channeltypes.Packet, error) { |
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.
I'd actually prefer if the main logic was happening in this function and have ParsePacketFromEvents
would call into this and pick the first element from the result. I'm ok either way though.
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.
Yeah, the implementation might become easier to reason like that.
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.
Good idea, so I added a test and then moved the logic from ParsePacketFromEvents
to ParsePacketsFromEvents
.
Thanks for this :) |
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.
Thank you, @tbruyelle.
testing/events.go
Outdated
func ParsePacketsFromEvents(events []abci.Event) ([]channeltypes.Packet, error) { | ||
var packets []channeltypes.Packet | ||
for i, ev := range events { | ||
if ev.Type == channeltypes.EventTypeSendPacket { |
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.
I think we could also remove this if
statement, because the same one is in ParsePacketFromEvents
.
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.
done with the logic moved from one function to the other.
testing/events.go
Outdated
@@ -120,6 +120,22 @@ func ParsePacketFromEvents(events []abci.Event) (channeltypes.Packet, error) { | |||
return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found") | |||
} | |||
|
|||
// ParsePacketsFromEvents parses events emitted from a MsgRecvPacket and returns | |||
// all the packets found. | |||
func ParsePacketsFromEvents(events []abci.Event) ([]channeltypes.Packet, error) { |
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.
Yeah, the implementation might become easier to reason like that.
@tbruyelle if you agree with the review comments, would you be able to work on them? Otherwise, if you're busy, we are happy to take over the PR and address the comments. If you give me permissions to push to your branch, I am happy to do that. |
@crodriguezvega oups indeed I was super busy the past weeks, but today is quieter, I'll address the comments, thanks for the ping! |
@crodriguezvega I addressed all the comments. As requested I moved the logic from one function to the other, and also added a test before that to ensure I didn't break anything during the process. |
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.
Thank you for addressing the comments and adding a test, @tbruyelle. I have left a few more comments now. :)
testing/events_test.go
Outdated
) | ||
|
||
func TestParsePacketsFromEvents(t *testing.T) { | ||
tests := []struct { |
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.
nit: this is the naming we use in the majority of other tests.
tests := []struct { | |
testCases := []struct { |
testing/events_test.go
Outdated
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require := require.New(t) | ||
assert := assert.New(t) | ||
|
||
allPackets, err := ibctesting.ParsePacketsFromEvents(tt.events) | ||
|
||
if tt.expectedError != "" { | ||
assert.ErrorContains(err, tt.expectedError) | ||
} else { | ||
require.NoError(err) | ||
assert.Equal(tt.expectedPackets, allPackets) | ||
} | ||
|
||
firstPacket, err := ibctesting.ParsePacketFromEvents(tt.events) | ||
|
||
if tt.expectedError != "" { | ||
assert.ErrorContains(err, tt.expectedError) | ||
} else { | ||
require.NoError(err) | ||
assert.Equal(tt.expectedPackets[0], firstPacket) | ||
} | ||
}) | ||
} |
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.
I think this could be written like this (assuming that tests
is renamed to testCases
):
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
allPackets, err := ibctesting.ParsePacketsFromEvents(tc.events)
if tc.expectedError == "" {
require.NoError(t, err)
require.Equal(t, tc.expectedPackets, allPackets)
} else {
require.ErrorContains(t, err, tc.expectedError)
}
firstPacket, err := ibctesting.ParsePacketFromEvents(tc.events)
if tc.expectedError == "" {
require.NoError(t, err)
require.Equal(t, tc.expectedPackets[0], firstPacket)
} else {
require.ErrorContains(t, err, tc.expectedError)
}
})
}
So that:
- We don't need to import
assert
. - We execute first the checks for the success case.
testing/events_test.go
Outdated
expectedError: "ibctesting.ParsePacketsFromEvents: strconv.ParseUint: parsing \"x\": invalid syntax", | ||
}, | ||
{ | ||
name: "ok: events with packets", |
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.
We usually have the success case the first on of the slice.
name: "ok: events with packets", | |
name: "success", |
testing/events_test.go
Outdated
{ | ||
name: "fail: no events", | ||
events: []abci.Event{}, | ||
expectedError: "ibctesting.ParsePacketsFromEvents: acknowledgement event attribute not found", |
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.
Since we are using ErrorContains
it could be enough to just have this (same for the other failure test cases):
expectedError: "ibctesting.ParsePacketsFromEvents: acknowledgement event attribute not found", | |
expectedError: "acknowledgement event attribute not found", |
@crodriguezvega comments addressed here 8e06417
|
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #4975 +/- ##
==========================================
- Coverage 79.59% 79.57% -0.03%
==========================================
Files 189 189
Lines 13256 13261 +5
==========================================
+ Hits 10551 10552 +1
- Misses 2284 2288 +4
Partials 421 421
|
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.
Nice work, @tbruyelle. Thank you for addressing all the comments!
If you rebase with main, I will try to merge this before your branch gets out of sync again. 🤞
8e06417
to
bc9bc8a
Compare
Thank you Carlos, I've just rebased on main! |
ah, those grandpa failures can be safely disregarded. This will be resolved with #5092 |
Closes: cosmos#4287 Returns all the packets found in the events. Comes in addition with ParsePacketFromEvents (singular version), which only returns the first packet found in the events.
Because errmod used in clienttypes.ParseHeight() returns a local path.
- rename tests to testCases, tt to tc - remove assert package usage - revert `if tc.expectedError` condition - rename success case to "success", move it on top - remove `expectedError` common prefix
bc9bc8a
to
4974057
Compare
Description
Returns all the packets found in the events. Comes in addition with ParsePacketFromEvents (singular version), which only returns the first packet found in the events.
closes: #4287
Commit Message / Changelog Entry
see the guidelines for commit messages. (view raw markdown for examples)
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.
docs/
) or specification (x/<module>/spec/
).godoc
comments.Files changed
in the Github PR explorer.Codecov Report
in the comment section below once CI passes.