cron_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. // Test for #34. Adding a job after calling start results in multiple job invocations
  67. func TestAddWhileRunningWithDelay(t *testing.T) {
  68. cron := New()
  69. cron.Start()
  70. defer cron.Stop()
  71. time.Sleep(5 * time.Second)
  72. var calls = 0
  73. cron.AddFunc("* * * * * *", func() { calls += 1 });
  74. <- time.After(ONE_SECOND)
  75. if calls != 1 {
  76. fmt.Printf("called %d times, expected 1\n", calls)
  77. t.Fail()
  78. }
  79. }
  80. // Test timing with Entries.
  81. func TestSnapshotEntries(t *testing.T) {
  82. wg := &sync.WaitGroup{}
  83. wg.Add(1)
  84. cron := New()
  85. cron.AddFunc("@every 2s", func() { wg.Done() })
  86. cron.Start()
  87. defer cron.Stop()
  88. // Cron should fire in 2 seconds. After 1 second, call Entries.
  89. select {
  90. case <-time.After(ONE_SECOND):
  91. cron.Entries()
  92. }
  93. // Even though Entries was called, the cron should fire at the 2 second mark.
  94. select {
  95. case <-time.After(ONE_SECOND):
  96. t.FailNow()
  97. case <-wait(wg):
  98. }
  99. }
  100. // Test that the entries are correctly sorted.
  101. // Add a bunch of long-in-the-future entries, and an immediate entry, and ensure
  102. // that the immediate entry runs immediately.
  103. // Also: Test that multiple jobs run in the same instant.
  104. func TestMultipleEntries(t *testing.T) {
  105. wg := &sync.WaitGroup{}
  106. wg.Add(2)
  107. cron := New()
  108. cron.AddFunc("0 0 0 1 1 ?", func() {})
  109. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  110. cron.AddFunc("0 0 0 31 12 ?", func() {})
  111. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  112. cron.Start()
  113. defer cron.Stop()
  114. select {
  115. case <-time.After(ONE_SECOND):
  116. t.FailNow()
  117. case <-wait(wg):
  118. }
  119. }
  120. // Test running the same job twice.
  121. func TestRunningJobTwice(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("0 0 0 31 12 ?", func() {})
  127. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  128. cron.Start()
  129. defer cron.Stop()
  130. select {
  131. case <-time.After(2 * ONE_SECOND):
  132. t.FailNow()
  133. case <-wait(wg):
  134. }
  135. }
  136. func TestRunningMultipleSchedules(t *testing.T) {
  137. wg := &sync.WaitGroup{}
  138. wg.Add(2)
  139. cron := New()
  140. cron.AddFunc("0 0 0 1 1 ?", func() {})
  141. cron.AddFunc("0 0 0 31 12 ?", func() {})
  142. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  143. cron.Schedule(Every(time.Minute), FuncJob(func() {}))
  144. cron.Schedule(Every(time.Second), FuncJob(func() { wg.Done() }))
  145. cron.Schedule(Every(time.Hour), FuncJob(func() {}))
  146. cron.Start()
  147. defer cron.Stop()
  148. select {
  149. case <-time.After(2 * ONE_SECOND):
  150. t.FailNow()
  151. case <-wait(wg):
  152. }
  153. }
  154. // Test that the cron is run in the local time zone (as opposed to UTC).
  155. func TestLocalTimezone(t *testing.T) {
  156. wg := &sync.WaitGroup{}
  157. wg.Add(1)
  158. now := time.Now().Local()
  159. spec := fmt.Sprintf("%d %d %d %d %d ?",
  160. now.Second()+1, now.Minute(), now.Hour(), now.Day(), now.Month())
  161. cron := New()
  162. cron.AddFunc(spec, func() { wg.Done() })
  163. cron.Start()
  164. defer cron.Stop()
  165. select {
  166. case <-time.After(ONE_SECOND):
  167. t.FailNow()
  168. case <-wait(wg):
  169. }
  170. }
  171. // Test that calling stop before start silently returns without
  172. // blocking the stop channel.
  173. func TestStopWithoutStart(t *testing.T) {
  174. cron := New()
  175. cron.Stop()
  176. }
  177. type testJob struct {
  178. wg *sync.WaitGroup
  179. name string
  180. }
  181. func (t testJob) Run() {
  182. t.wg.Done()
  183. }
  184. // Simple test using Runnables.
  185. func TestJob(t *testing.T) {
  186. wg := &sync.WaitGroup{}
  187. wg.Add(1)
  188. cron := New()
  189. cron.AddJob("0 0 0 30 Feb ?", testJob{wg, "job0"})
  190. cron.AddJob("0 0 0 1 1 ?", testJob{wg, "job1"})
  191. cron.AddJob("* * * * * ?", testJob{wg, "job2"})
  192. cron.AddJob("1 0 0 1 1 ?", testJob{wg, "job3"})
  193. cron.Schedule(Every(5*time.Second+5*time.Nanosecond), testJob{wg, "job4"})
  194. cron.Schedule(Every(5*time.Minute), testJob{wg, "job5"})
  195. cron.Start()
  196. defer cron.Stop()
  197. select {
  198. case <-time.After(ONE_SECOND):
  199. t.FailNow()
  200. case <-wait(wg):
  201. }
  202. // Ensure the entries are in the right order.
  203. expecteds := []string{"job2", "job4", "job5", "job1", "job3", "job0"}
  204. var actuals []string
  205. for _, entry := range cron.Entries() {
  206. actuals = append(actuals, entry.Job.(testJob).name)
  207. }
  208. for i, expected := range expecteds {
  209. if actuals[i] != expected {
  210. t.Errorf("Jobs not in the right order. (expected) %s != %s (actual)", expecteds, actuals)
  211. t.FailNow()
  212. }
  213. }
  214. }
  215. func wait(wg *sync.WaitGroup) chan bool {
  216. ch := make(chan bool)
  217. go func() {
  218. wg.Wait()
  219. ch <- true
  220. }()
  221. return ch
  222. }
  223. func stop(cron *Cron) chan bool {
  224. ch := make(chan bool)
  225. go func() {
  226. cron.Stop()
  227. ch <- true
  228. }()
  229. return ch
  230. }