-
Notifications
You must be signed in to change notification settings - Fork 2
/
eventually_test.go
75 lines (69 loc) · 2.01 KB
/
eventually_test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package verify_test
import (
"testing"
"time"
"github.com/fluentassert/verify"
)
func TestEventually(t *testing.T) {
timeout := 100 * time.Millisecond
interval := 10 * time.Millisecond
t.Run("InitialPassed", func(t *testing.T) {
msg := verify.Eventually(timeout, interval, func() verify.FailureMessage {
return ""
})
assertPassed(t, msg)
})
t.Run("SecondPassed", func(t *testing.T) {
shouldPass := false
msg := verify.Eventually(timeout, interval, func() verify.FailureMessage {
if !shouldPass {
shouldPass = true // next exeucution will pass
return "fail"
}
return ""
})
assertPassed(t, msg)
})
t.Run("ReturnedTooLate", func(t *testing.T) {
msg := verify.Eventually(timeout, interval, func() verify.FailureMessage {
time.Sleep(2 * timeout)
return ""
})
assertFailed(t, msg, "function never passed, last failure message:\n")
})
t.Run("Failed", func(t *testing.T) {
msg := verify.Eventually(timeout, interval, func() verify.FailureMessage {
return "constant failure"
})
assertFailed(t, msg, "function never passed, last failure message:\nconstant failure")
})
}
func TestEventuallyChan(t *testing.T) {
timeout := 100 * time.Millisecond
interval := 10 * time.Millisecond
t.Run("Passed", func(t *testing.T) {
timer := time.NewTimer(timeout)
defer timer.Stop()
ticker := time.NewTicker(interval)
defer ticker.Stop()
msg := verify.EventuallyChan(timer.C, ticker.C, func() verify.FailureMessage {
return ""
})
assertPassed(t, msg)
})
t.Run("TimeoutBeforeStart", func(t *testing.T) {
ch := make(chan struct{})
close(ch)
msg := verify.EventuallyChan(ch, ch, func() verify.FailureMessage {
return ""
})
assertFailed(t, msg, "function never passed, last failure message:\n")
})
t.Run("Failed", func(t *testing.T) {
ch := make(chan struct{})
msg := verify.EventuallyChan(time.After(timeout), ch, func() verify.FailureMessage {
return "constant failure"
})
assertFailed(t, msg, "function never passed, last failure message:\nconstant failure")
})
}