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