Parcourir la source

[master] Merge branch 'trac3729'

Stephen Morris il y a 10 ans
Parent
commit
3e0882c3a5
2 fichiers modifiés avec 41 ajouts et 18 suppressions
  1. 6 5
      src/bin/perfdhcp/rate_control.cc
  2. 35 13
      src/bin/perfdhcp/tests/rate_control_unittest.cc

+ 6 - 5
src/bin/perfdhcp/rate_control.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013, 2015 Internet Systems Consortium, Inc. ("ISC")
 //
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
 // purpose with or without fee is hereby granted, provided that the above
@@ -51,15 +51,16 @@ RateControl::getOutboundMessageCount() {
         // Reset number of exchanges.
         // Reset number of exchanges.
         uint64_t due_exchanges = 0;
         uint64_t due_exchanges = 0;
         // If rate is specified from the command line we have to
         // If rate is specified from the command line we have to
-        // synchornize with it.
+        // synchronize with it.
         if (getRate() != 0) {
         if (getRate() != 0) {
             time_period period(send_due_, now);
             time_period period(send_due_, now);
             time_duration duration = period.length();
             time_duration duration = period.length();
             // due_factor indicates the number of seconds that
             // due_factor indicates the number of seconds that
             // sending next chunk of packets will take.
             // sending next chunk of packets will take.
-            double due_factor = duration.fractional_seconds() /
-                time_duration::ticks_per_second();
-            due_factor += duration.total_seconds();
+            double due_factor =
+                static_cast<double>(duration.fractional_seconds()) /
+                static_cast<double>(time_duration::ticks_per_second());
+            due_factor += static_cast<double>(duration.total_seconds());
             // Multiplying due_factor by expected rate gives the number
             // Multiplying due_factor by expected rate gives the number
             // of exchanges to be initiated.
             // of exchanges to be initiated.
             due_exchanges = static_cast<uint64_t>(due_factor * getRate());
             due_exchanges = static_cast<uint64_t>(due_factor * getRate());

+ 35 - 13
src/bin/perfdhcp/tests/rate_control_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013, 2015 Internet Systems Consortium, Inc. ("ISC")
 //
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
 // purpose with or without fee is hereby granted, provided that the above
@@ -129,29 +129,51 @@ TEST(RateControl, isLateSent) {
 // it is quite hard to fully test this function as its behaviour strongly
 // it is quite hard to fully test this function as its behaviour strongly
 // depends on time.
 // depends on time.
 TEST(RateControl, getOutboundMessageCount) {
 TEST(RateControl, getOutboundMessageCount) {
-    NakedRateControl rc1(1000, 1);
-    // Set the timestamp of the last sent message well to the past.
-    // The resulting due time will be in the past too.
+    // Test that the number of outbound messages is correctly defined by the
+    // rate.  The agressivity is set high enough so that it will not restrict
+    // the calculated count.
+    NakedRateControl rc1(1000, 1000000);
+    // Set the timestamp of the last sent message well to the past. The
+    // resulting due time will be in the past too.  A fractional number of
+    // seconds is used to check the floating point arithmetic performed inside
+    // the RateControl class.
     rc1.last_sent_ =
     rc1.last_sent_ =
-        NakedRateControl::currentTime() - boost::posix_time::seconds(5);
-    // The number of messages to be sent must be greater than 0.
+        NakedRateControl::currentTime() - boost::posix_time::seconds(5) -
+        boost::posix_time::milliseconds(250);
+    // The number of messages to be sent must be roughly equal to the time
+    // between the last sent message and the current time multiplied by the
+    // rate.  ("Roughly", as current time is advancing, so the actual interval
+    // when the calcuation is made may be different from the interval set.)  The
+    // margin in this test is reasonably generous, allowing for a timing error
+    // of around 10ms.
     uint64_t count;
     uint64_t count;
     ASSERT_NO_THROW(count = rc1.getOutboundMessageCount());
     ASSERT_NO_THROW(count = rc1.getOutboundMessageCount());
-    EXPECT_GT(count, 0);
+    EXPECT_TRUE((count >= 5240) && (count <= 5260)) <<
+        "count is " << count << ", expected range 5240-5260";
+
+    // Check that the agressivity limits the count of messages.
+    NakedRateControl rc2(1000, 3500);
+    rc2.last_sent_ =
+        NakedRateControl::currentTime() - boost::posix_time::seconds(5) -
+        boost::posix_time::milliseconds(250);
+    ASSERT_NO_THROW(count = rc2.getOutboundMessageCount());
+    EXPECT_EQ(3500, count);
+
     // Now, don't specify the rate. In this case the aggressivity dictates
     // Now, don't specify the rate. In this case the aggressivity dictates
     // how many messages to send.
     // how many messages to send.
-    NakedRateControl rc2(0, 3);
-    rc2.last_sent_ =
+    NakedRateControl rc3(0, 3);
+    rc3.last_sent_ =
         NakedRateControl::currentTime() - boost::posix_time::seconds(5);
         NakedRateControl::currentTime() - boost::posix_time::seconds(5);
-    ASSERT_NO_THROW(count = rc2.getOutboundMessageCount());
+    ASSERT_NO_THROW(count = rc3.getOutboundMessageCount());
     EXPECT_EQ(3, count);
     EXPECT_EQ(3, count);
+
     // Specify the rate and set the timestamp of the last sent message well
     // Specify the rate and set the timestamp of the last sent message well
     // to the future. If the resulting due time is well in the future too,
     // to the future. If the resulting due time is well in the future too,
     // the number of messages to be sent must be 0.
     // the number of messages to be sent must be 0.
-    NakedRateControl rc3(10, 3);
-    rc3.last_sent_ = NakedRateControl::currentTime() +
+    NakedRateControl rc4(10, 3);
+    rc4.last_sent_ = NakedRateControl::currentTime() +
         boost::posix_time::seconds(5);
         boost::posix_time::seconds(5);
-    ASSERT_NO_THROW(count = rc3.getOutboundMessageCount());
+    ASSERT_NO_THROW(count = rc4.getOutboundMessageCount());
     EXPECT_EQ(0, count);
     EXPECT_EQ(0, count);
 
 
 }
 }