You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tasks_test.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cron
  4. import (
  5. "strconv"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestAddTaskToScheduler(t *testing.T) {
  10. assert.Len(t, scheduler.Jobs(), 0)
  11. defer scheduler.Clear()
  12. // no seconds
  13. err := addTaskToScheduler(&Task{
  14. Name: "task 1",
  15. config: &BaseConfig{
  16. Schedule: "5 4 * * *",
  17. },
  18. })
  19. assert.NoError(t, err)
  20. assert.Len(t, scheduler.Jobs(), 1)
  21. assert.Equal(t, "task 1", scheduler.Jobs()[0].Tags()[0])
  22. assert.Equal(t, "5 4 * * *", scheduler.Jobs()[0].Tags()[1])
  23. // with seconds
  24. err = addTaskToScheduler(&Task{
  25. Name: "task 2",
  26. config: &BaseConfig{
  27. Schedule: "30 5 4 * * *",
  28. },
  29. })
  30. assert.NoError(t, err)
  31. assert.Len(t, scheduler.Jobs(), 2)
  32. assert.Equal(t, "task 2", scheduler.Jobs()[1].Tags()[0])
  33. assert.Equal(t, "30 5 4 * * *", scheduler.Jobs()[1].Tags()[1])
  34. }
  35. func TestScheduleHasSeconds(t *testing.T) {
  36. tests := []struct {
  37. schedule string
  38. hasSecond bool
  39. }{
  40. {"* * * * * *", true},
  41. {"* * * * *", false},
  42. {"5 4 * * *", false},
  43. {"5 4 * * *", false},
  44. {"5,8 4 * * *", false},
  45. {"* * * * * *", true},
  46. {"5,8 4 * * *", false},
  47. }
  48. for i, test := range tests {
  49. t.Run(strconv.Itoa(i), func(t *testing.T) {
  50. assert.Equal(t, test.hasSecond, scheduleHasSeconds(test.schedule))
  51. })
  52. }
  53. }