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 } config1 := NewParallelConfig().SetResultHandler(func(taskName string, result []interface{}, err error) { // t.Log("finished here") // t.Log(utils.Format4Output(result, false)) }).SetParallelCount(100).SetBatchSize(7) task1 := RunManagedParallelTask("test", config1, "autotest", func(task *ParallelTask, 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) config2 := NewParallelConfig().SetResultHandler(func(taskName string, result []interface{}, err error) { // t.Log("finished here") // t.Log(utils.Format4Output(result, false)) }).SetParallelCount(100).SetBatchSize(7) task2 := RunManagedParallelTask("test", config2, "autotest", func(task *ParallelTask, 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(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") } }