job_test.go 3.6 KB

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