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 task result order #221

Merged
merged 1 commit into from
Jun 16, 2022
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
33 changes: 25 additions & 8 deletions parallel/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func (p *ParallelTasks) Do() *ParallelTasks {
}
}()

p.results = make([]interface{}, len(p.tasks))
p.errs = make([]error, len(p.tasks))

for i, task := range p.tasks {
if !p.waitThreshold() {
return p
Expand All @@ -213,7 +216,7 @@ func (p *ParallelTasks) Do() *ParallelTasks {
if err != nil {
p.errLock.Lock()
defer p.errLock.Unlock()
p.errs = append(p.errs, err)
p.errs[index] = err
if p.Options.FailFast {
log.Debugw("fail fast, will cancel", "task-index", index, "result", result)
p.Cancel(err)
Expand All @@ -223,9 +226,7 @@ func (p *ParallelTasks) Do() *ParallelTasks {

// error is nil, we should save result
if !isNil(result) {
p.resultsLock.Lock()
p.results = append(p.results, result)
p.resultsLock.Unlock()
p.results[index] = result
}

}(i, task)
Expand Down Expand Up @@ -266,15 +267,31 @@ func (p *ParallelTasks) Wait() ([]interface{}, error) {
p.Log.Debugw("waiting done.")
<-p.doneChan
p.Log.Debugw("waited done.")

var (
results = make([]interface{}, 0)
errs = make([]error, 0)
)
for _, result := range p.results {
if result != nil {
results = append(results, result)
}
}
for _, err := range p.errs {
if err != nil {
errs = append(errs, err)
}
}

if p.doneError != nil {
return p.results, p.doneError
return results, p.doneError
}

if len(p.errs) > 0 {
return p.results, errors.NewAggregate(p.errs)
if len(errs) > 0 {
return results, errors.NewAggregate(errs)
}

return p.results, nil
return results, nil
}

func isNil(i interface{}) bool {
Expand Down
7 changes: 7 additions & 0 deletions parallel/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ var _ = Describe("P().Do().Wait()", func() {
Expect(res).To(ContainElement(fmt.Sprintf("task-%d", i)))
}
})

It("should return with same sort", func() {
Expect(errs).To(BeNil())
for i := 0; i <= 9; i++ {
Expect(res[i]).To(Equal(fmt.Sprintf("task-%d", i+1)))
}
})
})

Context("when set conccurrent", func() {
Expand Down
2 changes: 1 addition & 1 deletion webhook/admission/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestValidatorDelete(t *testing.T) {
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Delete,
OldObject: runtime.RawExtension{
Raw: []byte(`{"metadata":{"name":"def","namespace":"default"}}`),
airycanon marked this conversation as resolved.
Show resolved Hide resolved
Raw: []byte(`{"metadata":{"name":"def"}}`),
Object: &MyObject{},
},
},
Expand Down