-
-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AttachProgressReporter allows users to provide arbitrary information …
…when a ProgressReport is requested
- Loading branch information
Showing
11 changed files
with
247 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"sync" | ||
) | ||
|
||
type ProgressReporterManager struct { | ||
lock *sync.Mutex | ||
progressReporters map[int]func() string | ||
prCounter int | ||
} | ||
|
||
func NewProgressReporterManager() *ProgressReporterManager { | ||
return &ProgressReporterManager{ | ||
progressReporters: map[int]func() string{}, | ||
lock: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (prm *ProgressReporterManager) AttachProgressReporter(reporter func() string) func() { | ||
prm.lock.Lock() | ||
defer prm.lock.Unlock() | ||
prm.prCounter += 1 | ||
prCounter := prm.prCounter | ||
prm.progressReporters[prCounter] = reporter | ||
|
||
return func() { | ||
prm.lock.Lock() | ||
defer prm.lock.Unlock() | ||
delete(prm.progressReporters, prCounter) | ||
} | ||
} | ||
|
||
func (prm *ProgressReporterManager) QueryProgressReporters(ctx context.Context) []string { | ||
prm.lock.Lock() | ||
keys := []int{} | ||
for key := range prm.progressReporters { | ||
keys = append(keys, key) | ||
} | ||
sort.Ints(keys) | ||
reporters := []func() string{} | ||
for _, key := range keys { | ||
reporters = append(reporters, prm.progressReporters[key]) | ||
} | ||
prm.lock.Unlock() | ||
|
||
if len(reporters) == 0 { | ||
return nil | ||
} | ||
out := []string{} | ||
for _, reporter := range reporters { | ||
reportC := make(chan string, 1) | ||
go func() { | ||
reportC <- reporter() | ||
}() | ||
var report string | ||
select { | ||
case report = <-reportC: | ||
case <-ctx.Done(): | ||
return out | ||
} | ||
out = append(out, report) | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package internal_test | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "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())).Should(BeEmpty()) | ||
cancelA := manager.AttachProgressReporter(func() string { return "A" }) | ||
Ω(manager.QueryProgressReporters(context.Background())).Should(Equal([]string{"A"})) | ||
cancelB := manager.AttachProgressReporter(func() string { return "B" }) | ||
Ω(manager.QueryProgressReporters(context.Background())).Should(Equal([]string{"A", "B"})) | ||
cancelC := manager.AttachProgressReporter(func() string { return "C" }) | ||
Ω(manager.QueryProgressReporters(context.Background())).Should(Equal([]string{"A", "B", "C"})) | ||
cancelB() | ||
Ω(manager.QueryProgressReporters(context.Background())).Should(Equal([]string{"A", "C"})) | ||
cancelA() | ||
Ω(manager.QueryProgressReporters(context.Background())).Should(Equal([]string{"C"})) | ||
cancelC() | ||
Ω(manager.QueryProgressReporters(context.Background())).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) | ||
Ω(result).Should(Equal([]string{"A", "B"})) | ||
cancel() | ||
close(c) | ||
|
||
Eventually(gleak.Goroutines).ShouldNot(gleak.HaveLeaked(startingGoroutines)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.