go.temporal.io/server@v1.23.0/common/backoff/cron_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package backoff 26 27 import ( 28 "strconv" 29 "testing" 30 "time" 31 32 "github.com/stretchr/testify/assert" 33 ) 34 35 var crontests = []struct { 36 cron string 37 scheduledTime string 38 now string 39 result time.Duration 40 err string 41 }{ 42 {"0 0 31 2 *", "2018-12-17T08:00:00-08:00", "", 0, "invalid CronSchedule, no time can be found to satisfy the schedule"}, 43 {"invalid-cron-spec", "2018-12-17T00:04:00+00:00", "2018-12-17T01:02:00+00:00", NoBackoff, "invalid CronSchedule"}, 44 45 {"0 10 * * *", "2018-12-17T08:00:00-08:00", "", time.Hour * 18, ""}, 46 {"0 10 * * *", "2018-12-18T02:00:00-08:00", "", time.Hour * 24, ""}, 47 {"0 * * * *", "2018-12-17T08:08:00+00:00", "", time.Minute * 52, ""}, 48 {"0 * * * *", "2018-12-17T09:00:00+00:00", "", time.Hour, ""}, 49 {"* * * * *", "2018-12-17T08:08:18+00:00", "", time.Second * 42, ""}, 50 {"0 * * * *", "2018-12-17T09:00:00+00:00", "", time.Minute * 60, ""}, 51 {"0 10 * * *", "2018-12-17T08:00:00+00:00", "2018-12-20T00:00:00+00:00", time.Hour * 10, ""}, 52 {"0 10 * * *", "2018-12-17T08:00:00+00:00", "2018-12-17T09:00:00+00:00", time.Hour, ""}, 53 {"*/10 * * * *", "2018-12-17T00:04:00+00:00", "2018-12-17T01:02:00+00:00", time.Minute * 8, ""}, 54 {"@every 5h", "2018-12-17T08:00:00+00:00", "2018-12-17T09:00:00+00:00", time.Hour * 4, ""}, 55 {"@every 5h", "2018-12-17T08:00:00+00:00", "2018-12-18T00:00:00+00:00", time.Hour * 4, ""}, 56 {"0 3 * * 0-6", "2018-12-17T08:00:00-08:00", "", time.Hour * 11, ""}, 57 {"@every 30s", "2020-07-17T09:00:02-01:00", "2020-07-17T09:00:02-01:00", time.Second * 30, ""}, 58 {"@every 30s", "2020-07-17T09:00:02-01:00", "2020-09-17T03:00:53-01:00", time.Second * 9, ""}, 59 {"@every 30m", "2020-07-17T09:00:00-01:00", "2020-07-17T08:45:00-01:00", time.Minute * 15, ""}, 60 // At 16:05 East Coast (Day light saving on) 61 {"CRON_TZ=America/New_York 5 16 * * *", "2021-03-14T00:00:00-04:00", "2021-03-14T15:05:00-04:00", time.Hour * 1, ""}, 62 // At 04:05 East Coast (Day light saving off) 63 {"CRON_TZ=America/New_York 5 4 * * *", "2021-11-25T00:00:00-05:00", "2021-11-25T03:05:00-05:00", time.Hour * 1, ""}, 64 } 65 66 func TestCron(t *testing.T) { 67 for idx, tt := range crontests { 68 t.Run(strconv.Itoa(idx), func(t *testing.T) { 69 start, err := time.Parse(time.RFC3339, tt.scheduledTime) 70 assert.NoError(t, err, "Parse start time: %v", tt.scheduledTime) 71 end := start 72 if tt.now != "" { 73 end, err = time.Parse(time.RFC3339, tt.now) 74 assert.NoError(t, err, "Parse end time: %v", tt.now) 75 } 76 err = ValidateSchedule(tt.cron) 77 if len(tt.err) > 0 { 78 assert.Error(t, err) 79 assert.Contains(t, err.Error(), tt.err) 80 } else { 81 assert.NoError(t, err, "Failed on cron schedule: %v", tt.cron) 82 83 backoff := GetBackoffForNextSchedule(tt.cron, start, end) 84 assert.Equal(t, tt.result, backoff, "The cron spec is %s and the expected result is %s", tt.cron, tt.result) 85 } 86 }) 87 } 88 }