util_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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 unversioned
  14. import (
  15. "fmt"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api/errors"
  18. "k8s.io/kubernetes/pkg/api/unversioned"
  19. "k8s.io/kubernetes/pkg/util/wait"
  20. )
  21. func TestRetryOnConflict(t *testing.T) {
  22. opts := wait.Backoff{Factor: 1.0, Steps: 3}
  23. conflictErr := errors.NewConflict(unversioned.GroupResource{Resource: "test"}, "other", nil)
  24. // never returns
  25. err := RetryOnConflict(opts, func() error {
  26. return conflictErr
  27. })
  28. if err != conflictErr {
  29. t.Errorf("unexpected error: %v", err)
  30. }
  31. // returns immediately
  32. i := 0
  33. err = RetryOnConflict(opts, func() error {
  34. i++
  35. return nil
  36. })
  37. if err != nil || i != 1 {
  38. t.Errorf("unexpected error: %v", err)
  39. }
  40. // returns immediately on error
  41. testErr := fmt.Errorf("some other error")
  42. err = RetryOnConflict(opts, func() error {
  43. return testErr
  44. })
  45. if err != testErr {
  46. t.Errorf("unexpected error: %v", err)
  47. }
  48. // keeps retrying
  49. i = 0
  50. err = RetryOnConflict(opts, func() error {
  51. if i < 2 {
  52. i++
  53. return errors.NewConflict(unversioned.GroupResource{Resource: "test"}, "other", nil)
  54. }
  55. return nil
  56. })
  57. if err != nil || i != 2 {
  58. t.Errorf("unexpected error: %v", err)
  59. }
  60. }