From 64ba404b715205fccced8930040bb84967e3086b Mon Sep 17 00:00:00 2001 From: domgoer <814172254@qq.com> Date: Wed, 21 Nov 2018 11:54:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96waitgroup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry.go | 9 ++++++--- task_test.go | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/entry.go b/entry.go index ad69f9d..18109f0 100644 --- a/entry.go +++ b/entry.go @@ -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 } } @@ -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 diff --git a/task_test.go b/task_test.go index bee4dc7..e0bc86d 100644 --- a/task_test.go +++ b/task_test.go @@ -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 }