123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- package worker
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
- "time"
- . "github.com/smartystreets/goconvey/convey"
- . "github.com/tuna/tunasync/internal"
- )
- func TestMirrorJob(t *testing.T) {
- InitLogger(true, true, false)
- Convey("MirrorJob should work", t, func(ctx C) {
- tmpDir, err := ioutil.TempDir("", "tunasync")
- defer os.RemoveAll(tmpDir)
- So(err, ShouldBeNil)
- scriptFile := filepath.Join(tmpDir, "cmd.sh")
- tmpFile := filepath.Join(tmpDir, "log_file")
- c := cmdConfig{
- name: "tuna-cmd-jobtest",
- upstreamURL: "http://mirrors.tuna.moe/",
- command: "bash " + scriptFile,
- workingDir: tmpDir,
- logDir: tmpDir,
- logFile: tmpFile,
- interval: 1 * time.Second,
- timeout: 7 * time.Second,
- }
- provider, err := newCmdProvider(c)
- So(err, ShouldBeNil)
- So(provider.Name(), ShouldEqual, c.name)
- So(provider.WorkingDir(), ShouldEqual, c.workingDir)
- So(provider.LogDir(), ShouldEqual, c.logDir)
- So(provider.LogFile(), ShouldEqual, c.logFile)
- So(provider.Interval(), ShouldEqual, c.interval)
- So(provider.Timeout(), ShouldEqual, c.timeout)
- Convey("For a normal mirror job", func(ctx C) {
- scriptContent := `#!/bin/bash
- echo $TUNASYNC_WORKING_DIR
- echo $TUNASYNC_MIRROR_NAME
- echo $TUNASYNC_UPSTREAM_URL
- echo $TUNASYNC_LOG_FILE
- `
- expectedOutput := fmt.Sprintf(
- "%s\n%s\n%s\n%s\n",
- provider.WorkingDir(),
- provider.Name(),
- provider.upstreamURL,
- provider.LogFile(),
- )
- err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
- So(err, ShouldBeNil)
- readedScriptContent, err := ioutil.ReadFile(scriptFile)
- So(err, ShouldBeNil)
- So(readedScriptContent, ShouldResemble, []byte(scriptContent))
- Convey("If we let it run several times", func(ctx C) {
- managerChan := make(chan jobMessage, 10)
- semaphore := make(chan empty, 1)
- 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)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Success)
- loggedContent, err := ioutil.ReadFile(provider.LogFile())
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- job.ctrlChan <- jobStart
- }
- select {
- case msg := <-managerChan:
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Success)
- case <-time.After(2 * time.Second):
- So(0, ShouldEqual, 1)
- }
- job.ctrlChan <- jobDisable
- select {
- case <-managerChan:
- So(0, ShouldEqual, 1) // made this fail
- case <-job.disabled:
- So(0, ShouldEqual, 0)
- }
- })
- })
- 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
- sleep 5
- echo $TUNASYNC_WORKING_DIR
- `
- err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
- So(err, ShouldBeNil)
- 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 <- jobStart // should be ignored
- job.ctrlChan <- jobStop
- msg = <-managerChan
- So(msg.status, ShouldEqual, Failed)
- expectedOutput := fmt.Sprintf("%s\n", provider.WorkingDir())
- loggedContent, err := ioutil.ReadFile(provider.LogFile())
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- job.ctrlChan <- jobDisable
- <-job.disabled
- })
- 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)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Success)
- expectedOutput := fmt.Sprintf(
- "%s\n%s\n",
- provider.WorkingDir(), provider.WorkingDir(),
- )
- loggedContent, err := ioutil.ReadFile(provider.LogFile())
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- job.ctrlChan <- jobDisable
- <-job.disabled
- })
- Convey("If we restart it", func(ctx C) {
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- msg := <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- job.ctrlChan <- jobRestart
- msg = <-managerChan
- So(msg.status, ShouldEqual, Failed)
- So(msg.msg, ShouldEqual, "killed by manager")
- msg = <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Success)
- expectedOutput := fmt.Sprintf(
- "%s\n%s\n",
- provider.WorkingDir(), provider.WorkingDir(),
- )
- loggedContent, err := ioutil.ReadFile(provider.LogFile())
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- job.ctrlChan <- jobDisable
- <-job.disabled
- })
- Convey("If we disable it", func(ctx C) {
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- msg := <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- job.ctrlChan <- jobDisable
- msg = <-managerChan
- So(msg.status, ShouldEqual, Failed)
- So(msg.msg, ShouldEqual, "killed by manager")
- <-job.disabled
- })
- Convey("If we stop it twice, than start it", func(ctx C) {
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- msg := <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- job.ctrlChan <- jobStop
- msg = <-managerChan
- So(msg.status, ShouldEqual, Failed)
- So(msg.msg, ShouldEqual, "killed by manager")
- job.ctrlChan <- jobStop // should be ignored
- job.ctrlChan <- jobStart
- msg = <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Success)
- expectedOutput := fmt.Sprintf(
- "%s\n%s\n",
- provider.WorkingDir(), provider.WorkingDir(),
- )
- loggedContent, err := ioutil.ReadFile(provider.LogFile())
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- job.ctrlChan <- jobDisable
- <-job.disabled
- })
- })
- Convey("When a job timed out", func(ctx C) {
- scriptContent := `#!/bin/bash
- echo $TUNASYNC_WORKING_DIR
- sleep 10
- echo $TUNASYNC_WORKING_DIR
- `
- err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
- So(err, ShouldBeNil)
- managerChan := make(chan jobMessage, 10)
- semaphore := make(chan empty, 1)
- job := newMirrorJob(provider)
- Convey("It should be automatically terminated", 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 <- jobStart // should be ignored
- msg = <-managerChan
- So(msg.status, ShouldEqual, Failed)
- expectedOutput := fmt.Sprintf("%s\n", provider.WorkingDir())
- loggedContent, err := ioutil.ReadFile(provider.LogFile())
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- job.ctrlChan <- jobDisable
- <-job.disabled
- })
- Convey("It should be retried", func(ctx C) {
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- time.Sleep(1 * time.Second)
- msg := <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- for i := 0; i < defaultMaxRetry; i++ {
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- job.ctrlChan <- jobStart // should be ignored
- msg = <-managerChan
- So(msg.status, ShouldEqual, Failed)
- So(msg.msg, ShouldContainSubstring, "timeout after")
- // re-schedule after last try
- So(msg.schedule, ShouldEqual, i == defaultMaxRetry-1)
- }
- job.ctrlChan <- jobDisable
- <-job.disabled
- })
- })
- })
- }
- func TestConcurrentMirrorJobs(t *testing.T) {
- InitLogger(true, true, false)
- Convey("Concurrent MirrorJobs should work", t, func(ctx C) {
- tmpDir, err := ioutil.TempDir("", "tunasync")
- defer os.RemoveAll(tmpDir)
- So(err, ShouldBeNil)
- const CONCURRENT = 5
- var providers [CONCURRENT]*cmdProvider
- var jobs [CONCURRENT]*mirrorJob
- for i := 0; i < CONCURRENT; i++ {
- c := cmdConfig{
- name: fmt.Sprintf("job-%d", i),
- upstreamURL: "http://mirrors.tuna.moe/",
- command: "sleep 2",
- workingDir: tmpDir,
- logDir: tmpDir,
- logFile: "/dev/null",
- interval: 10 * time.Second,
- }
- var err error
- providers[i], err = newCmdProvider(c)
- So(err, ShouldBeNil)
- jobs[i] = newMirrorJob(providers[i])
- }
- managerChan := make(chan jobMessage, 10)
- semaphore := make(chan empty, CONCURRENT-2)
- countingJobs := func(managerChan chan jobMessage, totalJobs, concurrentCheck int) (peakConcurrent, counterFailed int) {
- counterEnded := 0
- counterRunning := 0
- peakConcurrent = 0
- counterFailed = 0
- for counterEnded < totalJobs {
- msg := <-managerChan
- switch msg.status {
- case PreSyncing:
- counterRunning++
- case Syncing:
- case Failed:
- counterFailed++
- fallthrough
- case Success:
- counterEnded++
- counterRunning--
- default:
- So(0, ShouldEqual, 1)
- }
- // Test if semaphore works
- So(counterRunning, ShouldBeLessThanOrEqualTo, concurrentCheck)
- if counterRunning > peakConcurrent {
- peakConcurrent = counterRunning
- }
- }
- // select {
- // case msg := <-managerChan:
- // logger.Errorf("extra message received: %v", msg)
- // So(0, ShouldEqual, 1)
- // case <-time.After(2 * time.Second):
- // }
- return
- }
- Convey("When we run them all", func(ctx C) {
- for _, job := range jobs {
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- }
- peakConcurrent, counterFailed := countingJobs(managerChan, CONCURRENT, CONCURRENT-2)
- So(peakConcurrent, ShouldEqual, CONCURRENT-2)
- So(counterFailed, ShouldEqual, 0)
- for _, job := range jobs {
- job.ctrlChan <- jobDisable
- <-job.disabled
- }
- })
- Convey("If we cancel one job", func(ctx C) {
- for _, job := range jobs {
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobRestart
- time.Sleep(200 * time.Millisecond)
- }
- // Cancel the one waiting for semaphore
- jobs[len(jobs)-1].ctrlChan <- jobStop
- peakConcurrent, counterFailed := countingJobs(managerChan, CONCURRENT-1, CONCURRENT-2)
- So(peakConcurrent, ShouldEqual, CONCURRENT-2)
- So(counterFailed, ShouldEqual, 0)
- for _, job := range jobs {
- job.ctrlChan <- jobDisable
- <-job.disabled
- }
- })
- Convey("If we override the concurrent limit", func(ctx C) {
- for _, job := range jobs {
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- time.Sleep(200 * time.Millisecond)
- }
- jobs[len(jobs)-1].ctrlChan <- jobForceStart
- jobs[len(jobs)-2].ctrlChan <- jobForceStart
- peakConcurrent, counterFailed := countingJobs(managerChan, CONCURRENT, CONCURRENT)
- So(peakConcurrent, ShouldEqual, CONCURRENT)
- So(counterFailed, ShouldEqual, 0)
- time.Sleep(1 * time.Second)
- // fmt.Println("Restart them")
- for _, job := range jobs {
- job.ctrlChan <- jobStart
- }
- peakConcurrent, counterFailed = countingJobs(managerChan, CONCURRENT, CONCURRENT-2)
- So(peakConcurrent, ShouldEqual, CONCURRENT-2)
- So(counterFailed, ShouldEqual, 0)
- for _, job := range jobs {
- job.ctrlChan <- jobDisable
- <-job.disabled
- }
- })
- })
- }
|