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 enum DeferOp { | |
jkarlin
2017/07/12 16:15:55
I don't think we want this enum in the net:: names
shivanisha
2017/07/13 14:47:26
Moved it to MockDiskEntry
| |
25 DEFER_NONE, | |
26 DEFER_CREATE, | |
27 DEFER_READ, | |
28 }; | |
29 | |
24 //----------------------------------------------------------------------------- | 30 //----------------------------------------------------------------------------- |
25 // Mock disk cache (a very basic memory cache implementation). | 31 // Mock disk cache (a very basic memory cache implementation). |
26 | 32 |
27 class MockDiskEntry : public disk_cache::Entry, | 33 class MockDiskEntry : public disk_cache::Entry, |
28 public base::RefCounted<MockDiskEntry> { | 34 public base::RefCounted<MockDiskEntry> { |
29 public: | 35 public: |
30 explicit MockDiskEntry(const std::string& key); | 36 explicit MockDiskEntry(const std::string& key); |
31 | 37 |
32 bool is_doomed() const { return doomed_; } | 38 bool is_doomed() const { return doomed_; } |
33 | 39 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 // Fail most subsequent requests. | 73 // Fail most subsequent requests. |
68 void set_fail_requests() { fail_requests_ = true; } | 74 void set_fail_requests() { fail_requests_ = true; } |
69 | 75 |
70 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 76 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
71 | 77 |
72 // If |value| is true, don't deliver any completion callbacks until called | 78 // If |value| is true, don't deliver any completion callbacks until called |
73 // again with |value| set to false. Caution: remember to enable callbacks | 79 // again with |value| set to false. Caution: remember to enable callbacks |
74 // again or all subsequent tests will fail. | 80 // again or all subsequent tests will fail. |
75 static void IgnoreCallbacks(bool value); | 81 static void IgnoreCallbacks(bool value); |
76 | 82 |
83 void SetDefer(DeferOp defer_op) { defer_op_ = defer_op; } | |
84 | |
85 // Resumes deferred cache operation by posting |resume_callback_| with | |
86 // |resume_return_code_|. Returns ERR_IO_PENDING. | |
87 int ResumeCacheOperation(); | |
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 // Used for pause and restart. | |
123 DeferOp defer_op_; | |
124 CompletionCallback resume_callback_; | |
125 int resume_return_code_; | |
126 | |
109 static bool ignore_callbacks_; | 127 static bool ignore_callbacks_; |
110 }; | 128 }; |
111 | 129 |
112 class MockDiskCache : public disk_cache::Backend { | 130 class MockDiskCache : public disk_cache::Backend { |
113 public: | 131 public: |
114 MockDiskCache(); | 132 MockDiskCache(); |
115 ~MockDiskCache() override; | 133 ~MockDiskCache() override; |
116 | 134 |
117 CacheType GetCacheType() const override; | 135 CacheType GetCacheType() const override; |
118 int32_t GetEntryCount() const override; | 136 int32_t GetEntryCount() const override; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 void set_soft_failures(bool value) { soft_failures_ = value; } | 172 void set_soft_failures(bool value) { soft_failures_ = value; } |
155 | 173 |
156 // Makes sure that CreateEntry is not called twice for a given key. | 174 // Makes sure that CreateEntry is not called twice for a given key. |
157 void set_double_create_check(bool value) { double_create_check_ = value; } | 175 void set_double_create_check(bool value) { double_create_check_ = value; } |
158 | 176 |
159 // Makes all requests for data ranges to fail as not implemented. | 177 // Makes all requests for data ranges to fail as not implemented. |
160 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 178 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
161 | 179 |
162 void ReleaseAll(); | 180 void ReleaseAll(); |
163 | 181 |
182 // Returns true if a doomed entry exists with this key. | |
164 bool IsDiskEntryDoomed(const std::string& key); | 183 bool IsDiskEntryDoomed(const std::string& key); |
165 | 184 |
185 // Returns true if a non-doomed entry exists with this key. | |
186 bool IsDiskEntryNotDoomed(const std::string& key); | |
187 | |
188 // Invokes ResumeCacheOperation on the corresponding MockDiskEntry with |key|. | |
189 int ResumeCacheOperation(const std::string& key); | |
190 int ResumeDoomedEntryCacheOperation(const std::string& key); | |
191 | |
192 // Resume deferred cache operation by posting |resume_callback_| with | |
193 // |resume_return_code_|. Returns ERR_IO_PENDING. | |
194 int ResumeCacheOperation(); | |
195 | |
196 void SetDefer(DeferOp defer_op) { defer_op_ = defer_op; } | |
197 | |
198 // Invokes SetDefer on the corresponding MockDiskEntry with |key|. | |
199 void SetDefer(const std::string& key, DeferOp defer_op); | |
200 | |
166 private: | 201 private: |
167 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; | 202 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; |
168 class NotImplementedIterator; | 203 class NotImplementedIterator; |
169 | 204 |
170 void CallbackLater(const CompletionCallback& callback, int result); | 205 void CallbackLater(const CompletionCallback& callback, int result); |
171 | 206 |
172 EntryMap entries_; | 207 EntryMap entries_; |
208 EntryMap doomed_entries_; | |
173 int open_count_; | 209 int open_count_; |
174 int create_count_; | 210 int create_count_; |
175 int doomed_count_; | 211 int doomed_count_; |
176 bool fail_requests_; | 212 bool fail_requests_; |
177 bool soft_failures_; | 213 bool soft_failures_; |
178 bool double_create_check_; | 214 bool double_create_check_; |
179 bool fail_sparse_requests_; | 215 bool fail_sparse_requests_; |
216 | |
217 // Used for pause and restart. | |
218 DeferOp defer_op_; | |
219 CompletionCallback resume_callback_; | |
220 int resume_return_code_; | |
180 }; | 221 }; |
181 | 222 |
182 class MockBackendFactory : public HttpCache::BackendFactory { | 223 class MockBackendFactory : public HttpCache::BackendFactory { |
183 public: | 224 public: |
184 int CreateBackend(NetLog* net_log, | 225 int CreateBackend(NetLog* net_log, |
185 std::unique_ptr<disk_cache::Backend>* backend, | 226 std::unique_ptr<disk_cache::Backend>* backend, |
186 const CompletionCallback& callback) override; | 227 const CompletionCallback& callback) override; |
187 }; | 228 }; |
188 | 229 |
189 class MockHttpCache { | 230 class MockHttpCache { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 NetLog* net_log); | 278 NetLog* net_log); |
238 | 279 |
239 // Returns the test mode after considering the global override. | 280 // Returns the test mode after considering the global override. |
240 static int GetTestMode(int test_mode); | 281 static int GetTestMode(int test_mode); |
241 | 282 |
242 // Overrides the test mode for a given operation. Remember to reset it after | 283 // Overrides the test mode for a given operation. Remember to reset it after |
243 // the test! (by setting test_mode to zero). | 284 // the test! (by setting test_mode to zero). |
244 static void SetTestMode(int test_mode); | 285 static void SetTestMode(int test_mode); |
245 | 286 |
246 // Functions to test the state of ActiveEntry. | 287 // Functions to test the state of ActiveEntry. |
247 | |
248 bool IsWriterPresent(const std::string& key); | 288 bool IsWriterPresent(const std::string& key); |
249 bool IsHeadersTransactionPresent(const std::string& key); | 289 bool IsHeadersTransactionPresent(const std::string& key); |
250 int GetCountReaders(const std::string& key); | 290 int GetCountReaders(const std::string& key); |
251 int GetCountAddToEntryQueue(const std::string& key); | 291 int GetCountAddToEntryQueue(const std::string& key); |
252 int GetCountDoneHeadersQueue(const std::string& key); | 292 int GetCountDoneHeadersQueue(const std::string& key); |
253 | 293 |
254 private: | 294 private: |
255 HttpCache http_cache_; | 295 HttpCache http_cache_; |
256 }; | 296 }; |
257 | 297 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 | 333 |
294 std::unique_ptr<disk_cache::Backend>* backend_; | 334 std::unique_ptr<disk_cache::Backend>* backend_; |
295 CompletionCallback callback_; | 335 CompletionCallback callback_; |
296 bool block_; | 336 bool block_; |
297 bool fail_; | 337 bool fail_; |
298 }; | 338 }; |
299 | 339 |
300 } // namespace net | 340 } // namespace net |
301 | 341 |
302 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 342 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
OLD | NEW |