Skip to content

Commit

Permalink
Refactor any type / fix for callback test (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSutul authored Dec 30, 2022
1 parent 9aaee3a commit ef9722c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func ExampleScheduler_Weeks() {
// ---------------------------------------------------------------------

func ExampleSetPanicHandler() {
gocron.SetPanicHandler(func(jobName string, recoverData interface{}) {
gocron.SetPanicHandler(func(jobName string, _ any) {
fmt.Printf("Panic in job: %s", jobName)
fmt.Println("do something to handle the panic")
})
Expand Down
4 changes: 2 additions & 2 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (e *executor) start() {

if panicHandler != nil {
defer func() {
if r := recover(); r != interface{}(nil) {
if r := recover(); r != any(nil) {
panicHandler(f.name, r)
}
}()
Expand Down Expand Up @@ -99,7 +99,7 @@ func (e *executor) start() {
case defaultMode:
runJob()
case singletonMode:
_, _, _ = f.limiter.Do("main", func() (interface{}, error) {
_, _, _ = f.limiter.Do("main", func() (any, error) {
select {
case <-stopCtx.Done():
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_ExecutorExecute(t *testing.T) {
assert.Equal(t, arg, "test")
wg.Done()
},
parameters: []interface{}{"test"},
parameters: []any{"test"},
runState: &runState,
}

Expand All @@ -33,7 +33,7 @@ func Test_ExecutorExecute(t *testing.T) {
func Test_ExecutorPanicHandling(t *testing.T) {
panicHandled := make(chan bool, 1)

handler := func(jobName string, recoverData interface{}) {
handler := func(jobName string, recoverData any) {
fmt.Println("PanicHandler called:")
fmt.Println("panic in " + jobName)
fmt.Println(recoverData)
Expand Down
8 changes: 4 additions & 4 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// PanicHandlerFunc represents a type that can be set to handle panics occurring
// during job execution.
type PanicHandlerFunc func(jobName string, recoverData interface{})
type PanicHandlerFunc func(jobName string, recoverData any)

// The global panic handler
var (
Expand Down Expand Up @@ -88,13 +88,13 @@ const (
crontab
)

func callJobFunc(jobFunc interface{}) {
func callJobFunc(jobFunc any) {
if jobFunc != nil {
reflect.ValueOf(jobFunc).Call([]reflect.Value{})
}
}

func callJobFuncWithParams(jobFunc interface{}, params []interface{}) {
func callJobFuncWithParams(jobFunc any, params []any) {
f := reflect.ValueOf(jobFunc)
if len(params) != f.Type().NumIn() {
return
Expand All @@ -106,7 +106,7 @@ func callJobFuncWithParams(jobFunc interface{}, params []interface{}) {
f.Call(in)
}

func getFunctionName(fn interface{}) string {
func getFunctionName(fn any) string {
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
}

Expand Down
12 changes: 6 additions & 6 deletions gocron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func TestParseTime(t *testing.T) {

func Test_callJobFuncWithParams(t *testing.T) {
type args struct {
jobFunc interface{}
params []interface{}
jobFunc any
params []any
}
tests := []struct {
name string
Expand All @@ -128,22 +128,22 @@ func Test_callJobFuncWithParams(t *testing.T) {
name: "test call func with single arg",
args: args{
jobFunc: func(arg string) {},
params: []interface{}{"test"},
params: []any{"test"},
},
},
{
name: "test call func with wrong arg type",
args: args{
jobFunc: func(arg int) {},
params: []interface{}{"test"},
params: []any{"test"},
},
err: true,
},
{
name: "test call func with wrong arg count",
args: args{
jobFunc: func(arg int) {},
params: []interface{}{},
params: []any{},
},
err: true,
},
Expand Down Expand Up @@ -174,7 +174,7 @@ func panicFnToErr(fn func()) (err error) {

func Test_getFunctionName(t *testing.T) {
type args struct {
fn interface{}
fn any
}
tests := []struct {
name string
Expand Down
12 changes: 6 additions & 6 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ type random struct {

type jobFunction struct {
eventListeners // additional functions to allow run 'em during job performing
function interface{} // task's function
parameters []interface{} // task's function parameters
function any // task's function
parameters []any // task's function parameters
parametersLen int // length of the passed parameters
name string //nolint the function name to run
name string // nolint the function name to run
runConfig runConfig // configuration for how many times to run the job
limiter *singleflight.Group // limits inflight runs of job to one
ctx context.Context // for cancellation
Expand All @@ -56,8 +56,8 @@ type jobFunction struct {
}

type eventListeners struct {
onBeforeJobExecution interface{} // performs before job executing
onAfterJobExecution interface{} // performs after job executing
onBeforeJobExecution any // performs before job executing
onAfterJobExecution any // performs after job executing
}

type jobMutex struct {
Expand Down Expand Up @@ -318,7 +318,7 @@ func (j *Job) Tags() []string {
}

// SetEventListeners accepts two functions that will be called, one before and one after the job is run
func (j *Job) SetEventListeners(onBeforeJobExecution interface{}, onAfterJobExecution interface{}) {
func (j *Job) SetEventListeners(onBeforeJobExecution any, onAfterJobExecution any) {
j.eventListeners = eventListeners{
onBeforeJobExecution: onBeforeJobExecution,
onAfterJobExecution: onAfterJobExecution,
Expand Down
2 changes: 1 addition & 1 deletion job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func TestJob_SetEventListeners(t *testing.T) {
)
wg.Add(1)
s := NewScheduler(time.UTC)
job, err := s.Tag("tag1").Every("1ms").Do(func() {
job, err := s.Tag("tag1").Every("100ms").Do(func() {
jobRanPassed = true
})
job.SetEventListeners(func() {
Expand Down
18 changes: 9 additions & 9 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
type limitMode int8

// Scheduler struct stores a list of Jobs and the location of time used by the Scheduler,
// and implements the sort.Interface{} for sorting Jobs, by the time of nextRun
// and implements the sort. any for sorting Jobs, by the time of nextRun
type Scheduler struct {
jobsMutex sync.RWMutex
jobs []*Job
Expand Down Expand Up @@ -494,7 +494,7 @@ func (s *Scheduler) EveryRandom(lower, upper int) *Scheduler {
// Interval can be an int, time.Duration or a string that
// parses with time.ParseDuration().
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func (s *Scheduler) Every(interval interface{}) *Scheduler {
func (s *Scheduler) Every(interval any) *Scheduler {
job := s.newJob(0)
if s.updateJob || s.jobCreated {
job = s.getCurrentJob()
Expand Down Expand Up @@ -641,7 +641,7 @@ func (s *Scheduler) RunByTagWithDelay(tag string, d time.Duration) error {
// it through gocron as https://pkg.go.dev/time#Timer.Stop explains.
// The job function would need to have implemented a means of
// stopping, e.g. using a context.WithCancel().
func (s *Scheduler) Remove(job interface{}) {
func (s *Scheduler) Remove(job any) {
fName := getFunctionName(job)
j := s.findJobByTaskName(fName)
s.removeJobsUniqueTags(j)
Expand Down Expand Up @@ -783,7 +783,7 @@ func (s *Scheduler) SingletonModeAll() {
}

// TaskPresent checks if specific job's function was added to the scheduler.
func (s *Scheduler) TaskPresent(j interface{}) bool {
func (s *Scheduler) TaskPresent(j any) bool {
for _, job := range s.Jobs() {
if job.name == getFunctionName(j) {
return true
Expand Down Expand Up @@ -817,7 +817,7 @@ func (s *Scheduler) Clear() {
s.setJobs(make([]*Job, 0))
// If unique tags was enabled, delete all the tags loaded in the tags sync.Map
if s.tagsUnique {
s.tags.Range(func(key interface{}, value interface{}) bool {
s.tags.Range(func(key any, value any) bool {
s.tags.Delete(key)
return true
})
Expand Down Expand Up @@ -845,7 +845,7 @@ func (s *Scheduler) stopJobs(jobs []*Job) {
}
}

func (s *Scheduler) doCommon(jobFun interface{}, params ...interface{}) (*Job, error) {
func (s *Scheduler) doCommon(jobFun any, params ...any) (*Job, error) {
job := s.getCurrentJob()

jobUnit := job.getUnit()
Expand Down Expand Up @@ -912,15 +912,15 @@ func (s *Scheduler) doCommon(jobFun interface{}, params ...interface{}) (*Job, e
}

// Do specifies the jobFunc that should be called every time the Job runs
func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error) {
func (s *Scheduler) Do(jobFun any, params ...any) (*Job, error) {
return s.doCommon(jobFun, params...)
}

// DoWithJobDetails specifies the jobFunc that should be called every time the Job runs
// and additionally passes the details of the current job to the jobFunc.
// The last argument of the function must be a gocron.Job that will be passed by
// the scheduler when the function is called.
func (s *Scheduler) DoWithJobDetails(jobFun interface{}, params ...interface{}) (*Job, error) {
func (s *Scheduler) DoWithJobDetails(jobFun any, params ...any) (*Job, error) {
job := s.getCurrentJob()
job.runWithDetails = true
job.parametersLen = len(params)
Expand All @@ -929,7 +929,7 @@ func (s *Scheduler) DoWithJobDetails(jobFun interface{}, params ...interface{})

// At schedules the Job at a specific time of day in the form "HH:MM:SS" or "HH:MM"
// or time.Time (note that only the hours, minutes, seconds and nanos are used).
func (s *Scheduler) At(i interface{}) *Scheduler {
func (s *Scheduler) At(i any) *Scheduler {
job := s.getCurrentJob()

switch t := i.(type) {
Expand Down
36 changes: 18 additions & 18 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestImmediateExecution(t *testing.T) {
func TestScheduler_Every_InvalidInterval(t *testing.T) {
testCases := []struct {
description string
interval interface{}
interval any
expectedError string
}{
{"zero", 0, ErrInvalidInterval.Error()},
Expand Down Expand Up @@ -271,27 +271,27 @@ func TestMultipleAtTimesDecoding(t *testing.T) {
exp := []time.Duration{_getHours(1), _getHours(3), _getHours(4), _getHours(7), _getHours(15)}
testCases := []struct {
name string
params []interface{}
params []any
result []time.Duration
}{
{
name: "multiple simple strings",
params: []interface{}{"03:00", "15:00", "01:00", "07:00", "04:00"},
params: []any{"03:00", "15:00", "01:00", "07:00", "04:00"},
result: exp,
},
{
name: "single string separated by semicolons",
params: []interface{}{"03:00;15:00;01:00;07:00;04:00"},
params: []any{"03:00;15:00;01:00;07:00;04:00"},
result: exp,
},
{
name: "interpolation of semicolons string, time.Time and simple string",
params: []interface{}{"03:00;15:00;01:00", time.Date(0, 0, 0, 7, 0, 0, 0, time.UTC), "04:00"},
params: []any{"03:00;15:00;01:00", time.Date(0, 0, 0, 7, 0, 0, 0, time.UTC), "04:00"},
result: exp,
},
{
name: "repeated values on input don't get duplicated after decoding",
params: []interface{}{"03:00;15:00;01:00;07:00;04:00;01:00"},
params: []any{"03:00;15:00;01:00;07:00;04:00;01:00"},
result: exp,
},
}
Expand Down Expand Up @@ -773,7 +773,7 @@ func TestClearUnique(t *testing.T) {
// be stopped on s.Clear()
assert.Equal(t, 1, counter)

s.tags.Range(func(key, value interface{}) bool {
s.tags.Range(func(key, value any) bool {
assert.FailNow(t, "map should be empty")
return true
})
Expand Down Expand Up @@ -1177,7 +1177,7 @@ func TestRunJobsWithLimit(t *testing.T) {
require.LessOrEqual(t, counter, 1)
}

s.tags.Range(func(key, value interface{}) bool {
s.tags.Range(func(key, value any) bool {
assert.FailNow(t, "map should be empty")
return true
})
Expand Down Expand Up @@ -1535,10 +1535,10 @@ func TestScheduler_MultipleTagsChained(t *testing.T) {
func TestScheduler_DoParameterValidation(t *testing.T) {
testCases := []struct {
description string
parameters []interface{}
parameters []any
}{
{"less than expected", []interface{}{"p1"}},
{"more than expected", []interface{}{"p1", "p2", "p3"}},
{"less than expected", []any{"p1"}},
{"more than expected", []any{"p1", "p2", "p3"}},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -2195,15 +2195,15 @@ func TestScheduler_MultipleAtTime(t *testing.T) {
func TestScheduler_DoWithJobDetails(t *testing.T) {
testCases := []struct {
description string
jobFunc interface{}
params []interface{}
jobFunc any
params []any
expectedError string
}{
{"no error", func(foo, bar string, job Job) {}, []interface{}{"foo", "bar"}, ""},
{"too few params", func(foo, bar string, job Job) {}, []interface{}{"foo"}, ErrWrongParams.Error()},
{"too many params", func(foo, bar string, job Job) {}, []interface{}{"foo", "bar", "baz"}, ErrWrongParams.Error()},
{"jobFunc doesn't have Job param", func(foo, bar string) {}, []interface{}{"foo"}, ErrDoWithJobDetails.Error()},
{"jobFunc has Job param but not last param", func(job Job, foo, bar string) {}, []interface{}{"foo", "bar"}, ErrDoWithJobDetails.Error()},
{"no error", func(foo, bar string, job Job) {}, []any{"foo", "bar"}, ""},
{"too few params", func(foo, bar string, job Job) {}, []any{"foo"}, ErrWrongParams.Error()},
{"too many params", func(foo, bar string, job Job) {}, []any{"foo", "bar", "baz"}, ErrWrongParams.Error()},
{"jobFunc doesn't have Job param", func(foo, bar string) {}, []any{"foo"}, ErrDoWithJobDetails.Error()},
{"jobFunc has Job param but not last param", func(job Job, foo, bar string) {}, []any{"foo", "bar"}, ErrDoWithJobDetails.Error()},
}

for _, tc := range testCases {
Expand Down

0 comments on commit ef9722c

Please sign in to comment.