rate_control_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include "rate_control.h"
  16. #include <gtest/gtest.h>
  17. using namespace isc;
  18. using namespace isc::perfdhcp;
  19. /// \brief A class which exposes protected methods and members of the
  20. /// RateControl class (under test).
  21. class NakedRateControl : public RateControl {
  22. public:
  23. /// \brief Default constructor.
  24. NakedRateControl()
  25. : RateControl() {
  26. }
  27. /// \brief Constructor which sets up the rate and aggressivity.
  28. ///
  29. /// \param rate A rate at which messages are sent.
  30. /// \param aggressivity A value of aggressivity. This value controls the
  31. /// maximal number of messages sent in one chunk.
  32. NakedRateControl(const int rate, const int aggressivity)
  33. : RateControl(rate, aggressivity) {
  34. }
  35. using RateControl::currentTime;
  36. using RateControl::updateSendTime;
  37. using RateControl::updateSendDue;
  38. using RateControl::send_due_;
  39. using RateControl::last_sent_;
  40. using RateControl::late_sent_;
  41. };
  42. // Test default constructor.
  43. TEST(RateControl, constructorDefault) {
  44. NakedRateControl rc;
  45. EXPECT_EQ(1, rc.getAggressivity());
  46. EXPECT_EQ(0, rc.getRate());
  47. EXPECT_FALSE(rc.getDue().is_not_a_date_time());
  48. EXPECT_FALSE(rc.last_sent_.is_not_a_date_time());
  49. EXPECT_FALSE(rc.isLateSent());
  50. }
  51. // Test the constructor which sets the rate and aggressivity.
  52. TEST(RateControl, constructor) {
  53. // Call the constructor and verify that it sets the appropriate
  54. // values.
  55. NakedRateControl rc1(3, 2);
  56. EXPECT_EQ(2, rc1.getAggressivity());
  57. EXPECT_EQ(3, rc1.getRate());
  58. EXPECT_FALSE(rc1.getDue().is_not_a_date_time());
  59. EXPECT_FALSE(rc1.last_sent_.is_not_a_date_time());
  60. EXPECT_FALSE(rc1.isLateSent());
  61. // Call the constructor again and make sure that different values
  62. // will be set correctly.
  63. NakedRateControl rc2(5, 6);
  64. EXPECT_EQ(6, rc2.getAggressivity());
  65. EXPECT_EQ(5, rc2.getRate());
  66. EXPECT_FALSE(rc2.getDue().is_not_a_date_time());
  67. EXPECT_FALSE(rc2.last_sent_.is_not_a_date_time());
  68. EXPECT_FALSE(rc2.isLateSent());
  69. // The 0 value of aggressivity < 1 is not acceptable.
  70. EXPECT_THROW(RateControl(3, 0), isc::BadValue);
  71. // The negative value of rate is not acceptable.
  72. EXPECT_THROW(RateControl(-1, 3), isc::BadValue);
  73. }
  74. // Check the aggressivity accessor.
  75. TEST(RateControl, getAggressivity) {
  76. RateControl rc;
  77. ASSERT_EQ(1, rc.getAggressivity());
  78. rc.setAggressivity(5);
  79. ASSERT_EQ(5, rc.getAggressivity());
  80. rc.setAggressivity(10);
  81. EXPECT_EQ(10, rc.getAggressivity());
  82. }
  83. // Check the due time accessor.
  84. TEST(RateControl, getDue) {
  85. NakedRateControl rc;
  86. ASSERT_FALSE(rc.getDue().is_not_a_date_time());
  87. rc.send_due_ = NakedRateControl::currentTime();
  88. EXPECT_TRUE(NakedRateControl::currentTime() >= rc.getDue());
  89. rc.send_due_ = NakedRateControl::currentTime() +
  90. boost::posix_time::seconds(10);
  91. EXPECT_TRUE(NakedRateControl::currentTime() < rc.getDue());
  92. }
  93. // Check the rate accessor.
  94. TEST(RateControl, getRate) {
  95. RateControl rc;
  96. ASSERT_EQ(0, rc.getRate());
  97. rc.setRate(5);
  98. ASSERT_EQ(5, rc.getRate());
  99. rc.setRate(10);
  100. EXPECT_EQ(10, rc.getRate());
  101. }
  102. // Check if late send flag accessor.
  103. TEST(RateControl, isLateSent) {
  104. NakedRateControl rc;
  105. ASSERT_FALSE(rc.isLateSent());
  106. rc.late_sent_ = true;
  107. EXPECT_TRUE(rc.isLateSent());
  108. }
  109. // Check that the function returns the number of messages to be sent "now"
  110. // correctly.
  111. // @todo Possibly extend this test to cover more complex scenarios. Note that
  112. // it is quite hard to fully test this function as its behaviour strongly
  113. // depends on time.
  114. TEST(RateControl, getOutboundMessageCount) {
  115. NakedRateControl rc1(1000, 1);
  116. // Set the timestamp of the last sent message well to the past.
  117. // The resulting due time will be in the past too.
  118. rc1.last_sent_ =
  119. NakedRateControl::currentTime() - boost::posix_time::seconds(5);
  120. // The number of messages to be sent must be greater than 0.
  121. uint64_t count;
  122. ASSERT_NO_THROW(count = rc1.getOutboundMessageCount());
  123. EXPECT_GT(count, 0);
  124. // Now, don't specify the rate. In this case the aggressivity dictates
  125. // how many messages to send.
  126. NakedRateControl rc2(0, 3);
  127. rc2.last_sent_ =
  128. NakedRateControl::currentTime() - boost::posix_time::seconds(5);
  129. ASSERT_NO_THROW(count = rc2.getOutboundMessageCount());
  130. EXPECT_EQ(3, count);
  131. // Specify the rate and set the timestamp of the last sent message well
  132. // to the future. If the resulting due time is well in the future too,
  133. // the number of messages to be sent must be 0.
  134. NakedRateControl rc3(10, 3);
  135. rc3.last_sent_ = NakedRateControl::currentTime() +
  136. boost::posix_time::seconds(5);
  137. ASSERT_NO_THROW(count = rc3.getOutboundMessageCount());
  138. EXPECT_EQ(0, count);
  139. }
  140. // Test the aggressivity modifier for valid and invalid values.
  141. TEST(RateControl, setAggressivity) {
  142. NakedRateControl rc;
  143. ASSERT_NO_THROW(rc.setAggressivity(1));
  144. EXPECT_THROW(rc.setAggressivity(0), isc::BadValue);
  145. EXPECT_THROW(rc.setAggressivity(-1), isc::BadValue);
  146. }
  147. // Test the rate modifier for valid and invalid rate values.
  148. TEST(RateControl, setRate) {
  149. NakedRateControl rc;
  150. EXPECT_NO_THROW(rc.setRate(1));
  151. EXPECT_NO_THROW(rc.setRate(0));
  152. EXPECT_THROW(rc.setRate(-1), isc::BadValue);
  153. }
  154. // Test the function which calculates the due time to send next set of
  155. // messages.
  156. TEST(RateControl, updateSendDue) {
  157. NakedRateControl rc;
  158. // Set the send due timestamp to the value which is well in the future.
  159. // If we don't hit the due time, the function should not modify the
  160. // due time.
  161. rc.send_due_ =
  162. NakedRateControl::currentTime() + boost::posix_time::seconds(10);
  163. boost::posix_time::ptime last_send_due = rc.send_due_;
  164. ASSERT_NO_THROW(rc.updateSendDue());
  165. EXPECT_TRUE(rc.send_due_ == last_send_due);
  166. // Set the due time to the value which is already behind.
  167. rc.send_due_ =
  168. NakedRateControl::currentTime() - boost::posix_time::seconds(10);
  169. last_send_due = rc.send_due_;
  170. ASSERT_NO_THROW(rc.updateSendDue());
  171. // The value should be modified to the new value.
  172. EXPECT_TRUE(rc.send_due_ > last_send_due);
  173. }
  174. // Test that the message send time is updated to the current time.
  175. TEST(RateControl, updateSendTime) {
  176. NakedRateControl rc;
  177. // Set the timestamp to the future.
  178. rc.last_sent_ =
  179. NakedRateControl::currentTime() + boost::posix_time::seconds(5);
  180. rc.updateSendTime();
  181. // Updated timestamp should be set to now or to the past.
  182. EXPECT_TRUE(rc.last_sent_ <= NakedRateControl::currentTime());
  183. }