1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 21:30:55 +00:00
Beego/toolbox/task_test.go

60 lines
1.3 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
2014-06-25 02:39:37 +00:00
2014-04-12 05:18:18 +00:00
// @description beego is an open-source, high-performance web framework for the Go programming language.
2014-06-25 02:39:37 +00:00
2014-04-12 05:18:18 +00:00
// @link http://github.com/astaxie/beego for the canonical source repository
2014-06-25 02:39:37 +00:00
2014-04-12 05:18:18 +00:00
// @license http://github.com/astaxie/beego/blob/master/LICENSE
2014-06-25 02:39:37 +00:00
2014-04-12 05:18:18 +00:00
// @authors astaxie
2013-11-20 15:53:54 +00:00
package toolbox
import (
"fmt"
"sync"
"testing"
"time"
)
func TestParse(t *testing.T) {
tk := NewTask("taska", "0/30 * * * * *", func() error { fmt.Println("hello world"); return nil })
err := tk.Run()
if err != nil {
t.Fatal(err)
}
AddTask("taska", tk)
StartTask()
time.Sleep(6 * time.Second)
StopTask()
}
func TestSpec(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(2)
tk1 := NewTask("tk1", "0 12 * * * *", func() error { fmt.Println("tk1"); return nil })
tk2 := NewTask("tk2", "0,10,20 * * * * *", func() error { fmt.Println("tk2"); wg.Done(); return nil })
tk3 := NewTask("tk3", "0 10 * * * *", func() error { fmt.Println("tk3"); wg.Done(); return nil })
AddTask("tk1", tk1)
AddTask("tk2", tk2)
AddTask("tk3", tk3)
StartTask()
defer StopTask()
select {
case <-time.After(200 * time.Second):
t.FailNow()
case <-wait(wg):
}
}
func wait(wg *sync.WaitGroup) chan bool {
ch := make(chan bool)
go func() {
wg.Wait()
ch <- true
}()
return ch
}