1/* 2Copyright 2017 Google LLC 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package spanner 18 19import ( 20 "context" 21 "errors" 22 "fmt" 23 "testing" 24 "time" 25 26 "github.com/golang/protobuf/proto" 27 "github.com/golang/protobuf/ptypes" 28 edpb "google.golang.org/genproto/googleapis/rpc/errdetails" 29 "google.golang.org/grpc/codes" 30 "google.golang.org/grpc/metadata" 31 "google.golang.org/grpc/status" 32) 33 34// Test if runRetryable loop deals with various errors correctly. 35func TestRetry(t *testing.T) { 36 if testing.Short() { 37 t.SkipNow() 38 } 39 responses := []error{ 40 status.Errorf(codes.Internal, "transport is closing"), 41 status.Errorf(codes.Unknown, "unexpected EOF"), 42 status.Errorf(codes.Internal, "unexpected EOF"), 43 status.Errorf(codes.Internal, "stream terminated by RST_STREAM with error code: 2"), 44 status.Errorf(codes.Unavailable, "service is currently unavailable"), 45 errRetry(fmt.Errorf("just retry it")), 46 } 47 err := runRetryable(context.Background(), func(ct context.Context) error { 48 var r error 49 if len(responses) > 0 { 50 r = responses[0] 51 responses = responses[1:] 52 } 53 return r 54 }) 55 if err != nil { 56 t.Errorf("runRetryable should be able to survive all retryable errors, but it returns %v", err) 57 } 58 // Unretryable errors 59 injErr := errors.New("this is unretryable") 60 err = runRetryable(context.Background(), func(ct context.Context) error { 61 return injErr 62 }) 63 if wantErr := toSpannerError(injErr); !testEqual(err, wantErr) { 64 t.Errorf("runRetryable returns error %v, want %v", err, wantErr) 65 } 66 // Timeout 67 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 68 defer cancel() 69 retryErr := errRetry(fmt.Errorf("still retrying")) 70 err = runRetryable(ctx, func(ct context.Context) error { 71 // Expect to trigger timeout in retryable runner after 10 executions. 72 <-time.After(100 * time.Millisecond) 73 // Let retryable runner to retry so that timeout will eventually happen. 74 return retryErr 75 }) 76 // Check error code and error message 77 if wantErrCode, wantErr := codes.DeadlineExceeded, errContextCanceled(ctx, retryErr); ErrCode(err) != wantErrCode || !testEqual(err, wantErr) { 78 t.Errorf("<err code, err>=\n<%v, %v>, want:\n<%v, %v>", ErrCode(err), err, wantErrCode, wantErr) 79 } 80 // Cancellation 81 ctx, cancel = context.WithCancel(context.Background()) 82 retries := 3 83 retryErr = errRetry(fmt.Errorf("retry before cancel")) 84 err = runRetryable(ctx, func(ct context.Context) error { 85 retries-- 86 if retries == 0 { 87 cancel() 88 } 89 return retryErr 90 }) 91 // Check error code, error message, retry count 92 if wantErrCode, wantErr := codes.Canceled, errContextCanceled(ctx, retryErr); ErrCode(err) != wantErrCode || !testEqual(err, wantErr) || retries != 0 { 93 t.Errorf("<err code, err, retries>=\n<%v, %v, %v>, want:\n<%v, %v, %v>", ErrCode(err), err, retries, wantErrCode, wantErr, 0) 94 } 95} 96 97func TestRetryInfo(t *testing.T) { 98 b, _ := proto.Marshal(&edpb.RetryInfo{ 99 RetryDelay: ptypes.DurationProto(time.Second), 100 }) 101 trailers := map[string]string{ 102 retryInfoKey: string(b), 103 } 104 gotDelay, ok := extractRetryDelay(errRetry(toSpannerErrorWithMetadata(status.Errorf(codes.Aborted, ""), metadata.New(trailers)))) 105 if !ok || !testEqual(time.Second, gotDelay) { 106 t.Errorf("<ok, retryDelay> = <%t, %v>, want <true, %v>", ok, gotDelay, time.Second) 107 } 108} 109