cron_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package cron
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestCronNew(t *testing.T) {
  9. c := New()
  10. expectedInterval := 1 * time.Minute
  11. if c.interval != expectedInterval {
  12. t.Fatalf("Expected default interval %v, got %v", expectedInterval, c.interval)
  13. }
  14. expectedTimezone := time.UTC
  15. if c.timezone.String() != expectedTimezone.String() {
  16. t.Fatalf("Expected default timezone %v, got %v", expectedTimezone, c.timezone)
  17. }
  18. if len(c.jobs) != 0 {
  19. t.Fatalf("Expected no jobs by default, got \n%v", c.jobs)
  20. }
  21. if c.ticker != nil {
  22. t.Fatal("Expected the ticker NOT to be initialized")
  23. }
  24. }
  25. func TestCronSetInterval(t *testing.T) {
  26. c := New()
  27. interval := 2 * time.Minute
  28. c.SetInterval(interval)
  29. if c.interval != interval {
  30. t.Fatalf("Expected interval %v, got %v", interval, c.interval)
  31. }
  32. }
  33. func TestCronSetTimezone(t *testing.T) {
  34. c := New()
  35. timezone, _ := time.LoadLocation("Asia/Tokyo")
  36. c.SetTimezone(timezone)
  37. if c.timezone.String() != timezone.String() {
  38. t.Fatalf("Expected timezone %v, got %v", timezone, c.timezone)
  39. }
  40. }
  41. func TestCronAddAndRemove(t *testing.T) {
  42. c := New()
  43. if err := c.Add("test0", "* * * * *", nil); err == nil {
  44. t.Fatal("Expected nil function error")
  45. }
  46. if err := c.Add("test1", "invalid", func() {}); err == nil {
  47. t.Fatal("Expected invalid cron expression error")
  48. }
  49. if err := c.Add("test2", "* * * * *", func() {}); err != nil {
  50. t.Fatal(err)
  51. }
  52. if err := c.Add("test3", "* * * * *", func() {}); err != nil {
  53. t.Fatal(err)
  54. }
  55. if err := c.Add("test4", "* * * * *", func() {}); err != nil {
  56. t.Fatal(err)
  57. }
  58. // overwrite test2
  59. if err := c.Add("test2", "1 2 3 4 5", func() {}); err != nil {
  60. t.Fatal(err)
  61. }
  62. if err := c.Add("test5", "1 2 3 4 5", func() {}); err != nil {
  63. t.Fatal(err)
  64. }
  65. // mock job deletion
  66. c.Remove("test4")
  67. // try to remove non-existing (should be no-op)
  68. c.Remove("missing")
  69. // check job keys
  70. {
  71. expectedKeys := []string{"test3", "test2", "test5"}
  72. if v := len(c.jobs); v != len(expectedKeys) {
  73. t.Fatalf("Expected %d jobs, got %d", len(expectedKeys), v)
  74. }
  75. for _, k := range expectedKeys {
  76. if c.jobs[k] == nil {
  77. t.Fatalf("Expected job with key %s, got nil", k)
  78. }
  79. }
  80. }
  81. // check the jobs schedule
  82. {
  83. expectedSchedules := map[string]string{
  84. "test2": `{"minutes":{"1":{}},"hours":{"2":{}},"days":{"3":{}},"months":{"4":{}},"daysOfWeek":{"5":{}}}`,
  85. "test3": `{"minutes":{"0":{},"1":{},"10":{},"11":{},"12":{},"13":{},"14":{},"15":{},"16":{},"17":{},"18":{},"19":{},"2":{},"20":{},"21":{},"22":{},"23":{},"24":{},"25":{},"26":{},"27":{},"28":{},"29":{},"3":{},"30":{},"31":{},"32":{},"33":{},"34":{},"35":{},"36":{},"37":{},"38":{},"39":{},"4":{},"40":{},"41":{},"42":{},"43":{},"44":{},"45":{},"46":{},"47":{},"48":{},"49":{},"5":{},"50":{},"51":{},"52":{},"53":{},"54":{},"55":{},"56":{},"57":{},"58":{},"59":{},"6":{},"7":{},"8":{},"9":{}},"hours":{"0":{},"1":{},"10":{},"11":{},"12":{},"13":{},"14":{},"15":{},"16":{},"17":{},"18":{},"19":{},"2":{},"20":{},"21":{},"22":{},"23":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"9":{}},"days":{"1":{},"10":{},"11":{},"12":{},"13":{},"14":{},"15":{},"16":{},"17":{},"18":{},"19":{},"2":{},"20":{},"21":{},"22":{},"23":{},"24":{},"25":{},"26":{},"27":{},"28":{},"29":{},"3":{},"30":{},"31":{},"4":{},"5":{},"6":{},"7":{},"8":{},"9":{}},"months":{"1":{},"10":{},"11":{},"12":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"9":{}},"daysOfWeek":{"0":{},"1":{},"2":{},"3":{},"4":{},"5":{},"6":{}}}`,
  86. "test5": `{"minutes":{"1":{}},"hours":{"2":{}},"days":{"3":{}},"months":{"4":{}},"daysOfWeek":{"5":{}}}`,
  87. }
  88. for k, v := range expectedSchedules {
  89. raw, err := json.Marshal(c.jobs[k].schedule)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if string(raw) != v {
  94. t.Fatalf("Expected %q schedule \n%s, \ngot \n%s", k, v, raw)
  95. }
  96. }
  97. }
  98. }
  99. func TestCronMustAdd(t *testing.T) {
  100. c := New()
  101. defer func() {
  102. if r := recover(); r == nil {
  103. t.Errorf("test1 didn't panic")
  104. }
  105. }()
  106. c.MustAdd("test1", "* * * * *", nil)
  107. c.MustAdd("test2", "* * * * *", func() {})
  108. if _, ok := c.jobs["test2"]; !ok {
  109. t.Fatal("Couldn't find job test2")
  110. }
  111. }
  112. func TestCronRemoveAll(t *testing.T) {
  113. c := New()
  114. if err := c.Add("test1", "* * * * *", func() {}); err != nil {
  115. t.Fatal(err)
  116. }
  117. if err := c.Add("test2", "* * * * *", func() {}); err != nil {
  118. t.Fatal(err)
  119. }
  120. if err := c.Add("test3", "* * * * *", func() {}); err != nil {
  121. t.Fatal(err)
  122. }
  123. if v := len(c.jobs); v != 3 {
  124. t.Fatalf("Expected %d jobs, got %d", 3, v)
  125. }
  126. c.RemoveAll()
  127. if v := len(c.jobs); v != 0 {
  128. t.Fatalf("Expected %d jobs, got %d", 0, v)
  129. }
  130. }
  131. func TestCronTotal(t *testing.T) {
  132. c := New()
  133. if v := c.Total(); v != 0 {
  134. t.Fatalf("Expected 0 jobs, got %v", v)
  135. }
  136. if err := c.Add("test1", "* * * * *", func() {}); err != nil {
  137. t.Fatal(err)
  138. }
  139. if err := c.Add("test2", "* * * * *", func() {}); err != nil {
  140. t.Fatal(err)
  141. }
  142. // overwrite
  143. if err := c.Add("test1", "* * * * *", func() {}); err != nil {
  144. t.Fatal(err)
  145. }
  146. if v := c.Total(); v != 2 {
  147. t.Fatalf("Expected 2 jobs, got %v", v)
  148. }
  149. }
  150. func TestCronStartStop(t *testing.T) {
  151. c := New()
  152. c.SetInterval(1 * time.Second)
  153. test1 := 0
  154. test2 := 0
  155. err := c.Add("test1", "* * * * *", func() {
  156. test1++
  157. })
  158. require.NoError(t, err)
  159. err = c.Add("test2", "* * * * *", func() {
  160. test2++
  161. })
  162. require.NoError(t, err)
  163. expectedCalls := 3
  164. // call twice Start to check if the previous ticker will be reseted
  165. c.Start()
  166. c.Start()
  167. time.Sleep(3250 * time.Millisecond)
  168. // call twice Stop to ensure that the second stop is no-op
  169. c.Stop()
  170. c.Stop()
  171. if test1 != expectedCalls {
  172. t.Fatalf("Expected %d test1, got %d", expectedCalls, test1)
  173. }
  174. if test2 != expectedCalls {
  175. t.Fatalf("Expected %d test2, got %d", expectedCalls, test2)
  176. }
  177. // resume for ~5 seconds
  178. c.Start()
  179. time.Sleep(5250 * time.Millisecond)
  180. c.Stop()
  181. expectedCalls += 5
  182. if test1 != expectedCalls {
  183. t.Fatalf("Expected %d test1, got %d", expectedCalls, test1)
  184. }
  185. if test2 != expectedCalls {
  186. t.Fatalf("Expected %d test2, got %d", expectedCalls, test2)
  187. }
  188. }