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.
 
 
 

50 lines
1.5 KiB

  1. /*
  2. Copyright 2015 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 gax
  14. import (
  15. "context"
  16. "testing"
  17. "time"
  18. "google.golang.org/grpc/codes"
  19. "google.golang.org/grpc/status"
  20. )
  21. func TestRandomizedDelays(t *testing.T) {
  22. max := 200 * time.Millisecond
  23. settings := []CallOption{
  24. WithRetryCodes([]codes.Code{codes.Unavailable, codes.DeadlineExceeded}),
  25. WithDelayTimeoutSettings(10*time.Millisecond, max, 1.5),
  26. }
  27. deadline := time.Now().Add(1 * time.Second)
  28. ctx, _ := context.WithDeadline(context.Background(), deadline)
  29. var invokeTime time.Time
  30. _ = Invoke(ctx, func(childCtx context.Context) error {
  31. // Keep failing, make sure we never slept more than max (plus a fudge factor)
  32. if !invokeTime.IsZero() {
  33. if got, want := time.Since(invokeTime), max; got > (want + 20*time.Millisecond) {
  34. t.Logf("Slept too long. Got: %v, want: %v", got, max)
  35. }
  36. }
  37. invokeTime = time.Now()
  38. // Workaround for `go vet`: https://github.com/grpc/grpc-go/issues/90
  39. errf := status.Errorf
  40. return errf(codes.Unavailable, "")
  41. }, settings...)
  42. }