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

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: GetDiskEntryRef added and doomed_entries_ removed 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 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
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
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 // Defers invoking the callback for the given operation.
186 void SetDefer(MockDiskEntry::DeferOp defer_op) { defer_op_ = defer_op; }
187
188 // Resume deferred cache operation by posting |resume_callback_| with
189 // |resume_return_code_|. Returns ERR_IO_PENDING.
190 int ResumeCacheOperation();
jkarlin 2017/07/13 17:27:24 It's unclear when you should get the entry and cal
shivanisha 2017/07/13 18:53:19 Renaming this ResumeDiskEntryOperation to let it b
191
192 // Returns a reference to the disk entry with the given |key|. Also adds a
193 // reference for that entry, so the test should invoke Close() on it to
194 // release the reference count.
195 MockDiskEntry* GetDiskEntryRef(const std::string& key);
jkarlin 2017/07/13 17:27:25 scoped_refptr<MockDiskEntry> GetDiskEntryRef(const
shivanisha 2017/07/13 18:53:45 done
196
166 private: 197 private:
167 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; 198 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>;
168 class NotImplementedIterator; 199 class NotImplementedIterator;
169 200
170 void CallbackLater(const CompletionCallback& callback, int result); 201 void CallbackLater(const CompletionCallback& callback, int result);
171 202
172 EntryMap entries_; 203 EntryMap entries_;
173 int open_count_; 204 int open_count_;
174 int create_count_; 205 int create_count_;
175 int doomed_count_; 206 int doomed_count_;
176 bool fail_requests_; 207 bool fail_requests_;
177 bool soft_failures_; 208 bool soft_failures_;
178 bool double_create_check_; 209 bool double_create_check_;
179 bool fail_sparse_requests_; 210 bool fail_sparse_requests_;
211
212 // Used for pause and restart.
213 MockDiskEntry::DeferOp defer_op_;
214 CompletionCallback resume_callback_;
215 int resume_return_code_;
180 }; 216 };
181 217
182 class MockBackendFactory : public HttpCache::BackendFactory { 218 class MockBackendFactory : public HttpCache::BackendFactory {
183 public: 219 public:
184 int CreateBackend(NetLog* net_log, 220 int CreateBackend(NetLog* net_log,
185 std::unique_ptr<disk_cache::Backend>* backend, 221 std::unique_ptr<disk_cache::Backend>* backend,
186 const CompletionCallback& callback) override; 222 const CompletionCallback& callback) override;
187 }; 223 };
188 224
189 class MockHttpCache { 225 class MockHttpCache {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 NetLog* net_log); 273 NetLog* net_log);
238 274
239 // Returns the test mode after considering the global override. 275 // Returns the test mode after considering the global override.
240 static int GetTestMode(int test_mode); 276 static int GetTestMode(int test_mode);
241 277
242 // Overrides the test mode for a given operation. Remember to reset it after 278 // Overrides the test mode for a given operation. Remember to reset it after
243 // the test! (by setting test_mode to zero). 279 // the test! (by setting test_mode to zero).
244 static void SetTestMode(int test_mode); 280 static void SetTestMode(int test_mode);
245 281
246 // Functions to test the state of ActiveEntry. 282 // Functions to test the state of ActiveEntry.
247
248 bool IsWriterPresent(const std::string& key); 283 bool IsWriterPresent(const std::string& key);
249 bool IsHeadersTransactionPresent(const std::string& key); 284 bool IsHeadersTransactionPresent(const std::string& key);
250 int GetCountReaders(const std::string& key); 285 int GetCountReaders(const std::string& key);
251 int GetCountAddToEntryQueue(const std::string& key); 286 int GetCountAddToEntryQueue(const std::string& key);
252 int GetCountDoneHeadersQueue(const std::string& key); 287 int GetCountDoneHeadersQueue(const std::string& key);
253 288
254 private: 289 private:
255 HttpCache http_cache_; 290 HttpCache http_cache_;
256 }; 291 };
257 292
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 328
294 std::unique_ptr<disk_cache::Backend>* backend_; 329 std::unique_ptr<disk_cache::Backend>* backend_;
295 CompletionCallback callback_; 330 CompletionCallback callback_;
296 bool block_; 331 bool block_;
297 bool fail_; 332 bool fail_;
298 }; 333 };
299 334
300 } // namespace net 335 } // namespace net
301 336
302 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ 337 #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