File: progress_report_test.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 4,112 kB
  • sloc: javascript: 59; sh: 14; makefile: 7
file content (504 lines) | stat: -rw-r--r-- 16,275 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package internal_integration_test

import (
	"time"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
	"github.com/onsi/ginkgo/v2/types"
	. "github.com/onsi/gomega"
)

var _ = Describe("Progress Reporting", func() {
	BeforeEach(func() {
		conf.PollProgressAfter = 100 * time.Millisecond
		conf.PollProgressInterval = 50 * time.Millisecond
	})

	AfterEach(func() {
		Ω(triggerProgressSignal).Should(BeNil(), "It should have unregistered the progress signal handler")
	})

	Context("when progress is reported in a BeforeSuite", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress in a BeforeSuite", func() {
				BeforeSuite(func() {
					cl = types.NewCodeLocation(0)
					writer.Print("ginkgo-writer-content")
					triggerProgressSignal()
				})
				It("runs", func() {})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress when asked and includes source code", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(1))
			pr := reporter.ProgressReports[0]
			Ω(pr.Message).Should(Equal("{{bold}}You've requested a progress report:{{/}}"))
			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeBeforeSuite))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
			Ω(pr.CurrentStepText).Should(Equal(""))
			Ω(pr.CapturedGinkgoWriterOutput).Should(Equal("ginkgo-writer-content"))
			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(2)))

			By("validating that source code is extracted")
			targetCl := clLine(2)
			var functionCall types.FunctionCall
			for _, call := range pr.SpecGoroutine().Stack {
				if call.Filename == targetCl.FileName && call.Line == targetCl.LineNumber {
					functionCall = call
					break
				}
			}
			Ω(functionCall).ShouldNot(BeZero())
			//note - these are the lines from up above
			Ω(functionCall.Source).Should(Equal([]string{
				"\t\t\t\t\tcl = types.NewCodeLocation(0)",
				"\t\t\t\t\twriter.Print(\"ginkgo-writer-content\")",
				"\t\t\t\t\ttriggerProgressSignal()",
				"\t\t\t\t})",
				"\t\t\t\tIt(\"runs\", func() {})",
			}))
			Ω(functionCall.SourceHighlight).Should(Equal(2))
		})
	})

	Context("when progress is emitted in an It", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func() {
						cl = types.NewCodeLocation(0)
						triggerProgressSignal()
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress when asked ", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(1))
			pr := reporter.ProgressReports[0]

			Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
			Ω(pr.LeafNodeText).Should(Equal("A"))

			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
			Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))

			Ω(pr.CurrentStepText).Should(Equal(""))

			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(1)))
		})
	})

	Context("when progress is emitted in a setup node", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					BeforeEach(func() {
						cl = types.NewCodeLocation(0)
						triggerProgressSignal()
					})

					It("A", func() {})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress when asked ", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(1))
			pr := reporter.ProgressReports[0]

			Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(4)))
			Ω(pr.LeafNodeText).Should(Equal("A"))

			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeBeforeEach))
			Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))

			Ω(pr.CurrentStepText).Should(Equal(""))

			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(1)))
		})
	})

	Context("when progress is emitted in a By", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func() {
						cl = types.NewCodeLocation(0)
						By("B")
						By("C", func() {
							triggerProgressSignal()
						})
						By("D")
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress when asked and includes the By step", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(1))
			pr := reporter.ProgressReports[0]

			Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
			Ω(pr.LeafNodeText).Should(Equal("A"))

			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
			Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))

			Ω(pr.CurrentStepText).Should(Equal("C"))
			Ω(pr.CurrentStepLocation).Should(Equal(clLine(2)))

			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(3)))
		})
	})

	Context("when progress is emitted after a By", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func() {
						cl = types.NewCodeLocation(0)
						By("B")
						By("C")
						triggerProgressSignal()
						By("D")
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress when asked and includes the last run By step", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(1))
			pr := reporter.ProgressReports[0]

			Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
			Ω(pr.LeafNodeText).Should(Equal("A"))

			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
			Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))

			Ω(pr.CurrentStepText).Should(Equal("C"))
			Ω(pr.CurrentStepLocation).Should(Equal(clLine(2)))

			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(3)))
		})
	})

	Context("when progress is emitted in a node with no By", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					BeforeEach(func() {
						By("B")
					})

					It("A", func() {
						cl = types.NewCodeLocation(0)
						triggerProgressSignal()
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress when asked and does not include the By step", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(1))
			pr := reporter.ProgressReports[0]

			Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
			Ω(pr.LeafNodeText).Should(Equal("A"))

			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
			Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))

			Ω(pr.CurrentStepText).Should(Equal(""))

			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(1)))
		})
	})

	Context("when a goroutine is launched by the spec", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func() {
						cl = types.NewCodeLocation(0)
						c := make(chan bool, 0)
						go func() {
							c <- true
							<-c
						}()
						<-c
						triggerProgressSignal()
						c <- true
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("lists the goroutine as a line of interest", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(1))
			pr := reporter.ProgressReports[0]

			Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
			Ω(pr.LeafNodeText).Should(Equal("A"))

			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
			Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))

			Ω(pr.CurrentStepText).Should(Equal(""))

			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(7)))

			var waitingGoroutine types.Goroutine
			Ω(pr.HighlightedGoroutines()).Should(ContainElement(HaveField("State", "chan receive"), &waitingGoroutine))
			Ω(waitingGoroutine.Stack).Should(HaveHighlightedStackLine(clLine(2)), "the goroutine invocation")
			Ω(waitingGoroutine.Stack).Should(HaveHighlightedStackLine(clLine(4)), "the <-c line in the goroutine")
		})
	})

	Context("when a test takes longer then the configured PollProgressAfter", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func() {
						cl = types.NewCodeLocation(0)
						time.Sleep(300 * time.Millisecond)
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress periodically", func() {
			Ω(len(reporter.ProgressReports)).Should(BeNumerically(">", 1))

			for _, pr := range reporter.ProgressReports {
				Ω(pr.Message).Should(Equal("{{bold}}Automatically polling progress:{{/}}"))
				Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
				Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
				Ω(pr.LeafNodeText).Should(Equal("A"))

				Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
				Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))
			}
		})
	})

	Context("when a test takes longer then the overridden PollProgressAfter", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func() {
						cl = types.NewCodeLocation(0)
						time.Sleep(50 * time.Millisecond)
					}, PollProgressAfter(20*time.Millisecond), PollProgressInterval(10*time.Millisecond))
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress periodically", func() {
			Ω(len(reporter.ProgressReports)).Should(BeNumerically(">", 1))

			for _, pr := range reporter.ProgressReports {
				Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
				Ω(pr.LeafNodeLocation).Should(Equal(clLine(-1)))
				Ω(pr.LeafNodeText).Should(Equal("A"))

				Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
				Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))
			}
		})
	})

	Context("SynchronizedBeforeSuite can also be decorated", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				SynchronizedBeforeSuite(func() []byte {
					cl = types.NewCodeLocation(0)
					return []byte("hello")
				}, func(_ []byte) {
					time.Sleep(50 * time.Millisecond)
				}, PollProgressAfter(20*time.Millisecond), PollProgressInterval(10*time.Millisecond))
				Describe("a container", func() {
					It("A", func() {})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress periodically", func() {
			Ω(len(reporter.ProgressReports)).Should(BeNumerically(">", 1))

			for _, pr := range reporter.ProgressReports {
				Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeSynchronizedBeforeSuite))
				Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-1)))
			}
		})
	})

	Context("DeferCleanup can also be decorated", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func() {
						cl = types.NewCodeLocation(0)
						DeferCleanup(func() {
							time.Sleep(50 * time.Millisecond)
						}, PollProgressAfter(20*time.Millisecond), PollProgressInterval(10*time.Millisecond))
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("emits progress periodically", func() {
			Ω(len(reporter.ProgressReports)).Should(BeNumerically(">", 1))

			for _, pr := range reporter.ProgressReports {
				Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeCleanupAfterEach))
				Ω(pr.CurrentNodeLocation).Should(Equal(clLine(1)))
			}
		})
	})

	Context("when an additional progress report provider has been registered with the current context", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func(ctx SpecContext) {
						cancel := ctx.AttachProgressReporter(func() string { return "Some Additional Information" })
						cl = types.NewCodeLocation(0)
						triggerProgressSignal()
						cancel()
						ctx.AttachProgressReporter(func() string { return "Some Different Information (never cancelled)" })
						triggerProgressSignal()
						cancel = ctx.AttachProgressReporter(func() string { return "Yet More Information" })
						triggerProgressSignal()
						cancel()
						triggerProgressSignal()
					})

					AfterEach(func() {
						triggerProgressSignal()
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("includes information from that progress report provider", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(5))
			pr := reporter.ProgressReports[0]

			Ω(pr.ContainerHierarchyTexts).Should(ConsistOf("a container"))
			Ω(pr.LeafNodeLocation).Should(Equal(clLine(-2)))
			Ω(pr.LeafNodeText).Should(Equal("A"))

			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeIt))
			Ω(pr.CurrentNodeLocation).Should(Equal(clLine(-2)))

			Ω(pr.CurrentStepText).Should(Equal(""))

			Ω(pr.SpecGoroutine().State).Should(Equal("running"))
			Ω(pr.SpecGoroutine().Stack).Should(HaveHighlightedStackLine(clLine(1)))
			Ω(pr.AdditionalReports).Should(Equal([]string{"Some Additional Information"}))

			pr = reporter.ProgressReports[1]
			Ω(pr.AdditionalReports).Should(Equal([]string{"Some Different Information (never cancelled)"}))

			pr = reporter.ProgressReports[2]
			Ω(pr.AdditionalReports).Should(Equal([]string{"Some Different Information (never cancelled)", "Yet More Information"}))

			pr = reporter.ProgressReports[3]
			Ω(pr.AdditionalReports).Should(Equal([]string{"Some Different Information (never cancelled)"}))

			pr = reporter.ProgressReports[4]
			Ω(pr.CurrentNodeType).Should(Equal(types.NodeTypeAfterEach))
			Ω(pr.AdditionalReports).Should(BeEmpty())
		})
	})

	Context("when a global progress report provider has been registered", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func(ctx SpecContext) {
						cancelGlobal := AttachProgressReporter(func() string { return "Some Global Information" })
						AttachProgressReporter(func() string { return "Some More (Never Cancelled) Global Information" })
						ctx.AttachProgressReporter(func() string { return "Some Additional Information" })
						cl = types.NewCodeLocation(0)
						triggerProgressSignal()
						cancelGlobal()
						triggerProgressSignal()
					})

					It("B", func() {
						triggerProgressSignal()
					})
				})
			})
			Ω(success).Should(BeTrue())
		})

		It("includes information from that progress report provider", func() {
			Ω(reporter.ProgressReports).Should(HaveLen(3))
			pr := reporter.ProgressReports[0]

			Ω(pr.LeafNodeText).Should(Equal("A"))
			Ω(pr.AdditionalReports).Should(Equal([]string{"Some Additional Information", "Some Global Information", "Some More (Never Cancelled) Global Information"}))

			pr = reporter.ProgressReports[1]
			Ω(pr.LeafNodeText).Should(Equal("A"))
			Ω(pr.AdditionalReports).Should(Equal([]string{"Some Additional Information", "Some More (Never Cancelled) Global Information"}))

			pr = reporter.ProgressReports[2]
			Ω(pr.LeafNodeText).Should(Equal("B"))
			Ω(pr.AdditionalReports).Should(Equal([]string{"Some More (Never Cancelled) Global Information"}))
		})
	})

	Context("when a global progress reporter fails", func() {
		BeforeEach(func() {
			success, _ := RunFixture("emitting spec progress", func() {
				Describe("a container", func() {
					It("A", func(ctx SpecContext) {
						AttachProgressReporter(func() string {
							F("bam")
							return "Some Global Information"
						})
						triggerProgressSignal()
					})
				})
			})
			Ω(success).Should(BeFalse())
		})

		It("marks the spec as failed", func() {
			Ω(reporter.Did.Find("A")).Should(HaveFailed("bam"))
		})
	})

})