Skip to content

Commit

Permalink
Merge pull request #2 from gochore/dev
Browse files Browse the repository at this point in the history
support retry; add testings
  • Loading branch information
wolfogre authored Mar 2, 2020
2 parents 33051a8 + e8a28fa commit 699f8c1
Show file tree
Hide file tree
Showing 10 changed files with 1,202 additions and 107 deletions.
20 changes: 18 additions & 2 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/robfig/cron/v3"
)

type CronMeta interface {
Key() string
Hostname() string
}

type Cron struct {
key string
hostname string
Expand All @@ -28,9 +33,20 @@ func NewCron(options ...CronOption) *Cron {

func (c *Cron) AddJob(job Job) error {
j := &innerJob{
Job: job,
cron: c,
cron: c,
entryGetter: c.cron,
key: job.Key(),
spec: job.Spec(),
run: job.Run,
}

for _, option := range job.Options() {
option(j)
}
if j.retryTimes < 1 {
j.retryTimes = 1
}

entryID, err := c.cron.AddJob(j.Spec(), j)
if err != nil {
return err
Expand Down
313 changes: 296 additions & 17 deletions cron_test.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,322 @@
package dcron

import (
"log"
"testing"
"time"

"github.com/gochore/dcron/mock_dcron"
"github.com/golang/mock/gomock"
"github.com/robfig/cron/v3"
)

func Test_Cron(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mutex := mock_dcron.NewMockMutex(ctrl)

c := NewCron(WithKey("test_cron"), WithMutex(mutex))

mutex.EXPECT().
SetIfNotExists(gomock.Any(), gomock.Any()).
SetIfNotExists(gomock.Any(), c.Hostname()).
Return(true).
MinTimes(1)
Times(2)

cron := NewCron(WithKey("test_cron"), WithMutex(mutex))
job, err := NewJob("test", "*/5 * * * * *", func() error {
log.Println("run")
job := NewJob("test", "*/5 * * * * *", func(task Task) error {
task.Value("")
select {
case <-task.Done():
t.Logf("exit: %+v", task)
case <-time.After(time.Second):
t.Logf("run: %+v", task)
}
return nil
}, WithBeforeFunc(func(task Task) (skip bool) {
log.Println("before")
log.Printf("%+v", task)
t.Logf("before: %+v", task)
return false
}), WithAfterFunc(func(task Task) {
log.Println("after")
log.Printf("%+v", task)
t.Logf("after: %+v", task)
}))
if err != nil {
if err := c.AddJob(job); err != nil {
t.Fatal(err)
}
if err := cron.AddJob(job); err != nil {
t.Fatal(err)
}
cron.Start()
c.Start()
c.Run() // should be not working
time.Sleep(10 * time.Second)
<-cron.Stop().Done()
<-c.Stop().Done()
}

func TestCron_AddJob(t *testing.T) {
c := cron.New(cron.WithSeconds())

type fields struct {
key string
hostname string
cron *cron.Cron
mutex Mutex
jobs []*innerJob
}
type args struct {
job Job
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "regular",
fields: fields{
cron: c,
},
args: args{
job: NewJob("test_job", "* * * * * *", nil),
},
wantErr: false,
},
{
name: "with option",
fields: fields{
cron: c,
},
args: args{
job: NewJob("test_job", "* * * * * *", nil, WithRetryTimes(3)),
},
wantErr: false,
},
{
name: "wrong spec",
fields: fields{
cron: c,
},
args: args{
job: NewJob("test_job", "* * * * *", nil),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cron{
key: tt.fields.key,
hostname: tt.fields.hostname,
cron: tt.fields.cron,
mutex: tt.fields.mutex,
jobs: tt.fields.jobs,
}
if err := c.AddJob(tt.args.job); (err != nil) != tt.wantErr {
t.Errorf("AddJob() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestCron_Hostname(t *testing.T) {
type fields struct {
key string
hostname string
cron *cron.Cron
mutex Mutex
jobs []*innerJob
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "regular",
fields: fields{
hostname: "test_hostname",
},
want: "test_hostname",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cron{
key: tt.fields.key,
hostname: tt.fields.hostname,
cron: tt.fields.cron,
mutex: tt.fields.mutex,
jobs: tt.fields.jobs,
}
if got := c.Hostname(); got != tt.want {
t.Errorf("Hostname() = %v, want %v", got, tt.want)
}
})
}
}

func TestCron_Key(t *testing.T) {
type fields struct {
key string
hostname string
cron *cron.Cron
mutex Mutex
jobs []*innerJob
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "regular",
fields: fields{
key: "test_key",
},
want: "test_key",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cron{
key: tt.fields.key,
hostname: tt.fields.hostname,
cron: tt.fields.cron,
mutex: tt.fields.mutex,
jobs: tt.fields.jobs,
}
if got := c.Key(); got != tt.want {
t.Errorf("Key() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewCron(t *testing.T) {
type args struct {
options []CronOption
}
tests := []struct {
name string
args args
check func(t *testing.T, c *Cron)
}{
{
name: "regular",
args: args{
options: nil,
},
check: func(t *testing.T, c *Cron) {
if c == nil {
t.Fatal(t)
}
},
},
{
name: "with_option",
args: args{
options: []CronOption{WithKey("test_cron")},
},
check: func(t *testing.T, c *Cron) {
if c == nil {
t.Fatal(t)
}
if c.key != "test_cron" {
t.Fatal(c.key)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewCron(tt.args.options...)
tt.check(t, got)
})
}
}

func TestWithHostname(t *testing.T) {
type args struct {
hostname string
}
tests := []struct {
name string
args args
check func(t *testing.T, option CronOption)
}{
{
name: "regular",
args: args{
hostname: "test_hostname",
},
check: func(t *testing.T, option CronOption) {
c := NewCron()
option(c)
if c.hostname != "test_hostname" {
t.Fatal(c.hostname)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := WithHostname(tt.args.hostname)
tt.check(t, got)
})
}
}

func TestWithKey(t *testing.T) {
type args struct {
key string
}
tests := []struct {
name string
args args
check func(t *testing.T, option CronOption)
}{
{
name: "regular",
args: args{
key: "test_cron",
},
check: func(t *testing.T, option CronOption) {
c := NewCron()
option(c)
if c.key != "test_cron" {
t.Fatal(c.key)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := WithKey(tt.args.key)
tt.check(t, got)
})
}
}

func TestWithMutex(t *testing.T) {
type args struct {
mutex Mutex
}
tests := []struct {
name string
args args
check func(t *testing.T, option CronOption)
}{
{
name: "regular",
args: args{
mutex: mock_dcron.NewMockMutex(nil),
},
check: func(t *testing.T, option CronOption) {
c := NewCron()
option(c)
if c.mutex == nil {
t.Fatal(c.mutex)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := WithMutex(tt.args.mutex)
tt.check(t, got)
})
}
}
Loading

0 comments on commit 699f8c1

Please sign in to comment.