code.gitea.io/gitea@v1.21.7/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  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestAddTaskToScheduler(t *testing.T) {
    14  	assert.Len(t, scheduler.Jobs(), 0)
    15  	defer scheduler.Clear()
    16  
    17  	// no seconds
    18  	err := addTaskToScheduler(&Task{
    19  		Name: "task 1",
    20  		config: &BaseConfig{
    21  			Schedule: "5 4 * * *",
    22  		},
    23  	})
    24  	assert.NoError(t, err)
    25  	assert.Len(t, scheduler.Jobs(), 1)
    26  	assert.Equal(t, "task 1", scheduler.Jobs()[0].Tags()[0])
    27  	assert.Equal(t, "5 4 * * *", scheduler.Jobs()[0].Tags()[1])
    28  
    29  	// with seconds
    30  	err = addTaskToScheduler(&Task{
    31  		Name: "task 2",
    32  		config: &BaseConfig{
    33  			Schedule: "30 5 4 * * *",
    34  		},
    35  	})
    36  	assert.NoError(t, err)
    37  	assert.Len(t, scheduler.Jobs(), 2)
    38  	assert.Equal(t, "task 2", scheduler.Jobs()[1].Tags()[0])
    39  	assert.Equal(t, "30 5 4 * * *", scheduler.Jobs()[1].Tags()[1])
    40  }
    41  
    42  func TestScheduleHasSeconds(t *testing.T) {
    43  	tests := []struct {
    44  		schedule  string
    45  		hasSecond bool
    46  	}{
    47  		{"* * * * * *", true},
    48  		{"* * * * *", false},
    49  		{"5 4 * * *", false},
    50  		{"5 4 * * *", false},
    51  		{"5,8 4 * * *", false},
    52  		{"*   *   *  * * *", true},
    53  		{"5,8 4   *  *   *", false},
    54  	}
    55  
    56  	for i, test := range tests {
    57  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    58  			assert.Equal(t, test.hasSecond, scheduleHasSeconds(test.schedule))
    59  		})
    60  	}
    61  }