job_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package worker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. . "github.com/smartystreets/goconvey/convey"
  10. . "github.com/tuna/tunasync/internal"
  11. )
  12. func TestMirrorJob(t *testing.T) {
  13. InitLogger(true, true, false)
  14. Convey("MirrorJob should work", t, func(ctx C) {
  15. tmpDir, err := ioutil.TempDir("", "tunasync")
  16. defer os.RemoveAll(tmpDir)
  17. So(err, ShouldBeNil)
  18. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  19. tmpFile := filepath.Join(tmpDir, "log_file")
  20. c := cmdConfig{
  21. name: "tuna-cmd-jobtest",
  22. upstreamURL: "http://mirrors.tuna.moe/",
  23. command: "bash " + scriptFile,
  24. workingDir: tmpDir,
  25. logDir: tmpDir,
  26. logFile: tmpFile,
  27. interval: 1 * time.Second,
  28. }
  29. provider, err := newCmdProvider(c)
  30. So(err, ShouldBeNil)
  31. So(provider.Name(), ShouldEqual, c.name)
  32. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  33. So(provider.LogDir(), ShouldEqual, c.logDir)
  34. So(provider.LogFile(), ShouldEqual, c.logFile)
  35. So(provider.Interval(), ShouldEqual, c.interval)
  36. Convey("For a normal mirror job", func(ctx C) {
  37. scriptContent := `#!/bin/bash
  38. echo $TUNASYNC_WORKING_DIR
  39. echo $TUNASYNC_MIRROR_NAME
  40. echo $TUNASYNC_UPSTREAM_URL
  41. echo $TUNASYNC_LOG_FILE
  42. `
  43. exceptedOutput := fmt.Sprintf(
  44. "%s\n%s\n%s\n%s\n",
  45. provider.WorkingDir(),
  46. provider.Name(),
  47. provider.upstreamURL,
  48. provider.LogFile(),
  49. )
  50. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  51. So(err, ShouldBeNil)
  52. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  53. So(err, ShouldBeNil)
  54. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  55. Convey("If we let it run several times", func(ctx C) {
  56. ctrlChan := make(chan ctrlAction)
  57. managerChan := make(chan struct{})
  58. semaphore := make(chan empty, 1)
  59. semaphore <- empty{}
  60. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  61. for i := 0; i < 2; i++ {
  62. <-managerChan
  63. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  64. So(err, ShouldBeNil)
  65. So(string(loggedContent), ShouldEqual, exceptedOutput)
  66. ctrlChan <- jobStart
  67. }
  68. select {
  69. case <-managerChan:
  70. So(0, ShouldEqual, 0) // made this fail
  71. case <-time.After(2 * time.Second):
  72. So(0, ShouldEqual, 1)
  73. }
  74. ctrlChan <- jobDisable
  75. select {
  76. case <-managerChan:
  77. So(0, ShouldEqual, 1) // made this fail
  78. case <-time.After(2 * time.Second):
  79. So(0, ShouldEqual, 0)
  80. }
  81. })
  82. })
  83. Convey("When running long jobs", func(ctx C) {
  84. scriptContent := `#!/bin/bash
  85. echo $TUNASYNC_WORKING_DIR
  86. sleep 5
  87. echo $TUNASYNC_WORKING_DIR
  88. `
  89. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  90. So(err, ShouldBeNil)
  91. ctrlChan := make(chan ctrlAction)
  92. managerChan := make(chan struct{})
  93. semaphore := make(chan empty, 1)
  94. semaphore <- empty{}
  95. Convey("If we kill it", func(ctx C) {
  96. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  97. time.Sleep(1 * time.Second)
  98. ctrlChan <- jobStop
  99. <-managerChan
  100. exceptedOutput := fmt.Sprintf("%s\n", provider.WorkingDir())
  101. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  102. So(err, ShouldBeNil)
  103. So(string(loggedContent), ShouldEqual, exceptedOutput)
  104. ctrlChan <- jobDisable
  105. })
  106. Convey("If we don't kill it", func(ctx C) {
  107. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  108. <-managerChan
  109. exceptedOutput := fmt.Sprintf(
  110. "%s\n%s\n",
  111. provider.WorkingDir(), provider.WorkingDir(),
  112. )
  113. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  114. So(err, ShouldBeNil)
  115. So(string(loggedContent), ShouldEqual, exceptedOutput)
  116. ctrlChan <- jobDisable
  117. })
  118. })
  119. })
  120. }