-
Notifications
You must be signed in to change notification settings - Fork 2
/
eventually.go
49 lines (41 loc) · 1.02 KB
/
eventually.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package verify
import (
"time"
)
// Eventually executes the test function until it returns an empty FailureMessage
// or timeout elapses.
func Eventually(timeout, interval time.Duration, fn func() FailureMessage) FailureMessage {
timer := time.NewTimer(timeout)
defer timer.Stop()
ticker := time.NewTicker(interval)
defer ticker.Stop()
return EventuallyChan(timer.C, ticker.C, fn)
}
// EventuallyChan executes the test function until it returns an empty FailureMessage or timeout elapses.
func EventuallyChan[TTimerPayload, TTickPayload any](timeout <-chan (TTimerPayload), ticker <-chan (TTickPayload), fn func() FailureMessage) FailureMessage {
var err string
fail := func() FailureMessage {
return FailureMessage("function never passed, last failure message:\n" + err)
}
for {
select {
case <-timeout:
return fail()
default:
}
err = string(fn())
select {
case <-timeout:
return fail()
default:
}
if err == "" {
return ""
}
select {
case <-timeout:
return fail()
case <-ticker:
}
}
}