From bc416a608870a95ff73a89b61b1ff5cb3185e3fd Mon Sep 17 00:00:00 2001 From: z4yx Date: Sat, 30 Nov 2019 00:08:46 +0800 Subject: [PATCH] add tests for post-hooks --- worker/job_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/worker/job_test.go b/worker/job_test.go index 370c9be..8bce93b 100644 --- a/worker/job_test.go +++ b/worker/job_test.go @@ -112,6 +112,74 @@ func TestMirrorJob(t *testing.T) { }) + Convey("When running long jobs with post-fail hook", func(ctx C) { + scriptContent := `#!/bin/bash +echo '++++++' +echo $TUNASYNC_WORKING_DIR +echo $0 sleeping +sleep 3 +echo $TUNASYNC_WORKING_DIR +echo '------' + ` + err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755) + So(err, ShouldBeNil) + + hookScriptFile := filepath.Join(tmpDir, "hook.sh") + err = ioutil.WriteFile(hookScriptFile, []byte(scriptContent), 0755) + So(err, ShouldBeNil) + + h, err := newExecPostHook(provider, execOnFailure, hookScriptFile) + So(err, ShouldBeNil) + provider.AddHook(h) + + managerChan := make(chan jobMessage, 10) + semaphore := make(chan empty, 1) + job := newMirrorJob(provider) + + Convey("If we kill it", func(ctx C) { + go job.Run(managerChan, semaphore) + job.ctrlChan <- jobStart + + time.Sleep(1 * time.Second) + msg := <-managerChan + So(msg.status, ShouldEqual, PreSyncing) + msg = <-managerChan + So(msg.status, ShouldEqual, Syncing) + + job.ctrlChan <- jobStop + + msg = <-managerChan + So(msg.status, ShouldEqual, Failed) + + job.ctrlChan <- jobDisable + <-job.disabled + }) + + Convey("If we kill it then start it", func(ctx C) { + go job.Run(managerChan, semaphore) + job.ctrlChan <- jobStart + + time.Sleep(1 * time.Second) + msg := <-managerChan + So(msg.status, ShouldEqual, PreSyncing) + msg = <-managerChan + So(msg.status, ShouldEqual, Syncing) + + job.ctrlChan <- jobStop + + time.Sleep(2 * time.Second) + logger.Debugf("Now starting...\n") + job.ctrlChan <- jobStart + + msg = <-managerChan + So(msg.status, ShouldEqual, Failed) + + job.ctrlChan <- jobDisable + <-job.disabled + }) + + }) + Convey("When running long jobs", func(ctx C) { scriptContent := `#!/bin/bash echo $TUNASYNC_WORKING_DIR