git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/cron/constantdelay_test.go (about)

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