`NeverWithT` for using assertions inside `Never`
Description
If we want to check multiple conditions inside Never, currently, we have to do
assert.Never(t, func() bool {
if GetVal1() == 1 {
return true
}
if GetVal2() == 2 {
return true
}
return false
}, ...)
or
assert.Never(t, func() bool {
val1 := GetVal1()
val2 := GetVal2()
return val1 == 1 || val2 == 2
}, ...)
or
assert.Never(t, func() bool {
return GetVal1() == 1
}, ...)
assert.Never(t, func() bool {
return GetVal2() == 2
}, ...)
however for N conditions
- has the problem of
Nif statements - has the problem of chaining
Ncomparisons in the return statement - is slower, because we wait
Ntimes as long
Proposed solution
For calling assertions inside Eventually, there is EventuallyWithT, e.g.
assert.EventuallyWithT(t, func(c *assert.CollectT) {
assert.Equal(c, 2, GetVal())
}, ...)
It would be cool if we could write:
assert.NeverWithT(t, func(c *assert.CollectT) {
assert.Equal(c, 1, GetVal1())
assert.Equal(c, 2, GetVal2())
}, ...)
However, regarding the semantics: The function would need to know that ALL assertions failed, which is technically not possible right now to my understanding.
Use case
I'm writing a lot of Kubernetes integration tests for operators, where we often need to check that async operations DO or DO NOT change the Kubernetes states -- via Eventually and Never.
I'm writing a lot of Kubernetes integration tests for operators, where we often need to check that async operations DO or DO NOT change the Kubernetes states -- via Eventually and Never.
What's the difference here between use of Never vs time.Sleep(...) followed by the assertions?
Good point, some cases can be covered like this. However when the order of processing operations cannot be predicted, I would really want to know that certain conditions never occurred over a period of time, e.g. 5s in an interval of 500ms, not just at one point in the future.
I guess the user could create a ticker and invert their assertions, but that's the reason why I opened the issue.
That's not what "never" means though, the condition most certainly could have happened but not been sampled. In my opinion the Never assertion should not have been added to testify for this reason. Eventually is useful because it allows you to assert the outcome of an asynchronous function in close to as short a time as it will take. I have the same beef with Consistently (the inverse of Never).