code.gitea.io/gitea@v1.22.3/services/cron/tasks_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package cron 5 6 import ( 7 "sort" 8 "strconv" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestAddTaskToScheduler(t *testing.T) { 15 assert.Len(t, scheduler.Jobs(), 0) 16 defer scheduler.Clear() 17 18 // no seconds 19 err := addTaskToScheduler(&Task{ 20 Name: "task 1", 21 config: &BaseConfig{ 22 Schedule: "5 4 * * *", 23 }, 24 }) 25 assert.NoError(t, err) 26 jobs := scheduler.Jobs() 27 assert.Len(t, jobs, 1) 28 assert.Equal(t, "task 1", jobs[0].Tags()[0]) 29 assert.Equal(t, "5 4 * * *", jobs[0].Tags()[1]) 30 31 // with seconds 32 err = addTaskToScheduler(&Task{ 33 Name: "task 2", 34 config: &BaseConfig{ 35 Schedule: "30 5 4 * * *", 36 }, 37 }) 38 assert.NoError(t, err) 39 jobs = scheduler.Jobs() // the item order is not guaranteed, so we need to sort it before "assert" 40 sort.Slice(jobs, func(i, j int) bool { 41 return jobs[i].Tags()[0] < jobs[j].Tags()[0] 42 }) 43 assert.Len(t, jobs, 2) 44 assert.Equal(t, "task 2", jobs[1].Tags()[0]) 45 assert.Equal(t, "30 5 4 * * *", jobs[1].Tags()[1]) 46 } 47 48 func TestScheduleHasSeconds(t *testing.T) { 49 tests := []struct { 50 schedule string 51 hasSecond bool 52 }{ 53 {"* * * * * *", true}, 54 {"* * * * *", false}, 55 {"5 4 * * *", false}, 56 {"5 4 * * *", false}, 57 {"5,8 4 * * *", false}, 58 {"* * * * * *", true}, 59 {"5,8 4 * * *", false}, 60 } 61 62 for i, test := range tests { 63 t.Run(strconv.Itoa(i), func(t *testing.T) { 64 assert.Equal(t, test.hasSecond, scheduleHasSeconds(test.schedule)) 65 }) 66 } 67 }