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{})
| 2081 | // assert.True(c, externalValue, "expected 'externalValue' to be true") |
| 2082 | // }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false") |
| 2083 | func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { |
| 2084 | if h, ok := t.(tHelper); ok { |
| 2085 | h.Helper() |
| 2086 | } |
| 2087 | |
| 2088 | var lastFinishedTickErrs []error |
| 2089 | ch := make(chan *CollectT, 1) |
| 2090 | |
| 2091 | checkCond := func() { |
| 2092 | collect := new(CollectT) |
| 2093 | defer func() { |
| 2094 | ch <- collect |
| 2095 | }() |
| 2096 | condition(collect) |
| 2097 | } |
| 2098 | |
| 2099 | timer := time.NewTimer(waitFor) |
| 2100 | defer timer.Stop() |
| 2101 | |
| 2102 | ticker := time.NewTicker(tick) |
| 2103 | defer ticker.Stop() |
| 2104 | |
| 2105 | var tickC <-chan time.Time |
| 2106 | |
| 2107 | // Check the condition once first on the initial call. |
| 2108 | go checkCond() |
| 2109 | |
| 2110 | for { |
| 2111 | select { |
| 2112 | case <-timer.C: |
| 2113 | for _, err := range lastFinishedTickErrs { |
| 2114 | t.Errorf("%v", err) |
| 2115 | } |
| 2116 | return Fail(t, "Condition never satisfied", msgAndArgs...) |
| 2117 | case <-tickC: |
| 2118 | tickC = nil |
| 2119 | go checkCond() |
| 2120 | case collect := <-ch: |
| 2121 | if !collect.failed() { |
| 2122 | return true |
| 2123 | } |
| 2124 | // Keep the errors from the last ended condition, so that they can be copied to t if timeout is reached. |
| 2125 | lastFinishedTickErrs = collect.errors |
| 2126 | tickC = ticker.C |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | // Never asserts that the given condition doesn't satisfy in waitFor time, |
| 2132 | // periodically checking the target function each tick. |