cron_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. }
  48. for _, test := range tests {
  49. actual := matches(getTime(test.time), Parse(test.spec))
  50. if test.expected != actual {
  51. t.Logf("Actual Minutes mask: %b", Parse(test.spec).Minute)
  52. t.Errorf("Fail evaluating %s on %s: (expected) %t != %t (actual)",
  53. test.spec, test.time, test.expected, actual)
  54. }
  55. }
  56. }
  57. func getTime(value string) time.Time {
  58. t, err := time.Parse("Mon Jan 2 15:04 2006", value)
  59. if err != nil {
  60. panic(err)
  61. }
  62. return t
  63. }