169 | 169 |
})
|
170 | 170 |
})
|
171 | 171 |
|
172 | |
Context("when the passed-in context is cancelled", func() {
|
173 | |
It("stops and returns a failure", func() {
|
174 | |
ctx, cancel := context.WithCancel(context.Background())
|
175 | |
counter := 0
|
176 | |
ig.G.Eventually(func() string {
|
177 | |
counter++
|
178 | |
if counter == 2 {
|
179 | |
cancel()
|
180 | |
} else if counter == 10 {
|
|
172 |
Context("with a passed-in context", func() {
|
|
173 |
Context("when the passed-in context is cancelled", func() {
|
|
174 |
It("stops and returns a failure", func() {
|
|
175 |
ctx, cancel := context.WithCancel(context.Background())
|
|
176 |
counter := 0
|
|
177 |
ig.G.Eventually(func() string {
|
|
178 |
counter++
|
|
179 |
if counter == 2 {
|
|
180 |
cancel()
|
|
181 |
} else if counter == 10 {
|
|
182 |
return MATCH
|
|
183 |
}
|
|
184 |
return NO_MATCH
|
|
185 |
}, time.Hour, ctx).Should(SpecMatch())
|
|
186 |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
|
187 |
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
|
|
188 |
})
|
|
189 |
|
|
190 |
It("can also be configured via WithContext()", func() {
|
|
191 |
ctx, cancel := context.WithCancel(context.Background())
|
|
192 |
counter := 0
|
|
193 |
ig.G.Eventually(func() string {
|
|
194 |
counter++
|
|
195 |
if counter == 2 {
|
|
196 |
cancel()
|
|
197 |
} else if counter == 10 {
|
|
198 |
return MATCH
|
|
199 |
}
|
|
200 |
return NO_MATCH
|
|
201 |
}, time.Hour).WithContext(ctx).Should(SpecMatch())
|
|
202 |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
|
203 |
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
|
|
204 |
})
|
|
205 |
|
|
206 |
It("counts as a failure for Consistently", func() {
|
|
207 |
ctx, cancel := context.WithCancel(context.Background())
|
|
208 |
counter := 0
|
|
209 |
ig.G.Consistently(func() string {
|
|
210 |
counter++
|
|
211 |
if counter == 2 {
|
|
212 |
cancel()
|
|
213 |
} else if counter == 10 {
|
|
214 |
return NO_MATCH
|
|
215 |
}
|
181 | 216 |
return MATCH
|
182 | |
}
|
183 | |
return NO_MATCH
|
184 | |
}, time.Hour, ctx).Should(SpecMatch())
|
185 | |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
186 | |
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
|
187 | |
})
|
188 | |
|
189 | |
It("can also be configured via WithContext()", func() {
|
190 | |
ctx, cancel := context.WithCancel(context.Background())
|
191 | |
counter := 0
|
192 | |
ig.G.Eventually(func() string {
|
193 | |
counter++
|
194 | |
if counter == 2 {
|
195 | |
cancel()
|
196 | |
} else if counter == 10 {
|
197 | |
return MATCH
|
198 | |
}
|
199 | |
return NO_MATCH
|
200 | |
}, time.Hour).WithContext(ctx).Should(SpecMatch())
|
201 | |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
202 | |
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
|
203 | |
})
|
204 | |
|
205 | |
It("counts as a failure for Consistently", func() {
|
206 | |
ctx, cancel := context.WithCancel(context.Background())
|
207 | |
counter := 0
|
208 | |
ig.G.Consistently(func() string {
|
209 | |
counter++
|
210 | |
if counter == 2 {
|
211 | |
cancel()
|
212 | |
} else if counter == 10 {
|
|
217 |
}, time.Hour).WithContext(ctx).Should(SpecMatch())
|
|
218 |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
|
219 |
Ω(ig.FailureMessage).Should(ContainSubstring("positive: match"))
|
|
220 |
})
|
|
221 |
})
|
|
222 |
|
|
223 |
Context("when the passed-in context is a Ginkgo SpecContext that can take a progress reporter attachment", func() {
|
|
224 |
It("attaches a progress reporter context that allows it to report on demand", func() {
|
|
225 |
fakeSpecContext := &FakeGinkgoSpecContext{}
|
|
226 |
var message string
|
|
227 |
ctx := context.WithValue(context.Background(), "GINKGO_SPEC_CONTEXT", fakeSpecContext)
|
|
228 |
ig.G.Eventually(func() string {
|
|
229 |
if fakeSpecContext.Attached != nil {
|
|
230 |
message = fakeSpecContext.Attached()
|
|
231 |
}
|
213 | 232 |
return NO_MATCH
|
214 | |
}
|
215 | |
return MATCH
|
216 | |
}, time.Hour).WithContext(ctx).Should(SpecMatch())
|
217 | |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
218 | |
Ω(ig.FailureMessage).Should(ContainSubstring("positive: match"))
|
219 | |
})
|
220 | |
})
|
221 | |
|
222 | |
Context("when the passed-in context is a Ginkgo SpecContext that can take a progress reporter attachment", func() {
|
223 | |
It("attaches a progress reporter context that allows it to report on demand", func() {
|
224 | |
fakeSpecContext := &FakeGinkgoSpecContext{}
|
225 | |
var message string
|
226 | |
ctx := context.WithValue(context.Background(), "GINKGO_SPEC_CONTEXT", fakeSpecContext)
|
227 | |
ig.G.Eventually(func() string {
|
228 | |
if fakeSpecContext.Attached != nil {
|
229 | |
message = fakeSpecContext.Attached()
|
230 | |
}
|
231 | |
return NO_MATCH
|
232 | |
}).WithTimeout(time.Millisecond * 20).WithContext(ctx).Should(Equal(MATCH))
|
233 | |
|
234 | |
Ω(message).Should(Equal("Expected\n <string>: no match\nto equal\n <string>: match"))
|
235 | |
Ω(fakeSpecContext.Cancelled).Should(BeTrue())
|
|
233 |
}).WithTimeout(time.Millisecond * 20).WithContext(ctx).Should(Equal(MATCH))
|
|
234 |
|
|
235 |
Ω(message).Should(Equal("Expected\n <string>: no match\nto equal\n <string>: match"))
|
|
236 |
Ω(fakeSpecContext.Cancelled).Should(BeTrue())
|
|
237 |
})
|
|
238 |
})
|
|
239 |
|
|
240 |
Describe("the interaction between the context and the timeout", func() {
|
|
241 |
It("only relies on context cancellation when no explicit timeout is specified", func() {
|
|
242 |
ig.G.SetDefaultEventuallyTimeout(time.Millisecond * 10)
|
|
243 |
ig.G.SetDefaultEventuallyPollingInterval(time.Millisecond * 40)
|
|
244 |
t := time.Now()
|
|
245 |
ctx, cancel := context.WithCancel(context.Background())
|
|
246 |
iterations := 0
|
|
247 |
ig.G.Eventually(func() string {
|
|
248 |
iterations += 1
|
|
249 |
if time.Since(t) > time.Millisecond*200 {
|
|
250 |
cancel()
|
|
251 |
}
|
|
252 |
return "A"
|
|
253 |
}).WithContext(ctx).Should(Equal("B"))
|
|
254 |
Ω(time.Since(t)).Should(BeNumerically("~", time.Millisecond*200, time.Millisecond*100))
|
|
255 |
Ω(iterations).Should(BeNumerically("~", 200/40, 2))
|
|
256 |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
|
257 |
})
|
|
258 |
|
|
259 |
It("uses the explicit timeout when it is provided", func() {
|
|
260 |
t := time.Now()
|
|
261 |
ctx, cancel := context.WithCancel(context.Background())
|
|
262 |
iterations := 0
|
|
263 |
ig.G.Eventually(func() string {
|
|
264 |
iterations += 1
|
|
265 |
if time.Since(t) > time.Millisecond*200 {
|
|
266 |
cancel()
|
|
267 |
}
|
|
268 |
return "A"
|
|
269 |
}).WithContext(ctx).WithTimeout(time.Millisecond * 80).ProbeEvery(time.Millisecond * 40).Should(Equal("B"))
|
|
270 |
Ω(time.Since(t)).Should(BeNumerically("~", time.Millisecond*80, time.Millisecond*40))
|
|
271 |
Ω(iterations).Should(BeNumerically("~", 80/40, 2))
|
|
272 |
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
|
|
273 |
})
|
236 | 274 |
})
|
237 | 275 |
})
|
238 | 276 |
})
|
|
349 | 387 |
It("calls the optional description if it is a function", func() {
|
350 | 388 |
ig.G.Consistently(NO_MATCH).Should(SpecMatch(), func() string { return "boop" })
|
351 | 389 |
Ω(ig.FailureMessage).Should(ContainSubstring("boop"))
|
|
390 |
})
|
|
391 |
})
|
|
392 |
|
|
393 |
Context("with a passed-in context", func() {
|
|
394 |
Context("when the passed-in context is cancelled", func() {
|
|
395 |
It("counts as a failure for Consistently", func() {
|
|
396 |
ctx, cancel := context.WithCancel(context.Background())
|
|
397 |
counter := 0
|
|
398 |
ig.G.Consistently(func() string {
|
|
399 |
counter++
|
|
400 |
if counter == 2 {
|
|
401 |
cancel()
|
|
402 |
} else if counter == 10 {
|
|
403 |
return NO_MATCH
|
|
404 |
}
|
|
405 |
return MATCH
|
|
406 |
}, time.Hour).WithContext(ctx).Should(SpecMatch())
|
|
407 |
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
|
|
408 |
Ω(ig.FailureMessage).Should(ContainSubstring("positive: match"))
|
|
409 |
})
|
|
410 |
})
|
|
411 |
|
|
412 |
Describe("the interaction between the context and the timeout", func() {
|
|
413 |
It("only always uses the default interval even if not explicit duration is provided", func() {
|
|
414 |
ig.G.SetDefaultConsistentlyDuration(time.Millisecond * 200)
|
|
415 |
ig.G.SetDefaultConsistentlyPollingInterval(time.Millisecond * 40)
|
|
416 |
t := time.Now()
|
|
417 |
ctx, cancel := context.WithCancel(context.Background())
|
|
418 |
defer cancel()
|
|
419 |
iterations := 0
|
|
420 |
ig.G.Consistently(func() string {
|
|
421 |
iterations += 1
|
|
422 |
return "A"
|
|
423 |
}).WithContext(ctx).Should(Equal("A"))
|
|
424 |
Ω(time.Since(t)).Should(BeNumerically("~", time.Millisecond*200, time.Millisecond*100))
|
|
425 |
Ω(iterations).Should(BeNumerically("~", 200/40, 2))
|
|
426 |
Ω(ig.FailureMessage).Should(BeZero())
|
|
427 |
})
|
352 | 428 |
})
|
353 | 429 |
})
|
354 | 430 |
})
|