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

feat: add assert.Consistently #1606

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jfmyers9
Copy link

@jfmyers9 jfmyers9 commented Jun 4, 2024

Summary

This changeset adds the assert.Consistently and it's associated functions to assert that a condition is true over the entire period of waitFor. This is useful when testing the behavior of asynchronous functions.

Changes

  • assert.Consistently checks that a provided function is true for the entirety of waitFor
  • assert.Consistentlyf checks that a provided function is true for the entirety of waitFor and allows for formatting the error message
  • assert.ConsistentlyWithT checks that a provided function that accepts a testing.T is true for the entirety of waitFor
  • assert.ConsistentlyWithTf checks that a provided function that accepts a testing.T is true for the entirety of waitFor and allows for formatting the error message

Motivation

See: #1087 (comment)

Related issues

Closes #1087

This changeset adds the `assert.Consistently` and it's associated
functions to assert that a condition is true over the entire period of
`waitFor`. This is useful when testing the behavior of asynchronous
functions.

Closes stretchr#1087
@jfmyers9 jfmyers9 force-pushed the add-consistently branch from a11e144 to 5e6e95d Compare June 4, 2024 05:45
@dolmen
Copy link
Collaborator

dolmen commented Jun 13, 2024

I don't want more uses of CollectT (which should never have been exposed as a concrete type in the first place). So let's remove all the WithT functions for now.

@dolmen dolmen added enhancement pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require assert.Eventually About assert.Eventually/EventuallyWithT labels Jun 13, 2024
@dolmen
Copy link
Collaborator

dolmen commented Jun 13, 2024

We already have Never and Neverf which match the use case: it just need a reversed condition.

@z0marlin
Copy link

I don't want more uses of CollectT (which should never have been exposed as a concrete type in the first place). So let's remove all the WithT functions for now.

@dolmen, curious, what are the issues with CollectT? Also, I cannot say much about the CollectT type but I have found WithT quite useful in cases when I already have a certain set of assertions and want to assert that they succeed eventually (testing k8s controllers).

@dolmen
Copy link
Collaborator

dolmen commented Jun 13, 2024

CollectT should have been an interface to keep its implementation opaque.

@ccoVeille
Copy link
Contributor

We already have Never and Neverf which match the use case: it just need a reversed condition.

Make sense, what would be the reversed condition asserter ? NotNever ? The name would be strange. But using another name that is not the opposite of Never would be strange no?

@jfmyers9
Copy link
Author

In my opinion the opposite of Never would be Always.

Consistently works better as a name when paired with Eventually.

I don't have a strong preference on naming here.

go func() { ch <- condition() }()
case v := <-ch:
if !v {
return Fail(t, "Condition never satisfied", msgAndArgs...)

Choose a reason for hiding this comment

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

nit: not sure if this is the right error message. To me, "Condition never satisfied" makes sense in the case of Eventually. The term never is not applicable in the context of Consistently.

}()
case errs := <-ch:
if len(errs) > 0 {
return Fail(t, "Condition never satisfied", msgAndArgs...)

Choose a reason for hiding this comment

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

add the errors to t first before calling Fail. Reference: https://github.com/stretchr/testify/blob/master/assert/assertions.go#L2017

@jfmyers9
Copy link
Author

If we are happy with Always, then this just becomes:

func Always(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool {
	return Never(t, func() bool { return !condition() }, waitFor, tick, msgAndArgs)
}

I missed the fact that Never exists when implementing this. Thoughts on moving to that approach?

@z0marlin
Copy link

CollectT should have been an interface to keep its implementation opaque.

Got it. So you would like to get rid of CollectT but not the WithT feature?

@z0marlin
Copy link

I missed the fact that Never exists when implementing this. Thoughts on moving to that approach?

My only concern here is that using Never will not allow having something like ConsistentlyWithT or AlwaysWithT (not really concerned about the naming). Not sure about how big of a use-case it is though.

@dolmen
Copy link
Collaborator

dolmen commented Jun 13, 2024

Never isn't a great name as it isn't what users expect. Always is in the same bucket, or even worse. I think that Consistently is a better name as it is consistent (pun intended) with Eventually, even if inconsistent with Never.

I was reluctant to add Consistently (I want to limit the increase the API surface), but as Never already exists it would be consistent (pun intended) to have it also. I understand also that ConsistentlyWithT would be convenient as it would allow to use require. NeverWithT would not make sense.

About the implementation, I think it's a bit early because we are not yet done with Eventually's bugs. I would like every eyes to focus on #1611.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert.Eventually About assert.Eventually/EventuallyWithT enhancement pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: assert.Consistently
4 participants