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.

173 lines
5.4 KiB

  1. package cron
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestActivation(t *testing.T) {
  7. tests := []struct {
  8. time, spec string
  9. expected bool
  10. }{
  11. // Every fifteen minutes.
  12. {"Mon Jul 9 15:00 2012", "0 0/15 * * *", true},
  13. {"Mon Jul 9 15:45 2012", "0 0/15 * * *", true},
  14. {"Mon Jul 9 15:40 2012", "0 0/15 * * *", false},
  15. // Every fifteen minutes, starting at 5 minutes.
  16. {"Mon Jul 9 15:05 2012", "0 5/15 * * *", true},
  17. {"Mon Jul 9 15:20 2012", "0 5/15 * * *", true},
  18. {"Mon Jul 9 15:50 2012", "0 5/15 * * *", true},
  19. // Named months
  20. {"Sun Jul 15 15:00 2012", "0 0/15 * * Jul", true},
  21. {"Sun Jul 15 15:00 2012", "0 0/15 * * Jun", false},
  22. // Everything set.
  23. {"Sun Jul 15 08:30 2012", "0 30 08 ? Jul Sun", true},
  24. {"Sun Jul 15 08:30 2012", "0 30 08 15 Jul ?", true},
  25. {"Mon Jul 16 08:30 2012", "0 30 08 ? Jul Sun", false},
  26. {"Mon Jul 16 08:30 2012", "0 30 08 15 Jul ?", false},
  27. // Predefined schedules
  28. {"Mon Jul 9 15:00 2012", "@hourly", true},
  29. {"Mon Jul 9 15:04 2012", "@hourly", false},
  30. {"Mon Jul 9 15:00 2012", "@daily", false},
  31. {"Mon Jul 9 00:00 2012", "@daily", true},
  32. {"Mon Jul 9 00:00 2012", "@weekly", false},
  33. {"Sun Jul 8 00:00 2012", "@weekly", true},
  34. {"Sun Jul 8 01:00 2012", "@weekly", false},
  35. {"Sun Jul 8 00:00 2012", "@monthly", false},
  36. {"Sun Jul 1 00:00 2012", "@monthly", true},
  37. // Test interaction of DOW and DOM.
  38. // If both are specified, then only one needs to match.
  39. {"Sun Jul 15 00:00 2012", "0 * * 1,15 * Sun", true},
  40. {"Fri Jun 15 00:00 2012", "0 * * 1,15 * Sun", true},
  41. {"Wed Aug 1 00:00 2012", "0 * * 1,15 * Sun", true},
  42. // However, if one has a star, then both need to match.
  43. {"Sun Jul 15 00:00 2012", "0 * * * * Mon", false},
  44. {"Sun Jul 15 00:00 2012", "0 * * */10 * Sun", false},
  45. {"Mon Jul 9 00:00 2012", "0 * * 1,15 * *", false},
  46. {"Sun Jul 15 00:00 2012", "0 * * 1,15 * *", true},
  47. {"Sun Jul 15 00:00 2012", "0 * * */2 * Sun", true},
  48. }
  49. for _, test := range tests {
  50. sched, err := Parse(test.spec)
  51. if err != nil {
  52. t.Error(err)
  53. continue
  54. }
  55. actual := sched.Next(getTime(test.time).Add(-1 * time.Second))
  56. expected := getTime(test.time)
  57. if test.expected && expected != actual || !test.expected && expected == actual {
  58. t.Errorf("Fail evaluating %s on %s: (expected) %s != %s (actual)",
  59. test.spec, test.time, expected, actual)
  60. }
  61. }
  62. }
  63. func TestNext(t *testing.T) {
  64. runs := []struct {
  65. time, spec string
  66. expected string
  67. }{
  68. // Simple cases
  69. {"Mon Jul 9 14:45 2012", "0 0/15 * * *", "Mon Jul 9 15:00 2012"},
  70. {"Mon Jul 9 14:59 2012", "0 0/15 * * *", "Mon Jul 9 15:00 2012"},
  71. {"Mon Jul 9 14:59:59 2012", "0 0/15 * * *", "Mon Jul 9 15:00 2012"},
  72. // Wrap around hours
  73. {"Mon Jul 9 15:45 2012", "0 20-35/15 * * *", "Mon Jul 9 16:20 2012"},
  74. // Wrap around days
  75. {"Mon Jul 9 23:46 2012", "0 */15 * * *", "Tue Jul 10 00:00 2012"},
  76. {"Mon Jul 9 23:45 2012", "0 20-35/15 * * *", "Tue Jul 10 00:20 2012"},
  77. {"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 * * *", "Tue Jul 10 00:20:15 2012"},
  78. {"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 1/2 * *", "Tue Jul 10 01:20:15 2012"},
  79. {"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 10-12 * *", "Tue Jul 10 10:20:15 2012"},
  80. {"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 1/2 */2 * *", "Thu Jul 11 01:20:15 2012"},
  81. {"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 * 9-20 * *", "Wed Jul 10 00:20:15 2012"},
  82. {"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 * 9-20 Jul *", "Wed Jul 10 00:20:15 2012"},
  83. // Wrap around months
  84. {"Mon Jul 9 23:35 2012", "0 0 0 9 Apr-Oct ?", "Thu Aug 9 00:00 2012"},
  85. {"Mon Jul 9 23:35 2012", "0 0 0 */5 Apr,Aug,Oct Mon", "Mon Aug 6 00:00 2012"},
  86. {"Mon Jul 9 23:35 2012", "0 0 0 */5 Oct Mon", "Mon Oct 1 00:00 2012"},
  87. // Wrap around years
  88. {"Mon Jul 9 23:35 2012", "0 0 0 * Feb Mon", "Mon Feb 4 00:00 2013"},
  89. {"Mon Jul 9 23:35 2012", "0 0 0 * Feb Mon/2", "Fri Feb 1 00:00 2013"},
  90. // Wrap around minute, hour, day, month, and year
  91. {"Mon Dec 31 23:59:45 2012", "0 * * * * *", "Tue Jan 1 00:00:00 2013"},
  92. // Leap year
  93. {"Mon Jul 9 23:35 2012", "0 0 0 29 Feb ?", "Mon Feb 29 00:00 2016"},
  94. // Daylight savings time EST -> EDT
  95. {"2012-03-11T00:00:00-0500", "0 30 2 11 Mar ?", "2013-03-11T02:30:00-0400"},
  96. // Daylight savings time EDT -> EST
  97. {"2012-11-04T00:00:00-0400", "0 30 2 04 Nov ?", "2012-11-04T02:30:00-0500"},
  98. {"2012-11-04T01:45:00-0400", "0 30 1 04 Nov ?", "2012-11-04T01:30:00-0500"},
  99. // Unsatisfiable
  100. {"Mon Jul 9 23:35 2012", "0 0 0 30 Feb ?", ""},
  101. {"Mon Jul 9 23:35 2012", "0 0 0 31 Apr ?", ""},
  102. }
  103. for _, c := range runs {
  104. sched, err := Parse(c.spec)
  105. if err != nil {
  106. t.Error(err)
  107. continue
  108. }
  109. actual := sched.Next(getTime(c.time))
  110. expected := getTime(c.expected)
  111. if !actual.Equal(expected) {
  112. t.Errorf("%s, \"%s\": (expected) %v != %v (actual)", c.time, c.spec, expected, actual)
  113. }
  114. }
  115. }
  116. func TestErrors(t *testing.T) {
  117. invalidSpecs := []string{
  118. "xyz",
  119. "60 0 * * *",
  120. "0 60 * * *",
  121. "0 0 * * XYZ",
  122. }
  123. for _, spec := range invalidSpecs {
  124. _, err := Parse(spec)
  125. if err == nil {
  126. t.Error("expected an error parsing: ", spec)
  127. }
  128. }
  129. }
  130. func getTime(value string) time.Time {
  131. if value == "" {
  132. return time.Time{}
  133. }
  134. t, err := time.Parse("Mon Jan 2 15:04 2006", value)
  135. if err != nil {
  136. t, err = time.Parse("Mon Jan 2 15:04:05 2006", value)
  137. if err != nil {
  138. t, err = time.Parse("2006-01-02T15:04:05-0700", value)
  139. if err != nil {
  140. panic(err)
  141. }
  142. // Daylight savings time tests require location
  143. if ny, err := time.LoadLocation("America/New_York"); err == nil {
  144. t = t.In(ny)
  145. }
  146. }
  147. }
  148. return t
  149. }