Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(501)

Unified Diff: remoting/base/rate_counter.cc

Issue 12803008: Add unit tests for sub-components of CaptureScheduler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/base/rate_counter.cc
diff --git a/remoting/base/rate_counter.cc b/remoting/base/rate_counter.cc
index fff9b6cebcd18f3734cf7e5c9ed1c925b36fc1a4..49466fa2209772286fed01d800e3bd0f14d19b5c 100644
--- a/remoting/base/rate_counter.cc
+++ b/remoting/base/rate_counter.cc
@@ -4,6 +4,8 @@
#include "remoting/base/rate_counter.h"
+#include "base/logging.h"
alexeypa (please no reviews) 2013/03/18 19:39:39 Is this header actually used?
Wez 2013/03/19 23:23:14 No; used it during development. It's needed for th
+
namespace remoting {
RateCounter::RateCounter(base::TimeDelta time_window)
@@ -15,24 +17,29 @@ RateCounter::~RateCounter() {
}
void RateCounter::Record(int64 value) {
- base::Time current_time = base::Time::Now();
- Evict(current_time);
+ base::Time current_time = CurrentTime();
base::AutoLock auto_lock(lock_);
+ EvictOldDataPoints(current_time);
sum_ += value;
data_points_.push(std::make_pair(current_time, value));
}
double RateCounter::Rate() {
- Evict(base::Time::Now());
+ base::Time current_time = CurrentTime();
alexeypa (please no reviews) 2013/03/18 19:39:39 Should CurrentTime() be called under the lock?
Wez 2013/03/19 23:23:14 No, I don't think so. This way it captures the ti
base::AutoLock auto_lock(lock_);
+ EvictOldDataPoints(current_time);
return static_cast<double>(base::Time::kMillisecondsPerSecond) * sum_ /
alexeypa (please no reviews) 2013/03/18 19:39:39 nit: Since it is converting to double anyway it wi
Wez 2013/03/19 23:23:14 Done.
time_window_.InMilliseconds();
}
-void RateCounter::Evict(base::Time current_time) {
- base::AutoLock auto_lock(lock_);
+void RateCounter::SetCurrentTimeForTest(base::Time current_time) {
+ current_time_for_test_ = current_time;
+}
+
+void RateCounter::EvictOldDataPoints(base::Time current_time) {
+ lock_.AssertAcquired();
// Remove data points outside of the window.
base::Time window_start = current_time - time_window_;
@@ -46,4 +53,10 @@ void RateCounter::Evict(base::Time current_time) {
}
}
+base::Time RateCounter::CurrentTime() const {
+ if (current_time_for_test_ == base::Time())
+ return base::Time::Now();
+ return current_time_for_test_;
+}
+
} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698