OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 <algorithm> | 5 #include <algorithm> |
6 #include <memory> | 6 #include <memory> |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 } | 52 } |
53 | 53 |
54 void CountEntries(base::Time begin_time, base::Time end_time) { | 54 void CountEntries(base::Time begin_time, base::Time end_time) { |
55 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
56 last_size_ = -1; | 56 last_size_ = -1; |
57 auto helper = ConditionalCacheCountingHelper::CreateForRange( | 57 auto helper = ConditionalCacheCountingHelper::CreateForRange( |
58 cache_util_->partition(), begin_time, end_time); | 58 cache_util_->partition(), begin_time, end_time); |
59 helper->CountAndDestroySelfWhenFinished(count_callback_); | 59 helper->CountAndDestroySelfWhenFinished(count_callback_); |
60 } | 60 } |
61 | 61 |
| 62 void CountEntriesAndCancel(base::Time begin_time, base::Time end_time) { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 64 last_size_ = -1; |
| 65 auto helper = ConditionalCacheCountingHelper::CreateForRange( |
| 66 cache_util_->partition(), begin_time, end_time); |
| 67 // Cancel first to make sure we don't cancel after the IO thread finishes. |
| 68 helper->CancelCounting(); |
| 69 helper->CountAndDestroySelfWhenFinished(count_callback_); |
| 70 } |
| 71 |
62 int64_t GetResult() { | 72 int64_t GetResult() { |
63 DCHECK_GT(last_size_, 0); | 73 DCHECK_GT(last_size_, 0); |
64 return last_size_; | 74 return last_size_; |
65 } | 75 } |
66 | 76 |
67 int64_t GetResultOrError() { return last_size_; } | 77 int64_t GetResultOrError() { return last_size_; } |
68 | 78 |
69 CacheTestUtil* GetCacheTestUtil() { return cache_util_.get(); } | 79 CacheTestUtil* GetCacheTestUtil() { return cache_util_.get(); } |
70 | 80 |
71 private: | 81 private: |
72 ConditionalCacheCountingHelper::CacheCountCallback count_callback_; | 82 ConditionalCacheCountingHelper::CacheCountCallback count_callback_; |
73 std::unique_ptr<base::RunLoop> run_loop_; | 83 std::unique_ptr<base::RunLoop> run_loop_; |
74 std::unique_ptr<CacheTestUtil> cache_util_; | 84 std::unique_ptr<CacheTestUtil> cache_util_; |
75 | 85 |
76 int64_t last_size_; | 86 int64_t last_size_; |
77 }; | 87 }; |
78 | 88 |
79 // Tests that ConditionalCacheCountingHelper only counts those cache entries | 89 // Tests that ConditionalCacheCountingHelper only counts those cache entries |
80 // that match the condition. | 90 // that match the condition. |
81 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, Count) { | 91 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, Count) { |
82 // Create 5 entries. | 92 // Create 5 entries. |
83 std::set<std::string> keys1 = {"1", "2", "3", "4", "5"}; | 93 std::set<std::string> keys1 = {"1", "2", "3", "4", "5"}; |
84 | 94 |
85 base::Time t1 = base::Time::Now(); | 95 base::Time t1 = base::Time::Now(); |
86 GetCacheTestUtil()->CreateCacheEntries(keys1); | 96 GetCacheTestUtil()->CreateCacheEntries(keys1); |
87 | 97 |
88 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | 98 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); |
89 // base::Time t2 = base::Time::Now(); | 99 base::Time t2 = base::Time::Now(); |
90 | 100 |
91 std::set<std::string> keys2 = {"6", "7"}; | 101 std::set<std::string> keys2 = {"6", "7"}; |
92 GetCacheTestUtil()->CreateCacheEntries(keys2); | 102 GetCacheTestUtil()->CreateCacheEntries(keys2); |
93 | 103 |
94 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | 104 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); |
95 base::Time t3 = base::Time::Now(); | 105 base::Time t3 = base::Time::Now(); |
96 | 106 |
97 // TODO(dullweber): Add test for time ranges when GetEntrySize() is done. | 107 // Count the size of the first set of entries. |
| 108 CountEntries(t1, t2); |
| 109 WaitForTasksOnIOThread(); |
| 110 int64_t size_1_2 = GetResult(); |
| 111 |
| 112 // Count the size of the second set of entries. |
| 113 CountEntries(t2, t3); |
| 114 WaitForTasksOnIOThread(); |
| 115 int64_t size_2_3 = GetResult(); |
98 | 116 |
99 // Count all entries. | 117 // Count all entries. |
100 CountEntries(t1, t3); | 118 CountEntries(t1, t3); |
101 WaitForTasksOnIOThread(); | 119 WaitForTasksOnIOThread(); |
102 int64_t size_1_3 = GetResult(); | 120 int64_t size_1_3 = GetResult(); |
| 121 EXPECT_EQ(size_1_2 + size_2_3, size_1_3); |
103 | 122 |
104 // Count everything | 123 // Count everything |
105 CountEntries(base::Time(), base::Time::Max()); | 124 CountEntries(base::Time(), base::Time::Max()); |
106 WaitForTasksOnIOThread(); | 125 WaitForTasksOnIOThread(); |
107 EXPECT_EQ(size_1_3, GetResult()); | 126 EXPECT_EQ(size_1_3, GetResult()); |
108 } | 127 } |
| 128 |
| 129 // Tests that ConditionalCacheCountingHelper returns net::ERR_ABORTED when |
| 130 // cancelled. |
| 131 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, |
| 132 CancelCounting) { |
| 133 std::set<std::string> keys = {"1", "2", "3", "4", "5"}; |
| 134 base::Time t1 = base::Time::Now(); |
| 135 GetCacheTestUtil()->CreateCacheEntries(keys); |
| 136 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); |
| 137 |
| 138 // Count and cancel |
| 139 CountEntriesAndCancel(t1, base::Time::Now()); |
| 140 WaitForTasksOnIOThread(); |
| 141 EXPECT_EQ(net::ERR_ABORTED, GetResultOrError()); |
| 142 } |
OLD | NEW |