job_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  60. for i := 0; i < 2; i++ {
  61. <-managerChan
  62. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  63. So(err, ShouldBeNil)
  64. So(string(loggedContent), ShouldEqual, exceptedOutput)
  65. ctrlChan <- jobStart
  66. }
  67. select {
  68. case <-managerChan:
  69. So(0, ShouldEqual, 0) // made this fail
  70. case <-time.After(2 * time.Second):
  71. So(0, ShouldEqual, 1)
  72. }
  73. ctrlChan <- jobDisable
  74. select {
  75. case <-managerChan:
  76. So(0, ShouldEqual, 1) // made this fail
  77. case <-time.After(2 * time.Second):
  78. So(0, ShouldEqual, 0)
  79. }
  80. })
  81. })
  82. Convey("When running long jobs", func(ctx C) {
  83. scriptContent := `#!/bin/bash
  84. echo $TUNASYNC_WORKING_DIR
  85. sleep 5
  86. echo $TUNASYNC_WORKING_DIR
  87. `
  88. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  89. So(err, ShouldBeNil)
  90. ctrlChan := make(chan ctrlAction)
  91. managerChan := make(chan struct{})
  92. semaphore := make(chan empty, 1)
  93. Convey("If we kill it", func(ctx C) {
  94. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  95. time.Sleep(1 * time.Second)
  96. ctrlChan <- jobStop
  97. <-managerChan
  98. exceptedOutput := fmt.Sprintf("%s\n", provider.WorkingDir())
  99. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  100. So(err, ShouldBeNil)
  101. So(string(loggedContent), ShouldEqual, exceptedOutput)
  102. ctrlChan <- jobDisable
  103. })
  104. Convey("If we don't kill it", func(ctx C) {
  105. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  106. <-managerChan
  107. exceptedOutput := fmt.Sprintf(
  108. "%s\n%s\n",
  109. provider.WorkingDir(), provider.WorkingDir(),
  110. )
  111. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  112. So(err, ShouldBeNil)
  113. So(string(loggedContent), ShouldEqual, exceptedOutput)
  114. ctrlChan <- jobDisable
  115. })
  116. })
  117. })
  118. }