- refactor tasksch
- sequence task added. - task tree added.
This commit is contained in:
70
business/jxutils/tasksch/parallel_task_test.go
Normal file
70
business/jxutils/tasksch/parallel_task_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package tasksch
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
func TestRunTask(t *testing.T) {
|
||||
itemList := make([]int, 100)
|
||||
for k := range itemList {
|
||||
itemList[k] = k
|
||||
}
|
||||
task := RunTask("test", false, func(taskName string, result []interface{}, err error) {
|
||||
// t.Log("finished here")
|
||||
// t.Log(utils.Format4Output(result, false))
|
||||
}, 100, 7, "autotest", func(batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
sleepSecond := rand.Intn(5)
|
||||
t.Logf("sleep %d seconds", sleepSecond)
|
||||
time.Sleep(time.Duration(sleepSecond) * time.Second)
|
||||
retSlice := make([]string, len(batchItemList))
|
||||
for k := range retSlice {
|
||||
retSlice[k] = "hello:" + utils.Int2Str(batchItemList[k].(int)*2)
|
||||
}
|
||||
return retSlice, nil
|
||||
}, itemList, "a", "b", 1, 2)
|
||||
result, err := task.GetResult(1 * time.Microsecond)
|
||||
if err == nil || task.GetStatus() != TaskStatusWorking {
|
||||
t.Fatal("task can not be done in 1 microsecond")
|
||||
}
|
||||
result, err = task.GetResult(0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result) != len(itemList) {
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
t.Fatal("result size doesn't match with itemList")
|
||||
}
|
||||
t.Log(task.GetStatus())
|
||||
}
|
||||
|
||||
func TestCancelTask(t *testing.T) {
|
||||
itemList := make([]int, 100)
|
||||
for k := range itemList {
|
||||
itemList[k] = k
|
||||
}
|
||||
task := RunTask("test", false, func(taskName string, result []interface{}, err error) {
|
||||
// t.Log("finished here")
|
||||
// t.Log(utils.Format4Output(result, false))
|
||||
}, 100, 7, "autotest", func(batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
sleepSecond := rand.Intn(5)
|
||||
t.Logf("sleep %d seconds", sleepSecond)
|
||||
time.Sleep(time.Duration(sleepSecond) * time.Second)
|
||||
retSlice := make([]string, len(batchItemList))
|
||||
for k := range retSlice {
|
||||
retSlice[k] = "hello:" + utils.Int2Str(batchItemList[k].(int)*2)
|
||||
}
|
||||
return retSlice, nil
|
||||
}, itemList, "a", "b", 1, 2)
|
||||
// time.Sleep(time.Second * 6)
|
||||
t.Logf("finishedItemCount:%d, finishedJobCount:%d", task.GetFinishedItemCount(), task.GetFinishedJobCount())
|
||||
task.Cancel()
|
||||
_, err := task.GetResult(0)
|
||||
if err != ErrTaskIsCanceled {
|
||||
t.Fatal("task should in canceled status")
|
||||
}
|
||||
// t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
Reference in New Issue
Block a user