From 731fba842f1ad31ef0c7b202f5955099c297fb95 Mon Sep 17 00:00:00 2001 From: bigeagle Date: Thu, 28 Apr 2016 10:44:21 +0800 Subject: [PATCH] feature(worker): job need to be started by jobStart signal --- worker/job.go | 6 +++++- worker/job_test.go | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/worker/job.go b/worker/job.go index 186764b..8a75efc 100644 --- a/worker/job.go +++ b/worker/job.go @@ -28,6 +28,7 @@ type jobMessage struct { type mirrorJob struct { provider mirrorProvider ctrlChan chan ctrlAction + stopped chan empty enabled bool } @@ -35,7 +36,7 @@ func newMirrorJob(provider mirrorProvider) *mirrorJob { return &mirrorJob{ provider: provider, ctrlChan: make(chan ctrlAction, 1), - enabled: true, + enabled: false, } } @@ -52,6 +53,9 @@ func (m *mirrorJob) Name() string { // TODO: message struct for managerChan func (m *mirrorJob) Run(managerChan chan<- jobMessage, semaphore chan empty) error { + m.stopped = make(chan empty) + defer close(m.stopped) + provider := m.provider // to make code shorter diff --git a/worker/job_test.go b/worker/job_test.go index 065d10e..15bc716 100644 --- a/worker/job_test.go +++ b/worker/job_test.go @@ -68,6 +68,15 @@ func TestMirrorJob(t *testing.T) { job := newMirrorJob(provider) go job.Run(managerChan, semaphore) + // job should not start if we don't start it + select { + case <-managerChan: + So(0, ShouldEqual, 1) // made this fail + case <-time.After(1 * time.Second): + So(0, ShouldEqual, 0) + } + + job.ctrlChan <- jobStart for i := 0; i < 2; i++ { msg := <-managerChan So(msg.status, ShouldEqual, PreSyncing) @@ -96,7 +105,7 @@ func TestMirrorJob(t *testing.T) { select { case <-managerChan: So(0, ShouldEqual, 1) // made this fail - case <-time.After(2 * time.Second): + case <-job.stopped: So(0, ShouldEqual, 0) } }) @@ -118,6 +127,7 @@ echo $TUNASYNC_WORKING_DIR Convey("If we kill it", func(ctx C) { go job.Run(managerChan, semaphore) + job.ctrlChan <- jobStart time.Sleep(1 * time.Second) msg := <-managerChan @@ -135,10 +145,12 @@ echo $TUNASYNC_WORKING_DIR So(err, ShouldBeNil) So(string(loggedContent), ShouldEqual, exceptedOutput) job.ctrlChan <- jobDisable + <-job.stopped }) Convey("If we don't kill it", func(ctx C) { go job.Run(managerChan, semaphore) + job.ctrlChan <- jobStart msg := <-managerChan So(msg.status, ShouldEqual, PreSyncing) @@ -156,6 +168,7 @@ echo $TUNASYNC_WORKING_DIR So(err, ShouldBeNil) So(string(loggedContent), ShouldEqual, exceptedOutput) job.ctrlChan <- jobDisable + <-job.stopped }) })