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

Unified Diff: net/base/network_activity_monitor_unittest.cc

Issue 726673002: Add a new NetworkActivityMonitor to track network activity (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add Reset method Created 6 years, 1 month 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: net/base/network_activity_monitor_unittest.cc
diff --git a/net/base/network_activity_monitor_unittest.cc b/net/base/network_activity_monitor_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4ffeffca911dcf1a5756e1e1c9f1651724a687fe
--- /dev/null
+++ b/net/base/network_activity_monitor_unittest.cc
@@ -0,0 +1,117 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/network_activity_monitor.h"
+
+#include "base/bind.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/threading/thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace test {
+
+class NetworkActivityMonitorPeer {
+ public:
+ static void ResetMonitor() {
+ NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
+ monitor->bytes_sent_ = 0;
eroman 2014/11/14 20:57:04 It might be possible for TSAN to bark about this,
Ryan Hamilton 2014/11/14 22:18:17 Done.
+ monitor->bytes_received_ = 0;
+ monitor->last_received_ticks_ = base::TimeTicks();
+ monitor->last_write_ticks_ = base::TimeTicks();
+ }
+};
+
+class NetworkActivityMontiorTest : public testing::Test {
+ public:
+ NetworkActivityMontiorTest()
+ : monitor_(NetworkActivityMonitor::GetInstance()) {
+ NetworkActivityMonitorPeer::ResetMonitor();
+ }
+ NetworkActivityMonitor* monitor_;
+};
+
+TEST_F(NetworkActivityMontiorTest, GetInstance) {
+ NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
+ EXPECT_TRUE(monitor != NULL);
+ EXPECT_TRUE(monitor == NetworkActivityMonitor::GetInstance());
+}
+
+TEST_F(NetworkActivityMontiorTest, BytesReceived) {
+ base::TimeTicks start = base::TimeTicks::Now();
+ EXPECT_EQ(0u, monitor_->GetBytesReceived());
+ uint64_t bytes = 12345;
+ monitor_->IncrementBytesReceived(bytes);
+ EXPECT_EQ(bytes, monitor_->GetBytesReceived());
+ base::TimeDelta delta = monitor_->GetTimeSinceLastReceived();
+ EXPECT_LE(base::TimeDelta(), delta);
+ EXPECT_GE(base::TimeTicks::Now() - start, delta);
+}
+
+TEST_F(NetworkActivityMontiorTest, BytesSent) {
+ base::TimeTicks start = base::TimeTicks::Now();
+ EXPECT_EQ(0u, monitor_->GetBytesSent());
+ uint64_t bytes = 12345;
+ monitor_->IncrementBytesSent(bytes);
+ EXPECT_EQ(bytes, monitor_->GetBytesSent());
+ base::TimeDelta delta = monitor_->GetTimeSinceLastSent();
+ EXPECT_LE(base::TimeDelta(), delta);
+ EXPECT_GE(base::TimeTicks::Now() - start, delta);
+}
+
+class Verifier {
+ public:
+ void VerifyBytesReceivedIsMultipleOf(NetworkActivityMonitor* monitor,
+ uint64_t bytes) {
+ EXPECT_EQ(0u, monitor->GetBytesReceived() % bytes);
+ }
+
+ void VerifyBytesSentIsMultipleOf(NetworkActivityMonitor* monitor,
+ uint64_t bytes) {
+ EXPECT_EQ(0u, monitor->GetBytesSent() % bytes);
+ }
+};
+
+TEST_F(NetworkActivityMontiorTest, Threading) {
+ base::Thread* threads[3];
+ for (size_t i = 0; i < arraysize(threads); ++i) {
+ threads[i] = new base::Thread(base::UintToString(i));
+ threads[i]->Start();
eroman 2014/11/14 20:57:04 nit: I suggest wrapping this inside an ASSERT_TRUE
Ryan Hamilton 2014/11/14 22:18:17 Done.
+ }
+
+ Verifier verifier;
+
+ size_t num_increments = 157;
eroman 2014/11/14 20:57:04 Fancy... i like it!
Ryan Hamilton 2014/11/14 22:18:17 Thanks!
+ uint64_t bytes_received = 7294954321u;
eroman 2014/11/14 20:57:04 [no action necessary] Strictly speaking you probab
Ryan Hamilton 2014/11/14 22:18:17 Oh! Thanks for the reminder. I actually need to us
+ uint64_t bytes_sent = 91294998765u;
+ for (size_t i = 0; i < num_increments; ++i) {
+ threads[i % arraysize(threads)]->task_runner()->PostTask(
+ FROM_HERE, base::Bind(&NetworkActivityMonitor::IncrementBytesReceived,
+ base::Unretained(monitor_), bytes_received));
+ threads[i % arraysize(threads)]->task_runner()->PostTask(
+ FROM_HERE, base::Bind(&NetworkActivityMonitor::IncrementBytesSent,
+ base::Unretained(monitor_), bytes_sent));
+ threads[i % arraysize(threads)]->task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(&Verifier::VerifyBytesSentIsMultipleOf,
eroman 2014/11/14 20:57:04 The Verifier class feels it could just be static m
Ryan Hamilton 2014/11/14 22:18:17 AH! For some reason I forgot that this as an optio
+ base::Unretained(&verifier), monitor_, bytes_sent));
+ threads[i % arraysize(threads)]->task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(&Verifier::VerifyBytesReceivedIsMultipleOf,
+ base::Unretained(&verifier), monitor_, bytes_received));
+ }
+
+ for (size_t i = 0; i < arraysize(threads); ++i) {
+ threads[i]->Stop();
eroman 2014/11/14 20:57:04 Calling Stop() is not necessary, the dtor handles
Ryan Hamilton 2014/11/14 22:18:17 Done.
+ delete threads[i];
eroman 2014/11/14 20:57:04 [optional] If you wanted to be fancy, could use ST
Ryan Hamilton 2014/11/14 22:18:17 I like it! Done.
+ }
+
+ EXPECT_EQ(num_increments * bytes_received, monitor_->GetBytesReceived());
+ EXPECT_EQ(num_increments * bytes_sent, monitor_->GetBytesSent());
eroman 2014/11/14 20:57:04 Overall I like this test, thanks!
Ryan Hamilton 2014/11/14 22:18:17 Thank you very much for encouraging me to write it
+}
+
+} // namespace test
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698