You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.3 KiB

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