OLD | NEW |
---|---|
(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 "base/bind.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/threading/thread.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace net { | |
13 | |
14 namespace test { | |
15 | |
16 class NetworkActivityMonitorPeer { | |
17 public: | |
18 static void ResetMonitor() { | |
19 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance(); | |
20 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.
| |
21 monitor->bytes_received_ = 0; | |
22 monitor->last_received_ticks_ = base::TimeTicks(); | |
23 monitor->last_write_ticks_ = base::TimeTicks(); | |
24 } | |
25 }; | |
26 | |
27 class NetworkActivityMontiorTest : public testing::Test { | |
28 public: | |
29 NetworkActivityMontiorTest() | |
30 : monitor_(NetworkActivityMonitor::GetInstance()) { | |
31 NetworkActivityMonitorPeer::ResetMonitor(); | |
32 } | |
33 NetworkActivityMonitor* monitor_; | |
34 }; | |
35 | |
36 TEST_F(NetworkActivityMontiorTest, GetInstance) { | |
37 NetworkActivityMonitor* monitor = NetworkActivityMonitor::GetInstance(); | |
38 EXPECT_TRUE(monitor != NULL); | |
39 EXPECT_TRUE(monitor == NetworkActivityMonitor::GetInstance()); | |
40 } | |
41 | |
42 TEST_F(NetworkActivityMontiorTest, BytesReceived) { | |
43 base::TimeTicks start = base::TimeTicks::Now(); | |
44 EXPECT_EQ(0u, monitor_->GetBytesReceived()); | |
45 uint64_t bytes = 12345; | |
46 monitor_->IncrementBytesReceived(bytes); | |
47 EXPECT_EQ(bytes, monitor_->GetBytesReceived()); | |
48 base::TimeDelta delta = monitor_->GetTimeSinceLastReceived(); | |
49 EXPECT_LE(base::TimeDelta(), delta); | |
50 EXPECT_GE(base::TimeTicks::Now() - start, delta); | |
51 } | |
52 | |
53 TEST_F(NetworkActivityMontiorTest, BytesSent) { | |
54 base::TimeTicks start = base::TimeTicks::Now(); | |
55 EXPECT_EQ(0u, monitor_->GetBytesSent()); | |
56 uint64_t bytes = 12345; | |
57 monitor_->IncrementBytesSent(bytes); | |
58 EXPECT_EQ(bytes, monitor_->GetBytesSent()); | |
59 base::TimeDelta delta = monitor_->GetTimeSinceLastSent(); | |
60 EXPECT_LE(base::TimeDelta(), delta); | |
61 EXPECT_GE(base::TimeTicks::Now() - start, delta); | |
62 } | |
63 | |
64 class Verifier { | |
65 public: | |
66 void VerifyBytesReceivedIsMultipleOf(NetworkActivityMonitor* monitor, | |
67 uint64_t bytes) { | |
68 EXPECT_EQ(0u, monitor->GetBytesReceived() % bytes); | |
69 } | |
70 | |
71 void VerifyBytesSentIsMultipleOf(NetworkActivityMonitor* monitor, | |
72 uint64_t bytes) { | |
73 EXPECT_EQ(0u, monitor->GetBytesSent() % bytes); | |
74 } | |
75 }; | |
76 | |
77 TEST_F(NetworkActivityMontiorTest, Threading) { | |
78 base::Thread* threads[3]; | |
79 for (size_t i = 0; i < arraysize(threads); ++i) { | |
80 threads[i] = new base::Thread(base::UintToString(i)); | |
81 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.
| |
82 } | |
83 | |
84 Verifier verifier; | |
85 | |
86 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!
| |
87 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
| |
88 uint64_t bytes_sent = 91294998765u; | |
89 for (size_t i = 0; i < num_increments; ++i) { | |
90 threads[i % arraysize(threads)]->task_runner()->PostTask( | |
91 FROM_HERE, base::Bind(&NetworkActivityMonitor::IncrementBytesReceived, | |
92 base::Unretained(monitor_), bytes_received)); | |
93 threads[i % arraysize(threads)]->task_runner()->PostTask( | |
94 FROM_HERE, base::Bind(&NetworkActivityMonitor::IncrementBytesSent, | |
95 base::Unretained(monitor_), bytes_sent)); | |
96 threads[i % arraysize(threads)]->task_runner()->PostTask( | |
97 FROM_HERE, | |
98 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
| |
99 base::Unretained(&verifier), monitor_, bytes_sent)); | |
100 threads[i % arraysize(threads)]->task_runner()->PostTask( | |
101 FROM_HERE, | |
102 base::Bind(&Verifier::VerifyBytesReceivedIsMultipleOf, | |
103 base::Unretained(&verifier), monitor_, bytes_received)); | |
104 } | |
105 | |
106 for (size_t i = 0; i < arraysize(threads); ++i) { | |
107 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.
| |
108 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.
| |
109 } | |
110 | |
111 EXPECT_EQ(num_increments * bytes_received, monitor_->GetBytesReceived()); | |
112 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
| |
113 } | |
114 | |
115 } // namespace test | |
116 | |
117 } // namespace net | |
OLD | NEW |