...

Source file src/cloud.google.com/go/internal/retry_test.go

Documentation: cloud.google.com/go/internal

     1  // Copyright 2016 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  	"time"
    22  
    23  	gax "github.com/googleapis/gax-go/v2"
    24  	"google.golang.org/grpc/codes"
    25  	"google.golang.org/grpc/status"
    26  )
    27  
    28  func TestRetry(t *testing.T) {
    29  	ctx := context.Background()
    30  	// Without a context deadline, retry will run until the function
    31  	// says not to retry any more.
    32  	n := 0
    33  	endRetry := errors.New("end retry")
    34  	err := retry(ctx, gax.Backoff{},
    35  		func() (bool, error) {
    36  			n++
    37  			if n < 10 {
    38  				return false, nil
    39  			}
    40  			return true, endRetry
    41  		},
    42  		func(context.Context, time.Duration) error { return nil })
    43  	if got, want := err, endRetry; got != want {
    44  		t.Errorf("got %v, want %v", err, endRetry)
    45  	}
    46  	if n != 10 {
    47  		t.Errorf("n: got %d, want %d", n, 10)
    48  	}
    49  
    50  	// If the context has a deadline, sleep will return an error
    51  	// and end the function.
    52  	n = 0
    53  	err = retry(ctx, gax.Backoff{},
    54  		func() (bool, error) { return false, nil },
    55  		func(context.Context, time.Duration) error {
    56  			n++
    57  			if n < 10 {
    58  				return nil
    59  			}
    60  			return context.DeadlineExceeded
    61  		})
    62  	if err == nil {
    63  		t.Error("got nil, want error")
    64  	}
    65  }
    66  
    67  func TestRetryPreserveError(t *testing.T) {
    68  	// Retry tries to preserve the type and other information from
    69  	// the last error returned by the function.
    70  	err := retry(context.Background(), gax.Backoff{},
    71  		func() (bool, error) {
    72  			return false, status.Error(codes.NotFound, "not found")
    73  		},
    74  		func(context.Context, time.Duration) error {
    75  			return context.DeadlineExceeded
    76  		})
    77  	if err == nil {
    78  		t.Fatalf("unexpectedly got nil error")
    79  	}
    80  	wantError := "retry failed with context deadline exceeded; last error: rpc error: code = NotFound desc = not found"
    81  	if g, w := err.Error(), wantError; g != w {
    82  		t.Errorf("got error %q, want %q", g, w)
    83  	}
    84  	got, ok := status.FromError(err)
    85  	if !ok {
    86  		t.Fatalf("got %T, wanted a status", got)
    87  	}
    88  	if g, w := got.Code(), codes.NotFound; g != w {
    89  		t.Errorf("got code %v, want %v", g, w)
    90  	}
    91  	wantMessage := "retry failed with context deadline exceeded; last error: rpc error: code = NotFound desc = not found"
    92  	if g, w := got.Message(), wantMessage; g != w {
    93  		t.Errorf("got message %q, want %q", g, w)
    94  	}
    95  }
    96  
    97  func TestRetryWrapsErrorWithStatusUnknown(t *testing.T) {
    98  	// When retrying on an error that is not a grpc error, make sure to return
    99  	// a valid gRPC status.
   100  	err := retry(context.Background(), gax.Backoff{},
   101  		func() (bool, error) {
   102  			return false, errors.New("test error")
   103  		},
   104  		func(context.Context, time.Duration) error {
   105  			return context.DeadlineExceeded
   106  		})
   107  	if err == nil {
   108  		t.Fatalf("unexpectedly got nil error")
   109  	}
   110  	wantError := "retry failed with context deadline exceeded; last error: test error"
   111  	if g, w := err.Error(), wantError; g != w {
   112  		t.Errorf("got error %q, want %q", g, w)
   113  	}
   114  	got, _ := status.FromError(err)
   115  	if g, w := got.Code(), codes.Unknown; g != w {
   116  		t.Errorf("got code %v, want %v", g, w)
   117  	}
   118  }
   119  

View as plain text