github.com/lingyao2333/mo-zero@v1.4.1/core/fx/retry_test.go (about)

     1  package fx
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestRetry(t *testing.T) {
    11  	assert.NotNil(t, DoWithRetry(func() error {
    12  		return errors.New("any")
    13  	}))
    14  
    15  	var times int
    16  	assert.Nil(t, DoWithRetry(func() error {
    17  		times++
    18  		if times == defaultRetryTimes {
    19  			return nil
    20  		}
    21  		return errors.New("any")
    22  	}))
    23  
    24  	times = 0
    25  	assert.NotNil(t, DoWithRetry(func() error {
    26  		times++
    27  		if times == defaultRetryTimes+1 {
    28  			return nil
    29  		}
    30  		return errors.New("any")
    31  	}))
    32  
    33  	total := 2 * defaultRetryTimes
    34  	times = 0
    35  	assert.Nil(t, DoWithRetry(func() error {
    36  		times++
    37  		if times == total {
    38  			return nil
    39  		}
    40  		return errors.New("any")
    41  	}, WithRetry(total)))
    42  }