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
|
package internal_test
import (
"context"
"time"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gleak"
"github.com/onsi/ginkgo/v2/internal"
)
var _ = Describe("ProgressReporterManager", func() {
var manager *internal.ProgressReporterManager
BeforeEach(func() {
manager = internal.NewProgressReporterManager()
})
It("can attach and detach progress reporters", func() {
Ω(manager.QueryProgressReporters(context.Background(), nil)).Should(BeEmpty())
cancelA := manager.AttachProgressReporter(func() string { return "A" })
Ω(manager.QueryProgressReporters(context.Background(), nil)).Should(Equal([]string{"A"}))
cancelB := manager.AttachProgressReporter(func() string { return "B" })
Ω(manager.QueryProgressReporters(context.Background(), nil)).Should(Equal([]string{"A", "B"}))
cancelC := manager.AttachProgressReporter(func() string { return "C" })
Ω(manager.QueryProgressReporters(context.Background(), nil)).Should(Equal([]string{"A", "B", "C"}))
cancelB()
Ω(manager.QueryProgressReporters(context.Background(), nil)).Should(Equal([]string{"A", "C"}))
cancelA()
Ω(manager.QueryProgressReporters(context.Background(), nil)).Should(Equal([]string{"C"}))
cancelC()
Ω(manager.QueryProgressReporters(context.Background(), nil)).Should(BeEmpty())
})
It("bails if a progress reporter takes longer than the passed-in context's deadline", func() {
startingGoroutines := gleak.Goroutines()
c := make(chan struct{})
manager.AttachProgressReporter(func() string { return "A" })
manager.AttachProgressReporter(func() string { return "B" })
manager.AttachProgressReporter(func() string {
<-c
return "C"
})
manager.AttachProgressReporter(func() string { return "D" })
context, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
result := manager.QueryProgressReporters(context, nil)
Ω(result).Should(Equal([]string{"A", "B"}))
cancel()
close(c)
Eventually(gleak.Goroutines).ShouldNot(gleak.HaveLeaked(startingGoroutines))
})
It("ignores empty progress reports", func() {
manager.AttachProgressReporter(func() string { return "A" })
manager.AttachProgressReporter(func() string { return "" })
manager.AttachProgressReporter(func() string { return " " })
manager.AttachProgressReporter(func() string { return "C" })
result := manager.QueryProgressReporters(context.Background(), nil)
Ω(result).Should(Equal([]string{"A", "C"}))
})
It("catches panics and reports them as failures", func() {
manager.AttachProgressReporter(func() string {
panic("bam")
})
manager.AttachProgressReporter(func() string { return "B" })
failer := internal.NewFailer()
result := manager.QueryProgressReporters(context.Background(), failer)
Ω(result).Should(Equal([]string{"failed to query attached progress reporter", "B"}))
state, failure := failer.Drain()
Ω(state).Should(Equal(types.SpecStatePanicked))
Ω(failure.Message).Should(Equal("Test Panicked"))
Ω(failure.ForwardedPanic).Should(Equal("bam"))
})
})
|