retry_policy_test.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "common/retry_policy.h"
  19. #include <gmock/gmock.h>
  20. using namespace hdfs;
  21. TEST(RetryPolicyTest, TestNoRetry) {
  22. NoRetryPolicy policy;
  23. EXPECT_EQ(RetryAction::FAIL, policy.ShouldRetry(Status::Unimplemented(), 0, 0, true).action);
  24. }
  25. TEST(RetryPolicyTest, TestFixedDelay) {
  26. static const uint64_t DELAY = 100;
  27. FixedDelayRetryPolicy policy(DELAY, 10);
  28. // No error
  29. RetryAction result = policy.ShouldRetry(Status::Unimplemented(), 0, 0, true);
  30. EXPECT_EQ(RetryAction::RETRY, result.action);
  31. EXPECT_EQ(DELAY, result.delayMillis);
  32. // Few errors
  33. result = policy.ShouldRetry(Status::Unimplemented(), 2, 2, true);
  34. EXPECT_EQ(RetryAction::RETRY, result.action);
  35. EXPECT_EQ(DELAY, result.delayMillis);
  36. result = policy.ShouldRetry(Status::Unimplemented(), 9, 0, true);
  37. EXPECT_EQ(RetryAction::RETRY, result.action);
  38. EXPECT_EQ(DELAY, result.delayMillis);
  39. // Too many errors
  40. result = policy.ShouldRetry(Status::Unimplemented(), 10, 0, true);
  41. EXPECT_EQ(RetryAction::FAIL, result.action);
  42. EXPECT_TRUE(result.reason.size() > 0); // some error message
  43. result = policy.ShouldRetry(Status::Unimplemented(), 0, 10, true);
  44. EXPECT_EQ(RetryAction::FAIL, result.action);
  45. EXPECT_TRUE(result.reason.size() > 0); // some error message
  46. }
  47. int main(int argc, char *argv[]) {
  48. // The following line must be executed to initialize Google Mock
  49. // (and Google Test) before running the tests.
  50. ::testing::InitGoogleMock(&argc, argv);
  51. return RUN_ALL_TESTS();
  52. }