mirror of
https://github.com/tuna/tunasync.git
synced 2025-06-15 05:52:43 +00:00
feature(worker): job need to be started by jobStart signal
This commit is contained in:
parent
13161d77cf
commit
731fba842f
@ -28,6 +28,7 @@ type jobMessage struct {
|
|||||||
type mirrorJob struct {
|
type mirrorJob struct {
|
||||||
provider mirrorProvider
|
provider mirrorProvider
|
||||||
ctrlChan chan ctrlAction
|
ctrlChan chan ctrlAction
|
||||||
|
stopped chan empty
|
||||||
enabled bool
|
enabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +36,7 @@ func newMirrorJob(provider mirrorProvider) *mirrorJob {
|
|||||||
return &mirrorJob{
|
return &mirrorJob{
|
||||||
provider: provider,
|
provider: provider,
|
||||||
ctrlChan: make(chan ctrlAction, 1),
|
ctrlChan: make(chan ctrlAction, 1),
|
||||||
enabled: true,
|
enabled: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,6 +53,9 @@ func (m *mirrorJob) Name() string {
|
|||||||
// TODO: message struct for managerChan
|
// TODO: message struct for managerChan
|
||||||
func (m *mirrorJob) Run(managerChan chan<- jobMessage, semaphore chan empty) error {
|
func (m *mirrorJob) Run(managerChan chan<- jobMessage, semaphore chan empty) error {
|
||||||
|
|
||||||
|
m.stopped = make(chan empty)
|
||||||
|
defer close(m.stopped)
|
||||||
|
|
||||||
provider := m.provider
|
provider := m.provider
|
||||||
|
|
||||||
// to make code shorter
|
// to make code shorter
|
||||||
|
@ -68,6 +68,15 @@ func TestMirrorJob(t *testing.T) {
|
|||||||
job := newMirrorJob(provider)
|
job := newMirrorJob(provider)
|
||||||
|
|
||||||
go job.Run(managerChan, semaphore)
|
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++ {
|
for i := 0; i < 2; i++ {
|
||||||
msg := <-managerChan
|
msg := <-managerChan
|
||||||
So(msg.status, ShouldEqual, PreSyncing)
|
So(msg.status, ShouldEqual, PreSyncing)
|
||||||
@ -96,7 +105,7 @@ func TestMirrorJob(t *testing.T) {
|
|||||||
select {
|
select {
|
||||||
case <-managerChan:
|
case <-managerChan:
|
||||||
So(0, ShouldEqual, 1) // made this fail
|
So(0, ShouldEqual, 1) // made this fail
|
||||||
case <-time.After(2 * time.Second):
|
case <-job.stopped:
|
||||||
So(0, ShouldEqual, 0)
|
So(0, ShouldEqual, 0)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -118,6 +127,7 @@ echo $TUNASYNC_WORKING_DIR
|
|||||||
|
|
||||||
Convey("If we kill it", func(ctx C) {
|
Convey("If we kill it", func(ctx C) {
|
||||||
go job.Run(managerChan, semaphore)
|
go job.Run(managerChan, semaphore)
|
||||||
|
job.ctrlChan <- jobStart
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
msg := <-managerChan
|
msg := <-managerChan
|
||||||
@ -135,10 +145,12 @@ echo $TUNASYNC_WORKING_DIR
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(string(loggedContent), ShouldEqual, exceptedOutput)
|
So(string(loggedContent), ShouldEqual, exceptedOutput)
|
||||||
job.ctrlChan <- jobDisable
|
job.ctrlChan <- jobDisable
|
||||||
|
<-job.stopped
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("If we don't kill it", func(ctx C) {
|
Convey("If we don't kill it", func(ctx C) {
|
||||||
go job.Run(managerChan, semaphore)
|
go job.Run(managerChan, semaphore)
|
||||||
|
job.ctrlChan <- jobStart
|
||||||
|
|
||||||
msg := <-managerChan
|
msg := <-managerChan
|
||||||
So(msg.status, ShouldEqual, PreSyncing)
|
So(msg.status, ShouldEqual, PreSyncing)
|
||||||
@ -156,6 +168,7 @@ echo $TUNASYNC_WORKING_DIR
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(string(loggedContent), ShouldEqual, exceptedOutput)
|
So(string(loggedContent), ShouldEqual, exceptedOutput)
|
||||||
job.ctrlChan <- jobDisable
|
job.ctrlChan <- jobDisable
|
||||||
|
<-job.stopped
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user