2
0

job_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. expectedOutput := 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. managerChan := make(chan jobMessage, 10)
  57. semaphore := make(chan empty, 1)
  58. job := newMirrorJob(provider)
  59. go job.Run(managerChan, semaphore)
  60. // job should not start if we don't start it
  61. select {
  62. case <-managerChan:
  63. So(0, ShouldEqual, 1) // made this fail
  64. case <-time.After(1 * time.Second):
  65. So(0, ShouldEqual, 0)
  66. }
  67. job.ctrlChan <- jobStart
  68. for i := 0; i < 2; i++ {
  69. msg := <-managerChan
  70. So(msg.status, ShouldEqual, PreSyncing)
  71. msg = <-managerChan
  72. So(msg.status, ShouldEqual, Syncing)
  73. msg = <-managerChan
  74. So(msg.status, ShouldEqual, Success)
  75. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  76. So(err, ShouldBeNil)
  77. So(string(loggedContent), ShouldEqual, expectedOutput)
  78. job.ctrlChan <- jobStart
  79. }
  80. select {
  81. case msg := <-managerChan:
  82. So(msg.status, ShouldEqual, PreSyncing)
  83. msg = <-managerChan
  84. So(msg.status, ShouldEqual, Syncing)
  85. msg = <-managerChan
  86. So(msg.status, ShouldEqual, Success)
  87. case <-time.After(2 * time.Second):
  88. So(0, ShouldEqual, 1)
  89. }
  90. job.ctrlChan <- jobDisable
  91. select {
  92. case <-managerChan:
  93. So(0, ShouldEqual, 1) // made this fail
  94. case <-job.disabled:
  95. So(0, ShouldEqual, 0)
  96. }
  97. })
  98. })
  99. Convey("When running long jobs", func(ctx C) {
  100. scriptContent := `#!/bin/bash
  101. echo $TUNASYNC_WORKING_DIR
  102. sleep 5
  103. echo $TUNASYNC_WORKING_DIR
  104. `
  105. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  106. So(err, ShouldBeNil)
  107. managerChan := make(chan jobMessage, 10)
  108. semaphore := make(chan empty, 1)
  109. job := newMirrorJob(provider)
  110. Convey("If we kill it", func(ctx C) {
  111. go job.Run(managerChan, semaphore)
  112. job.ctrlChan <- jobStart
  113. time.Sleep(1 * time.Second)
  114. msg := <-managerChan
  115. So(msg.status, ShouldEqual, PreSyncing)
  116. msg = <-managerChan
  117. So(msg.status, ShouldEqual, Syncing)
  118. job.ctrlChan <- jobStart // should be ignored
  119. job.ctrlChan <- jobStop
  120. msg = <-managerChan
  121. So(msg.status, ShouldEqual, Failed)
  122. expectedOutput := fmt.Sprintf("%s\n", provider.WorkingDir())
  123. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  124. So(err, ShouldBeNil)
  125. So(string(loggedContent), ShouldEqual, expectedOutput)
  126. job.ctrlChan <- jobDisable
  127. <-job.disabled
  128. })
  129. Convey("If we don't kill it", func(ctx C) {
  130. go job.Run(managerChan, semaphore)
  131. job.ctrlChan <- jobStart
  132. msg := <-managerChan
  133. So(msg.status, ShouldEqual, PreSyncing)
  134. msg = <-managerChan
  135. So(msg.status, ShouldEqual, Syncing)
  136. msg = <-managerChan
  137. So(msg.status, ShouldEqual, Success)
  138. expectedOutput := fmt.Sprintf(
  139. "%s\n%s\n",
  140. provider.WorkingDir(), provider.WorkingDir(),
  141. )
  142. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  143. So(err, ShouldBeNil)
  144. So(string(loggedContent), ShouldEqual, expectedOutput)
  145. job.ctrlChan <- jobDisable
  146. <-job.disabled
  147. })
  148. Convey("If we restart it", func(ctx C) {
  149. go job.Run(managerChan, semaphore)
  150. job.ctrlChan <- jobStart
  151. msg := <-managerChan
  152. So(msg.status, ShouldEqual, PreSyncing)
  153. msg = <-managerChan
  154. So(msg.status, ShouldEqual, Syncing)
  155. job.ctrlChan <- jobRestart
  156. msg = <-managerChan
  157. So(msg.status, ShouldEqual, Failed)
  158. So(msg.msg, ShouldEqual, "killed by manager")
  159. msg = <-managerChan
  160. So(msg.status, ShouldEqual, PreSyncing)
  161. msg = <-managerChan
  162. So(msg.status, ShouldEqual, Syncing)
  163. msg = <-managerChan
  164. So(msg.status, ShouldEqual, Success)
  165. expectedOutput := fmt.Sprintf(
  166. "%s\n%s\n",
  167. provider.WorkingDir(), provider.WorkingDir(),
  168. )
  169. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  170. So(err, ShouldBeNil)
  171. So(string(loggedContent), ShouldEqual, expectedOutput)
  172. job.ctrlChan <- jobDisable
  173. <-job.disabled
  174. })
  175. Convey("If we disable it", func(ctx C) {
  176. go job.Run(managerChan, semaphore)
  177. job.ctrlChan <- jobStart
  178. msg := <-managerChan
  179. So(msg.status, ShouldEqual, PreSyncing)
  180. msg = <-managerChan
  181. So(msg.status, ShouldEqual, Syncing)
  182. job.ctrlChan <- jobDisable
  183. msg = <-managerChan
  184. So(msg.status, ShouldEqual, Failed)
  185. So(msg.msg, ShouldEqual, "killed by manager")
  186. <-job.disabled
  187. })
  188. Convey("If we stop it twice, than start it", func(ctx C) {
  189. go job.Run(managerChan, semaphore)
  190. job.ctrlChan <- jobStart
  191. msg := <-managerChan
  192. So(msg.status, ShouldEqual, PreSyncing)
  193. msg = <-managerChan
  194. So(msg.status, ShouldEqual, Syncing)
  195. job.ctrlChan <- jobStop
  196. msg = <-managerChan
  197. So(msg.status, ShouldEqual, Failed)
  198. So(msg.msg, ShouldEqual, "killed by manager")
  199. job.ctrlChan <- jobStop // should be ignored
  200. job.ctrlChan <- jobStart
  201. msg = <-managerChan
  202. So(msg.status, ShouldEqual, PreSyncing)
  203. msg = <-managerChan
  204. So(msg.status, ShouldEqual, Syncing)
  205. msg = <-managerChan
  206. So(msg.status, ShouldEqual, Success)
  207. expectedOutput := fmt.Sprintf(
  208. "%s\n%s\n",
  209. provider.WorkingDir(), provider.WorkingDir(),
  210. )
  211. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  212. So(err, ShouldBeNil)
  213. So(string(loggedContent), ShouldEqual, expectedOutput)
  214. job.ctrlChan <- jobDisable
  215. <-job.disabled
  216. })
  217. })
  218. })
  219. }
  220. func TestConcurrentMirrorJobs(t *testing.T) {
  221. InitLogger(true, true, false)
  222. Convey("Concurrent MirrorJobs should work", t, func(ctx C) {
  223. tmpDir, err := ioutil.TempDir("", "tunasync")
  224. defer os.RemoveAll(tmpDir)
  225. So(err, ShouldBeNil)
  226. const CONCURRENT = 5
  227. var providers [CONCURRENT]*cmdProvider
  228. var jobs [CONCURRENT]*mirrorJob
  229. for i := 0; i < CONCURRENT; i++ {
  230. c := cmdConfig{
  231. name: fmt.Sprintf("job-%d", i),
  232. upstreamURL: "http://mirrors.tuna.moe/",
  233. command: "sleep 2",
  234. workingDir: tmpDir,
  235. logDir: tmpDir,
  236. logFile: "/dev/null",
  237. interval: 10 * time.Second,
  238. }
  239. var err error
  240. providers[i], err = newCmdProvider(c)
  241. So(err, ShouldBeNil)
  242. jobs[i] = newMirrorJob(providers[i])
  243. }
  244. managerChan := make(chan jobMessage, 10)
  245. semaphore := make(chan empty, CONCURRENT-2)
  246. countingJobs := func(managerChan chan jobMessage, totalJobs, concurrentCheck int) (peakConcurrent, counterFailed int) {
  247. counterEnded := 0
  248. counterRunning := 0
  249. peakConcurrent = 0
  250. counterFailed = 0
  251. for counterEnded < totalJobs {
  252. msg := <-managerChan
  253. switch msg.status {
  254. case PreSyncing:
  255. counterRunning++
  256. case Syncing:
  257. case Failed:
  258. counterFailed++
  259. fallthrough
  260. case Success:
  261. counterEnded++
  262. counterRunning--
  263. default:
  264. So(0, ShouldEqual, 1)
  265. }
  266. // Test if semaphore works
  267. So(counterRunning, ShouldBeLessThanOrEqualTo, concurrentCheck)
  268. if counterRunning > peakConcurrent {
  269. peakConcurrent = counterRunning
  270. }
  271. }
  272. // select {
  273. // case msg := <-managerChan:
  274. // logger.Errorf("extra message received: %v", msg)
  275. // So(0, ShouldEqual, 1)
  276. // case <-time.After(2 * time.Second):
  277. // }
  278. return
  279. }
  280. Convey("When we run them all", func(ctx C) {
  281. for _, job := range jobs {
  282. go job.Run(managerChan, semaphore)
  283. job.ctrlChan <- jobStart
  284. }
  285. peakConcurrent, counterFailed := countingJobs(managerChan, CONCURRENT, CONCURRENT-2)
  286. So(peakConcurrent, ShouldEqual, CONCURRENT-2)
  287. So(counterFailed, ShouldEqual, 0)
  288. for _, job := range jobs {
  289. job.ctrlChan <- jobDisable
  290. <-job.disabled
  291. }
  292. })
  293. Convey("If we cancel one job", func(ctx C) {
  294. for _, job := range jobs {
  295. go job.Run(managerChan, semaphore)
  296. job.ctrlChan <- jobRestart
  297. time.Sleep(200 * time.Millisecond)
  298. }
  299. // Cancel the one waiting for semaphore
  300. jobs[len(jobs)-1].ctrlChan <- jobStop
  301. peakConcurrent, counterFailed := countingJobs(managerChan, CONCURRENT-1, CONCURRENT-2)
  302. So(peakConcurrent, ShouldEqual, CONCURRENT-2)
  303. So(counterFailed, ShouldEqual, 0)
  304. for _, job := range jobs {
  305. job.ctrlChan <- jobDisable
  306. <-job.disabled
  307. }
  308. })
  309. Convey("If we override the concurrent limit", func(ctx C) {
  310. for _, job := range jobs {
  311. go job.Run(managerChan, semaphore)
  312. job.ctrlChan <- jobStart
  313. time.Sleep(200 * time.Millisecond)
  314. }
  315. jobs[len(jobs)-1].ctrlChan <- jobForceStart
  316. jobs[len(jobs)-2].ctrlChan <- jobForceStart
  317. peakConcurrent, counterFailed := countingJobs(managerChan, CONCURRENT, CONCURRENT)
  318. So(peakConcurrent, ShouldEqual, CONCURRENT)
  319. So(counterFailed, ShouldEqual, 0)
  320. time.Sleep(1 * time.Second)
  321. // fmt.Println("Restart them")
  322. for _, job := range jobs {
  323. job.ctrlChan <- jobStart
  324. }
  325. peakConcurrent, counterFailed = countingJobs(managerChan, CONCURRENT, CONCURRENT-2)
  326. So(peakConcurrent, ShouldEqual, CONCURRENT-2)
  327. So(counterFailed, ShouldEqual, 0)
  328. for _, job := range jobs {
  329. job.ctrlChan <- jobDisable
  330. <-job.disabled
  331. }
  332. })
  333. })
  334. }