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_ |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 int buf_len, | 57 int buf_len, |
58 const CompletionCallback& callback) override; | 58 const CompletionCallback& callback) override; |
59 int GetAvailableRange(int64_t offset, | 59 int GetAvailableRange(int64_t offset, |
60 int len, | 60 int len, |
61 int64_t* start, | 61 int64_t* start, |
62 const CompletionCallback& callback) override; | 62 const CompletionCallback& callback) override; |
63 bool CouldBeSparse() const override; | 63 bool CouldBeSparse() const override; |
64 void CancelSparseIO() override; | 64 void CancelSparseIO() override; |
65 int ReadyForSparseIO(const CompletionCallback& completion_callback) override; | 65 int ReadyForSparseIO(const CompletionCallback& completion_callback) override; |
66 | 66 |
67 uint8_t memory_entry_data() const { return memory_entry_data_; } | |
68 void set_memory_entry_data(uint8_t val) { memory_entry_data_ = val; } | |
69 | |
67 // Fail most subsequent requests. | 70 // Fail most subsequent requests. |
68 void set_fail_requests() { fail_requests_ = true; } | 71 void set_fail_requests() { fail_requests_ = true; } |
69 | 72 |
70 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 73 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
71 | 74 |
72 // If |value| is true, don't deliver any completion callbacks until called | 75 // If |value| is true, don't deliver any completion callbacks until called |
73 // again with |value| set to false. Caution: remember to enable callbacks | 76 // again with |value| set to false. Caution: remember to enable callbacks |
74 // again or all subsequent tests will fail. | 77 // again or all subsequent tests will fail. |
75 static void IgnoreCallbacks(bool value); | 78 static void IgnoreCallbacks(bool value); |
76 | 79 |
(...skipping 14 matching lines...) Expand all Loading... | |
91 // delivers any callback previously stored. | 94 // delivers any callback previously stored. |
92 static void StoreAndDeliverCallbacks(bool store, | 95 static void StoreAndDeliverCallbacks(bool store, |
93 MockDiskEntry* entry, | 96 MockDiskEntry* entry, |
94 const CompletionCallback& callback, | 97 const CompletionCallback& callback, |
95 int result); | 98 int result); |
96 | 99 |
97 static const int kNumCacheEntryDataIndices = 3; | 100 static const int kNumCacheEntryDataIndices = 3; |
98 | 101 |
99 std::string key_; | 102 std::string key_; |
100 std::vector<char> data_[kNumCacheEntryDataIndices]; | 103 std::vector<char> data_[kNumCacheEntryDataIndices]; |
104 unsigned char memory_entry_data_; | |
101 int test_mode_; | 105 int test_mode_; |
102 bool doomed_; | 106 bool doomed_; |
103 bool sparse_; | 107 bool sparse_; |
104 bool fail_requests_; | 108 bool fail_requests_; |
105 bool fail_sparse_requests_; | 109 bool fail_sparse_requests_; |
106 bool busy_; | 110 bool busy_; |
107 bool delayed_; | 111 bool delayed_; |
108 bool cancel_; | 112 bool cancel_; |
109 static bool ignore_callbacks_; | 113 static bool ignore_callbacks_; |
110 }; | 114 }; |
(...skipping 19 matching lines...) Expand all Loading... | |
130 const CompletionCallback& callback) override; | 134 const CompletionCallback& callback) override; |
131 int DoomEntriesSince(base::Time initial_time, | 135 int DoomEntriesSince(base::Time initial_time, |
132 const CompletionCallback& callback) override; | 136 const CompletionCallback& callback) override; |
133 int CalculateSizeOfAllEntries(const CompletionCallback& callback) override; | 137 int CalculateSizeOfAllEntries(const CompletionCallback& callback) override; |
134 std::unique_ptr<Iterator> CreateIterator() override; | 138 std::unique_ptr<Iterator> CreateIterator() override; |
135 void GetStats(base::StringPairs* stats) override; | 139 void GetStats(base::StringPairs* stats) override; |
136 void OnExternalCacheHit(const std::string& key) override; | 140 void OnExternalCacheHit(const std::string& key) override; |
137 size_t DumpMemoryStats( | 141 size_t DumpMemoryStats( |
138 base::trace_event::ProcessMemoryDump* pmd, | 142 base::trace_event::ProcessMemoryDump* pmd, |
139 const std::string& parent_absolute_name) const override; | 143 const std::string& parent_absolute_name) const override; |
144 uint8_t GetMemoryEntryData(const std::string& key) override; | |
145 void SetMemoryEntryData(const std::string& key, uint8_t data) override; | |
140 | 146 |
141 // Returns number of times a cache entry was successfully opened. | 147 // Returns number of times a cache entry was successfully opened. |
142 int open_count() const { return open_count_; } | 148 int open_count() const { return open_count_; } |
143 | 149 |
144 // Returns number of times a cache entry was successfully created. | 150 // Returns number of times a cache entry was successfully created. |
145 int create_count() const { return create_count_; } | 151 int create_count() const { return create_count_; } |
146 | 152 |
147 // Fail any subsequent CreateEntry and OpenEntry. | 153 // Fail any subsequent CreateEntry and OpenEntry. |
148 void set_fail_requests() { fail_requests_ = true; } | 154 void set_fail_requests() { fail_requests_ = true; } |
149 | 155 |
150 // Return entries that fail some of their requests. | 156 // Return entries that fail some of their requests. |
151 void set_soft_failures(bool value) { soft_failures_ = value; } | 157 void set_soft_failures(bool value) { soft_failures_ = value; } |
152 | 158 |
153 // Makes sure that CreateEntry is not called twice for a given key. | 159 // Makes sure that CreateEntry is not called twice for a given key. |
154 void set_double_create_check(bool value) { double_create_check_ = value; } | 160 void set_double_create_check(bool value) { double_create_check_ = value; } |
155 | 161 |
162 // Determines whether to provide the GetMemoryEntryData/SetMemoryEntryData | |
163 // interface. Default is true | |
164 void set_support_memory_entry_data(bool value) { | |
165 support_memory_entry_data_ = value; | |
166 } | |
167 | |
156 // Makes all requests for data ranges to fail as not implemented. | 168 // Makes all requests for data ranges to fail as not implemented. |
157 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 169 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
158 | 170 |
159 void ReleaseAll(); | 171 void ReleaseAll(); |
160 | 172 |
161 bool IsDiskEntryDoomed(const std::string& key); | 173 bool IsDiskEntryDoomed(const std::string& key); |
162 | 174 |
163 private: | 175 private: |
164 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; | 176 using EntryMap = std::unordered_map<std::string, MockDiskEntry*>; |
165 class NotImplementedIterator; | 177 class NotImplementedIterator; |
166 | 178 |
167 void CallbackLater(const CompletionCallback& callback, int result); | 179 void CallbackLater(const CompletionCallback& callback, int result); |
168 | 180 |
169 EntryMap entries_; | 181 EntryMap entries_; |
170 int open_count_; | 182 int open_count_; |
171 int create_count_; | 183 int create_count_; |
172 bool fail_requests_; | 184 bool fail_requests_; |
173 bool soft_failures_; | 185 bool soft_failures_; |
174 bool double_create_check_; | 186 bool double_create_check_; |
175 bool fail_sparse_requests_; | 187 bool fail_sparse_requests_; |
188 bool support_memory_entry_data_; | |
gavinp
2017/08/04 17:33:14
This CL could be a good chance to switch to in-cla
Maks Orlovich
2017/08/23 18:52:06
Not a fan of mixing in very wide-scale cleanups...
| |
176 }; | 189 }; |
177 | 190 |
178 class MockBackendFactory : public HttpCache::BackendFactory { | 191 class MockBackendFactory : public HttpCache::BackendFactory { |
179 public: | 192 public: |
180 int CreateBackend(NetLog* net_log, | 193 int CreateBackend(NetLog* net_log, |
181 std::unique_ptr<disk_cache::Backend>* backend, | 194 std::unique_ptr<disk_cache::Backend>* backend, |
182 const CompletionCallback& callback) override; | 195 const CompletionCallback& callback) override; |
183 }; | 196 }; |
184 | 197 |
185 class MockHttpCache { | 198 class MockHttpCache { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 | 302 |
290 std::unique_ptr<disk_cache::Backend>* backend_; | 303 std::unique_ptr<disk_cache::Backend>* backend_; |
291 CompletionCallback callback_; | 304 CompletionCallback callback_; |
292 bool block_; | 305 bool block_; |
293 bool fail_; | 306 bool fail_; |
294 }; | 307 }; |
295 | 308 |
296 } // namespace net | 309 } // namespace net |
297 | 310 |
298 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 311 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
OLD | NEW |