github.com/songzhibin97/gkit@v1.2.13/distributed/task/task_test.go (about)

     1  package task
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestTaskCallError(t *testing.T) {
    13  	t.Parallel()
    14  	retrievable := func() error { return NewErrRetryTaskLater("error", time.Minute) }
    15  
    16  	task, err := NewTask(retrievable, []Arg{})
    17  	assert.NoError(t, err)
    18  
    19  	results, err := task.Call()
    20  	assert.Nil(t, results)
    21  	assert.NotNil(t, err)
    22  	_, ok := interface{}(err).(ErrRetryTaskLater)
    23  	assert.True(t, ok, "err must is ErrRetryTaskLater type")
    24  
    25  	errFn := func() error { return errors.New("error") }
    26  	task, err = NewTask(errFn, []Arg{})
    27  	assert.NoError(t, err)
    28  	results, err = task.Call()
    29  	assert.Nil(t, results)
    30  	assert.NotNil(t, err)
    31  	assert.Equal(t, "error", err.Error())
    32  }
    33  
    34  func TestTransformArgs(t *testing.T) {
    35  	t.Parallel()
    36  	task := &Task{}
    37  	args := []Arg{
    38  		{
    39  			Type:  "[]int64",
    40  			Value: []int64{1, 2},
    41  		},
    42  	}
    43  	err := task.TransformArgs(args)
    44  	assert.NoError(t, err)
    45  	assert.Equal(t, 1, len(task.Args))
    46  	assert.Equal(t, "[]int64", task.Args[0].Type().String())
    47  }
    48  
    49  func TestTaskCallBeyondExpectationErr(t *testing.T) {
    50  	t.Parallel()
    51  	f1 := func(x int) error { return nil }
    52  	args := []Arg{
    53  		{
    54  			Type:  "bool",
    55  			Value: true,
    56  		},
    57  	}
    58  	task, err := NewTask(f1, args)
    59  	assert.NoError(t, err)
    60  	results, err := task.Call()
    61  	assert.Equal(t, "reflect: Call using bool as type int", err.Error())
    62  	assert.Nil(t, results)
    63  
    64  	f2 := func() (interface{}, error) { return 1.11, nil }
    65  	task, err = NewTask(f2, []Arg{})
    66  	assert.NoError(t, err)
    67  
    68  	results, err = task.Call()
    69  	assert.NoError(t, err)
    70  	assert.Equal(t, "float64", results[0].Type)
    71  	assert.Equal(t, 1.11, results[0].Value)
    72  }
    73  
    74  func TestTaskCallWithContext(t *testing.T) {
    75  	t.Parallel()
    76  	f := func(ctx context.Context) (interface{}, error) {
    77  		assert.NotNil(t, ctx)
    78  		assert.Nil(t, SignatureFromContext(ctx))
    79  		return 1.11, nil
    80  	}
    81  	task, err := NewTask(f, []Arg{})
    82  	assert.NoError(t, err)
    83  	results, err := task.Call()
    84  	assert.NoError(t, err)
    85  	assert.Equal(t, "float64", results[0].Type)
    86  	assert.Equal(t, 1.11, results[0].Value)
    87  }
    88  
    89  func TestTaskCallWithSignatureInContext(t *testing.T) {
    90  	t.Parallel()
    91  
    92  	f := func(ctx context.Context) (interface{}, error) {
    93  		assert.NotNil(t, ctx)
    94  		signature := SignatureFromContext(ctx)
    95  		assert.NotNil(t, signature)
    96  		assert.Equal(t, "bar", signature.Name)
    97  		return 1.11, nil
    98  	}
    99  	signature := NewSignature("", "bar")
   100  	task, err := NewTaskWithSignature(f, signature)
   101  	assert.NoError(t, err)
   102  	results, err := task.Call()
   103  	assert.NoError(t, err)
   104  	assert.Equal(t, "float64", results[0].Type)
   105  	assert.Equal(t, 1.11, results[0].Value)
   106  }