Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/cron/schedule_test.go (135 lines, 3.5 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
// Copyright (C) 2023, Jakob Wakeling
// All rights reserved.

package cron_test

import (
	"testing"
	"time"

	"github.com/Jamozed/Goit/src/cron"
)

func TestNext(t *testing.T) {
	t.Run("Month", func(t *testing.T) {
		schedule := cron.Schedule{6, -1, -1, -1, -1, -1}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 6, 1, 0, 0, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Month with Wrap", func(t *testing.T) {
		schedule := cron.Schedule{6, -1, -1, -1, -1, -1}
		baseTime := time.Date(1970, 8, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1971, 6, 1, 0, 0, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Day", func(t *testing.T) {
		schedule := cron.Schedule{-1, 12, -1, -1, -1, -1}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 1, 12, 0, 0, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Day with Wrap", func(t *testing.T) {
		schedule := cron.Schedule{-1, 12, -1, -1, -1, -1}
		baseTime := time.Date(1970, 1, 24, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 2, 12, 0, 0, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Weekday", func(t *testing.T) {
		schedule := cron.Schedule{-1, -1, 3, -1, -1, -1}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 1, 7, 0, 0, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Day and weekday", func(t *testing.T) {
		schedule := cron.Schedule{-1, 12, 3, -1, -1, -1}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 8, 12, 0, 0, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Hour", func(t *testing.T) {
		schedule := cron.Schedule{-1, -1, -1, 18, -1, -1}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 1, 1, 18, 0, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Minute", func(t *testing.T) {
		schedule := cron.Schedule{-1, -1, -1, -1, 30, -1}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 1, 1, 0, 30, 0, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Second", func(t *testing.T) {
		schedule := cron.Schedule{-1, -1, -1, -1, -1, 30}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 1, 1, 0, 0, 30, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("All", func(t *testing.T) {
		schedule := cron.Schedule{3, 6, 2, 6, 45, 15}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1973, 3, 6, 6, 45, 15, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})

	t.Run("Immediate", func(t *testing.T) {
		schedule := cron.Schedule{-1, -1, -1, -1, -1, -1}
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
		expected := time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC)

		r := schedule.Next(baseTime)
		if r != expected {
			t.Error("Expected", expected, "got", r)
		}
	})
}