Refactor double negatives in bgp/clustermesh/ipam testcase#44588
Refactor double negatives in bgp/clustermesh/ipam testcase#44588liyihuang wants to merge 1 commit intocilium:mainfrom
Conversation
Change the double negatives based on the comment cilium#44545 (comment). This update affects the BGP test cases as well as ClusterMesh and IPAM components based on search using assert.No Signed-off-by: Liyi Huang <liyi.huang@isovalent.com>
ba602ef to
6cd9581
Compare
|
/test |
giorio94
left a comment
There was a problem hiding this comment.
I personally feel the current implementation to be better aligned with the rest of the tests that also leverage testify helpers, but the proposed alternative looks reasonable to me. A few hunks are missing the actual check though, and would cause the test to incorrectly succeed also in case of failures.
| InitNodesRouterIDs := make(map[string]struct{}) | ||
| nodeConfigs, err := f.bgpnClient.List(ctx, meta_v1.ListOptions{}) | ||
| if !assert.NoError(c, err) { | ||
| if err != nil { |
There was a problem hiding this comment.
A few changes in this file are not correct, because the error check is no longer performed.
| sf, _ := statuses.Load("cluster4") | ||
| if !assert.NotNil(c, sf, "The status function for cluster4 should have been registered") { | ||
| if sf == nil { | ||
| c.Errorf("status function for cluster4 is nil") |
There was a problem hiding this comment.
Why was the error message changed?
I dont have personal preference if we should leverage |
/cc @joestringer It would be good to get your opinion as well, as you commented on the original PR. |
|
My intended feedback was effectively: "Don't assert that something is not negative". And if you're having trouble parsing that sentence, that's exactly my point ;). To put another way, prefer The The one other thing I'd add is that I have a mild preference for (a) / (b) over (c), just in order to minimize cognitive burden (one function call per line rather than multiple): (b) (c) (Comments unnecessary, just including them for explanation here) |
The two assertions are not equivalent, and lead to a completely different behavior though, as mentioned here. Consider the following snippet, that mimics the different hunks touched by this PR. The func TestError(t *testing.T) {
var cnt = 0
get := func() (*int, error) {
if cnt = cnt + 1; cnt < 3 {
return nil, errors.New("error")
}
return &cnt, nil
}
require.EventuallyWithT(t, func(c *assert.CollectT) {
var get, err = get()
if !assert.NoError(c, err) {
return
}
assert.Equal(c, 3, *get)
}, 10*time.Millisecond, 1*time.Millisecond)
assert.Equal(t, 3, cnt, "[get] should have been called three times")
}The Replacing it with For instance, the following test (the only difference is the proposed change): func TestError(t *testing.T) {
var cnt = 0
get := func() (*int, error) {
if cnt = cnt + 1; cnt < 3 {
return nil, errors.New("error")
}
return &cnt, nil
}
require.EventuallyWithT(t, func(c *assert.CollectT) {
var get, err = get()
if assert.Error(c, err) {
return
}
assert.Equal(c, 3, *get)
}, 10*time.Millisecond, 1*time.Millisecond)
assert.Equal(t, 3, cnt, "[get] should have been called three times")
}fails due to: which shows that '[get] The correct alternative would be something along the lines of (as also partially done in this PR): var get, err = get()
if err != nil {
c.Errorf("Unexpected error")
return
}
assert.Equal(c, 3, *get)Which has the downside of mixing bare checks and |
|
I see. This makes me think the double-negative pattern is even more problematic. What about just splitting the |
IMO the correct way of reading it is along the lines of "if the given assertion failed, then return", which avoids any double-negative confusion.
Sure, I'm not opposed if you feel that is easier to read. |
see the commit message