You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

109 lines
3.7 KiB

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