Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Dec 28, 2022
2 parents 9405618 + af62be0 commit 5d2926b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
8 changes: 2 additions & 6 deletions worker_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func (c *WorkerGroup) AddJob(jobs ...Job) {
}

// StartAndWait 启动并等待所有任务执行完成
func (c *WorkerGroup) StartAndWait() {
func (c *WorkerGroup) StartAndWait() error {
var taskTotal = int64(c.Len())
if taskTotal == 0 {
return
return nil
}

var co = min(c.config.Concurrency, taskTotal)
Expand All @@ -110,9 +110,5 @@ func (c *WorkerGroup) StartAndWait() {
}

<-c.done
}

// Err 获取错误返回
func (c *WorkerGroup) Err() error {
return c.err
}
22 changes: 11 additions & 11 deletions worker_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestNewTaskGroup(t *testing.T) {

t.Run("0 task", func(t *testing.T) {
cc := NewWorkerGroup()
cc.StartAndWait()
as.NoError(cc.Err())
err := cc.StartAndWait()
as.NoError(err)
})

t.Run("1 task", func(t *testing.T) {
Expand All @@ -27,8 +27,8 @@ func TestNewTaskGroup(t *testing.T) {
return nil
},
})
cc.StartAndWait()
as.NoError(cc.Err())
err := cc.StartAndWait()
as.NoError(err)
})

t.Run("100 task", func(t *testing.T) {
Expand All @@ -43,7 +43,7 @@ func TestNewTaskGroup(t *testing.T) {
},
})
}
w.StartAndWait()
_ = w.StartAndWait()
as.Equal(sum, int64(5050))
})

Expand All @@ -63,8 +63,8 @@ func TestNewTaskGroup(t *testing.T) {
},
},
)
cc.StartAndWait()
as.Error(cc.Err())
err := cc.StartAndWait()
as.Error(err)
})

t.Run("timeout", func(t *testing.T) {
Expand All @@ -87,8 +87,8 @@ func TestNewTaskGroup(t *testing.T) {
Job{7, do},
Job{9, do},
)
ctl.StartAndWait()
as.NoError(ctl.Err())
err := ctl.StartAndWait()
as.NoError(err)
as.ElementsMatch(list, []int{1, 3})
})

Expand All @@ -100,7 +100,7 @@ func TestNewTaskGroup(t *testing.T) {
return args.(error)
},
})
ctl.StartAndWait()
as.Error(ctl.Err())
err := ctl.StartAndWait()
as.Error(err)
})
}

0 comments on commit 5d2926b

Please sign in to comment.