50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package tasksch
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
)
|
|
|
|
func TestTaskMan(t *testing.T) {
|
|
itemList := make([]int, 100)
|
|
for k := range itemList {
|
|
itemList[k] = k
|
|
}
|
|
task1 := RunManagedTask("test", 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
|
|
}, func(taskName string, result []interface{}, err error) {
|
|
// t.Log("finished here")
|
|
// t.Log(utils.Format4Output(result, false))
|
|
}, 100, 7, "autotest", itemList, "a", "b", 1, 2)
|
|
|
|
task2 := RunManagedTask("test", 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
|
|
}, func(taskName string, result []interface{}, err error) {
|
|
// t.Log("finished here")
|
|
// t.Log(utils.Format4Output(result, false))
|
|
}, 100, 7, "autotest", itemList, "a", "b", 1, 2)
|
|
time.Sleep(2 * time.Second)
|
|
task2.Cancel()
|
|
if task1.GetStatus() == task2.GetStatus() {
|
|
t.Log(utils.Format4Output(GetTasks("", TaskStatusBegin, TaskStatusEnd, 5), false))
|
|
t.Fatal("status should not be same")
|
|
}
|
|
}
|