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

Side by Side Diff: net/http/http_cache_lookup_manager_unittest.cc

Issue 2675343002: Server push cancellation: add NetLogs to track cache lookup transaction (Closed)
Patch Set: address rch's comments Created 3 years, 10 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
OLDNEW
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 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 #include <string> 5 #include <string>
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/base/test_completion_callback.h" 10 #include "net/base/test_completion_callback.h"
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 EXPECT_THAT(rv, IsOk()); 77 EXPECT_THAT(rv, IsOk());
78 std::string expected(mock_trans->data); 78 std::string expected(mock_trans->data);
79 EXPECT_EQ(expected, content); 79 EXPECT_EQ(expected, content);
80 RemoveMockTransaction(mock_trans.get()); 80 RemoveMockTransaction(mock_trans.get());
81 } 81 }
82 82
83 } // namespace 83 } // namespace
84 84
85 TEST(HttpCacheLookupManagerTest, ServerPushMissCache) { 85 TEST(HttpCacheLookupManagerTest, ServerPushMissCache) {
86 MockHttpCache mock_cache; 86 MockHttpCache mock_cache;
87 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), 87 NetLog net_log;
88 NetLogWithSource()); 88 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), &net_log);
89 GURL request_url("http://www.example.com/pushed.jpg"); 89 GURL request_url("http://www.example.com/pushed.jpg");
90 90
91 std::unique_ptr<MockServerPushHelper> push_helper = 91 std::unique_ptr<MockServerPushHelper> push_helper =
92 base::MakeUnique<MockServerPushHelper>(request_url); 92 base::MakeUnique<MockServerPushHelper>(request_url);
93 MockServerPushHelper* push_helper_ptr = push_helper.get(); 93 MockServerPushHelper* push_helper_ptr = push_helper.get();
94 94
95 // Receive a server push and should not cancel the push. 95 // Receive a server push and should not cancel the push.
96 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0); 96 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
97 push_delegate.OnPush(std::move(push_helper)); 97 push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
98 base::RunLoop().RunUntilIdle(); 98 base::RunLoop().RunUntilIdle();
99 99
100 // Make sure no network transaction is created. 100 // Make sure no network transaction is created.
101 EXPECT_EQ(0, mock_cache.network_layer()->transaction_count()); 101 EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
102 EXPECT_EQ(0, mock_cache.disk_cache()->open_count()); 102 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
103 EXPECT_EQ(0, mock_cache.disk_cache()->create_count()); 103 EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
104 } 104 }
105 105
106 TEST(HttpCacheLookupManagerTest, ServerPushDoNotCreateCacheEntry) { 106 TEST(HttpCacheLookupManagerTest, ServerPushDoNotCreateCacheEntry) {
107 MockHttpCache mock_cache; 107 MockHttpCache mock_cache;
108 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), 108 NetLog net_log;
109 NetLogWithSource()); 109 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), &net_log);
110 GURL request_url("http://www.example.com/pushed.jpg"); 110 GURL request_url("http://www.example.com/pushed.jpg");
111 111
112 std::unique_ptr<MockServerPushHelper> push_helper = 112 std::unique_ptr<MockServerPushHelper> push_helper =
113 base::MakeUnique<MockServerPushHelper>(request_url); 113 base::MakeUnique<MockServerPushHelper>(request_url);
114 MockServerPushHelper* push_helper_ptr = push_helper.get(); 114 MockServerPushHelper* push_helper_ptr = push_helper.get();
115 115
116 // Receive a server push and should not cancel the push. 116 // Receive a server push and should not cancel the push.
117 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0); 117 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
118 push_delegate.OnPush(std::move(push_helper)); 118 push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
119 base::RunLoop().RunUntilIdle(); 119 base::RunLoop().RunUntilIdle();
120 120
121 // Receive another server push for the same url. 121 // Receive another server push for the same url.
122 std::unique_ptr<MockServerPushHelper> push_helper2 = 122 std::unique_ptr<MockServerPushHelper> push_helper2 =
123 base::MakeUnique<MockServerPushHelper>(request_url); 123 base::MakeUnique<MockServerPushHelper>(request_url);
124 MockServerPushHelper* push_helper_ptr2 = push_helper2.get(); 124 MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
125 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0); 125 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
126 push_delegate.OnPush(std::move(push_helper2)); 126 push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
127 base::RunLoop().RunUntilIdle(); 127 base::RunLoop().RunUntilIdle();
128 128
129 // Verify no network transaction is created. 129 // Verify no network transaction is created.
130 EXPECT_EQ(0, mock_cache.network_layer()->transaction_count()); 130 EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
131 EXPECT_EQ(0, mock_cache.disk_cache()->open_count()); 131 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
132 // Verify no cache entry is created for server push lookup. 132 // Verify no cache entry is created for server push lookup.
133 EXPECT_EQ(0, mock_cache.disk_cache()->create_count()); 133 EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
134 } 134 }
135 135
136 TEST(HttpCacheLookupManagerTest, ServerPushHitCache) { 136 TEST(HttpCacheLookupManagerTest, ServerPushHitCache) {
137 MockHttpCache mock_cache; 137 MockHttpCache mock_cache;
138 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), 138 NetLog net_log;
139 NetLogWithSource()); 139 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), &net_log);
140 GURL request_url("http://www.example.com/pushed.jpg"); 140 GURL request_url("http://www.example.com/pushed.jpg");
141 141
142 // Populate the cache entry so that the cache lookup for server push hits. 142 // Populate the cache entry so that the cache lookup for server push hits.
143 PopulateCacheEntry(mock_cache.http_cache(), request_url); 143 PopulateCacheEntry(mock_cache.http_cache(), request_url);
144 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 144 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
145 EXPECT_EQ(0, mock_cache.disk_cache()->open_count()); 145 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
146 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 146 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
147 147
148 // Add another mock transaction since the OnPush will create a new cache 148 // Add another mock transaction since the OnPush will create a new cache
149 // transaction. 149 // transaction.
150 std::unique_ptr<MockTransaction> mock_trans = 150 std::unique_ptr<MockTransaction> mock_trans =
151 CreateMockTransaction(request_url); 151 CreateMockTransaction(request_url);
152 AddMockTransaction(mock_trans.get()); 152 AddMockTransaction(mock_trans.get());
153 153
154 std::unique_ptr<MockServerPushHelper> push_helper = 154 std::unique_ptr<MockServerPushHelper> push_helper =
155 base::MakeUnique<MockServerPushHelper>(request_url); 155 base::MakeUnique<MockServerPushHelper>(request_url);
156 MockServerPushHelper* push_helper_ptr = push_helper.get(); 156 MockServerPushHelper* push_helper_ptr = push_helper.get();
157 157
158 // Receive a server push and should cancel the push. 158 // Receive a server push and should cancel the push.
159 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1); 159 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
160 push_delegate.OnPush(std::move(push_helper)); 160 push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
161 base::RunLoop().RunUntilIdle(); 161 base::RunLoop().RunUntilIdle();
162 162
163 // Make sure no new net layer transaction is created. 163 // Make sure no new net layer transaction is created.
164 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 164 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
165 EXPECT_EQ(1, mock_cache.disk_cache()->open_count()); 165 EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
166 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 166 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
167 167
168 RemoveMockTransaction(mock_trans.get()); 168 RemoveMockTransaction(mock_trans.get());
169 } 169 }
170 170
171 // Test when a server push is received while the HttpCacheLookupManager has a 171 // Test when a server push is received while the HttpCacheLookupManager has a
172 // pending lookup transaction for the same URL, the new server push will not 172 // pending lookup transaction for the same URL, the new server push will not
173 // send a new lookup transaction and should not be canceled. 173 // send a new lookup transaction and should not be canceled.
174 TEST(HttpCacheLookupManagerTest, ServerPushPendingLookup) { 174 TEST(HttpCacheLookupManagerTest, ServerPushPendingLookup) {
175 MockHttpCache mock_cache; 175 MockHttpCache mock_cache;
176 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), 176 NetLog net_log;
177 NetLogWithSource()); 177 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), &net_log);
178 GURL request_url("http://www.example.com/pushed.jpg"); 178 GURL request_url("http://www.example.com/pushed.jpg");
179 179
180 // Populate the cache entry so that the cache lookup for server push hits. 180 // Populate the cache entry so that the cache lookup for server push hits.
181 PopulateCacheEntry(mock_cache.http_cache(), request_url); 181 PopulateCacheEntry(mock_cache.http_cache(), request_url);
182 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 182 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
183 EXPECT_EQ(0, mock_cache.disk_cache()->open_count()); 183 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
184 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 184 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
185 185
186 // Add another mock transaction since the OnPush will create a new cache 186 // Add another mock transaction since the OnPush will create a new cache
187 // transaction. 187 // transaction.
188 std::unique_ptr<MockTransaction> mock_trans = 188 std::unique_ptr<MockTransaction> mock_trans =
189 CreateMockTransaction(request_url); 189 CreateMockTransaction(request_url);
190 AddMockTransaction(mock_trans.get()); 190 AddMockTransaction(mock_trans.get());
191 191
192 std::unique_ptr<MockServerPushHelper> push_helper = 192 std::unique_ptr<MockServerPushHelper> push_helper =
193 base::MakeUnique<MockServerPushHelper>(request_url); 193 base::MakeUnique<MockServerPushHelper>(request_url);
194 MockServerPushHelper* push_helper_ptr = push_helper.get(); 194 MockServerPushHelper* push_helper_ptr = push_helper.get();
195 195
196 // Receive a server push and should cancel the push eventually. 196 // Receive a server push and should cancel the push eventually.
197 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1); 197 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
198 push_delegate.OnPush(std::move(push_helper)); 198 push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
199 199
200 std::unique_ptr<MockServerPushHelper> push_helper2 = 200 std::unique_ptr<MockServerPushHelper> push_helper2 =
201 base::MakeUnique<MockServerPushHelper>(request_url); 201 base::MakeUnique<MockServerPushHelper>(request_url);
202 MockServerPushHelper* push_helper_ptr2 = push_helper2.get(); 202 MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
203 203
204 // Receive another server push and should not cancel the push. 204 // Receive another server push and should not cancel the push.
205 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0); 205 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
206 push_delegate.OnPush(std::move(push_helper2)); 206 push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
207 207
208 base::RunLoop().RunUntilIdle(); 208 base::RunLoop().RunUntilIdle();
209 209
210 // Make sure no new net layer transaction is created. 210 // Make sure no new net layer transaction is created.
211 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 211 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
212 EXPECT_EQ(1, mock_cache.disk_cache()->open_count()); 212 EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
213 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 213 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
214 214
215 RemoveMockTransaction(mock_trans.get()); 215 RemoveMockTransaction(mock_trans.get());
216 } 216 }
217 217
218 // Test the server push lookup is based on the full url. 218 // Test the server push lookup is based on the full url.
219 TEST(HttpCacheLookupManagerTest, ServerPushLookupOnUrl) { 219 TEST(HttpCacheLookupManagerTest, ServerPushLookupOnUrl) {
220 MockHttpCache mock_cache; 220 MockHttpCache mock_cache;
221 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), 221 NetLog net_log;
eroman 2017/02/07 22:33:42 If you change some of these to use TestNetLog, the
Zhongyi Shi 2017/02/08 00:40:53 Will do in a follow-up CL since I realized that th
222 NetLogWithSource()); 222 HttpCacheLookupManager push_delegate(mock_cache.http_cache(), &net_log);
223 GURL request_url("http://www.example.com/pushed.jpg?u=0"); 223 GURL request_url("http://www.example.com/pushed.jpg?u=0");
224 GURL request_url2("http://www.example.com/pushed.jpg?u=1"); 224 GURL request_url2("http://www.example.com/pushed.jpg?u=1");
225 225
226 // Populate the cache entry so that the cache lookup for server push hits. 226 // Populate the cache entry so that the cache lookup for server push hits.
227 PopulateCacheEntry(mock_cache.http_cache(), request_url); 227 PopulateCacheEntry(mock_cache.http_cache(), request_url);
228 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 228 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
229 EXPECT_EQ(0, mock_cache.disk_cache()->open_count()); 229 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
230 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 230 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
231 231
232 // Add another mock transaction since the OnPush will create a new cache 232 // Add another mock transaction since the OnPush will create a new cache
233 // transaction. 233 // transaction.
234 std::unique_ptr<MockTransaction> mock_trans = 234 std::unique_ptr<MockTransaction> mock_trans =
235 CreateMockTransaction(request_url); 235 CreateMockTransaction(request_url);
236 AddMockTransaction(mock_trans.get()); 236 AddMockTransaction(mock_trans.get());
237 237
238 std::unique_ptr<MockServerPushHelper> push_helper = 238 std::unique_ptr<MockServerPushHelper> push_helper =
239 base::MakeUnique<MockServerPushHelper>(request_url); 239 base::MakeUnique<MockServerPushHelper>(request_url);
240 MockServerPushHelper* push_helper_ptr = push_helper.get(); 240 MockServerPushHelper* push_helper_ptr = push_helper.get();
241 241
242 // Receive a server push and should cancel the push eventually. 242 // Receive a server push and should cancel the push eventually.
243 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1); 243 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
244 push_delegate.OnPush(std::move(push_helper)); 244 push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
245 // Run until the lookup transaction finishes for the first server push. 245 // Run until the lookup transaction finishes for the first server push.
246 base::RunLoop().RunUntilIdle(); 246 base::RunLoop().RunUntilIdle();
247 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 247 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
248 EXPECT_EQ(1, mock_cache.disk_cache()->open_count()); 248 EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
249 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 249 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
250 RemoveMockTransaction(mock_trans.get()); 250 RemoveMockTransaction(mock_trans.get());
251 251
252 AddMockTransaction(mock_trans.get()); 252 AddMockTransaction(mock_trans.get());
253 // Receive the second server push with same url after the first lookup 253 // Receive the second server push with same url after the first lookup
254 // finishes, and should cancel the push. 254 // finishes, and should cancel the push.
255 std::unique_ptr<MockServerPushHelper> push_helper2 = 255 std::unique_ptr<MockServerPushHelper> push_helper2 =
256 base::MakeUnique<MockServerPushHelper>(request_url); 256 base::MakeUnique<MockServerPushHelper>(request_url);
257 MockServerPushHelper* push_helper_ptr2 = push_helper2.get(); 257 MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
258 258
259 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(1); 259 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(1);
260 push_delegate.OnPush(std::move(push_helper2)); 260 push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
261 // Run until the lookup transaction finishes for the second server push. 261 // Run until the lookup transaction finishes for the second server push.
262 base::RunLoop().RunUntilIdle(); 262 base::RunLoop().RunUntilIdle();
263 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 263 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
264 EXPECT_EQ(2, mock_cache.disk_cache()->open_count()); 264 EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
265 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 265 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
266 RemoveMockTransaction(mock_trans.get()); 266 RemoveMockTransaction(mock_trans.get());
267 267
268 std::unique_ptr<MockTransaction> mock_trans3 = 268 std::unique_ptr<MockTransaction> mock_trans3 =
269 CreateMockTransaction(request_url2); 269 CreateMockTransaction(request_url2);
270 AddMockTransaction(mock_trans3.get()); 270 AddMockTransaction(mock_trans3.get());
271 // Receive the third server push with a different url after lookup for a 271 // Receive the third server push with a different url after lookup for a
272 // similar server push has been completed, should not cancel the push. 272 // similar server push has been completed, should not cancel the push.
273 std::unique_ptr<MockServerPushHelper> push_helper3 = 273 std::unique_ptr<MockServerPushHelper> push_helper3 =
274 base::MakeUnique<MockServerPushHelper>(request_url2); 274 base::MakeUnique<MockServerPushHelper>(request_url2);
275 MockServerPushHelper* push_helper_ptr3 = push_helper3.get(); 275 MockServerPushHelper* push_helper_ptr3 = push_helper3.get();
276 276
277 EXPECT_CALL(*push_helper_ptr3, Cancel()).Times(0); 277 EXPECT_CALL(*push_helper_ptr3, Cancel()).Times(0);
278 push_delegate.OnPush(std::move(push_helper3)); 278 push_delegate.OnPush(std::move(push_helper3), NetLogWithSource());
279 279
280 base::RunLoop().RunUntilIdle(); 280 base::RunLoop().RunUntilIdle();
281 // Make sure no new net layer transaction is created. 281 // Make sure no new net layer transaction is created.
282 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); 282 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
283 EXPECT_EQ(2, mock_cache.disk_cache()->open_count()); 283 EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
284 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); 284 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
285 RemoveMockTransaction(mock_trans3.get()); 285 RemoveMockTransaction(mock_trans3.get());
286 } 286 }
287 287
288 } // namespace net 288 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698