go-utils/common/runner/runner_test.go

34 lines
589 B
Go
Raw Normal View History

2018-01-22 10:14:27 +00:00
package runner
2017-12-22 08:01:46 +00:00
import (
"log"
"os"
"testing"
"time"
)
func TestRunner(t *testing.T) {
log.Println("...开始执行任务...")
timeout := 3 * time.Second
2017-12-22 09:44:04 +00:00
r := NewRunner(timeout)
2017-12-22 08:01:46 +00:00
r.Add(createTask(), createTask(), createTask())
if err := r.Start(); err != nil {
switch err {
case ErrTimeOut:
log.Println(err)
os.Exit(1)
case ErrInterrupt:
log.Println(err)
os.Exit(2)
}
}
log.Println("...任务执行结束...")
}
func createTask() func(int) {
return func(id int) {
log.Printf("正在执行任务%d", id)
time.Sleep(time.Duration(id) * time.Second)
}
}