cron_test.go 6.2 KB

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