go-utils/common/runner_test.go

34 lines
583 B
Go
Raw Normal View History

2017-12-22 08:01:46 +00:00
package common
import (
"log"
"os"
"testing"
"time"
)
func TestRunner(t *testing.T) {
log.Println("...开始执行任务...")
timeout := 3 * time.Second
r := New(timeout)
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)
}
}