Fix cron test

pull/19/head
miraclesu 2017-05-18 14:07:55 +08:00
parent 65a16cae70
commit f4c990cf44
3 changed files with 16 additions and 5 deletions

View File

@ -9,6 +9,7 @@ install:
- go get github.com/rogpeppe/fastuuid - go get github.com/rogpeppe/fastuuid
- go get golang.org/x/net/context - go get golang.org/x/net/context
- go get gopkg.in/mgo.v2 - go get gopkg.in/mgo.v2
- go get github.com/smartystreets/goconvey/convey
before_script: before_script:
- go vet -x ./... - go vet -x ./...
script: script:

View File

@ -52,7 +52,7 @@ Building with the source
``` ```
go get -u github.com/shunfei/cronsun go get -u github.com/shunfei/cronsun
cd $GOPATH/src/github.com/shunfei/cronsun cd $GOPATH/src/github.com/shunfei/cronsun
sh ./build.sh sh build.sh
``` ```
Or install with the binary [releases](https://github.com/shunfei/cronsun/releases) Or install with the binary [releases](https://github.com/shunfei/cronsun/releases)

View File

@ -2,7 +2,9 @@ package cron
import ( import (
"fmt" "fmt"
"reflect"
"sync" "sync"
"sync/atomic"
"testing" "testing"
"time" "time"
) )
@ -26,6 +28,10 @@ func TestFuncPanicRecovery(t *testing.T) {
type DummyJob struct{} type DummyJob struct{}
func (d DummyJob) GetID() string {
return fmt.Sprintf("pointer[%v]", reflect.ValueOf(&d).Pointer())
}
func (d DummyJob) Run() { func (d DummyJob) Run() {
panic("YOLO") panic("YOLO")
} }
@ -115,12 +121,12 @@ func TestAddWhileRunningWithDelay(t *testing.T) {
cron.Start() cron.Start()
defer cron.Stop() defer cron.Stop()
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
var calls = 0 var calls int32 = 0
cron.AddFunc("* * * * * *", func() { calls += 1 }) cron.AddFunc("* * * * * *", func() { atomic.AddInt32(&calls, 1) })
<-time.After(ONE_SECOND) <-time.After(ONE_SECOND)
if calls != 1 { if atomic.LoadInt32(&calls) != 1 {
fmt.Printf("called %d times, expected 1\n", calls) fmt.Printf("called %d times, expected 1\n", atomic.LoadInt32(&calls))
t.Fail() t.Fail()
} }
} }
@ -276,6 +282,10 @@ type testJob struct {
name string name string
} }
func (t testJob) GetID() string {
return t.name
}
func (t testJob) Run() { func (t testJob) Run() {
t.wg.Done() t.wg.Done()
} }