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

workerpool: add Len() to retrieve the count of running workers #21

Merged
merged 1 commit into from
Feb 26, 2021
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
5 changes: 5 additions & 0 deletions workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (wp *WorkerPool) Cap() int {
return cap(wp.workers)
}

// Len returns the count of concurrent workers currently running.
func (wp *WorkerPool) Len() int {
return len(wp.workers)
}

// Submit submits f for processing by a worker. The given id is useful for
// identifying the task once it is completed. The task f must return when the
// context ctx is cancelled.
Expand Down
20 changes: 20 additions & 0 deletions workerpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ func TestWorkerPoolCap(t *testing.T) {
}
}

func TestWorkerPoolLen(t *testing.T) {
wp := New(1)
defer wp.Close()
if l := wp.Len(); l != 0 {
t.Errorf("got %d; want %d", l, 0)
}

err := wp.Submit("", func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
if err != nil {
t.Fatalf("failed to submit task: %v", err)
}

if l := wp.Len(); l != 1 {
t.Errorf("got %d; want %d", l, 1)
}
}

// TestWorkerPoolConcurrentTasksCount ensure that there is at least, but no
// more than n workers running in the pool when more than n tasks are
// submitted.
Expand Down