package tasksch

import (
	"fmt"
	"math/rand"
	"testing"
	"time"

	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
)

func TestRunSeqTask(t *testing.T) {
	var seqTask ITask
	seqTask = NewSeqTask("TestSeqTask", jxcontext.AdminCtx,
		func(task *SeqTask, step int, params ...interface{}) (result interface{}, err error) {
			switch step {
			case 0:
				fmt.Println("ONE")
				task2 := NewParallelTask("hello", nil, jxcontext.AdminCtx,
					func(parallelTask *ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
						i := batchItemList[0].(int)
						time.Sleep(2 * time.Second)
						fmt.Println(i * 2)
						return nil, nil
					}, []int{1, 2, 3})
				seqTask.AddChild(task2)
				time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
				task2.Run()
			case 1:
				fmt.Println("TWO")
				time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
			case 2:
				fmt.Println("THREE")
				time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
			case 3:
				fmt.Println("FOUR")
				time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
			case 4:
				fmt.Println("FIVE")
				time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
			}
			return []string{"1"}, nil
		}, 5)

	seqTask.Run()
	time.Sleep(3 * time.Second)
	seqTask.Cancel()
	fmt.Println(utils.Format4Output(seqTask, false))
	result, err := seqTask.GetResult(0)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(result)
}
