-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2038,6 +2038,93 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time | |
} | ||
} | ||
|
||
// Consistently asserts that given condition will be met for the entire | ||
// duration of waitFor time, periodically checking target function each tick. | ||
// | ||
// assert.Consistently(t, func() bool { return true; }, time.Second, 10*time.Millisecond) | ||
func Consistently(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { | ||
if h, ok := t.(tHelper); ok { | ||
h.Helper() | ||
} | ||
|
||
ch := make(chan bool, 1) | ||
|
||
timer := time.NewTimer(waitFor) | ||
defer timer.Stop() | ||
|
||
ticker := time.NewTicker(tick) | ||
defer ticker.Stop() | ||
|
||
for tick := ticker.C; ; { | ||
select { | ||
case <-timer.C: | ||
return true | ||
case <-tick: | ||
tick = nil | ||
go func() { ch <- condition() }() | ||
case v := <-ch: | ||
if !v { | ||
return Fail(t, "Condition never satisfied", msgAndArgs...) | ||
} | ||
tick = ticker.C | ||
} | ||
} | ||
} | ||
|
||
// ConsistentlyWithT asserts that given condition will be met for the entire | ||
// waitFor time, periodically checking target function each tick. In contrast | ||
// to Consistently, it supplies a CollectT to the condition function, so that | ||
// the condition function can use the CollectT to call other assertions. The | ||
// condition is considered "met" if no errors are raised across all ticks. The | ||
// supplied CollectT collects all errors from one tick (if there are any). If | ||
// the condition is not met once before waitFor, the collected error of the | ||
// failing tick are copied to t. | ||
// | ||
// externalValue := false | ||
// go func() { | ||
// time.Sleep(8*time.Second) | ||
// externalValue = true | ||
// }() | ||
// assert.ConsistentlyWithT(t, func(c *assert.CollectT) { | ||
// // add assertions as needed; any assertion failure will fail the current tick | ||
// assert.True(c, externalValue, "expected 'externalValue' to be true") | ||
// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false") | ||
func ConsistentlyWithT(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { | ||
if h, ok := t.(tHelper); ok { | ||
h.Helper() | ||
} | ||
|
||
ch := make(chan []error, 1) | ||
|
||
timer := time.NewTimer(waitFor) | ||
defer timer.Stop() | ||
|
||
ticker := time.NewTicker(tick) | ||
defer ticker.Stop() | ||
|
||
for tick := ticker.C; ; { | ||
select { | ||
case <-timer.C: | ||
return true | ||
case <-tick: | ||
tick = nil | ||
go func() { | ||
collect := new(CollectT) | ||
defer func() { | ||
ch <- collect.errors | ||
}() | ||
condition(collect) | ||
}() | ||
case errs := <-ch: | ||
if len(errs) > 0 { | ||
return Fail(t, "Condition never satisfied", msgAndArgs...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the errors to |
||
} | ||
|
||
tick = ticker.C | ||
} | ||
} | ||
} | ||
|
||
// Never asserts that the given condition doesn't satisfy in waitFor time, | ||
// periodically checking the target function each tick. | ||
// | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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: 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 ofConsistently
.