Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: net/http/mock_http_cache.h

Issue 2970133002: DoomPartialEntry should not attempt to doom an already doomed entry. (Closed)
Patch Set: Feedback addressed Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_cache_unittest.cc ('k') | net/http/mock_http_cache.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 enum DeferOp {
31 DEFER_NONE,
32 DEFER_CREATE,
33 DEFER_READ,
34 };
35
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
34 void Doom() override; 40 void Doom() override;
35 void Close() override; 41 void Close() override;
36 std::string GetKey() const override; 42 std::string GetKey() const override;
37 base::Time GetLastUsed() const override; 43 base::Time GetLastUsed() const override;
38 base::Time GetLastModified() const override; 44 base::Time GetLastModified() const override;
39 int32_t GetDataSize(int index) const override; 45 int32_t GetDataSize(int index) const override;
(...skipping 27 matching lines...) Expand all
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 // Defers invoking the callback for the given operation. Calling code should
84 // invoke ResumeDiskEntryOperation to resume.
85 void SetDefer(DeferOp defer_op) { defer_op_ = defer_op; }
86
87 // Resumes deferred cache operation by posting |resume_callback_| with
88 // |resume_return_code_|.
89 void ResumeDiskEntryOperation();
90
77 private: 91 private:
78 friend class base::RefCounted<MockDiskEntry>; 92 friend class base::RefCounted<MockDiskEntry>;
79 struct CallbackInfo; 93 struct CallbackInfo;
80 94
81 ~MockDiskEntry() override; 95 ~MockDiskEntry() override;
82 96
83 // Unlike the callbacks for MockHttpTransaction, we want this one to run even 97 // 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 98 // if the consumer called Close on the MockDiskEntry. We achieve that by
85 // leveraging the fact that this class is reference counted. 99 // leveraging the fact that this class is reference counted.
86 void CallbackLater(const CompletionCallback& callback, int result); 100 void CallbackLater(const CompletionCallback& callback, int result);
(...skipping 12 matching lines...) Expand all
99 std::string key_; 113 std::string key_;
100 std::vector<char> data_[kNumCacheEntryDataIndices]; 114 std::vector<char> data_[kNumCacheEntryDataIndices];
101 int test_mode_; 115 int test_mode_;
102 bool doomed_; 116 bool doomed_;
103 bool sparse_; 117 bool sparse_;
104 bool fail_requests_; 118 bool fail_requests_;
105 bool fail_sparse_requests_; 119 bool fail_sparse_requests_;
106 bool busy_; 120 bool busy_;
107 bool delayed_; 121 bool delayed_;
108 bool cancel_; 122 bool cancel_;
123
124 // Used for pause and restart.
125 DeferOp defer_op_;
126 CompletionCallback resume_callback_;
127 int resume_return_code_;
128
109 static bool ignore_callbacks_; 129 static bool ignore_callbacks_;
110 }; 130 };
111 131
112 class MockDiskCache : public disk_cache::Backend { 132 class MockDiskCache : public disk_cache::Backend {
113 public: 133 public:
114 MockDiskCache(); 134 MockDiskCache();
115 ~MockDiskCache() override; 135 ~MockDiskCache() override;
116 136
117 CacheType GetCacheType() const override; 137 CacheType GetCacheType() const override;
118 int32_t GetEntryCount() const override; 138 int32_t GetEntryCount() const override;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 void set_soft_failures(bool value) { soft_failures_ = value; } 174 void set_soft_failures(bool value) { soft_failures_ = value; }
155 175
156 // Makes sure that CreateEntry is not called twice for a given key. 176 // Makes sure that CreateEntry is not called twice for a given key.
157 void set_double_create_check(bool value) { double_create_check_ = value; } 177 void set_double_create_check(bool value) { double_create_check_ = value; }
158 178
159 // Makes all requests for data ranges to fail as not implemented. 179 // Makes all requests for data ranges to fail as not implemented.
160 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } 180 void set_fail_sparse_requests() { fail_sparse_requests_ = true; }
161 181
162 void ReleaseAll(); 182 void ReleaseAll();
163 183
184 // Returns true if a doomed entry exists with this key.
164 bool IsDiskEntryDoomed(const std::string& key); 185 bool IsDiskEntryDoomed(const std::string& key);
165 186
187 // Defers invoking the callback for the given operation. Calling code should
188 // invoke ResumeCacheOperation to resume.
189 void SetDefer(MockDiskEntry::DeferOp defer_op) { defer_op_ = defer_op; }
190
191 // Resume deferred cache operation by posting |resume_callback_| with
192 // |resume_return_code_|.
193 void ResumeCacheOperation();
194
195 // Returns a reference to the disk entry with the given |key|.
196 scoped_refptr<MockDiskEntry> GetDiskEntryRef(const std::string& key);
197
166 private: 198 private:
167 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; 199 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>;
168 class NotImplementedIterator; 200 class NotImplementedIterator;
169 201
170 void CallbackLater(const CompletionCallback& callback, int result); 202 void CallbackLater(const CompletionCallback& callback, int result);
171 203
172 EntryMap entries_; 204 EntryMap entries_;
173 int open_count_; 205 int open_count_;
174 int create_count_; 206 int create_count_;
175 int doomed_count_; 207 int doomed_count_;
176 bool fail_requests_; 208 bool fail_requests_;
177 bool soft_failures_; 209 bool soft_failures_;
178 bool double_create_check_; 210 bool double_create_check_;
179 bool fail_sparse_requests_; 211 bool fail_sparse_requests_;
212
213 // Used for pause and restart.
214 MockDiskEntry::DeferOp defer_op_;
215 CompletionCallback resume_callback_;
216 int resume_return_code_;
180 }; 217 };
181 218
182 class MockBackendFactory : public HttpCache::BackendFactory { 219 class MockBackendFactory : public HttpCache::BackendFactory {
183 public: 220 public:
184 int CreateBackend(NetLog* net_log, 221 int CreateBackend(NetLog* net_log,
185 std::unique_ptr<disk_cache::Backend>* backend, 222 std::unique_ptr<disk_cache::Backend>* backend,
186 const CompletionCallback& callback) override; 223 const CompletionCallback& callback) override;
187 }; 224 };
188 225
189 class MockHttpCache { 226 class MockHttpCache {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 NetLog* net_log); 274 NetLog* net_log);
238 275
239 // Returns the test mode after considering the global override. 276 // Returns the test mode after considering the global override.
240 static int GetTestMode(int test_mode); 277 static int GetTestMode(int test_mode);
241 278
242 // Overrides the test mode for a given operation. Remember to reset it after 279 // Overrides the test mode for a given operation. Remember to reset it after
243 // the test! (by setting test_mode to zero). 280 // the test! (by setting test_mode to zero).
244 static void SetTestMode(int test_mode); 281 static void SetTestMode(int test_mode);
245 282
246 // Functions to test the state of ActiveEntry. 283 // Functions to test the state of ActiveEntry.
247
248 bool IsWriterPresent(const std::string& key); 284 bool IsWriterPresent(const std::string& key);
249 bool IsHeadersTransactionPresent(const std::string& key); 285 bool IsHeadersTransactionPresent(const std::string& key);
250 int GetCountReaders(const std::string& key); 286 int GetCountReaders(const std::string& key);
251 int GetCountAddToEntryQueue(const std::string& key); 287 int GetCountAddToEntryQueue(const std::string& key);
252 int GetCountDoneHeadersQueue(const std::string& key); 288 int GetCountDoneHeadersQueue(const std::string& key);
253 289
254 private: 290 private:
255 HttpCache http_cache_; 291 HttpCache http_cache_;
256 }; 292 };
257 293
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 329
294 std::unique_ptr<disk_cache::Backend>* backend_; 330 std::unique_ptr<disk_cache::Backend>* backend_;
295 CompletionCallback callback_; 331 CompletionCallback callback_;
296 bool block_; 332 bool block_;
297 bool fail_; 333 bool fail_;
298 }; 334 };
299 335
300 } // namespace net 336 } // namespace net
301 337
302 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ 338 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_
OLDNEW
« no previous file with comments | « net/http/http_cache_unittest.cc ('k') | net/http/mock_http_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698