Chromium Code Reviews| 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 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 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 ResumeDiskEntryOperation(); | |
| 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 // Defers invoking the callback for the given operation. | |
|
jkarlin
2017/07/14 16:45:42
Please add a comment here on when you should call
shivanisha
2017/07/14 16:56:49
Done
| |
| 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(); | |
| 191 | |
| 192 // Returns a reference to the disk entry with the given |key|. | |
| 193 scoped_refptr<MockDiskEntry> GetDiskEntryRef(const std::string& key); | |
| 194 | |
| 166 private: | 195 private: |
| 167 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; | 196 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; |
| 168 class NotImplementedIterator; | 197 class NotImplementedIterator; |
| 169 | 198 |
| 170 void CallbackLater(const CompletionCallback& callback, int result); | 199 void CallbackLater(const CompletionCallback& callback, int result); |
| 171 | 200 |
| 172 EntryMap entries_; | 201 EntryMap entries_; |
| 173 int open_count_; | 202 int open_count_; |
| 174 int create_count_; | 203 int create_count_; |
| 175 int doomed_count_; | 204 int doomed_count_; |
| 176 bool fail_requests_; | 205 bool fail_requests_; |
| 177 bool soft_failures_; | 206 bool soft_failures_; |
| 178 bool double_create_check_; | 207 bool double_create_check_; |
| 179 bool fail_sparse_requests_; | 208 bool fail_sparse_requests_; |
| 209 | |
| 210 // Used for pause and restart. | |
| 211 MockDiskEntry::DeferOp defer_op_; | |
| 212 CompletionCallback resume_callback_; | |
| 213 int resume_return_code_; | |
| 180 }; | 214 }; |
| 181 | 215 |
| 182 class MockBackendFactory : public HttpCache::BackendFactory { | 216 class MockBackendFactory : public HttpCache::BackendFactory { |
| 183 public: | 217 public: |
| 184 int CreateBackend(NetLog* net_log, | 218 int CreateBackend(NetLog* net_log, |
| 185 std::unique_ptr<disk_cache::Backend>* backend, | 219 std::unique_ptr<disk_cache::Backend>* backend, |
| 186 const CompletionCallback& callback) override; | 220 const CompletionCallback& callback) override; |
| 187 }; | 221 }; |
| 188 | 222 |
| 189 class MockHttpCache { | 223 class MockHttpCache { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 NetLog* net_log); | 271 NetLog* net_log); |
| 238 | 272 |
| 239 // Returns the test mode after considering the global override. | 273 // Returns the test mode after considering the global override. |
| 240 static int GetTestMode(int test_mode); | 274 static int GetTestMode(int test_mode); |
| 241 | 275 |
| 242 // Overrides the test mode for a given operation. Remember to reset it after | 276 // Overrides the test mode for a given operation. Remember to reset it after |
| 243 // the test! (by setting test_mode to zero). | 277 // the test! (by setting test_mode to zero). |
| 244 static void SetTestMode(int test_mode); | 278 static void SetTestMode(int test_mode); |
| 245 | 279 |
| 246 // Functions to test the state of ActiveEntry. | 280 // Functions to test the state of ActiveEntry. |
| 247 | |
| 248 bool IsWriterPresent(const std::string& key); | 281 bool IsWriterPresent(const std::string& key); |
| 249 bool IsHeadersTransactionPresent(const std::string& key); | 282 bool IsHeadersTransactionPresent(const std::string& key); |
| 250 int GetCountReaders(const std::string& key); | 283 int GetCountReaders(const std::string& key); |
| 251 int GetCountAddToEntryQueue(const std::string& key); | 284 int GetCountAddToEntryQueue(const std::string& key); |
| 252 int GetCountDoneHeadersQueue(const std::string& key); | 285 int GetCountDoneHeadersQueue(const std::string& key); |
| 253 | 286 |
| 254 private: | 287 private: |
| 255 HttpCache http_cache_; | 288 HttpCache http_cache_; |
| 256 }; | 289 }; |
| 257 | 290 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 | 326 |
| 294 std::unique_ptr<disk_cache::Backend>* backend_; | 327 std::unique_ptr<disk_cache::Backend>* backend_; |
| 295 CompletionCallback callback_; | 328 CompletionCallback callback_; |
| 296 bool block_; | 329 bool block_; |
| 297 bool fail_; | 330 bool fail_; |
| 298 }; | 331 }; |
| 299 | 332 |
| 300 } // namespace net | 333 } // namespace net |
| 301 | 334 |
| 302 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 335 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
| OLD | NEW |