pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/cron/cron_test.go (about) 1 package cron 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "testing" 12 "time" 13 14 . "pkg.re/essentialkaos/check.v1" 15 ) 16 17 // ////////////////////////////////////////////////////////////////////////////////// // 18 19 func Test(t *testing.T) { TestingT(t) } 20 21 type CronSuite struct{} 22 23 // ////////////////////////////////////////////////////////////////////////////////// // 24 25 var _ = Suite(&CronSuite{}) 26 27 // ////////////////////////////////////////////////////////////////////////////////// // 28 29 func (s *CronSuite) TestParsing(c *C) { 30 31 e0, err := Parse("* * * *") 32 33 c.Assert(err, NotNil) 34 c.Assert(e0, IsNil) 35 36 e1, err := Parse("* * * * *") 37 38 c.Assert(err, IsNil) 39 c.Assert(e1, NotNil) 40 c.Assert(e1.IsDue(time.Now()), Equals, true) 41 c.Assert(e1.IsDue(time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)), Equals, true) 42 43 c.Assert( 44 e1.Prev(time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)), 45 Equals, 46 time.Date(2014, 12, 31, 23, 59, 0, 0, time.Local), 47 ) 48 49 c.Assert( 50 e1.Next(time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)), 51 Equals, 52 time.Date(2015, 1, 1, 0, 1, 0, 0, time.Local), 53 ) 54 55 c.Assert(e1.Next(), Not(Equals), time.Unix(0, 0)) 56 c.Assert(e1.Prev(), Not(Equals), time.Unix(0, 0)) 57 58 e2, err := Parse("45 17 7 6 *") 59 60 c.Assert(err, IsNil) 61 c.Assert(e2, NotNil) 62 c.Assert(e2.IsDue(time.Date(2015, 6, 7, 17, 45, 0, 0, time.Local)), Equals, true) 63 c.Assert(e2.IsDue(time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)), Equals, false) 64 65 e3, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri") 66 67 c.Assert(err, IsNil) 68 c.Assert(e3, NotNil) 69 c.Assert(e3.IsDue(time.Date(2015, 1, 1, 12, 45, 0, 0, time.Local)), Equals, true) 70 c.Assert(e3.IsDue(time.Date(2015, 2, 1, 0, 0, 0, 0, time.Local)), Equals, false) 71 72 e4, err := Parse("*/15 */6 1,15,31 * 1-5") 73 74 c.Assert(err, IsNil) 75 c.Assert(e4, NotNil) 76 c.Assert(e4.IsDue(time.Date(2015, 1, 15, 18, 15, 0, 0, time.Local)), Equals, true) 77 c.Assert(e4.IsDue(time.Date(2015, 2, 15, 18, 15, 0, 0, time.Local)), Equals, false) 78 79 e5, err := Parse("* * * 1,3,5,7,9,11 *") 80 81 c.Assert(err, IsNil) 82 c.Assert(e5, NotNil) 83 c.Assert(e5.IsDue(time.Date(2015, 1, 15, 18, 15, 0, 0, time.Local)), Equals, true) 84 c.Assert(e5.IsDue(time.Date(2015, 2, 15, 18, 15, 0, 0, time.Local)), Equals, false) 85 86 e6, err := Parse("* * * Jan,Feb,Mar *") 87 88 c.Assert(err, IsNil) 89 c.Assert(e6, NotNil) 90 c.Assert(e6.IsDue(time.Date(2015, 2, 15, 20, 22, 0, 0, time.Local)), Equals, true) 91 c.Assert(e6.IsDue(time.Date(2015, 4, 15, 20, 22, 0, 0, time.Local)), Equals, false) 92 93 e7, err := Parse("* * * * *") 94 95 c.Assert(err, IsNil) 96 c.Assert(e7, NotNil) 97 c.Assert(e7.IsDue(), Equals, true) 98 99 e8, err := Parse("15 12 10 * *") 100 101 c.Assert(err, IsNil) 102 c.Assert(e8, NotNil) 103 c.Assert(e8.IsDue(time.Date(2015, 1, 1, 0, 15, 0, 0, time.Local)), Equals, false) 104 c.Assert(e8.IsDue(time.Date(2015, 1, 1, 12, 15, 0, 0, time.Local)), Equals, false) 105 106 c.Assert(e3.String(), Equals, "0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri") 107 108 e9, err := Parse("0 12 1 1 Mon") 109 110 c.Assert(err, IsNil) 111 c.Assert(e9, NotNil) 112 113 c.Assert( 114 e9.Prev(time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)), 115 Equals, 116 time.Unix(0, 0), 117 ) 118 119 e10, err := Parse("0 12 1 1 Wed") 120 121 c.Assert(err, IsNil) 122 c.Assert(e10, NotNil) 123 124 c.Assert( 125 e10.Next(time.Date(2015, 6, 1, 0, 0, 0, 0, time.Local)), 126 Equals, 127 time.Unix(0, 0), 128 ) 129 130 e11, err := Parse("45 17 7 0-5 1") 131 132 c.Assert(err, IsNil) 133 c.Assert(e11, NotNil) 134 135 c.Assert(between(1, 5, 10), Equals, uint8(5)) 136 c.Assert(between(15, 5, 10), Equals, uint8(10)) 137 c.Assert(between(7, 5, 10), Equals, uint8(7)) 138 } 139 140 func (s *CronSuite) TestAliases(c *C) { 141 c.Assert(getAliasExpression("@yearly"), Equals, YEARLY) 142 c.Assert(getAliasExpression("@annually"), Equals, ANNUALLY) 143 c.Assert(getAliasExpression("@monthly"), Equals, MONTHLY) 144 c.Assert(getAliasExpression("@weekly"), Equals, WEEKLY) 145 c.Assert(getAliasExpression("@daily"), Equals, DAILY) 146 c.Assert(getAliasExpression("@hourly"), Equals, HOURLY) 147 148 dn1, dn1Ok := getDayNumByName("sun") 149 dn2, dn2Ok := getDayNumByName("mon") 150 dn3, dn3Ok := getDayNumByName("tue") 151 dn4, dn4Ok := getDayNumByName("wed") 152 dn5, dn5Ok := getDayNumByName("thu") 153 dn6, dn6Ok := getDayNumByName("fri") 154 dn7, dn7Ok := getDayNumByName("sat") 155 dn8, dn8Ok := getDayNumByName("???") 156 157 c.Assert(dn1, Equals, uint8(0)) 158 c.Assert(dn1Ok, Equals, true) 159 c.Assert(dn2, Equals, uint8(1)) 160 c.Assert(dn2Ok, Equals, true) 161 c.Assert(dn3, Equals, uint8(2)) 162 c.Assert(dn3Ok, Equals, true) 163 c.Assert(dn4, Equals, uint8(3)) 164 c.Assert(dn4Ok, Equals, true) 165 c.Assert(dn5, Equals, uint8(4)) 166 c.Assert(dn5Ok, Equals, true) 167 c.Assert(dn6, Equals, uint8(5)) 168 c.Assert(dn6Ok, Equals, true) 169 c.Assert(dn7, Equals, uint8(6)) 170 c.Assert(dn7Ok, Equals, true) 171 c.Assert(dn8, Equals, uint8(0)) 172 c.Assert(dn8Ok, Equals, false) 173 174 mn1, mn1Ok := getMonthNumByName("jan") 175 mn2, mn2Ok := getMonthNumByName("feb") 176 mn3, mn3Ok := getMonthNumByName("mar") 177 mn4, mn4Ok := getMonthNumByName("apr") 178 mn5, mn5Ok := getMonthNumByName("may") 179 mn6, mn6Ok := getMonthNumByName("jun") 180 mn7, mn7Ok := getMonthNumByName("jul") 181 mn8, mn8Ok := getMonthNumByName("aug") 182 mn9, mn9Ok := getMonthNumByName("sep") 183 mn10, mn10Ok := getMonthNumByName("oct") 184 mn11, mn11Ok := getMonthNumByName("nov") 185 mn12, mn12Ok := getMonthNumByName("dec") 186 mn13, mn13Ok := getMonthNumByName("???") 187 188 c.Assert(mn1, Equals, uint8(1)) 189 c.Assert(mn1Ok, Equals, true) 190 c.Assert(mn2, Equals, uint8(2)) 191 c.Assert(mn2Ok, Equals, true) 192 c.Assert(mn3, Equals, uint8(3)) 193 c.Assert(mn3Ok, Equals, true) 194 c.Assert(mn4, Equals, uint8(4)) 195 c.Assert(mn4Ok, Equals, true) 196 c.Assert(mn5, Equals, uint8(5)) 197 c.Assert(mn5Ok, Equals, true) 198 c.Assert(mn6, Equals, uint8(6)) 199 c.Assert(mn6Ok, Equals, true) 200 c.Assert(mn7, Equals, uint8(7)) 201 c.Assert(mn7Ok, Equals, true) 202 c.Assert(mn8, Equals, uint8(8)) 203 c.Assert(mn8Ok, Equals, true) 204 c.Assert(mn9, Equals, uint8(9)) 205 c.Assert(mn9Ok, Equals, true) 206 c.Assert(mn10, Equals, uint8(10)) 207 c.Assert(mn10Ok, Equals, true) 208 c.Assert(mn11, Equals, uint8(11)) 209 c.Assert(mn11Ok, Equals, true) 210 c.Assert(mn12, Equals, uint8(12)) 211 c.Assert(mn12Ok, Equals, true) 212 c.Assert(mn13, Equals, uint8(0)) 213 c.Assert(mn13Ok, Equals, false) 214 } 215 216 func (s *CronSuite) TestNearIndex(c *C) { 217 items := []uint8{1, 2, 3, 4, 6, 7, 8, 9} 218 219 c.Assert(getNearNextIndex(items, 5), Equals, 4) 220 c.Assert(getNearNextIndex(items, 6), Equals, 4) 221 c.Assert(getNearNextIndex(items, 10), Equals, 0) 222 c.Assert(getNearPrevIndex(items, 5), Equals, 3) 223 c.Assert(getNearPrevIndex(items, 6), Equals, 4) 224 c.Assert(getNearPrevIndex(items, 0), Equals, 7) 225 } 226 227 func (s *CronSuite) TestErrors(c *C) { 228 e, err := Parse("0-A * * * *") 229 230 c.Assert(err, NotNil) 231 c.Assert(e, IsNil) 232 233 e, err = Parse("A-1 * * * *") 234 235 c.Assert(err, NotNil) 236 c.Assert(e, IsNil) 237 238 e, err = Parse("*/A * * * *") 239 240 c.Assert(err, NotNil) 241 c.Assert(e, IsNil) 242 243 e, err = Parse("*/0 * * * *") 244 245 c.Assert(err, NotNil) 246 c.Assert(e, IsNil) 247 248 e, err = Parse("A * * * *") 249 250 c.Assert(err, NotNil) 251 c.Assert(e, IsNil) 252 253 e, err = Parse("0,1,A * * * *") 254 255 c.Assert(err, NotNil) 256 c.Assert(e, IsNil) 257 258 e, err = Parse("0,1,2-A * * * *") 259 260 c.Assert(err, NotNil) 261 c.Assert(e, IsNil) 262 }