github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/retry/retry_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package retry
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestRetryEventuallyOk(t *testing.T) {
    29  	r := Retry{
    30  		MaximumDelay: 3 * time.Microsecond,
    31  		InitialDelay: 1 * time.Microsecond,
    32  	}
    33  	r.Do(context.Background(), "unit test", func(i int) (retry bool, err error) {
    34  		return i < 10, fmt.Errorf("pop")
    35  	})
    36  }
    37  
    38  func TestRetryDeadlineTimeout(t *testing.T) {
    39  	r := Retry{
    40  		MaximumDelay: 1 * time.Second,
    41  		InitialDelay: 1 * time.Second,
    42  	}
    43  	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Millisecond)
    44  	defer cancel()
    45  	err := r.DoCustomLog(ctx, func(i int) (retry bool, err error) {
    46  		return true, fmt.Errorf("pop")
    47  	})
    48  	assert.Regexp(t, "FF10158", err)
    49  }
    50  
    51  func TestRetryContextCancellled(t *testing.T) {
    52  	r := Retry{
    53  		MaximumDelay: 1 * time.Second,
    54  		InitialDelay: 1 * time.Second,
    55  	}
    56  	ctx, cancel := context.WithCancel(context.Background())
    57  	cancel()
    58  	err := r.Do(ctx, "unit test", func(i int) (retry bool, err error) {
    59  		return true, fmt.Errorf("pop")
    60  	})
    61  	assert.Regexp(t, "FF10158", err)
    62  }