cron_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package cron
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "time"
  7. )
  8. // Many tests schedule a job for every second, and then wait at most a second
  9. // for it to run. This amount is just slightly larger than 1 second to
  10. // compensate for a few milliseconds of runtime.
  11. const ONE_SECOND = 1*time.Second + 10*time.Millisecond
  12. // Start and stop cron with no entries.
  13. func TestNoEntries(t *testing.T) {
  14. cron := New()
  15. cron.Start()
  16. select {
  17. case <-time.After(ONE_SECOND):
  18. t.FailNow()
  19. case <-stop(cron):
  20. }
  21. }
  22. // Start, stop, then add an entry. Verify entry doesn't run.
  23. func TestStopCausesJobsToNotRun(t *testing.T) {
  24. wg := &sync.WaitGroup{}
  25. wg.Add(1)
  26. cron := New()
  27. cron.Start()
  28. cron.Stop()
  29. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  30. select {
  31. case <-time.After(ONE_SECOND):
  32. // No job ran!
  33. case <-wait(wg):
  34. t.FailNow()
  35. }
  36. }
  37. // Add a job, start cron, expect it runs.
  38. func TestAddBeforeRunning(t *testing.T) {
  39. wg := &sync.WaitGroup{}
  40. wg.Add(1)
  41. cron := New()
  42. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  43. cron.Start()
  44. defer cron.Stop()
  45. // Give cron 2 seconds to run our job (which is always activated).
  46. select {
  47. case <-time.After(ONE_SECOND):
  48. t.FailNow()
  49. case <-wait(wg):
  50. }
  51. }
  52. // Start cron, add a job, expect it runs.
  53. func TestAddWhileRunning(t *testing.T) {
  54. wg := &sync.WaitGroup{}
  55. wg.Add(1)
  56. cron := New()
  57. cron.Start()
  58. defer cron.Stop()
  59. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  60. select {
  61. case <-time.After(ONE_SECOND):
  62. t.FailNow()
  63. case <-wait(wg):
  64. }
  65. }
  66. // Add a job, remove a job, start cron, expect nothing runs.
  67. func TestRemoveBeforeRunning(t *testing.T) {
  68. wg := &sync.WaitGroup{}
  69. wg.Add(1)
  70. cron := New()
  71. id, _ := cron.AddFunc("* * * * * ?", func() { wg.Done() })
  72. cron.Remove(id)
  73. cron.Start()
  74. defer cron.Stop()
  75. select {
  76. case <-time.After(ONE_SECOND):
  77. // Success, shouldn't run
  78. case <-wait(wg):
  79. t.FailNow()
  80. }
  81. }
  82. // Start cron, add a job, remove it, expect it doesn't run.
  83. func TestRemoveWhileRunning(t *testing.T) {
  84. wg := &sync.WaitGroup{}
  85. wg.Add(1)
  86. cron := New()
  87. cron.Start()
  88. defer cron.Stop()
  89. id, _ := cron.AddFunc("* * * * * ?", func() { wg.Done() })
  90. cron.Remove(id)
  91. select {
  92. case <-time.After(ONE_SECOND):
  93. case <-wait(wg):
  94. t.FailNow()
  95. }
  96. }
  97. // Test timing with Entries.
  98. func TestSnapshotEntries(t *testing.T) {
  99. wg := &sync.WaitGroup{}
  100. wg.Add(1)
  101. cron := New()
  102. cron.AddFunc("@every 2s", func() { wg.Done() })
  103. cron.Start()
  104. defer cron.Stop()
  105. // Cron should fire in 2 seconds. After 1 second, call Entries.
  106. select {
  107. case <-time.After(ONE_SECOND):
  108. cron.Entries()
  109. }
  110. // Even though Entries was called, the cron should fire at the 2 second mark.
  111. select {
  112. case <-time.After(ONE_SECOND):
  113. t.FailNow()
  114. case <-wait(wg):
  115. }
  116. }
  117. // Test that the entries are correctly sorted.
  118. // Add a bunch of long-in-the-future entries, and an immediate entry, and ensure
  119. // that the immediate entry runs immediately.
  120. // Also: Test that multiple jobs run in the same instant.
  121. func TestMultipleEntries(t *testing.T) {
  122. wg := &sync.WaitGroup{}
  123. wg.Add(2)
  124. cron := New()
  125. cron.AddFunc("0 0 0 1 1 ?", func() {})
  126. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  127. id1, _ := cron.AddFunc("* * * * * ?", func() { t.Fatal() })
  128. id2, _ := cron.AddFunc("* * * * * ?", func() { t.Fatal() })
  129. cron.AddFunc("0 0 0 31 12 ?", func() {})
  130. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  131. cron.Remove(id1)
  132. cron.Start()
  133. cron.Remove(id2)
  134. defer cron.Stop()
  135. select {
  136. case <-time.After(ONE_SECOND):
  137. t.FailNow()
  138. case <-wait(wg):
  139. }
  140. }
  141. // Test running the same job twice.
  142. func TestRunningJobTwice(t *testing.T) {
  143. wg := &sync.WaitGroup{}
  144. wg.Add(2)
  145. cron := New()
  146. cron.AddFunc("0 0 0 1 1 ?", func() {})
  147. cron.AddFunc("0 0 0 31 12 ?", func() {})
  148. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  149. cron.Start()
  150. defer cron.Stop()
  151. select {
  152. case <-time.After(2 * ONE_SECOND):
  153. t.FailNow()
  154. case <-wait(wg):
  155. }
  156. }
  157. func TestRunningMultipleSchedules(t *testing.T) {
  158. wg := &sync.WaitGroup{}
  159. wg.Add(2)
  160. cron := New()
  161. cron.AddFunc("0 0 0 1 1 ?", func() {})
  162. cron.AddFunc("0 0 0 31 12 ?", func() {})
  163. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  164. cron.Schedule(Every(time.Minute), FuncJob(func() {}))
  165. cron.Schedule(Every(time.Second), FuncJob(func() { wg.Done() }))
  166. cron.Schedule(Every(time.Hour), FuncJob(func() {}))
  167. cron.Start()
  168. defer cron.Stop()
  169. select {
  170. case <-time.After(2 * ONE_SECOND):
  171. t.FailNow()
  172. case <-wait(wg):
  173. }
  174. }
  175. // Test that the cron is run in the local time zone (as opposed to UTC).
  176. func TestLocalTimezone(t *testing.T) {
  177. wg := &sync.WaitGroup{}
  178. wg.Add(1)
  179. now := time.Now().Local()
  180. spec := fmt.Sprintf("%d %d %d %d %d ?",
  181. now.Second()+1, now.Minute(), now.Hour(), now.Day(), now.Month())
  182. cron := New()
  183. cron.AddFunc(spec, func() { wg.Done() })
  184. cron.Start()
  185. defer cron.Stop()
  186. select {
  187. case <-time.After(ONE_SECOND):
  188. t.FailNow()
  189. case <-wait(wg):
  190. }
  191. }
  192. type testJob struct {
  193. wg *sync.WaitGroup
  194. name string
  195. }
  196. func (t testJob) Run() {
  197. t.wg.Done()
  198. }
  199. // Simple test using Runnables.
  200. func TestJob(t *testing.T) {
  201. wg := &sync.WaitGroup{}
  202. wg.Add(1)
  203. cron := New()
  204. cron.AddJob("0 0 0 30 Feb ?", testJob{wg, "job0"})
  205. cron.AddJob("0 0 0 1 1 ?", testJob{wg, "job1"})
  206. cron.AddJob("* * * * * ?", testJob{wg, "job2"})
  207. cron.AddJob("1 0 0 1 1 ?", testJob{wg, "job3"})
  208. cron.Schedule(Every(5*time.Second+5*time.Nanosecond), testJob{wg, "job4"})
  209. cron.Schedule(Every(5*time.Minute), testJob{wg, "job5"})
  210. cron.Start()
  211. defer cron.Stop()
  212. select {
  213. case <-time.After(ONE_SECOND):
  214. t.FailNow()
  215. case <-wait(wg):
  216. }
  217. // Ensure the entries are in the right order.
  218. expecteds := []string{"job2", "job4", "job5", "job1", "job3", "job0"}
  219. var actuals []string
  220. for _, entry := range cron.Entries() {
  221. actuals = append(actuals, entry.Job.(testJob).name)
  222. }
  223. for i, expected := range expecteds {
  224. if actuals[i] != expected {
  225. t.Errorf("Jobs not in the right order. (expected) %s != %s (actual)", expecteds, actuals)
  226. t.FailNow()
  227. }
  228. }
  229. }
  230. func wait(wg *sync.WaitGroup) chan bool {
  231. ch := make(chan bool)
  232. go func() {
  233. wg.Wait()
  234. ch <- true
  235. }()
  236. return ch
  237. }
  238. func stop(cron *Cron) chan bool {
  239. ch := make(chan bool)
  240. go func() {
  241. cron.Stop()
  242. ch <- true
  243. }()
  244. return ch
  245. }