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-- file download

570144e Jakob Wakeling 2023-12-15 23:28:06
0
// Copyright (C) 2023, Jakob Wakeling
570144e Jakob Wakeling 2023-12-15 23:28:06
1
// All rights reserved.
570144e Jakob Wakeling 2023-12-15 23:28:06
2
570144e Jakob Wakeling 2023-12-15 23:28:06
3
package cron_test
570144e Jakob Wakeling 2023-12-15 23:28:06
4
570144e Jakob Wakeling 2023-12-15 23:28:06
5
import (
570144e Jakob Wakeling 2023-12-15 23:28:06
6
	"testing"
570144e Jakob Wakeling 2023-12-15 23:28:06
7
	"time"
570144e Jakob Wakeling 2023-12-15 23:28:06
8
570144e Jakob Wakeling 2023-12-15 23:28:06
9
	"github.com/Jamozed/Goit/src/cron"
570144e Jakob Wakeling 2023-12-15 23:28:06
10
)
570144e Jakob Wakeling 2023-12-15 23:28:06
11
570144e Jakob Wakeling 2023-12-15 23:28:06
12
func TestNext(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
13
	t.Run("Month", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
14
		schedule := cron.Schedule{6, -1, -1, -1, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
15
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
16
		expected := time.Date(1970, 6, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
17
570144e Jakob Wakeling 2023-12-15 23:28:06
18
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
19
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
20
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
21
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
22
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
23
570144e Jakob Wakeling 2023-12-15 23:28:06
24
	t.Run("Month with Wrap", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
25
		schedule := cron.Schedule{6, -1, -1, -1, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
26
		baseTime := time.Date(1970, 8, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
27
		expected := time.Date(1971, 6, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
28
570144e Jakob Wakeling 2023-12-15 23:28:06
29
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
30
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
31
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
32
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
33
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
34
570144e Jakob Wakeling 2023-12-15 23:28:06
35
	t.Run("Day", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
36
		schedule := cron.Schedule{-1, 12, -1, -1, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
37
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
38
		expected := time.Date(1970, 1, 12, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
39
570144e Jakob Wakeling 2023-12-15 23:28:06
40
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
41
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
42
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
43
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
44
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
45
570144e Jakob Wakeling 2023-12-15 23:28:06
46
	t.Run("Day with Wrap", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
47
		schedule := cron.Schedule{-1, 12, -1, -1, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
48
		baseTime := time.Date(1970, 1, 24, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
49
		expected := time.Date(1970, 2, 12, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
50
570144e Jakob Wakeling 2023-12-15 23:28:06
51
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
52
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
53
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
54
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
55
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
56
570144e Jakob Wakeling 2023-12-15 23:28:06
57
	t.Run("Weekday", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
58
		schedule := cron.Schedule{-1, -1, 3, -1, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
59
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
60
		expected := time.Date(1970, 1, 7, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
61
570144e Jakob Wakeling 2023-12-15 23:28:06
62
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
63
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
64
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
65
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
66
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
67
570144e Jakob Wakeling 2023-12-15 23:28:06
68
	t.Run("Day and weekday", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
69
		schedule := cron.Schedule{-1, 12, 3, -1, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
70
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
71
		expected := time.Date(1970, 8, 12, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
72
570144e Jakob Wakeling 2023-12-15 23:28:06
73
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
74
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
75
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
76
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
77
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
78
570144e Jakob Wakeling 2023-12-15 23:28:06
79
	t.Run("Hour", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
80
		schedule := cron.Schedule{-1, -1, -1, 18, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
81
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
82
		expected := time.Date(1970, 1, 1, 18, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
83
570144e Jakob Wakeling 2023-12-15 23:28:06
84
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
85
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
86
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
87
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
88
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
89
570144e Jakob Wakeling 2023-12-15 23:28:06
90
	t.Run("Minute", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
91
		schedule := cron.Schedule{-1, -1, -1, -1, 30, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
92
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
93
		expected := time.Date(1970, 1, 1, 0, 30, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
94
570144e Jakob Wakeling 2023-12-15 23:28:06
95
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
96
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
97
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
98
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
99
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
100
570144e Jakob Wakeling 2023-12-15 23:28:06
101
	t.Run("Second", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
102
		schedule := cron.Schedule{-1, -1, -1, -1, -1, 30}
570144e Jakob Wakeling 2023-12-15 23:28:06
103
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
104
		expected := time.Date(1970, 1, 1, 0, 0, 30, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
105
570144e Jakob Wakeling 2023-12-15 23:28:06
106
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
107
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
108
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
109
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
110
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
111
570144e Jakob Wakeling 2023-12-15 23:28:06
112
	t.Run("All", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
113
		schedule := cron.Schedule{3, 6, 2, 6, 45, 15}
570144e Jakob Wakeling 2023-12-15 23:28:06
114
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
115
		expected := time.Date(1973, 3, 6, 6, 45, 15, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
116
570144e Jakob Wakeling 2023-12-15 23:28:06
117
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
118
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
119
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
120
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
121
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
122
570144e Jakob Wakeling 2023-12-15 23:28:06
123
	t.Run("Immediate", func(t *testing.T) {
570144e Jakob Wakeling 2023-12-15 23:28:06
124
		schedule := cron.Schedule{-1, -1, -1, -1, -1, -1}
570144e Jakob Wakeling 2023-12-15 23:28:06
125
		baseTime := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
61181bb Jakob Wakeling 2023-12-24 00:36:46
126
		expected := time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC)
570144e Jakob Wakeling 2023-12-15 23:28:06
127
570144e Jakob Wakeling 2023-12-15 23:28:06
128
		r := schedule.Next(baseTime)
570144e Jakob Wakeling 2023-12-15 23:28:06
129
		if r != expected {
570144e Jakob Wakeling 2023-12-15 23:28:06
130
			t.Error("Expected", expected, "got", r)
570144e Jakob Wakeling 2023-12-15 23:28:06
131
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
132
	})
570144e Jakob Wakeling 2023-12-15 23:28:06
133
}
134