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

fix: refactor SetMaxConcurrent method in ParallelTasks. #502

Merged
merged 2 commits into from
Nov 30, 2023
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
12 changes: 12 additions & 0 deletions parallel/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,23 @@ func (p *ParallelTasks) FailFast() *ParallelTasks {
return p
}

// SetConcurrent set the number of concurrency
func (p *ParallelTasks) SetConcurrent(count int) *ParallelTasks {
p.Options.ConcurrencyCount = count
return p
}

// SetMaxConcurrent set the number of concurrency.
// if count is greater than max, max is used.
func (p *ParallelTasks) SetMaxConcurrent(count int, max int) *ParallelTasks {
kycheng marked this conversation as resolved.
Show resolved Hide resolved
if count > max {
count = max
}

p.Options.ConcurrencyCount = count
return p
}

// Context will set context , up to now , task is not support to cancel
// if you cancel from context, wait will return immediately
func (p *ParallelTasks) Context(ctx context.Context) *ParallelTasks {
Expand Down
29 changes: 28 additions & 1 deletion parallel/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ var _ = Describe("P().Do().Wait()", func() {
})

Context("when set conccurrent", func() {

flag4 := &executeFlag{}
flag5 := &executeFlag{}
flag6 := &executeFlag{}
Expand Down Expand Up @@ -289,5 +288,33 @@ var _ = Describe("P().Do().Wait()", func() {
}
})
})
})

var _ = Describe("SetMaxConcurrent", func() {
var (
ptasks *parallel.ParallelTasks
zapLog, _ = zap.NewDevelopment()
log = zapLog.Sugar()
)

BeforeEach(func() {
ptasks = parallel.P(log, "custom case")
})

Context("SetMaxConcurrent", func() {
It("when max great concurrent", func() {
ptasks.SetMaxConcurrent(5, 6)
Expect(ptasks.Options.ConcurrencyCount).To(BeEquivalentTo(5))
})

It("when max less concurrent", func() {
ptasks.SetMaxConcurrent(5, 4)
Expect(ptasks.Options.ConcurrencyCount).To(BeEquivalentTo(4))
})

It("when max equal concurrent", func() {
ptasks.SetMaxConcurrent(5, 5)
Expect(ptasks.Options.ConcurrencyCount).To(BeEquivalentTo(5))
})
})
})