github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/pcsutil/taskframework/taskframework_test.go (about)

     1  package taskframework_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/fzfile/BaiduPCS-Go/pcsutil/taskframework"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  type (
    11  	TestUnit struct {
    12  		retry    bool
    13  		taskInfo *taskframework.TaskInfo
    14  	}
    15  )
    16  
    17  func (tu *TestUnit) SetTaskInfo(taskInfo *taskframework.TaskInfo) {
    18  	tu.taskInfo = taskInfo
    19  }
    20  
    21  func (tu *TestUnit) OnFailed(lastRunResult *taskframework.TaskUnitRunResult) {
    22  	fmt.Printf("[%s] error: %s, failed\n", tu.taskInfo.Id(), lastRunResult.Err)
    23  }
    24  
    25  func (tu *TestUnit) OnSuccess(lastRunResult *taskframework.TaskUnitRunResult) {
    26  	fmt.Printf("[%s] success\n", tu.taskInfo.Id())
    27  }
    28  
    29  func (tu *TestUnit) OnComplete(lastRunResult *taskframework.TaskUnitRunResult) {
    30  	fmt.Printf("[%s] complete\n", tu.taskInfo.Id())
    31  }
    32  
    33  func (tu *TestUnit) Run() (result *taskframework.TaskUnitRunResult) {
    34  	fmt.Printf("[%s] running...\n", tu.taskInfo.Id())
    35  	return &taskframework.TaskUnitRunResult{
    36  		//Succeed:   true,
    37  		NeedRetry: true,
    38  	}
    39  }
    40  
    41  func (tu *TestUnit) OnRetry(lastRunResult *taskframework.TaskUnitRunResult) {
    42  	fmt.Printf("[%s] prepare retry, times [%d/%d]...\n", tu.taskInfo.Id(), tu.taskInfo.Retry(), tu.taskInfo.MaxRetry())
    43  }
    44  
    45  func (tu *TestUnit) RetryWait() time.Duration {
    46  	return 1 * time.Second
    47  }
    48  
    49  func TestTaskExecutor(t *testing.T) {
    50  	te := taskframework.NewTaskExecutor()
    51  	te.SetParallel(2)
    52  	for i := 0; i < 3; i++ {
    53  		tu := TestUnit{
    54  			retry: false,
    55  		}
    56  		te.Append(&tu, 2)
    57  	}
    58  	te.Execute()
    59  }