cron_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 that the entries are correctly sorted.
  67. // Add a bunch of long-in-the-future entries, and an immediate entry, and ensure
  68. // that the immediate entry runs immediately.
  69. // Also: Test that multiple jobs run in the same instant.
  70. func TestMultipleEntries(t *testing.T) {
  71. wg := &sync.WaitGroup{}
  72. wg.Add(2)
  73. cron := New()
  74. cron.AddFunc("0 0 0 1 1 ?", func() {})
  75. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  76. cron.AddFunc("0 0 0 31 12 ?", func() {})
  77. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  78. cron.Start()
  79. defer cron.Stop()
  80. select {
  81. case <-time.After(ONE_SECOND):
  82. t.FailNow()
  83. case <-wait(wg):
  84. }
  85. }
  86. // Test running the same job twice.
  87. func TestRunningJobTwice(t *testing.T) {
  88. wg := &sync.WaitGroup{}
  89. wg.Add(2)
  90. cron := New()
  91. cron.AddFunc("0 0 0 1 1 ?", func() {})
  92. cron.AddFunc("0 0 0 31 12 ?", func() {})
  93. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  94. cron.Start()
  95. defer cron.Stop()
  96. select {
  97. case <-time.After(2 * ONE_SECOND):
  98. t.FailNow()
  99. case <-wait(wg):
  100. }
  101. }
  102. func TestRunningMultipleSchedules(t *testing.T) {
  103. wg := &sync.WaitGroup{}
  104. wg.Add(2)
  105. cron := New()
  106. cron.AddFunc("0 0 0 1 1 ?", func() {})
  107. cron.AddFunc("0 0 0 31 12 ?", func() {})
  108. cron.AddFunc("* * * * * ?", func() { wg.Done() })
  109. cron.Schedule(Every(time.Minute), FuncJob(func() {}))
  110. cron.Schedule(Every(time.Second), FuncJob(func() { wg.Done() }))
  111. cron.Schedule(Every(time.Hour), FuncJob(func() {}))
  112. cron.Start()
  113. defer cron.Stop()
  114. select {
  115. case <-time.After(2 * ONE_SECOND):
  116. t.FailNow()
  117. case <-wait(wg):
  118. }
  119. }
  120. // Test that the cron is run in the local time zone (as opposed to UTC).
  121. func TestLocalTimezone(t *testing.T) {
  122. wg := &sync.WaitGroup{}
  123. wg.Add(1)
  124. now := time.Now().Local()
  125. spec := fmt.Sprintf("%d %d %d %d %d ?",
  126. now.Second()+1, now.Minute(), now.Hour(), now.Day(), now.Month())
  127. cron := New()
  128. cron.AddFunc(spec, func() { wg.Done() })
  129. cron.Start()
  130. defer cron.Stop()
  131. select {
  132. case <-time.After(ONE_SECOND):
  133. t.FailNow()
  134. case <-wait(wg):
  135. }
  136. }
  137. type testJob struct {
  138. wg *sync.WaitGroup
  139. name string
  140. }
  141. func (t testJob) Run() {
  142. t.wg.Done()
  143. }
  144. // Simple test using Runnables.
  145. func TestJob(t *testing.T) {
  146. wg := &sync.WaitGroup{}
  147. wg.Add(1)
  148. cron := New()
  149. cron.AddJob("0 0 0 30 Feb ?", testJob{wg, "job0"})
  150. cron.AddJob("0 0 0 1 1 ?", testJob{wg, "job1"})
  151. cron.AddJob("* * * * * ?", testJob{wg, "job2"})
  152. cron.AddJob("1 0 0 1 1 ?", testJob{wg, "job3"})
  153. cron.Schedule(Every(5*time.Second+5*time.Nanosecond), testJob{wg, "job4"})
  154. cron.Schedule(Every(5*time.Minute), testJob{wg, "job5"})
  155. cron.Start()
  156. defer cron.Stop()
  157. select {
  158. case <-time.After(ONE_SECOND):
  159. t.FailNow()
  160. case <-wait(wg):
  161. }
  162. // Ensure the entries are in the right order.
  163. expecteds := []string{"job2", "job4", "job5", "job1", "job3", "job0"}
  164. var actuals []string
  165. for _, entry := range cron.Entries() {
  166. actuals = append(actuals, entry.Job.(testJob).name)
  167. }
  168. for i, expected := range expecteds {
  169. if actuals[i] != expected {
  170. t.Errorf("Jobs not in the right order. (expected) %s != %s (actual)", expecteds, actuals)
  171. t.FailNow()
  172. }
  173. }
  174. }
  175. func wait(wg *sync.WaitGroup) chan bool {
  176. ch := make(chan bool)
  177. go func() {
  178. wg.Wait()
  179. ch <- true
  180. }()
  181. return ch
  182. }
  183. func stop(cron *Cron) chan bool {
  184. ch := make(chan bool)
  185. go func() {
  186. cron.Stop()
  187. ch <- true
  188. }()
  189. return ch
  190. }