Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "base/run_loop.h" | |
| 6 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | |
| 7 #include "chrome/browser/task_manager/sampling/task_manager_impl.h" | |
| 8 #include "content/public/test/test_browser_thread_bundle.h" | |
| 9 #include "testing/gmock/include/gmock/gmock.h" | |
|
ncarter (slow)
2017/07/05 18:28:18
Add back
#include "testing/gtest/include/gtest/gt
cburn
2017/07/06 17:30:41
Done.
| |
| 10 | |
| 11 namespace task_manager { | |
| 12 | |
| 13 class TaskManagerIoThreadHelperTest : public testing::Test { | |
| 14 public: | |
| 15 TaskManagerIoThreadHelperTest() | |
| 16 : helper_manager_( | |
| 17 base::BindRepeating(&TaskManagerIoThreadHelperTest::GotData, | |
| 18 base::Unretained(this))) { | |
| 19 base::RunLoop().RunUntilIdle(); | |
| 20 } | |
| 21 ~TaskManagerIoThreadHelperTest() override {} | |
| 22 | |
| 23 void GotData(BytesTransferredMap params) { | |
| 24 EXPECT_TRUE(returned_map_.empty()) << "GotData() delivered twice"; | |
| 25 EXPECT_FALSE(params.empty()) << "GotData() called with empty map"; | |
| 26 returned_map_ = params; | |
| 27 } | |
| 28 | |
| 29 protected: | |
| 30 BytesTransferredMap returned_map_; | |
| 31 | |
| 32 private: | |
| 33 content::TestBrowserThreadBundle thread_bundle_; | |
| 34 IoThreadHelperManager helper_manager_; | |
| 35 | |
| 36 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelperTest); | |
| 37 }; | |
| 38 | |
| 39 // Tests that the origin_id is used in the map correctly. | |
| 40 TEST_F(TaskManagerIoThreadHelperTest, PidData) { | |
| 41 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 42 | |
| 43 BytesTransferredKey key = {123, -1, -1}; | |
| 44 | |
| 45 int64_t correct_read_bytes = 0; | |
| 46 int64_t correct_sent_bytes = 0; | |
| 47 | |
| 48 int read_bytes_array[] = {200, 400, 800}; | |
| 49 int sent_bytes_array[] = {100, 123, 234}; | |
| 50 | |
| 51 for (int i : read_bytes_array) { | |
| 52 TaskManagerIoThreadHelper::OnRawBytesTransferred(key, i, 0); | |
| 53 correct_read_bytes += i; | |
| 54 } | |
| 55 for (int i : sent_bytes_array) { | |
| 56 TaskManagerIoThreadHelper::OnRawBytesTransferred(key, 0, i); | |
| 57 correct_sent_bytes += i; | |
| 58 } | |
| 59 | |
| 60 EXPECT_TRUE(mock_main_runner->HasPendingTask()); | |
| 61 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 62 base::RunLoop().RunUntilIdle(); | |
| 63 EXPECT_FALSE(mock_main_runner->HasPendingTask()); | |
| 64 EXPECT_EQ(correct_sent_bytes, returned_map_[key].byte_sent_count); | |
| 65 EXPECT_EQ(correct_read_bytes, returned_map_[key].byte_read_count); | |
| 66 } | |
| 67 | |
| 68 // Tests that the |child_id| and |route_id| are used in the map correctly. | |
| 69 TEST_F(TaskManagerIoThreadHelperTest, ChildRouteData) { | |
| 70 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 71 | |
| 72 BytesTransferredKey key = {0, 100, 190}; | |
| 73 | |
| 74 int64_t correct_read_bytes = 0; | |
| 75 int64_t correct_sent_bytes = 0; | |
| 76 | |
| 77 int read_bytes_array[] = {900, 300, 100}; | |
| 78 int sent_bytes_array[] = {130, 153, 934}; | |
| 79 | |
| 80 for (int i : read_bytes_array) { | |
| 81 TaskManagerIoThreadHelper::OnRawBytesTransferred(key, i, 0); | |
| 82 correct_read_bytes += i; | |
| 83 } | |
| 84 for (int i : sent_bytes_array) { | |
| 85 TaskManagerIoThreadHelper::OnRawBytesTransferred(key, 0, i); | |
| 86 correct_sent_bytes += i; | |
| 87 } | |
| 88 | |
| 89 EXPECT_TRUE(mock_main_runner->HasPendingTask()); | |
| 90 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 91 base::RunLoop().RunUntilIdle(); | |
| 92 EXPECT_FALSE(mock_main_runner->HasPendingTask()); | |
| 93 EXPECT_EQ(1U, returned_map_.size()); | |
| 94 EXPECT_EQ(correct_sent_bytes, returned_map_[key].byte_sent_count); | |
| 95 EXPECT_EQ(correct_read_bytes, returned_map_[key].byte_read_count); | |
| 96 } | |
| 97 | |
| 98 // Tests that two distinct |origin_pid| are tracked separately in the unordered | |
| 99 // map. | |
| 100 TEST_F(TaskManagerIoThreadHelperTest, TwoPidData) { | |
| 101 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 102 | |
| 103 BytesTransferredKey key1 = {23, -1, -1}; | |
| 104 BytesTransferredKey key2 = {33, -1, -1}; | |
| 105 | |
| 106 int64_t correct_read_bytes1 = 0; | |
| 107 int64_t correct_sent_bytes1 = 0; | |
| 108 | |
| 109 int64_t correct_read_bytes2 = 0; | |
| 110 int64_t correct_sent_bytes2 = 0; | |
| 111 | |
| 112 int read_bytes_array1[] = {900, 300, 100}; | |
| 113 int sent_bytes_array1[] = {130, 153, 934}; | |
| 114 | |
| 115 int read_bytes_array2[] = {903, 340, 150}; | |
| 116 int sent_bytes_array2[] = {138, 157, 964}; | |
| 117 | |
| 118 for (int i : read_bytes_array1) { | |
| 119 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, i, 0); | |
| 120 correct_read_bytes1 += i; | |
| 121 } | |
| 122 for (int i : sent_bytes_array1) { | |
| 123 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, i); | |
| 124 correct_sent_bytes1 += i; | |
| 125 } | |
| 126 | |
| 127 for (int i : read_bytes_array2) { | |
| 128 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, i, 0); | |
| 129 correct_read_bytes2 += i; | |
| 130 } | |
| 131 for (int i : sent_bytes_array2) { | |
| 132 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, 0, i); | |
| 133 correct_sent_bytes2 += i; | |
| 134 } | |
| 135 | |
| 136 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 137 base::RunLoop().RunUntilIdle(); | |
| 138 const unsigned long number_of_keys = 2; | |
| 139 EXPECT_EQ(number_of_keys, returned_map_.size()); | |
| 140 EXPECT_EQ(correct_sent_bytes1, returned_map_[key1].byte_sent_count); | |
| 141 EXPECT_EQ(correct_read_bytes1, returned_map_[key1].byte_read_count); | |
| 142 EXPECT_EQ(correct_sent_bytes2, returned_map_[key2].byte_sent_count); | |
| 143 EXPECT_EQ(correct_read_bytes2, returned_map_[key2].byte_read_count); | |
| 144 } | |
| 145 | |
| 146 // Tests that two keys with the same |origin_pid| are tracked together in the | |
| 147 // unordered map. | |
| 148 TEST_F(TaskManagerIoThreadHelperTest, TwoSamePidData) { | |
| 149 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 150 | |
| 151 BytesTransferredKey key1 = {23, -1, -1}; | |
| 152 BytesTransferredKey key2 = {23, -1, -1}; | |
| 153 | |
| 154 int64_t correct_read_bytes = 0; | |
| 155 int64_t correct_sent_bytes = 0; | |
| 156 | |
| 157 int read_bytes_array[] = {900, 300, 100}; | |
| 158 int sent_bytes_array[] = {130, 153, 934}; | |
| 159 | |
| 160 for (int i : read_bytes_array) { | |
| 161 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, i, 0); | |
| 162 correct_read_bytes += i; | |
| 163 } | |
| 164 for (int i : sent_bytes_array) { | |
| 165 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, i); | |
| 166 correct_sent_bytes += i; | |
| 167 } | |
| 168 | |
| 169 for (int i : read_bytes_array) { | |
| 170 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, i, 0); | |
| 171 correct_read_bytes += i; | |
| 172 } | |
| 173 for (int i : sent_bytes_array) { | |
| 174 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, 0, i); | |
| 175 correct_sent_bytes += i; | |
| 176 } | |
| 177 | |
| 178 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 179 base::RunLoop().RunUntilIdle(); | |
| 180 EXPECT_EQ(1U, returned_map_.size()); | |
| 181 EXPECT_EQ(correct_sent_bytes, returned_map_[key1].byte_sent_count); | |
| 182 EXPECT_EQ(correct_read_bytes, returned_map_[key1].byte_read_count); | |
| 183 EXPECT_EQ(correct_sent_bytes, returned_map_[key2].byte_sent_count); | |
| 184 EXPECT_EQ(correct_read_bytes, returned_map_[key2].byte_read_count); | |
| 185 } | |
| 186 | |
| 187 // Tests that two distinct |child_id| and |route_id| pairs are tracked | |
| 188 // separately in the unordered map. | |
| 189 TEST_F(TaskManagerIoThreadHelperTest, TwoChildRouteData) { | |
| 190 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 191 | |
| 192 BytesTransferredKey key1 = {0, 32, 1}; | |
| 193 BytesTransferredKey key2 = {0, 17, 2}; | |
| 194 | |
| 195 int64_t correct_read_bytes1 = 0; | |
| 196 int64_t correct_sent_bytes1 = 0; | |
| 197 | |
| 198 int64_t correct_read_bytes2 = 0; | |
| 199 int64_t correct_sent_bytes2 = 0; | |
| 200 | |
| 201 int read_bytes_array1[] = {453, 987654, 946650}; | |
| 202 int sent_bytes_array1[] = {138450, 1556473, 954434}; | |
| 203 | |
| 204 int read_bytes_array2[] = {905643, 324340, 654150}; | |
| 205 int sent_bytes_array2[] = {1232138, 157312, 965464}; | |
| 206 | |
| 207 for (int i : read_bytes_array1) { | |
| 208 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, i, 0); | |
| 209 correct_read_bytes1 += i; | |
| 210 } | |
| 211 for (int i : sent_bytes_array1) { | |
| 212 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, i); | |
| 213 correct_sent_bytes1 += i; | |
| 214 } | |
| 215 | |
| 216 for (int i : read_bytes_array2) { | |
| 217 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, i, 0); | |
| 218 correct_read_bytes2 += i; | |
| 219 } | |
| 220 for (int i : sent_bytes_array2) { | |
| 221 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, 0, i); | |
| 222 correct_sent_bytes2 += i; | |
| 223 } | |
| 224 | |
| 225 EXPECT_TRUE(mock_main_runner->HasPendingTask()); | |
| 226 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 227 base::RunLoop().RunUntilIdle(); | |
| 228 const unsigned long number_of_keys = 2; | |
| 229 EXPECT_EQ(number_of_keys, returned_map_.size()); | |
| 230 EXPECT_FALSE(mock_main_runner->HasPendingTask()); | |
| 231 EXPECT_EQ(correct_sent_bytes1, returned_map_[key1].byte_sent_count); | |
| 232 EXPECT_EQ(correct_read_bytes1, returned_map_[key1].byte_read_count); | |
| 233 EXPECT_EQ(correct_sent_bytes2, returned_map_[key2].byte_sent_count); | |
| 234 EXPECT_EQ(correct_read_bytes2, returned_map_[key2].byte_read_count); | |
| 235 } | |
| 236 | |
| 237 // Tests that two keys with the same |child_id| and |route_id| are tracked | |
| 238 // together in the unordered map. | |
| 239 TEST_F(TaskManagerIoThreadHelperTest, TwoSameChildRouteData) { | |
| 240 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 241 | |
| 242 BytesTransferredKey key1 = {0, 123, 456}; | |
| 243 BytesTransferredKey key2 = {0, 123, 456}; | |
| 244 | |
| 245 int64_t correct_read_bytes = 0; | |
| 246 int64_t correct_sent_bytes = 0; | |
| 247 | |
| 248 int read_bytes_array[] = {90440, 12300, 103420}; | |
| 249 int sent_bytes_array[] = {44130, 12353, 93234}; | |
| 250 | |
| 251 for (int i : read_bytes_array) { | |
| 252 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, i, 0); | |
| 253 correct_read_bytes += i; | |
| 254 } | |
| 255 for (int i : sent_bytes_array) { | |
| 256 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, i); | |
| 257 correct_sent_bytes += i; | |
| 258 } | |
| 259 | |
| 260 for (int i : read_bytes_array) { | |
| 261 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, i, 0); | |
| 262 correct_read_bytes += i; | |
| 263 } | |
| 264 for (int i : sent_bytes_array) { | |
| 265 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, 0, i); | |
| 266 correct_sent_bytes += i; | |
| 267 } | |
| 268 | |
| 269 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 270 base::RunLoop().RunUntilIdle(); | |
| 271 EXPECT_EQ(1U, returned_map_.size()); | |
| 272 EXPECT_EQ(correct_sent_bytes, returned_map_[key1].byte_sent_count); | |
| 273 EXPECT_EQ(correct_read_bytes, returned_map_[key1].byte_read_count); | |
| 274 EXPECT_EQ(correct_sent_bytes, returned_map_[key2].byte_sent_count); | |
| 275 EXPECT_EQ(correct_read_bytes, returned_map_[key2].byte_read_count); | |
| 276 } | |
| 277 | |
| 278 // Tests that the unordered_map can hold both types of key in one map. | |
| 279 TEST_F(TaskManagerIoThreadHelperTest, OnePidOneChildRouteData) { | |
| 280 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 281 // Test the pathological condition of a collision. | |
| 282 BytesTransferredKey key1 = {0, 12, 143}; | |
| 283 BytesTransferredKey key2 = {static_cast<int>(base::HashInts(12, 143)), -1, | |
| 284 -1}; | |
| 285 | |
| 286 int64_t correct_read_bytes1 = 0; | |
| 287 int64_t correct_sent_bytes1 = 0; | |
| 288 | |
| 289 int64_t correct_read_bytes2 = 0; | |
| 290 int64_t correct_sent_bytes2 = 0; | |
| 291 | |
| 292 int read_bytes_array1[] = {453, 98754, 94650}; | |
| 293 int sent_bytes_array1[] = {1350, 15643, 95434}; | |
| 294 | |
| 295 int read_bytes_array2[] = {905643, 3243, 654150}; | |
| 296 int sent_bytes_array2[] = {12338, 157312, 9664}; | |
| 297 | |
| 298 for (int i : read_bytes_array1) { | |
| 299 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, i, 0); | |
| 300 correct_read_bytes1 += i; | |
| 301 } | |
| 302 for (int i : sent_bytes_array1) { | |
| 303 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, i); | |
| 304 correct_sent_bytes1 += i; | |
| 305 } | |
| 306 | |
| 307 for (int i : read_bytes_array2) { | |
| 308 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, i, 0); | |
| 309 correct_read_bytes2 += i; | |
| 310 } | |
| 311 for (int i : sent_bytes_array2) { | |
| 312 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, 0, i); | |
| 313 correct_sent_bytes2 += i; | |
| 314 } | |
| 315 | |
| 316 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 317 base::RunLoop().RunUntilIdle(); | |
| 318 EXPECT_EQ(2U, returned_map_.size()); | |
| 319 EXPECT_EQ(correct_sent_bytes1, returned_map_[key1].byte_sent_count); | |
| 320 EXPECT_EQ(correct_read_bytes1, returned_map_[key1].byte_read_count); | |
| 321 EXPECT_EQ(correct_sent_bytes2, returned_map_[key2].byte_sent_count); | |
| 322 EXPECT_EQ(correct_read_bytes2, returned_map_[key2].byte_read_count); | |
| 323 } | |
| 324 | |
| 325 // Tests that the map can store both types of keys and that it does update after | |
| 326 // 1 mocked second passes and there is new traffic. | |
| 327 TEST_F(TaskManagerIoThreadHelperTest, MultipleWavesMixedData) { | |
| 328 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; | |
| 329 | |
| 330 BytesTransferredKey key1 = {0, 12, 143}; | |
| 331 BytesTransferredKey key2 = {32, -1, -1}; | |
| 332 | |
| 333 int64_t correct_read_bytes1 = 0; | |
| 334 int64_t correct_sent_bytes1 = 0; | |
| 335 | |
| 336 int read_bytes_array1[] = {453, 98754, 94650}; | |
| 337 int sent_bytes_array1[] = {1350, 15643, 95434}; | |
| 338 | |
| 339 for (int i : read_bytes_array1) { | |
| 340 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, i, 0); | |
| 341 correct_read_bytes1 += i; | |
| 342 } | |
| 343 for (int i : sent_bytes_array1) { | |
| 344 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, i); | |
| 345 correct_sent_bytes1 += i; | |
| 346 } | |
| 347 | |
| 348 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 349 base::RunLoop().RunUntilIdle(); | |
| 350 returned_map_.clear(); | |
| 351 | |
| 352 correct_sent_bytes1 = 0; | |
| 353 correct_read_bytes1 = 0; | |
| 354 | |
| 355 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, 10); | |
| 356 correct_sent_bytes1 += 10; | |
| 357 | |
| 358 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 359 base::RunLoop().RunUntilIdle(); | |
| 360 EXPECT_EQ(1U, returned_map_.size()); | |
| 361 EXPECT_EQ(correct_sent_bytes1, returned_map_[key1].byte_sent_count); | |
| 362 EXPECT_EQ(correct_read_bytes1, returned_map_[key1].byte_read_count); | |
| 363 // |key2| has not been used yet so it should call the implicit constructor | |
| 364 // which starts at 0. | |
| 365 EXPECT_EQ(0U, returned_map_[key2].byte_sent_count); | |
| 366 EXPECT_EQ(0U, returned_map_[key2].byte_read_count); | |
| 367 returned_map_.clear(); | |
| 368 | |
| 369 correct_read_bytes1 = 0; | |
| 370 correct_sent_bytes1 = 0; | |
| 371 | |
| 372 int correct_read_bytes2 = 0; | |
| 373 int correct_sent_bytes2 = 0; | |
| 374 | |
| 375 int read_bytes_array_second_1[] = {4153, 987154, 946501}; | |
| 376 int sent_bytes_array_second_1[] = {13510, 115643, 954134}; | |
| 377 | |
| 378 int read_bytes_array2[] = {9056243, 32243, 6541250}; | |
| 379 int sent_bytes_array2[] = {123238, 1527312, 96624}; | |
| 380 | |
| 381 for (int i : read_bytes_array_second_1) { | |
| 382 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, i, 0); | |
| 383 correct_read_bytes1 += i; | |
| 384 } | |
| 385 for (int i : sent_bytes_array_second_1) { | |
| 386 TaskManagerIoThreadHelper::OnRawBytesTransferred(key1, 0, i); | |
| 387 correct_sent_bytes1 += i; | |
| 388 } | |
| 389 | |
| 390 for (int i : read_bytes_array2) { | |
| 391 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, i, 0); | |
| 392 correct_read_bytes2 += i; | |
| 393 } | |
| 394 for (int i : sent_bytes_array2) { | |
| 395 TaskManagerIoThreadHelper::OnRawBytesTransferred(key2, 0, i); | |
| 396 correct_sent_bytes2 += i; | |
| 397 } | |
| 398 | |
| 399 mock_main_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); | |
| 400 base::RunLoop().RunUntilIdle(); | |
| 401 EXPECT_EQ(2U, returned_map_.size()); | |
| 402 EXPECT_EQ(correct_sent_bytes1, returned_map_[key1].byte_sent_count); | |
| 403 EXPECT_EQ(correct_read_bytes1, returned_map_[key1].byte_read_count); | |
| 404 EXPECT_EQ(correct_sent_bytes2, returned_map_[key2].byte_sent_count); | |
| 405 EXPECT_EQ(correct_read_bytes2, returned_map_[key2].byte_read_count); | |
| 406 } | |
| 407 | |
| 408 } // namespace task_manager | |
| OLD | NEW |