initial commit
[govpp.git] / vendor / github.com / onsi / gomega / internal / asyncassertion / async_assertion_test.go
1 package asyncassertion_test
2
3 import (
4         "errors"
5         "time"
6
7         . "github.com/onsi/ginkgo"
8         . "github.com/onsi/gomega"
9         . "github.com/onsi/gomega/internal/asyncassertion"
10 )
11
12 var _ = Describe("Async Assertion", func() {
13         var (
14                 failureMessage string
15                 callerSkip     int
16         )
17
18         var fakeFailHandler = func(message string, skip ...int) {
19                 failureMessage = message
20                 callerSkip = skip[0]
21         }
22
23         BeforeEach(func() {
24                 failureMessage = ""
25                 callerSkip = 0
26         })
27
28         Describe("Eventually", func() {
29                 Context("the positive case", func() {
30                         It("should poll the function and matcher", func() {
31                                 counter := 0
32                                 a := New(AsyncAssertionTypeEventually, func() int {
33                                         counter++
34                                         return counter
35                                 }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
36
37                                 a.Should(BeNumerically("==", 5))
38                                 Ω(failureMessage).Should(BeZero())
39                         })
40
41                         It("should continue when the matcher errors", func() {
42                                 counter := 0
43                                 a := New(AsyncAssertionTypeEventually, func() interface{} {
44                                         counter++
45                                         if counter == 5 {
46                                                 return "not-a-number" //this should cause the matcher to error
47                                         }
48                                         return counter
49                                 }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
50
51                                 a.Should(BeNumerically("==", 5), "My description %d", 2)
52
53                                 Ω(failureMessage).Should(ContainSubstring("Timed out after"))
54                                 Ω(failureMessage).Should(ContainSubstring("My description 2"))
55                                 Ω(callerSkip).Should(Equal(4))
56                         })
57
58                         It("should be able to timeout", func() {
59                                 counter := 0
60                                 a := New(AsyncAssertionTypeEventually, func() int {
61                                         counter++
62                                         return counter
63                                 }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
64
65                                 a.Should(BeNumerically(">", 100), "My description %d", 2)
66
67                                 Ω(counter).Should(BeNumerically(">", 8))
68                                 Ω(counter).Should(BeNumerically("<=", 10))
69                                 Ω(failureMessage).Should(ContainSubstring("Timed out after"))
70                                 Ω(failureMessage).Should(MatchRegexp(`\<int\>: \d`), "Should pass the correct value to the matcher message formatter.")
71                                 Ω(failureMessage).Should(ContainSubstring("My description 2"))
72                                 Ω(callerSkip).Should(Equal(4))
73                         })
74                 })
75
76                 Context("the negative case", func() {
77                         It("should poll the function and matcher", func() {
78                                 counter := 0
79                                 a := New(AsyncAssertionTypeEventually, func() int {
80                                         counter += 1
81                                         return counter
82                                 }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
83
84                                 a.ShouldNot(BeNumerically("<", 3))
85
86                                 Ω(counter).Should(Equal(3))
87                                 Ω(failureMessage).Should(BeZero())
88                         })
89
90                         It("should timeout when the matcher errors", func() {
91                                 a := New(AsyncAssertionTypeEventually, func() interface{} {
92                                         return 0 //this should cause the matcher to error
93                                 }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
94
95                                 a.ShouldNot(HaveLen(0), "My description %d", 2)
96
97                                 Ω(failureMessage).Should(ContainSubstring("Timed out after"))
98                                 Ω(failureMessage).Should(ContainSubstring("Error:"))
99                                 Ω(failureMessage).Should(ContainSubstring("My description 2"))
100                                 Ω(callerSkip).Should(Equal(4))
101                         })
102
103                         It("should be able to timeout", func() {
104                                 a := New(AsyncAssertionTypeEventually, func() int {
105                                         return 0
106                                 }, fakeFailHandler, time.Duration(0.1*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
107
108                                 a.ShouldNot(Equal(0), "My description %d", 2)
109
110                                 Ω(failureMessage).Should(ContainSubstring("Timed out after"))
111                                 Ω(failureMessage).Should(ContainSubstring("<int>: 0"), "Should pass the correct value to the matcher message formatter.")
112                                 Ω(failureMessage).Should(ContainSubstring("My description 2"))
113                                 Ω(callerSkip).Should(Equal(4))
114                         })
115                 })
116
117                 Context("with a function that returns multiple values", func() {
118                         It("should eventually succeed if the additional arguments are nil", func() {
119                                 i := 0
120                                 Eventually(func() (int, error) {
121                                         i++
122                                         return i, nil
123                                 }).Should(Equal(10))
124                         })
125
126                         It("should eventually timeout if the additional arguments are not nil", func() {
127                                 i := 0
128                                 a := New(AsyncAssertionTypeEventually, func() (int, error) {
129                                         i++
130                                         return i, errors.New("bam")
131                                 }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
132                                 a.Should(Equal(2))
133
134                                 Ω(failureMessage).Should(ContainSubstring("Timed out after"))
135                                 Ω(failureMessage).Should(ContainSubstring("Error:"))
136                                 Ω(failureMessage).Should(ContainSubstring("bam"))
137                                 Ω(callerSkip).Should(Equal(4))
138                         })
139                 })
140
141                 Context("Making an assertion without a registered fail handler", func() {
142                         It("should panic", func() {
143                                 defer func() {
144                                         e := recover()
145                                         RegisterFailHandler(Fail)
146                                         if e == nil {
147                                                 Fail("expected a panic to have occurred")
148                                         }
149                                 }()
150
151                                 RegisterFailHandler(nil)
152                                 c := make(chan bool, 1)
153                                 c <- true
154                                 Eventually(c).Should(Receive())
155                         })
156                 })
157         })
158
159         Describe("Consistently", func() {
160                 Describe("The positive case", func() {
161                         Context("when the matcher consistently passes for the duration", func() {
162                                 It("should pass", func() {
163                                         calls := 0
164                                         a := New(AsyncAssertionTypeConsistently, func() string {
165                                                 calls++
166                                                 return "foo"
167                                         }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
168
169                                         a.Should(Equal("foo"))
170                                         Ω(calls).Should(BeNumerically(">", 8))
171                                         Ω(calls).Should(BeNumerically("<=", 10))
172                                         Ω(failureMessage).Should(BeZero())
173                                 })
174                         })
175
176                         Context("when the matcher fails at some point", func() {
177                                 It("should fail", func() {
178                                         calls := 0
179                                         a := New(AsyncAssertionTypeConsistently, func() interface{} {
180                                                 calls++
181                                                 if calls > 5 {
182                                                         return "bar"
183                                                 }
184                                                 return "foo"
185                                         }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
186
187                                         a.Should(Equal("foo"))
188                                         Ω(failureMessage).Should(ContainSubstring("to equal"))
189                                         Ω(callerSkip).Should(Equal(4))
190                                 })
191                         })
192
193                         Context("when the matcher errors at some point", func() {
194                                 It("should fail", func() {
195                                         calls := 0
196                                         a := New(AsyncAssertionTypeConsistently, func() interface{} {
197                                                 calls++
198                                                 if calls > 5 {
199                                                         return 3
200                                                 }
201                                                 return []int{1, 2, 3}
202                                         }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
203
204                                         a.Should(HaveLen(3))
205                                         Ω(failureMessage).Should(ContainSubstring("HaveLen matcher expects"))
206                                         Ω(callerSkip).Should(Equal(4))
207                                 })
208                         })
209                 })
210
211                 Describe("The negative case", func() {
212                         Context("when the matcher consistently passes for the duration", func() {
213                                 It("should pass", func() {
214                                         c := make(chan bool)
215                                         a := New(AsyncAssertionTypeConsistently, c, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
216
217                                         a.ShouldNot(Receive())
218                                         Ω(failureMessage).Should(BeZero())
219                                 })
220                         })
221
222                         Context("when the matcher fails at some point", func() {
223                                 It("should fail", func() {
224                                         c := make(chan bool)
225                                         go func() {
226                                                 time.Sleep(time.Duration(100 * time.Millisecond))
227                                                 c <- true
228                                         }()
229
230                                         a := New(AsyncAssertionTypeConsistently, c, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
231
232                                         a.ShouldNot(Receive())
233                                         Ω(failureMessage).Should(ContainSubstring("not to receive anything"))
234                                 })
235                         })
236
237                         Context("when the matcher errors at some point", func() {
238                                 It("should fail", func() {
239                                         calls := 0
240                                         a := New(AsyncAssertionTypeConsistently, func() interface{} {
241                                                 calls++
242                                                 return calls
243                                         }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
244
245                                         a.ShouldNot(BeNumerically(">", 5))
246                                         Ω(failureMessage).Should(ContainSubstring("not to be >"))
247                                         Ω(callerSkip).Should(Equal(4))
248                                 })
249                         })
250                 })
251
252                 Context("with a function that returns multiple values", func() {
253                         It("should consistently succeed if the additional arguments are nil", func() {
254                                 i := 2
255                                 Consistently(func() (int, error) {
256                                         i++
257                                         return i, nil
258                                 }).Should(BeNumerically(">=", 2))
259                         })
260
261                         It("should eventually timeout if the additional arguments are not nil", func() {
262                                 i := 2
263                                 a := New(AsyncAssertionTypeEventually, func() (int, error) {
264                                         i++
265                                         return i, errors.New("bam")
266                                 }, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
267                                 a.Should(BeNumerically(">=", 2))
268
269                                 Ω(failureMessage).Should(ContainSubstring("Error:"))
270                                 Ω(failureMessage).Should(ContainSubstring("bam"))
271                                 Ω(callerSkip).Should(Equal(4))
272                         })
273                 })
274
275                 Context("Making an assertion without a registered fail handler", func() {
276                         It("should panic", func() {
277                                 defer func() {
278                                         e := recover()
279                                         RegisterFailHandler(Fail)
280                                         if e == nil {
281                                                 Fail("expected a panic to have occurred")
282                                         }
283                                 }()
284
285                                 RegisterFailHandler(nil)
286                                 c := make(chan bool)
287                                 Consistently(c).ShouldNot(Receive())
288                         })
289                 })
290         })
291
292         Context("when passed a function with the wrong # or arguments & returns", func() {
293                 It("should panic", func() {
294                         Ω(func() {
295                                 New(AsyncAssertionTypeEventually, func() {}, fakeFailHandler, 0, 0, 1)
296                         }).Should(Panic())
297
298                         Ω(func() {
299                                 New(AsyncAssertionTypeEventually, func(a string) int { return 0 }, fakeFailHandler, 0, 0, 1)
300                         }).Should(Panic())
301
302                         Ω(func() {
303                                 New(AsyncAssertionTypeEventually, func() int { return 0 }, fakeFailHandler, 0, 0, 1)
304                         }).ShouldNot(Panic())
305
306                         Ω(func() {
307                                 New(AsyncAssertionTypeEventually, func() (int, error) { return 0, nil }, fakeFailHandler, 0, 0, 1)
308                         }).ShouldNot(Panic())
309                 })
310         })
311
312         Describe("bailing early", func() {
313                 Context("when actual is a value", func() {
314                         It("Eventually should bail out and fail early if the matcher says to", func() {
315                                 c := make(chan bool)
316                                 close(c)
317
318                                 t := time.Now()
319                                 failures := InterceptGomegaFailures(func() {
320                                         Eventually(c, 0.1).Should(Receive())
321                                 })
322                                 Ω(time.Since(t)).Should(BeNumerically("<", 90*time.Millisecond))
323
324                                 Ω(failures).Should(HaveLen(1))
325                         })
326                 })
327
328                 Context("when actual is a function", func() {
329                         It("should never bail early", func() {
330                                 c := make(chan bool)
331                                 close(c)
332
333                                 t := time.Now()
334                                 failures := InterceptGomegaFailures(func() {
335                                         Eventually(func() chan bool {
336                                                 return c
337                                         }, 0.1).Should(Receive())
338                                 })
339                                 Ω(time.Since(t)).Should(BeNumerically(">=", 90*time.Millisecond))
340
341                                 Ω(failures).Should(HaveLen(1))
342                         })
343                 })
344         })
345 })