EventuallyWithT asserts that given condition will be met in waitFor time, periodically checking target function each tick. In contrast to Eventually, it supplies a CollectT to the condition function, so that the condition function can use the CollectT to call other assertions. The condition is consi
(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{})
| 2014 | // assert.True(c, externalValue, "expected 'externalValue' to be true") |
| 2015 | // }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false") |
| 2016 | func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { |
| 2017 | if h, ok := t.(tHelper); ok { |
| 2018 | h.Helper() |
| 2019 | } |
| 2020 | |
| 2021 | var lastFinishedTickErrs []error |
| 2022 | ch := make(chan *CollectT, 1) |
| 2023 | |
| 2024 | timer := time.NewTimer(waitFor) |
| 2025 | defer timer.Stop() |
| 2026 | |
| 2027 | ticker := time.NewTicker(tick) |
| 2028 | defer ticker.Stop() |
| 2029 | |
| 2030 | for tick := ticker.C; ; { |
| 2031 | select { |
| 2032 | case <-timer.C: |
| 2033 | for _, err := range lastFinishedTickErrs { |
| 2034 | t.Errorf("%v", err) |
| 2035 | } |
| 2036 | return Fail(t, "Condition never satisfied", msgAndArgs...) |
| 2037 | case <-tick: |
| 2038 | tick = nil |
| 2039 | go func() { |
| 2040 | collect := new(CollectT) |
| 2041 | defer func() { |
| 2042 | ch <- collect |
| 2043 | }() |
| 2044 | condition(collect) |
| 2045 | }() |
| 2046 | case collect := <-ch: |
| 2047 | if !collect.failed() { |
| 2048 | return true |
| 2049 | } |
| 2050 | // Keep the errors from the last ended condition, so that they can be copied to t if timeout is reached. |
| 2051 | lastFinishedTickErrs = collect.errors |
| 2052 | tick = ticker.C |
| 2053 | } |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | // Never asserts that the given condition doesn't satisfy in waitFor time, |
| 2058 | // periodically checking the target function each tick. |