Skip to content

Commit

Permalink
refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jul 27, 2020
1 parent 15ba459 commit 3c090ac
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
57 changes: 29 additions & 28 deletions schtasks/taskscheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,20 +300,8 @@ func createDailyTrigger(task *taskmaster.Definition, schedule *calendar.Event) {
task.AddDailyTrigger(1, emptyPeriod, recurrences[0])
return
}
// now calculate the difference in between each
differences := make([]time.Duration, len(recurrences)-1)
for i := 0; i < len(recurrences)-1; i++ {
differences[i] = recurrences[i+1].Sub(recurrences[i])
}
// check if they're all the same
compactDifferences := make([]time.Duration, 0, len(differences))
var previous time.Duration = 0
for _, difference := range differences {
if difference.Seconds() != previous.Seconds() {
compactDifferences = append(compactDifferences, difference)
previous = difference
}
}
// now calculate the difference in between each, and check if they're all the same
_, compactDifferences := compileDifferences(recurrences)

if len(compactDifferences) == 1 {
// easy case
Expand Down Expand Up @@ -350,20 +338,8 @@ func createWeeklyTrigger(task *taskmaster.Definition, schedule *calendar.Event)
1, emptyPeriod, recurrences[0])
return
}
// now calculate the difference in between each
differences := make([]time.Duration, len(recurrences)-1)
for i := 0; i < len(recurrences)-1; i++ {
differences[i] = recurrences[i+1].Sub(recurrences[i])
}
// check if they're all the same
compactDifferences := make([]time.Duration, 0, len(differences))
var previous time.Duration = 0
for _, difference := range differences {
if difference.Seconds() != previous.Seconds() {
compactDifferences = append(compactDifferences, difference)
previous = difference
}
}
// now calculate the difference in between each, and check if they're all the same
_, compactDifferences := compileDifferences(recurrences)

if len(compactDifferences) == 1 {
// easy case
Expand Down Expand Up @@ -475,6 +451,31 @@ func getTaskPath(profileName, commandName string) string {
return fmt.Sprintf("%s%s %s", tasksPath, profileName, commandName)
}

// compileDifferences is creating two slices: the first one is the duration between each trigger,
// the second one is a list of all the differences in between
//
// Example:
// input = 01:00, 02:00, 03:00, 04:00, 06:00, 08:00
// first list = 1H, 1H, 1H, 2H, 2H
// second list = 1H, 2H
func compileDifferences(recurrences []time.Time) ([]time.Duration, []time.Duration) {
// now calculate the difference in between each
differences := make([]time.Duration, len(recurrences)-1)
for i := 0; i < len(recurrences)-1; i++ {
differences[i] = recurrences[i+1].Sub(recurrences[i])
}
// check if they're all the same
compactDifferences := make([]time.Duration, 0, len(differences))
var previous time.Duration = 0
for _, difference := range differences {
if difference.Seconds() != previous.Seconds() {
compactDifferences = append(compactDifferences, difference)
previous = difference
}
}
return differences, compactDifferences
}

func convertWeekdaysToBitmap(weekdays []int) int {
if weekdays == nil || len(weekdays) == 0 {
return 0
Expand Down
26 changes: 26 additions & 0 deletions schtasks/taskscheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ func BenchmarkExp2(b *testing.B) {
}
}

func TestCompileDifferences(t *testing.T) {
testData := []struct {
input string
differences []time.Duration
unique []time.Duration
}{
{
"1..4,6,8:00",
[]time.Duration{1 * time.Hour, 1 * time.Hour, 1 * time.Hour, 2 * time.Hour, 2 * time.Hour},
[]time.Duration{1 * time.Hour, 2 * time.Hour},
},
}

for _, testItem := range testData {
event := calendar.NewEvent()
err := event.Parse(testItem.input)
require.NoError(t, err)
ref, err := time.Parse(time.ANSIC, "Mon Jan 2 12:00:00 2006")
require.NoError(t, err)
start := event.Next(ref)
diff, uniques := compileDifferences(event.GetAllInBetween(start, start.Add(24*time.Hour)))
assert.ElementsMatch(t, testItem.differences, diff)
assert.ElementsMatch(t, testItem.unique, uniques)
}
}

func TestTaskSchedulerConversion(t *testing.T) {
testData := []string{
"2020-01-01",
Expand Down

0 comments on commit 3c090ac

Please sign in to comment.