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.
 
 
 

108 lines
3.6 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. "errors"
  16. "fmt"
  17. "testing"
  18. "time"
  19. "github.com/golang/protobuf/proto"
  20. "github.com/golang/protobuf/ptypes"
  21. "golang.org/x/net/context"
  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, "stream terminated by RST_STREAM with error code: 2"),
  36. status.Errorf(codes.Unavailable, "service is currently unavailable"),
  37. errRetry(fmt.Errorf("just retry it")),
  38. }
  39. err := runRetryable(context.Background(), func(ct context.Context) error {
  40. var r error
  41. if len(responses) > 0 {
  42. r = responses[0]
  43. responses = responses[1:]
  44. }
  45. return r
  46. })
  47. if err != nil {
  48. t.Errorf("runRetryable should be able to survive all retryable errors, but it returns %v", err)
  49. }
  50. // Unretryable errors
  51. injErr := errors.New("this is unretryable")
  52. err = runRetryable(context.Background(), func(ct context.Context) error {
  53. return injErr
  54. })
  55. if wantErr := toSpannerError(injErr); !testEqual(err, wantErr) {
  56. t.Errorf("runRetryable returns error %v, want %v", err, wantErr)
  57. }
  58. // Timeout
  59. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  60. defer cancel()
  61. retryErr := errRetry(fmt.Errorf("still retrying"))
  62. err = runRetryable(ctx, func(ct context.Context) error {
  63. // Expect to trigger timeout in retryable runner after 10 executions.
  64. <-time.After(100 * time.Millisecond)
  65. // Let retryable runner to retry so that timeout will eventually happen.
  66. return retryErr
  67. })
  68. // Check error code and error message
  69. if wantErrCode, wantErr := codes.DeadlineExceeded, errContextCanceled(ctx, retryErr); ErrCode(err) != wantErrCode || !testEqual(err, wantErr) {
  70. t.Errorf("<err code, err>=\n<%v, %v>, want:\n<%v, %v>", ErrCode(err), err, wantErrCode, wantErr)
  71. }
  72. // Cancellation
  73. ctx, cancel = context.WithCancel(context.Background())
  74. retries := 3
  75. retryErr = errRetry(fmt.Errorf("retry before cancel"))
  76. err = runRetryable(ctx, func(ct context.Context) error {
  77. retries--
  78. if retries == 0 {
  79. cancel()
  80. }
  81. return retryErr
  82. })
  83. // Check error code, error message, retry count
  84. if wantErrCode, wantErr := codes.Canceled, errContextCanceled(ctx, retryErr); ErrCode(err) != wantErrCode || !testEqual(err, wantErr) || retries != 0 {
  85. t.Errorf("<err code, err, retries>=\n<%v, %v, %v>, want:\n<%v, %v, %v>", ErrCode(err), err, retries, wantErrCode, wantErr, 0)
  86. }
  87. }
  88. func TestRetryInfo(t *testing.T) {
  89. b, _ := proto.Marshal(&edpb.RetryInfo{
  90. RetryDelay: ptypes.DurationProto(time.Second),
  91. })
  92. trailers := map[string]string{
  93. retryInfoKey: string(b),
  94. }
  95. gotDelay, ok := extractRetryDelay(errRetry(toSpannerErrorWithMetadata(status.Errorf(codes.Aborted, ""), metadata.New(trailers))))
  96. if !ok || !testEqual(time.Second, gotDelay) {
  97. t.Errorf("<ok, retryDelay> = <%t, %v>, want <true, %v>", ok, gotDelay, time.Second)
  98. }
  99. }