BackoffSequenceTest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #include "netdutils/BackoffSequence.h"
  18. namespace android {
  19. namespace netdutils {
  20. TEST(BackoffSequence, defaults) {
  21. BackoffSequence<uint32_t> backoff;
  22. EXPECT_TRUE(backoff.hasNextTimeout());
  23. EXPECT_EQ(0x00000001U, backoff.getNextTimeout());
  24. EXPECT_EQ(0x00000002U, backoff.getNextTimeout());
  25. EXPECT_EQ(0x00000004U, backoff.getNextTimeout());
  26. EXPECT_EQ(0x00000008U, backoff.getNextTimeout());
  27. EXPECT_EQ(0x00000010U, backoff.getNextTimeout());
  28. EXPECT_EQ(0x00000020U, backoff.getNextTimeout());
  29. EXPECT_EQ(0x00000040U, backoff.getNextTimeout());
  30. EXPECT_EQ(0x00000080U, backoff.getNextTimeout());
  31. EXPECT_EQ(0x00000100U, backoff.getNextTimeout());
  32. EXPECT_EQ(0x00000200U, backoff.getNextTimeout());
  33. EXPECT_EQ(0x00000400U, backoff.getNextTimeout());
  34. EXPECT_EQ(0x00000800U, backoff.getNextTimeout());
  35. EXPECT_EQ(0x00001000U, backoff.getNextTimeout());
  36. EXPECT_EQ(0x00002000U, backoff.getNextTimeout());
  37. EXPECT_EQ(0x00004000U, backoff.getNextTimeout());
  38. EXPECT_EQ(0x00008000U, backoff.getNextTimeout());
  39. EXPECT_EQ(0x00010000U, backoff.getNextTimeout());
  40. EXPECT_EQ(0x00020000U, backoff.getNextTimeout());
  41. EXPECT_EQ(0x00040000U, backoff.getNextTimeout());
  42. EXPECT_EQ(0x00080000U, backoff.getNextTimeout());
  43. EXPECT_EQ(0x00100000U, backoff.getNextTimeout());
  44. EXPECT_EQ(0x00200000U, backoff.getNextTimeout());
  45. EXPECT_EQ(0x00400000U, backoff.getNextTimeout());
  46. EXPECT_EQ(0x00800000U, backoff.getNextTimeout());
  47. EXPECT_EQ(0x01000000U, backoff.getNextTimeout());
  48. EXPECT_EQ(0x02000000U, backoff.getNextTimeout());
  49. EXPECT_EQ(0x04000000U, backoff.getNextTimeout());
  50. EXPECT_EQ(0x08000000U, backoff.getNextTimeout());
  51. EXPECT_EQ(0x10000000U, backoff.getNextTimeout());
  52. EXPECT_EQ(0x20000000U, backoff.getNextTimeout());
  53. EXPECT_EQ(0x40000000U, backoff.getNextTimeout());
  54. EXPECT_EQ(0x80000000U, backoff.getNextTimeout());
  55. // Maxes out, and stays there, ad infinitum.
  56. for (int i = 0; i < 10; i++) {
  57. EXPECT_TRUE(backoff.hasNextTimeout());
  58. EXPECT_EQ(0xffffffffU, backoff.getNextTimeout());
  59. }
  60. }
  61. TEST(BackoffSequence, backoffToOncePerHour) {
  62. auto backoff = BackoffSequence<uint32_t>::Builder()
  63. .withInitialRetransmissionTime(1)
  64. .withMaximumRetransmissionTime(3600)
  65. .build();
  66. EXPECT_TRUE(backoff.hasNextTimeout());
  67. EXPECT_EQ(0x00000001U, backoff.getNextTimeout());
  68. EXPECT_EQ(0x00000002U, backoff.getNextTimeout());
  69. EXPECT_EQ(0x00000004U, backoff.getNextTimeout());
  70. EXPECT_EQ(0x00000008U, backoff.getNextTimeout());
  71. EXPECT_EQ(0x00000010U, backoff.getNextTimeout());
  72. EXPECT_EQ(0x00000020U, backoff.getNextTimeout());
  73. EXPECT_EQ(0x00000040U, backoff.getNextTimeout());
  74. EXPECT_EQ(0x00000080U, backoff.getNextTimeout());
  75. EXPECT_EQ(0x00000100U, backoff.getNextTimeout());
  76. EXPECT_EQ(0x00000200U, backoff.getNextTimeout());
  77. EXPECT_EQ(0x00000400U, backoff.getNextTimeout());
  78. EXPECT_EQ(0x00000800U, backoff.getNextTimeout());
  79. // Maxes out, and stays there, ad infinitum.
  80. for (int i = 0; i < 10; i++) {
  81. EXPECT_TRUE(backoff.hasNextTimeout());
  82. EXPECT_EQ(3600U, backoff.getNextTimeout());
  83. }
  84. }
  85. TEST(BackoffSequence, simpleMaxRetransCount) {
  86. auto backoff = BackoffSequence<uint32_t>::Builder()
  87. .withInitialRetransmissionTime(3)
  88. .withMaximumRetransmissionCount(7)
  89. .build();
  90. EXPECT_TRUE(backoff.hasNextTimeout());
  91. EXPECT_EQ(0x00000003U, backoff.getNextTimeout());
  92. EXPECT_EQ(0x00000006U, backoff.getNextTimeout());
  93. EXPECT_EQ(0x0000000cU, backoff.getNextTimeout());
  94. EXPECT_EQ(0x00000018U, backoff.getNextTimeout());
  95. EXPECT_EQ(0x00000030U, backoff.getNextTimeout());
  96. EXPECT_EQ(0x00000060U, backoff.getNextTimeout());
  97. EXPECT_EQ(0x000000c0U, backoff.getNextTimeout());
  98. for (int i = 0; i < 10; i++) {
  99. EXPECT_FALSE(backoff.hasNextTimeout());
  100. EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
  101. }
  102. }
  103. TEST(BackoffSequence, simpleMaxDuration) {
  104. auto backoff = BackoffSequence<int>::Builder()
  105. .withInitialRetransmissionTime(3)
  106. .withMaximumRetransmissionDuration(7)
  107. .withEndOfSequenceIndicator(-1)
  108. .build();
  109. EXPECT_TRUE(backoff.hasNextTimeout());
  110. EXPECT_EQ(0x00000003, backoff.getNextTimeout());
  111. EXPECT_EQ(0x00000004, backoff.getNextTimeout());
  112. for (int i = 0; i < 10; i++) {
  113. EXPECT_FALSE(backoff.hasNextTimeout());
  114. EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
  115. EXPECT_EQ(-1, backoff.getNextTimeout());
  116. }
  117. }
  118. TEST(PathologicalBackoffSequence, ZeroInitialRetransTime) {
  119. auto backoff = BackoffSequence<std::chrono::seconds>::Builder()
  120. .withInitialRetransmissionTime(std::chrono::seconds(0))
  121. .build();
  122. for (int i = 0; i < 10; i++) {
  123. // TODO: Decide whether this needs fixing, and how.
  124. EXPECT_TRUE(backoff.hasNextTimeout());
  125. EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
  126. }
  127. }
  128. TEST(PathologicalBackoffSequence, MaxRetransDurationGreaterThanInitialRetransTime) {
  129. auto backoff = BackoffSequence<std::chrono::milliseconds>::Builder()
  130. .withInitialRetransmissionTime(std::chrono::milliseconds(5))
  131. .withMaximumRetransmissionDuration(std::chrono::milliseconds(3))
  132. .build();
  133. EXPECT_EQ(std::chrono::milliseconds(3), backoff.getNextTimeout());
  134. for (int i = 0; i < 10; i++) {
  135. EXPECT_FALSE(backoff.hasNextTimeout());
  136. EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
  137. }
  138. }
  139. TEST(PathologicalBackoffSequence, MaxRetransDurationEqualsInitialRetransTime) {
  140. auto backoff = BackoffSequence<std::chrono::hours>::Builder()
  141. .withInitialRetransmissionTime(std::chrono::hours(5))
  142. .withMaximumRetransmissionDuration(std::chrono::hours(5))
  143. .build();
  144. EXPECT_EQ(std::chrono::hours(5), backoff.getNextTimeout());
  145. for (int i = 0; i < 10; i++) {
  146. EXPECT_FALSE(backoff.hasNextTimeout());
  147. EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
  148. }
  149. }
  150. TEST(PathologicalBackoffSequence, MaxRetransTimeAndDurationGreaterThanInitialRetransTime) {
  151. auto backoff = BackoffSequence<std::chrono::nanoseconds>::Builder()
  152. .withInitialRetransmissionTime(std::chrono::nanoseconds(7))
  153. .withMaximumRetransmissionTime(std::chrono::nanoseconds(3))
  154. .withMaximumRetransmissionDuration(std::chrono::nanoseconds(5))
  155. .build();
  156. EXPECT_EQ(std::chrono::nanoseconds(3), backoff.getNextTimeout());
  157. EXPECT_EQ(std::chrono::nanoseconds(2), backoff.getNextTimeout());
  158. for (int i = 0; i < 10; i++) {
  159. EXPECT_FALSE(backoff.hasNextTimeout());
  160. EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
  161. }
  162. }
  163. } // namespace netdutils
  164. } // namespace android