Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return context in Stop() to notify user when everything has been stopped #67

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pond.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,14 @@ func (p *WorkerPool) SubmitBefore(task func(), deadline time.Duration) {
// Stop causes this pool to stop accepting new tasks and signals all workers to exit.
// Tasks being executed by workers will continue until completion (unless the process is terminated).
// Tasks in the queue will not be executed.
CorentinClabaut marked this conversation as resolved.
Show resolved Hide resolved
func (p *WorkerPool) Stop() {
go p.stop(false)
// This method returns a context object that is cancelled when the pool has stopped completely.
func (p *WorkerPool) Stop() context.Context {
ctx, cancel := context.WithCancel(context.Background())
go func() {
p.stop(false)
cancel()
}()
return ctx
}

// StopAndWait causes this pool to stop accepting new tasks and then waits for all tasks in the queue
Expand Down
4 changes: 2 additions & 2 deletions pond_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestSubmitAndStopWithoutWaiting(t *testing.T) {
<-started

// Stop without waiting for the rest of the tasks to start
pool.Stop()
ctx := pool.Stop()

// Let the first task complete now
completed <- true
Expand All @@ -129,7 +129,7 @@ func TestSubmitAndStopWithoutWaiting(t *testing.T) {
assertEqual(t, int32(1), atomic.LoadInt32(&doneCount))

// Make sure the exit lines in the worker pool are executed and covered
time.Sleep(6 * time.Millisecond)
<-ctx.Done()
}

func TestSubmitWithNilTask(t *testing.T) {
Expand Down