Skip to content

Commit

Permalink
优化waitgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
domechn committed Nov 21, 2018
1 parent 4580380 commit 64ba404
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func init() {

// AddToTaskList add the task to the execution list
func AddToTaskList(ts ...Tasker) {
wg.Add(1)
for _, t := range ts {
if t == nil {
continue
}
wg.Add(1)
editC <- t
}
}
Expand All @@ -75,10 +75,13 @@ func (tl *taskList) stop(id string) {
// ChangeInterval changes the interval between the tasks specified by the ID,
// Apply only to polling tasks.
func ChangeInterval(id string, interval time.Duration) error {
wg.Wait()
tsk := tasks.get(id)
if tsk == nil {
return fmt.Errorf("Task does not exist")
wg.Wait()
tsk = tasks.get(id)
if tsk == nil {
return fmt.Errorf("Task does not exist")
}
}
var task *Task
var ok bool
Expand Down
8 changes: 4 additions & 4 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func init() {

func TestChangeInterval(t *testing.T) {
var p []int
tk := NewTask(time.Second*1, func() {
tk := NewTask(time.Second*2, func() {
p = append(p, 1)
})
AddToTaskList(tk)
ChangeInterval(tk.ID(), time.Second*2)
ChangeInterval(tk.ID(), time.Second*1)
select {
case <-time.After(time.Second*2 + time.Millisecond*100):
if len(p) != 1 {
t.Errorf("TestTask() fail , need len : %d , actually len : %d", 1, len(p))
if len(p) != 2 {
t.Errorf("TestTask() fail , need len : %d , actually len : %d", 2, len(p))
}
return
}
Expand Down

0 comments on commit 64ba404

Please sign in to comment.