constantdelay_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //nolint:all
  2. package cron
  3. import (
  4. "testing"
  5. "time"
  6. )
  7. func TestConstantDelayNext(t *testing.T) {
  8. tests := []struct {
  9. time string
  10. delay time.Duration
  11. expected string
  12. }{
  13. // Simple cases
  14. {"Mon Jul 9 14:45 2012", 15*time.Minute + 50*time.Nanosecond, "Mon Jul 9 15:00 2012"},
  15. {"Mon Jul 9 14:59 2012", 15 * time.Minute, "Mon Jul 9 15:14 2012"},
  16. {"Mon Jul 9 14:59:59 2012", 15 * time.Minute, "Mon Jul 9 15:14:59 2012"},
  17. // Wrap around hours
  18. {"Mon Jul 9 15:45 2012", 35 * time.Minute, "Mon Jul 9 16:20 2012"},
  19. // Wrap around days
  20. {"Mon Jul 9 23:46 2012", 14 * time.Minute, "Tue Jul 10 00:00 2012"},
  21. {"Mon Jul 9 23:45 2012", 35 * time.Minute, "Tue Jul 10 00:20 2012"},
  22. {"Mon Jul 9 23:35:51 2012", 44*time.Minute + 24*time.Second, "Tue Jul 10 00:20:15 2012"},
  23. {"Mon Jul 9 23:35:51 2012", 25*time.Hour + 44*time.Minute + 24*time.Second, "Thu Jul 11 01:20:15 2012"},
  24. // Wrap around months
  25. {"Mon Jul 9 23:35 2012", 91*24*time.Hour + 25*time.Minute, "Thu Oct 9 00:00 2012"},
  26. // Wrap around minute, hour, day, month, and year
  27. {"Mon Dec 31 23:59:45 2012", 15 * time.Second, "Tue Jan 1 00:00:00 2013"},
  28. // Round to nearest second on the delay
  29. {"Mon Jul 9 14:45 2012", 15*time.Minute + 50*time.Nanosecond, "Mon Jul 9 15:00 2012"},
  30. // Round up to 1 second if the duration is less.
  31. {"Mon Jul 9 14:45:00 2012", 15 * time.Millisecond, "Mon Jul 9 14:45:01 2012"},
  32. // Round to nearest second when calculating the next time.
  33. {"Mon Jul 9 14:45:00.005 2012", 15 * time.Minute, "Mon Jul 9 15:00 2012"},
  34. // Round to nearest second for both.
  35. {"Mon Jul 9 14:45:00.005 2012", 15*time.Minute + 50*time.Nanosecond, "Mon Jul 9 15:00 2012"},
  36. }
  37. for _, c := range tests {
  38. actual := Every(c.delay).Next(getTime(c.time))
  39. expected := getTime(c.expected)
  40. if actual != expected {
  41. t.Errorf("%s, \"%s\": (expected) %v != %v (actual)", c.time, c.delay, expected, actual)
  42. }
  43. }
  44. }