...

Source file src/github.com/ory/x/resilience/retry_test.go

Documentation: github.com/ory/x/resilience

     1  package resilience
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus/hooks/test"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/ory/x/logrusx"
    12  )
    13  
    14  func TestRetry(t *testing.T) {
    15  	t.Run("case=fails after timeout", func(t *testing.T) {
    16  		l, _ := test.NewNullLogger()
    17  		logger := logrusx.New("", "", logrusx.UseLogger(l))
    18  
    19  		randomErr := fmt.Errorf("some error")
    20  
    21  		err := Retry(logger, 100*time.Millisecond, 100*time.Millisecond, func() error {
    22  			return randomErr
    23  		})
    24  
    25  		assert.Equal(t, err, randomErr)
    26  	})
    27  
    28  	t.Run("case=logs error when failing", func(t *testing.T) {
    29  		l, hook := test.NewNullLogger()
    30  		logger := logrusx.New("", "", logrusx.UseLogger(l))
    31  
    32  		const errPattern = "error %d"
    33  
    34  		var i int
    35  		err := Retry(logger, 100*time.Millisecond, 200*time.Millisecond, func() error {
    36  			defer func() { i++ }()
    37  			return fmt.Errorf(errPattern, i)
    38  		})
    39  
    40  		assert.Equal(t, fmt.Errorf(errPattern, 1), err)
    41  		assert.Len(t, hook.AllEntries(), 2)
    42  		assert.Equal(t, hook.LastEntry().Data["error"], map[string]interface{}{"message": fmt.Errorf(errPattern, 1).Error()})
    43  	})
    44  }
    45  

View as plain text