cron_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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("* * * * * ?", func() { wg.Done() })
  93. cron.AddFunc("0 0 0 31 12 ?", func() {})
  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. // Test that the cron is run in the local time zone (as opposed to UTC).
  103. func TestLocalTimezone(t *testing.T) {
  104. wg := &sync.WaitGroup{}
  105. wg.Add(1)
  106. now := time.Now().Local()
  107. spec := fmt.Sprintf("%d %d %d %d %d ?",
  108. now.Second()+1, now.Minute(), now.Hour(), now.Day(), now.Month())
  109. cron := New()
  110. cron.AddFunc(spec, func() { wg.Done() })
  111. cron.Start()
  112. defer cron.Stop()
  113. select {
  114. case <-time.After(ONE_SECOND):
  115. t.FailNow()
  116. case <-wait(wg):
  117. }
  118. }
  119. type testJob struct {
  120. wg *sync.WaitGroup
  121. name string
  122. }
  123. func (t testJob) Run() {
  124. t.wg.Done()
  125. }
  126. // Simple test using Runnables.
  127. func TestJob(t *testing.T) {
  128. wg := &sync.WaitGroup{}
  129. wg.Add(1)
  130. cron := New()
  131. cron.AddJob("0 0 0 30 Feb ?", testJob{wg, "job0"})
  132. cron.AddJob("0 0 0 1 1 ?", testJob{wg, "job1"})
  133. cron.AddJob("* * * * * ?", testJob{wg, "job2"})
  134. cron.AddJob("1 0 0 1 1 ?", testJob{wg, "job3"})
  135. cron.Start()
  136. defer cron.Stop()
  137. select {
  138. case <-time.After(ONE_SECOND):
  139. t.FailNow()
  140. case <-wait(wg):
  141. }
  142. // Ensure the entries are in the right order.
  143. answers := []string{"job2", "job1", "job3", "job0"}
  144. for i, answer := range answers {
  145. actual := cron.Entries()[i].Job.(testJob).name
  146. if actual != answer {
  147. t.Errorf("Jobs not in the right order. (expected) %s != %s (actual)", answer, actual)
  148. }
  149. }
  150. }
  151. func wait(wg *sync.WaitGroup) chan bool {
  152. ch := make(chan bool)
  153. go func() {
  154. wg.Wait()
  155. ch <- true
  156. }()
  157. return ch
  158. }
  159. func stop(cron *Cron) chan bool {
  160. ch := make(chan bool)
  161. go func() {
  162. cron.Stop()
  163. ch <- true
  164. }()
  165. return ch
  166. }