| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "content/browser/cache_storage/cache_storage_operation.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/test/histogram_tester.h" |
| 13 #include "base/test/test_mock_time_task_runner.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const char kSlowOperation[] = |
| 21 "ServiceWorkerCache.CacheStorage.Scheduler.IsOperationSlow"; |
| 22 |
| 23 class TestTask { |
| 24 public: |
| 25 TestTask() = default; |
| 26 ~TestTask() = default; |
| 27 void Run() { callback_count_++; } |
| 28 |
| 29 int callback_count() const { return callback_count_; } |
| 30 |
| 31 protected: |
| 32 int callback_count_ = 0; |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class CacheStorageOperationTest : public testing::Test { |
| 38 protected: |
| 39 CacheStorageOperationTest() |
| 40 : mock_task_runner_(new base::TestMockTimeTaskRunner()) { |
| 41 operation_ = base::MakeUnique<CacheStorageOperation>( |
| 42 base::Bind(&TestTask::Run, base::Unretained(&task_)), |
| 43 CacheStorageSchedulerClient::CLIENT_STORAGE, mock_task_runner_); |
| 44 } |
| 45 |
| 46 base::HistogramTester histogram_tester_; |
| 47 TestTask task_; |
| 48 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; |
| 49 std::unique_ptr<CacheStorageOperation> operation_; |
| 50 }; |
| 51 |
| 52 TEST_F(CacheStorageOperationTest, OperationFast) { |
| 53 operation_->Run(); |
| 54 mock_task_runner_->RunUntilIdle(); |
| 55 EXPECT_EQ(1, task_.callback_count()); |
| 56 histogram_tester_.ExpectTotalCount(kSlowOperation, 0); |
| 57 |
| 58 // Finish the operation. |
| 59 operation_.reset(); |
| 60 |
| 61 mock_task_runner_->RunUntilIdle(); |
| 62 EXPECT_EQ(1, task_.callback_count()); |
| 63 histogram_tester_.ExpectTotalCount(kSlowOperation, 1); |
| 64 histogram_tester_.ExpectBucketCount(kSlowOperation, false, 1); |
| 65 } |
| 66 |
| 67 TEST_F(CacheStorageOperationTest, OperationSlow) { |
| 68 operation_->Run(); |
| 69 mock_task_runner_->RunUntilIdle(); |
| 70 EXPECT_EQ(1, task_.callback_count()); |
| 71 histogram_tester_.ExpectTotalCount(kSlowOperation, 0); |
| 72 |
| 73 // The operation takes 10 seconds. |
| 74 mock_task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(10)); |
| 75 mock_task_runner_->RunUntilIdle(); |
| 76 histogram_tester_.ExpectTotalCount(kSlowOperation, 1); |
| 77 histogram_tester_.ExpectBucketCount(kSlowOperation, true, 1); |
| 78 |
| 79 // Finish the operation. |
| 80 operation_.reset(); |
| 81 mock_task_runner_->RunUntilIdle(); |
| 82 EXPECT_EQ(1, task_.callback_count()); |
| 83 histogram_tester_.ExpectTotalCount(kSlowOperation, 1); |
| 84 histogram_tester_.ExpectBucketCount(kSlowOperation, true, 1); |
| 85 } |
| 86 |
| 87 } // namespace content |
| OLD | NEW |