| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/network_activity_monitor.h" | 5 #include "net/base/network_activity_monitor.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 7 #include <vector> | 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/port.h" | |
| 11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 namespace test { | 20 namespace test { |
| 20 | 21 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 } // namespace | 95 } // namespace |
| 95 | 96 |
| 96 TEST_F(NetworkActivityMontiorTest, Threading) { | 97 TEST_F(NetworkActivityMontiorTest, Threading) { |
| 97 std::vector<base::Thread*> threads; | 98 std::vector<base::Thread*> threads; |
| 98 for (size_t i = 0; i < 3; ++i) { | 99 for (size_t i = 0; i < 3; ++i) { |
| 99 threads.push_back(new base::Thread(base::UintToString(i))); | 100 threads.push_back(new base::Thread(base::UintToString(i))); |
| 100 ASSERT_TRUE(threads.back()->Start()); | 101 ASSERT_TRUE(threads.back()->Start()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 size_t num_increments = 157; | 104 size_t num_increments = 157; |
| 104 uint64_t bytes_received = GG_UINT64_C(7294954321); | 105 uint64_t bytes_received = UINT64_C(7294954321); |
| 105 uint64_t bytes_sent = GG_UINT64_C(91294998765); | 106 uint64_t bytes_sent = UINT64_C(91294998765); |
| 106 for (size_t i = 0; i < num_increments; ++i) { | 107 for (size_t i = 0; i < num_increments; ++i) { |
| 107 size_t thread_num = i % threads.size(); | 108 size_t thread_num = i % threads.size(); |
| 108 threads[thread_num]->task_runner()->PostTask( | 109 threads[thread_num]->task_runner()->PostTask( |
| 109 FROM_HERE, | 110 FROM_HERE, |
| 110 base::Bind(&IncrementBytesReceived, bytes_received)); | 111 base::Bind(&IncrementBytesReceived, bytes_received)); |
| 111 threads[thread_num]->task_runner()->PostTask( | 112 threads[thread_num]->task_runner()->PostTask( |
| 112 FROM_HERE, | 113 FROM_HERE, |
| 113 base::Bind(&IncrementBytesSent, bytes_sent)); | 114 base::Bind(&IncrementBytesSent, bytes_sent)); |
| 114 threads[thread_num]->task_runner()->PostTask( | 115 threads[thread_num]->task_runner()->PostTask( |
| 115 FROM_HERE, | 116 FROM_HERE, |
| 116 base::Bind(&VerifyBytesSentIsMultipleOf, bytes_sent)); | 117 base::Bind(&VerifyBytesSentIsMultipleOf, bytes_sent)); |
| 117 threads[thread_num]->task_runner()->PostTask( | 118 threads[thread_num]->task_runner()->PostTask( |
| 118 FROM_HERE, | 119 FROM_HERE, |
| 119 base::Bind(&VerifyBytesReceivedIsMultipleOf, bytes_received)); | 120 base::Bind(&VerifyBytesReceivedIsMultipleOf, bytes_received)); |
| 120 } | 121 } |
| 121 | 122 |
| 122 STLDeleteElements(&threads); | 123 STLDeleteElements(&threads); |
| 123 | 124 |
| 124 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance(); | 125 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance(); |
| 125 EXPECT_EQ(num_increments * bytes_received, monitor->GetBytesReceived()); | 126 EXPECT_EQ(num_increments * bytes_received, monitor->GetBytesReceived()); |
| 126 EXPECT_EQ(num_increments * bytes_sent, monitor->GetBytesSent()); | 127 EXPECT_EQ(num_increments * bytes_sent, monitor->GetBytesSent()); |
| 127 } | 128 } |
| 128 | 129 |
| 129 } // namespace test | 130 } // namespace test |
| 130 | 131 |
| 131 } // namespace net | 132 } // namespace net |
| OLD | NEW |