OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This is a mock of the http cache and related testing classes. To be fair, it | 5 // This is a mock of the http cache and related testing classes. To be fair, it |
6 // is not really a mock http cache given that it uses the real implementation of | 6 // is not really a mock http cache given that it uses the real implementation of |
7 // the http cache, but it has fake implementations of all required components, | 7 // the http cache, but it has fake implementations of all required components, |
8 // so it is useful for unit tests at the http layer. | 8 // so it is useful for unit tests at the http layer. |
9 | 9 |
10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_ | 10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_ |
11 #define NET_HTTP_MOCK_HTTP_CACHE_H_ | 11 #define NET_HTTP_MOCK_HTTP_CACHE_H_ |
12 | 12 |
13 #include <stdint.h> | 13 #include <stdint.h> |
14 | 14 |
15 #include <unordered_map> | 15 #include <unordered_map> |
16 | 16 |
17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
18 #include "net/disk_cache/disk_cache.h" | 18 #include "net/disk_cache/disk_cache.h" |
19 #include "net/http/http_cache.h" | 19 #include "net/http/http_cache.h" |
20 #include "net/http/http_transaction_test_util.h" | 20 #include "net/http/http_transaction_test_util.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 //----------------------------------------------------------------------------- | 24 //----------------------------------------------------------------------------- |
25 // Mock disk cache (a very basic memory cache implementation). | 25 // Mock disk cache (a very basic memory cache implementation). |
26 | 26 |
27 class MockDiskEntry : public disk_cache::Entry, | 27 class MockDiskEntry : public disk_cache::Entry, |
28 public base::RefCounted<MockDiskEntry> { | 28 public base::RefCounted<MockDiskEntry> { |
29 public: | 29 public: |
30 // If |*defer| is set to true, the transaction will wait until | |
31 // ResumeCacheOperation is called before accessing the cache. | |
32 typedef base::Callback<void(bool* defer)> BeforeCacheOperationCallback; | |
jkarlin
2017/07/07 19:12:39
Using instead of typedef
jkarlin
2017/07/07 19:12:39
Instead of passing back a bool* defer (we always s
shivanisha
2017/07/10 18:51:27
MockHttpCache needs to keep track of doomed entrie
| |
33 | |
30 explicit MockDiskEntry(const std::string& key); | 34 explicit MockDiskEntry(const std::string& key); |
31 | 35 |
32 bool is_doomed() const { return doomed_; } | 36 bool is_doomed() const { return doomed_; } |
33 | 37 |
34 void Doom() override; | 38 void Doom() override; |
35 void Close() override; | 39 void Close() override; |
36 std::string GetKey() const override; | 40 std::string GetKey() const override; |
37 base::Time GetLastUsed() const override; | 41 base::Time GetLastUsed() const override; |
38 base::Time GetLastModified() const override; | 42 base::Time GetLastModified() const override; |
39 int32_t GetDataSize(int index) const override; | 43 int32_t GetDataSize(int index) const override; |
(...skipping 27 matching lines...) Expand all Loading... | |
67 // Fail most subsequent requests. | 71 // Fail most subsequent requests. |
68 void set_fail_requests() { fail_requests_ = true; } | 72 void set_fail_requests() { fail_requests_ = true; } |
69 | 73 |
70 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 74 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
71 | 75 |
72 // If |value| is true, don't deliver any completion callbacks until called | 76 // If |value| is true, don't deliver any completion callbacks until called |
73 // again with |value| set to false. Caution: remember to enable callbacks | 77 // again with |value| set to false. Caution: remember to enable callbacks |
74 // again or all subsequent tests will fail. | 78 // again or all subsequent tests will fail. |
75 static void IgnoreCallbacks(bool value); | 79 static void IgnoreCallbacks(bool value); |
76 | 80 |
81 // Sets the callback to be invoked before cache operation. | |
82 void SetBeforeCacheOperationCallback( | |
jkarlin
2017/07/07 19:12:39
Name is too general, specifically this is for Read
shivanisha
2017/07/10 18:51:27
Removed this for simplicity and using a DeferOp en
| |
83 const BeforeCacheOperationCallback& callback); | |
84 | |
85 // Resume deferred cache operation by posting |resume_callback_| with | |
86 // |resume_return_code_|. Returns ERR_IO_PENDING. | |
87 int ResumeCacheOperation(); | |
jkarlin
2017/07/07 19:12:39
BeforeCacheOperationCallback could be passed a cal
shivanisha
2017/07/10 18:51:27
The latest changes probably make this not applicab
| |
88 | |
77 private: | 89 private: |
78 friend class base::RefCounted<MockDiskEntry>; | 90 friend class base::RefCounted<MockDiskEntry>; |
79 struct CallbackInfo; | 91 struct CallbackInfo; |
80 | 92 |
81 ~MockDiskEntry() override; | 93 ~MockDiskEntry() override; |
82 | 94 |
83 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 95 // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
84 // if the consumer called Close on the MockDiskEntry. We achieve that by | 96 // if the consumer called Close on the MockDiskEntry. We achieve that by |
85 // leveraging the fact that this class is reference counted. | 97 // leveraging the fact that this class is reference counted. |
86 void CallbackLater(const CompletionCallback& callback, int result); | 98 void CallbackLater(const CompletionCallback& callback, int result); |
(...skipping 12 matching lines...) Expand all Loading... | |
99 std::string key_; | 111 std::string key_; |
100 std::vector<char> data_[kNumCacheEntryDataIndices]; | 112 std::vector<char> data_[kNumCacheEntryDataIndices]; |
101 int test_mode_; | 113 int test_mode_; |
102 bool doomed_; | 114 bool doomed_; |
103 bool sparse_; | 115 bool sparse_; |
104 bool fail_requests_; | 116 bool fail_requests_; |
105 bool fail_sparse_requests_; | 117 bool fail_sparse_requests_; |
106 bool busy_; | 118 bool busy_; |
107 bool delayed_; | 119 bool delayed_; |
108 bool cancel_; | 120 bool cancel_; |
121 | |
122 // TODO(shivanisha): Currently this is only used for ReadData. Extend to | |
123 // other cache operations as well. | |
124 BeforeCacheOperationCallback before_cache_callback_; | |
125 CompletionCallback resume_callback_; // used for pause and restart. | |
126 int resume_return_code_; | |
127 | |
109 static bool ignore_callbacks_; | 128 static bool ignore_callbacks_; |
110 }; | 129 }; |
111 | 130 |
112 class MockDiskCache : public disk_cache::Backend { | 131 class MockDiskCache : public disk_cache::Backend { |
113 public: | 132 public: |
114 MockDiskCache(); | 133 MockDiskCache(); |
115 ~MockDiskCache() override; | 134 ~MockDiskCache() override; |
116 | 135 |
117 CacheType GetCacheType() const override; | 136 CacheType GetCacheType() const override; |
118 int32_t GetEntryCount() const override; | 137 int32_t GetEntryCount() const override; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 // Makes sure that CreateEntry is not called twice for a given key. | 172 // Makes sure that CreateEntry is not called twice for a given key. |
154 void set_double_create_check(bool value) { double_create_check_ = value; } | 173 void set_double_create_check(bool value) { double_create_check_ = value; } |
155 | 174 |
156 // Makes all requests for data ranges to fail as not implemented. | 175 // Makes all requests for data ranges to fail as not implemented. |
157 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 176 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
158 | 177 |
159 void ReleaseAll(); | 178 void ReleaseAll(); |
160 | 179 |
161 bool IsDiskEntryDoomed(const std::string& key); | 180 bool IsDiskEntryDoomed(const std::string& key); |
162 | 181 |
182 // Add the cache operation callback to the entry. | |
183 void SetBeforeCacheOperationCallback( | |
184 const std::string& key, | |
185 const MockDiskEntry::BeforeCacheOperationCallback& callback); | |
186 | |
187 // Resume deferred cache operation on entry with |key|. | |
188 int ResumeCacheOperation(const std::string& key); | |
189 int ResumeDoomedEntryCacheOperation(const std::string& key); | |
190 | |
163 private: | 191 private: |
164 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; | 192 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; |
165 class NotImplementedIterator; | 193 class NotImplementedIterator; |
166 | 194 |
167 void CallbackLater(const CompletionCallback& callback, int result); | 195 void CallbackLater(const CompletionCallback& callback, int result); |
168 | 196 |
169 EntryMap entries_; | 197 EntryMap entries_; |
198 EntryMap doomed_entries_; | |
170 int open_count_; | 199 int open_count_; |
171 int create_count_; | 200 int create_count_; |
172 bool fail_requests_; | 201 bool fail_requests_; |
173 bool soft_failures_; | 202 bool soft_failures_; |
174 bool double_create_check_; | 203 bool double_create_check_; |
175 bool fail_sparse_requests_; | 204 bool fail_sparse_requests_; |
176 }; | 205 }; |
177 | 206 |
178 class MockBackendFactory : public HttpCache::BackendFactory { | 207 class MockBackendFactory : public HttpCache::BackendFactory { |
179 public: | 208 public: |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 NetLog* net_log); | 262 NetLog* net_log); |
234 | 263 |
235 // Returns the test mode after considering the global override. | 264 // Returns the test mode after considering the global override. |
236 static int GetTestMode(int test_mode); | 265 static int GetTestMode(int test_mode); |
237 | 266 |
238 // Overrides the test mode for a given operation. Remember to reset it after | 267 // Overrides the test mode for a given operation. Remember to reset it after |
239 // the test! (by setting test_mode to zero). | 268 // the test! (by setting test_mode to zero). |
240 static void SetTestMode(int test_mode); | 269 static void SetTestMode(int test_mode); |
241 | 270 |
242 // Functions to test the state of ActiveEntry. | 271 // Functions to test the state of ActiveEntry. |
243 | |
244 bool IsWriterPresent(const std::string& key); | 272 bool IsWriterPresent(const std::string& key); |
245 bool IsHeadersTransactionPresent(const std::string& key); | 273 bool IsHeadersTransactionPresent(const std::string& key); |
246 int GetCountReaders(const std::string& key); | 274 int GetCountReaders(const std::string& key); |
247 int GetCountAddToEntryQueue(const std::string& key); | 275 int GetCountAddToEntryQueue(const std::string& key); |
248 int GetCountDoneHeadersQueue(const std::string& key); | 276 int GetCountDoneHeadersQueue(const std::string& key); |
249 | 277 |
250 private: | 278 private: |
251 HttpCache http_cache_; | 279 HttpCache http_cache_; |
252 }; | 280 }; |
253 | 281 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 | 317 |
290 std::unique_ptr<disk_cache::Backend>* backend_; | 318 std::unique_ptr<disk_cache::Backend>* backend_; |
291 CompletionCallback callback_; | 319 CompletionCallback callback_; |
292 bool block_; | 320 bool block_; |
293 bool fail_; | 321 bool fail_; |
294 }; | 322 }; |
295 | 323 |
296 } // namespace net | 324 } // namespace net |
297 | 325 |
298 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 326 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
OLD | NEW |