Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / base / network_activity_monitor_unittest.cc
blob78b452117ec54ae769f300877e6dc228b1e6e2cb
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.
5 #include "net/base/network_activity_monitor.h"
7 #include <stdint.h>
8 #include <vector>
10 #include "base/bind.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"
17 namespace net {
19 namespace test {
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();
34 class NetworkActivityMontiorTest : public testing::Test {
35 public:
36 NetworkActivityMontiorTest() {
37 NetworkActivityMonitorPeer::ResetMonitor();
41 TEST_F(NetworkActivityMontiorTest, GetInstance) {
42 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
43 EXPECT_TRUE(monitor != NULL);
44 EXPECT_TRUE(monitor == NetworkActivityMonitor::GetInstance());
47 TEST_F(NetworkActivityMontiorTest, BytesReceived) {
48 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
50 EXPECT_EQ(0u, monitor->GetBytesReceived());
52 base::TimeTicks start = base::TimeTicks::Now();
53 uint64_t bytes = 12345;
54 monitor->IncrementBytesReceived(bytes);
55 EXPECT_EQ(bytes, monitor->GetBytesReceived());
56 base::TimeDelta delta = monitor->GetTimeSinceLastReceived();
57 EXPECT_LE(base::TimeDelta(), delta);
58 EXPECT_GE(base::TimeTicks::Now() - start, delta);
61 TEST_F(NetworkActivityMontiorTest, BytesSent) {
62 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
64 EXPECT_EQ(0u, monitor->GetBytesSent());
66 base::TimeTicks start = base::TimeTicks::Now();
67 uint64_t bytes = 12345;
68 monitor->IncrementBytesSent(bytes);
69 EXPECT_EQ(bytes, monitor->GetBytesSent());
70 base::TimeDelta delta = monitor->GetTimeSinceLastSent();
71 EXPECT_LE(base::TimeDelta(), delta);
72 EXPECT_GE(base::TimeTicks::Now() - start, delta);
75 namespace {
77 void VerifyBytesReceivedIsMultipleOf(uint64_t bytes) {
78 EXPECT_EQ(0u,
79 NetworkActivityMonitor::GetInstance()->GetBytesReceived() % bytes);
82 void VerifyBytesSentIsMultipleOf(uint64_t bytes) {
83 EXPECT_EQ(0u, NetworkActivityMonitor::GetInstance()->GetBytesSent() % bytes);
86 void IncrementBytesReceived(uint64_t bytes) {
87 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(bytes);
90 void IncrementBytesSent(uint64_t bytes) {
91 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(bytes);
94 } // namespace
96 TEST_F(NetworkActivityMontiorTest, Threading) {
97 std::vector<base::Thread*> threads;
98 for (size_t i = 0; i < 3; ++i) {
99 threads.push_back(new base::Thread(base::SizeTToString(i)));
100 ASSERT_TRUE(threads.back()->Start());
103 size_t num_increments = 157;
104 uint64_t bytes_received = UINT64_C(7294954321);
105 uint64_t bytes_sent = UINT64_C(91294998765);
106 for (size_t i = 0; i < num_increments; ++i) {
107 size_t thread_num = i % threads.size();
108 threads[thread_num]->task_runner()->PostTask(
109 FROM_HERE,
110 base::Bind(&IncrementBytesReceived, bytes_received));
111 threads[thread_num]->task_runner()->PostTask(
112 FROM_HERE,
113 base::Bind(&IncrementBytesSent, bytes_sent));
114 threads[thread_num]->task_runner()->PostTask(
115 FROM_HERE,
116 base::Bind(&VerifyBytesSentIsMultipleOf, bytes_sent));
117 threads[thread_num]->task_runner()->PostTask(
118 FROM_HERE,
119 base::Bind(&VerifyBytesReceivedIsMultipleOf, bytes_received));
122 STLDeleteElements(&threads);
124 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance();
125 EXPECT_EQ(num_increments * bytes_received, monitor->GetBytesReceived());
126 EXPECT_EQ(num_increments * bytes_sent, monitor->GetBytesSent());
129 } // namespace test
131 } // namespace net