-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitfor_test.go
155 lines (126 loc) · 3.13 KB
/
waitfor_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package waitfor_test
import (
"sync"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
"github.com/st3v/waitfor"
)
type condition struct {
sync.RWMutex
counter int
result bool
}
func (c *condition) Check() bool {
c.Lock()
defer c.Unlock()
c.counter++
return c.result
}
func (c *condition) CheckCount() int {
c.RLock()
defer c.RUnlock()
return c.counter
}
func (c *condition) SetResult(result bool) {
c.Lock()
defer c.Unlock()
c.result = result
}
var _ = Describe("waitfor", func() {
var (
cond condition
interval = 10 * time.Millisecond
)
BeforeEach(func() {
cond.Lock()
cond = condition{}
})
Describe(".ConditionWithTimeout", func() {
var (
err error
timeout = 100 * time.Millisecond
)
JustBeforeEach(func() {
err = waitfor.ConditionWithTimeout(cond.Check, interval, timeout)
})
Context("when the check does not succeed", func() {
It("returns a corresponding error", func() {
Expect(err).To(MatchError(waitfor.ErrTimeoutExceeded))
})
It("returns after the specified timeout", func() {
Expect(cond.CheckCount()).To(BeNumerically("<", 11))
})
It("repeatedly checked the condition", func() {
Expect(cond.CheckCount()).To(BeNumerically(">", 3))
})
})
Context("when the check succeeds initially", func() {
BeforeEach(func() {
cond.SetResult(true)
})
It("checked the condition only once", func() {
Expect(cond.CheckCount()).To(Equal(1))
})
It("does not return an error", func() {
Expect(err).ToNot(HaveOccurred())
})
})
Context("when the check succeeds eventually", func() {
BeforeEach(func() {
go func() {
<-time.After(timeout / 2)
cond.SetResult(true)
}()
})
It("checked the condition multiple times", func() {
Expect(cond.CheckCount()).To(BeNumerically(">", 1))
})
It("does not return an error", func() {
Expect(err).ToNot(HaveOccurred())
})
})
})
Describe(".Condition", func() {
var (
ctx context.Context
cancel context.CancelFunc
errChan chan error
)
JustBeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background())
errChan = make(chan error)
go waitfor.Condition(cond.Check, interval, errChan, ctx)
})
AfterEach(func() {
cancel()
})
Context("when the check does not succeed", func() {
It("keeps checking the condition repeatedly", func() {
Eventually(cond.CheckCount).Should(BeNumerically(">=", 20))
})
Context("when the context is canceled", func() {
It("stops checking the condition", func() {
cancel()
Eventually(errChan).Should(Receive())
count := cond.CheckCount()
Consistently(cond.CheckCount).Should(Equal(count))
})
It("returns a corresponding error on its error channel", func() {
cancel()
Eventually(errChan).Should(Receive(MatchError(context.Canceled)))
})
})
})
Context("when the check succeeds", func() {
BeforeEach(func() {
cond.SetResult(true)
})
It("checks the condition only once", func() {
Eventually(cond.CheckCount).Should(Equal(1))
Consistently(cond.CheckCount).Should(Equal(1))
})
})
})
})