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

Side by Side 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: fix final comments 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 unified diff | Download patch
« no previous file with comments | « net/base/network_activity_monitor.cc ('k') | net/net.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/network_activity_monitor.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/port.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace net {
18
19 namespace test {
20
21 class NetworkActivityMonitorPeer {
22 public:
23 static void ResetMonitor() {
24 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
25 base::AutoLock lock(monitor->lock_);
26 monitor->bytes_sent_ = 0;
27 monitor->bytes_received_ = 0;
28 monitor->last_received_ticks_ = base::TimeTicks();
29 monitor->last_sent_ticks_ = base::TimeTicks();
30 }
31 };
32
33 class NetworkActivityMontiorTest : public testing::Test {
34 public:
35 NetworkActivityMontiorTest()
36 : monitor_(NetworkActivityMonitor::GetInstance()) {
37 NetworkActivityMonitorPeer::ResetMonitor();
38 }
39 NetworkActivityMonitor* monitor_;
eroman 2014/11/14 22:36:22 Consider removing this, since it looks weird that
Ryan Hamilton 2014/11/14 22:52:41 Good idea. I thought about doing that but was too
40 };
41
42 TEST_F(NetworkActivityMontiorTest, GetInstance) {
43 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
44 EXPECT_TRUE(monitor != NULL);
45 EXPECT_TRUE(monitor == NetworkActivityMonitor::GetInstance());
46 }
47
48 TEST_F(NetworkActivityMontiorTest, BytesReceived) {
49 base::TimeTicks start = base::TimeTicks::Now();
50 EXPECT_EQ(0u, monitor_->GetBytesReceived());
51 uint64_t bytes = 12345;
52 monitor_->IncrementBytesReceived(bytes);
53 EXPECT_EQ(bytes, monitor_->GetBytesReceived());
54 base::TimeDelta delta = monitor_->GetTimeSinceLastReceived();
55 EXPECT_LE(base::TimeDelta(), delta);
56 EXPECT_GE(base::TimeTicks::Now() - start, delta);
57 }
58
59 TEST_F(NetworkActivityMontiorTest, BytesSent) {
60 base::TimeTicks start = base::TimeTicks::Now();
61 EXPECT_EQ(0u, monitor_->GetBytesSent());
62 uint64_t bytes = 12345;
63 monitor_->IncrementBytesSent(bytes);
64 EXPECT_EQ(bytes, monitor_->GetBytesSent());
65 base::TimeDelta delta = monitor_->GetTimeSinceLastSent();
66 EXPECT_LE(base::TimeDelta(), delta);
67 EXPECT_GE(base::TimeTicks::Now() - start, delta);
68 }
69
70 namespace {
71
72 void VerifyBytesReceivedIsMultipleOf(uint64_t bytes) {
73 EXPECT_EQ(0u,
74 NetworkActivityMonitor::GetInstance()->GetBytesReceived() % bytes);
75 }
76
77 void VerifyBytesSentIsMultipleOf(uint64_t bytes) {
78 EXPECT_EQ(0u, NetworkActivityMonitor::GetInstance()->GetBytesSent() % bytes);
79 }
80
81 void IncrementBytesReceived(uint64_t bytes) {
82 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(bytes);
83 }
84
85 void IncrementBytesSent(uint64_t bytes) {
86 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(bytes);
87 }
88
89 } // namespace
90
91 TEST_F(NetworkActivityMontiorTest, Threading) {
92 std::vector<base::Thread*> threads;
93 for (size_t i = 0; i < 3; ++i) {
94 threads.push_back(new base::Thread(base::UintToString(i)));
95 ASSERT_TRUE(threads.back()->Start());
96 }
97
98 size_t num_increments = 157;
99 uint64_t bytes_received = GG_UINT64_C(7294954321);
100 uint64_t bytes_sent = GG_UINT64_C(91294998765);
101 for (size_t i = 0; i < num_increments; ++i) {
102 size_t thread_num = i % threads.size();
103 threads[thread_num]->task_runner()->PostTask(
104 FROM_HERE,
105 base::Bind(&IncrementBytesReceived, bytes_received));
106 threads[thread_num]->task_runner()->PostTask(
107 FROM_HERE,
108 base::Bind(&IncrementBytesSent, bytes_sent));
109 threads[thread_num]->task_runner()->PostTask(
110 FROM_HERE,
111 base::Bind(&VerifyBytesSentIsMultipleOf, bytes_sent));
112 threads[thread_num]->task_runner()->PostTask(
113 FROM_HERE,
114 base::Bind(&VerifyBytesReceivedIsMultipleOf, bytes_received));
115 }
116
117 STLDeleteElements(&threads);
118
119 EXPECT_EQ(num_increments * bytes_received, monitor_->GetBytesReceived());
120 EXPECT_EQ(num_increments * bytes_sent, monitor_->GetBytesSent());
121 }
122
123 } // namespace test
124
125 } // namespace net
OLDNEW
« no previous file with comments | « net/base/network_activity_monitor.cc ('k') | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698