Skip to content

Commit

Permalink
added tests for executor and gocron (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaruGaru authored Nov 1, 2021
1 parent 2e45eba commit 560e7b9
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
29 changes: 29 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gocron

import (
"github.com/stretchr/testify/assert"
"sync"
"testing"
)

func Test_ExecutorExecute(t *testing.T) {
e := newExecutor()

wg := &sync.WaitGroup{}
wg.Add(1)
go e.start()

var runState = int64(0)
e.jobFunctions <- jobFunction{
name: "test_fn",
function: func(arg string) {
assert.Equal(t, arg, "test")
wg.Done()
},
parameters: []interface{}{"test"},
runState: &runState,
}

e.stop()
wg.Wait()
}
90 changes: 90 additions & 0 deletions gocron_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocron

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -105,3 +106,92 @@ func TestParseTime(t *testing.T) {
})
}
}

func Test_callJobFuncWithParams(t *testing.T) {
type args struct {
jobFunc interface{}
params []interface{}
}
tests := []struct {
name string
args args
err bool
}{
{
name: "test call func with no args",
args: args{
jobFunc: func() {},
params: nil,
},
},
{
name: "test call func with single arg",
args: args{
jobFunc: func(arg string) {},
params: []interface{}{"test"},
},
},
{
name: "test call func with wrong arg type",
args: args{
jobFunc: func(arg int) {},
params: []interface{}{"test"},
},
err: true,
},
{
name: "test call func with wrong arg count",
args: args{
jobFunc: func(arg int) {},
params: []interface{}{},
},
err: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := panicFnToErr(func() {
callJobFuncWithParams(tt.args.jobFunc, tt.args.params)
})

if err != nil && !tt.err {
t.Fatalf("unexpected panic: %s", err.Error())
}

})
}
}

func panicFnToErr(fn func()) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("func panic")
}
}()
fn()
return err
}

func Test_getFunctionName(t *testing.T) {
type args struct {
fn interface{}
}
tests := []struct {
name string
args args
want string
}{
{
name: "test get function name",
args: args{
fn: Test_getFunctionName,
},
want: "github.com/go-co-op/gocron.Test_getFunctionName",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, getFunctionName(tt.args.fn), "getFunctionName(%v)", tt.args.fn)
})
}
}

0 comments on commit 560e7b9

Please sign in to comment.