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

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

Issue 2519473002: Fixes the cache lock issue. (Closed)
Patch Set: Initial patch Created 4 years 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/http/http_cache.h" 5 #include "net/http/http_cache.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 void ReadAndVerifyTransaction(HttpTransaction* trans, 141 void ReadAndVerifyTransaction(HttpTransaction* trans,
142 const MockTransaction& trans_info) { 142 const MockTransaction& trans_info) {
143 std::string content; 143 std::string content;
144 int rv = ReadTransaction(trans, &content); 144 int rv = ReadTransaction(trans, &content);
145 145
146 EXPECT_THAT(rv, IsOk()); 146 EXPECT_THAT(rv, IsOk());
147 std::string expected(trans_info.data); 147 std::string expected(trans_info.data);
148 EXPECT_EQ(expected, content); 148 EXPECT_EQ(expected, content);
149 } 149 }
150 150
151 void ReadAndVerifySharedWritersCacheRead(std::vector<Context*>& context_list,
152 const MockTransaction& trans_info) {
153 std::vector<std::string> contents(context_list.size());
154 int rv = ReadSharedWritersCacheRead(context_list, contents);
155
156 EXPECT_THAT(rv, IsOk());
157 std::string expected(trans_info.data);
158 for (size_t i = 0; i < contents.size(); i++) {
159 EXPECT_EQ(expected, contents.at(i));
160 }
161 }
162
163 void ReadAndVerifySharedWritersJoinedRead(const MockTransaction& trans_info,
164 std::vector<Context*>& context_list) {
165 std::vector<std::string> results(context_list.size());
166 int rv = ReadSharedWritersJoinedRead(context_list, results);
167
168 EXPECT_THAT(rv, IsOk());
169 std::string expected(trans_info.data);
170 for (size_t i = 0; i < results.size(); i++) {
171 EXPECT_EQ(expected, results[i]);
172 }
173 }
174
175 void ReadAndVerifySharedWritersJoinedReadDoneReading(
176 const MockTransaction& trans_info,
177 std::vector<Context*>& context_list) {
178 std::vector<std::string> results(context_list.size());
179 int rv = ReadSharedWritersJoinedReadDoneReading(context_list, results);
180
181 EXPECT_THAT(rv, IsOk());
182 std::string expected(trans_info.data);
183 for (size_t i = 0; i < results.size(); i++) {
184 EXPECT_EQ(expected, results[i]);
185 }
186 }
187
188 void ReadAndVerifySharedWritersJoinedReadDoomCurrentWriter(
189 const MockTransaction& trans_info,
190 std::vector<Context*>& context_list) {
191 std::vector<std::string> results(context_list.size() - 1);
192 int rv = ReadSharedWritersJoinedReadDoomCurrentWriter(context_list, results);
193
194 EXPECT_THAT(rv, IsOk());
195 std::string expected(trans_info.data);
196 for (size_t i = 0; i < results.size(); i++) {
197 EXPECT_EQ(expected, results[i]);
198 }
199 }
200
201 void ReadAndVerifySharedWritersDeleteWaitingWriter(
202 const MockTransaction& trans_info,
203 std::vector<Context*>& context_list) {
204 std::vector<std::string> results(context_list.size() - 1);
205 int rv =
206 ReadSharedWritersJoinedReadDeleteWaitingWriter(context_list, results);
207
208 EXPECT_THAT(rv, IsOk());
209 std::string expected(trans_info.data);
210 for (size_t i = 0; i < results.size(); i++) {
211 EXPECT_EQ(expected, results[i]);
212 }
213 }
214
215 void ReadAndVerifySharedWritersDeleteIdleWriter(
216 const MockTransaction& trans_info,
217 std::vector<Context*>& context_list) {
218 std::vector<std::string> results(context_list.size() - 1);
219 int rv = ReadSharedWritersJoinedReadDeleteIdleWriter(context_list, results);
220
221 EXPECT_THAT(rv, IsOk());
222 std::string expected(trans_info.data);
223 for (size_t i = 0; i < results.size(); i++) {
224 EXPECT_EQ(expected, results[i]);
225 }
226 }
227
228 int ReadSharedWritersJoinedReadCacheWriteFailure(
229 std::vector<Context*>& context_list,
230 std::vector<std::string>& results,
231 MockHttpCache& cache) {
232 int rv = 0;
233 MockHttpRequest request(kSimpleGET_Transaction);
234
235 // Fail the request.
236 cache.disk_cache()->set_soft_failures(true);
237 // We have to open the entry again to propagate the failure flag.
238 disk_cache::Entry* en;
239 cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en);
240 en->Close();
241
242 // Invoke Read for a few transactions so they become current_writer_ and
243 // waiting_writers_ respectively.
244 std::vector<scoped_refptr<IOBuffer>> buf(2);
245 size_t i = 0, j = 0;
246 do {
247 HttpTransaction* trans = context_list[i]->trans.get();
248 buf[j] = new IOBuffer(30);
249 rv = trans->Read(buf[j].get(), 30, context_list[i]->callback.callback());
250 i = context_list.size() - 1;
251 j++;
252 } while (j < buf.size());
253
254 // current_writer_ which is transaction [0] should continue without writing
255 // to the cache, idle writer ([3]) should fail on its next Read call and
256 // waiting_writers_ ([6]) should return a failure back.
257 if (rv == ERR_IO_PENDING) {
258 Context* ct = *(context_list.begin());
259 rv = ct->callback.WaitForResult();
260 }
261
262 // Only [0] should succeed in the Read.
263 results[0].append(buf[0]->data(), 30);
264
265 // Invoke Start on 4 and 5. 4 will fail to read from the cache, doom the
266 // entry and go on to create a new entry. 5 will also get added to that
267 // entry.
268 for (size_t i = 4; i <= 5; i++) {
269 Context* c = context_list[i];
270 c->result =
271 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
272 }
273
274 base::RunLoop().RunUntilIdle();
275
276 // 3 and 6 failed, delete them.
277 Context* c = context_list[3];
278 scoped_refptr<IOBuffer> buf1 = new IOBuffer(30);
279 int rv_fail = c->trans->Read(buf1.get(), 30, c->callback.callback());
280 EXPECT_THAT(rv_fail, ERR_CACHE_WRITE_FAILURE);
281
282 Context* c6 = context_list[6];
283 for (auto itr = context_list.begin(); itr != context_list.end();) {
284 if (*itr == c || *itr == c6) {
285 Context* ct = *itr;
286 itr = context_list.erase(itr);
287 delete ct;
288 } else {
289 itr++;
290 }
291 }
292
293 cache.disk_cache()->set_soft_failures(false);
294
295 base::RunLoop().RunUntilIdle();
296
297 // Read for the rest of the transactions.
298 Context* c0 = context_list[0];
299 Context* c3 = context_list[3];
300 for (size_t i = 0; i < context_list.size(); i++) {
301 if (i == 1 || i == 2 || i == 4)
302 continue;
303 std::string res;
304 ReadTransaction(context_list[i]->trans.get(), &res);
305 results[i].append(res);
306 }
307
308 base::RunLoop().RunUntilIdle();
309
310 // Delete the done transactions so that pending queue can be processed.
311 // The reader transactions will error out with ERR_CACHE_MISS because of cache
312 // failures.
313 Context* c1 = context_list[1];
314 Context* c2 = context_list[2];
315 auto itr = context_list.begin();
316 for (; itr != context_list.end();) {
317 Context* c = *itr;
318 if (c == c0 || c == c1 || c == c2 || c == c3) {
319 itr = context_list.erase(itr);
320 delete c;
321 } else {
322 itr++;
323 }
324 }
325
326 std::string res;
327 ReadTransaction(context_list[0]->trans.get(), &res);
328 results[4].append(res);
329
330 return OK;
331 }
332
333 void ReadAndVerifySharedWritersJoinedReadCacheWriteFailure(
334 const MockTransaction& trans_info,
335 std::vector<Context*>& context_list,
336 MockHttpCache& cache) {
337 std::vector<std::string> results(context_list.size());
338 int rv = ReadSharedWritersJoinedReadCacheWriteFailure(context_list, results,
339 cache);
340
341 EXPECT_THAT(rv, IsOk());
342 std::string expected(trans_info.data);
343 for (size_t i = 0; i < context_list.size(); i++) {
344 EXPECT_EQ(expected, results[i]);
345 }
346 }
347
151 void RunTransactionTestBase(HttpCache* cache, 348 void RunTransactionTestBase(HttpCache* cache,
152 const MockTransaction& trans_info, 349 const MockTransaction& trans_info,
153 const MockHttpRequest& request, 350 const MockHttpRequest& request,
154 HttpResponseInfo* response_info, 351 HttpResponseInfo* response_info,
155 const NetLogWithSource& net_log, 352 const NetLogWithSource& net_log,
156 LoadTimingInfo* load_timing_info, 353 LoadTimingInfo* load_timing_info,
157 int64_t* sent_bytes, 354 int64_t* sent_bytes,
158 int64_t* received_bytes, 355 int64_t* received_bytes,
159 IPEndPoint* remote_endpoint) { 356 IPEndPoint* remote_endpoint) {
160 TestCompletionCallback callback;
161
162 // write to the cache 357 // write to the cache
163 358
164 std::unique_ptr<HttpTransaction> trans; 359 Context* c = new Context();
165 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); 360 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &c->trans);
166 EXPECT_THAT(rv, IsOk()); 361 EXPECT_THAT(rv, IsOk());
167 ASSERT_TRUE(trans.get()); 362 ASSERT_TRUE(c->trans.get());
168 363
169 rv = trans->Start(&request, callback.callback(), net_log); 364 rv = c->trans->Start(&request, c->callback.callback(), net_log);
170 if (rv == ERR_IO_PENDING) 365 if (rv == ERR_IO_PENDING)
171 rv = callback.WaitForResult(); 366 rv = c->callback.WaitForResult();
172 ASSERT_EQ(trans_info.return_code, rv); 367 ASSERT_EQ(trans_info.return_code, rv);
173 368
174 if (OK != rv) 369 if (OK != rv)
175 return; 370 return;
176 371
177 const HttpResponseInfo* response = trans->GetResponseInfo(); 372 const HttpResponseInfo* response = c->trans->GetResponseInfo();
178 ASSERT_TRUE(response); 373 ASSERT_TRUE(response);
179 374
180 if (response_info) 375 if (response_info)
181 *response_info = *response; 376 *response_info = *response;
182 377
183 if (load_timing_info) { 378 if (load_timing_info) {
184 // If a fake network connection is used, need a NetLog to get a fake socket 379 // If a fake network connection is used, need a NetLog to get a fake socket
185 // ID. 380 // ID.
186 EXPECT_TRUE(net_log.net_log()); 381 EXPECT_TRUE(net_log.net_log());
187 *load_timing_info = LoadTimingInfo(); 382 *load_timing_info = LoadTimingInfo();
188 trans->GetLoadTimingInfo(load_timing_info); 383 c->trans->GetLoadTimingInfo(load_timing_info);
189 } 384 }
190 385
191 if (remote_endpoint) 386 if (remote_endpoint)
192 ASSERT_TRUE(trans->GetRemoteEndpoint(remote_endpoint)); 387 ASSERT_TRUE(c->trans->GetRemoteEndpoint(remote_endpoint));
193 388
194 ReadAndVerifyTransaction(trans.get(), trans_info); 389 ReadAndVerifyTransaction(c->trans.get(), trans_info);
195 390
196 if (sent_bytes) 391 if (sent_bytes)
197 *sent_bytes = trans->GetTotalSentBytes(); 392 *sent_bytes = c->trans->GetTotalSentBytes();
198 if (received_bytes) 393 if (received_bytes)
199 *received_bytes = trans->GetTotalReceivedBytes(); 394 *received_bytes = c->trans->GetTotalReceivedBytes();
395
396 delete c;
200 } 397 }
201 398
202 void RunTransactionTestWithRequest(HttpCache* cache, 399 void RunTransactionTestWithRequest(HttpCache* cache,
203 const MockTransaction& trans_info, 400 const MockTransaction& trans_info,
204 const MockHttpRequest& request, 401 const MockHttpRequest& request,
205 HttpResponseInfo* response_info) { 402 HttpResponseInfo* response_info) {
206 RunTransactionTestBase(cache, trans_info, request, response_info, 403 RunTransactionTestBase(cache, trans_info, request, response_info,
207 NetLogWithSource(), nullptr, nullptr, nullptr, 404 NetLogWithSource(), nullptr, nullptr, nullptr,
208 nullptr); 405 nullptr);
209 } 406 }
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 762
566 std::string status_and_headers() const { 763 std::string status_and_headers() const {
567 return std::string(status) + "\n" + std::string(headers); 764 return std::string(status) + "\n" + std::string(headers);
568 } 765 }
569 766
570 const char* status; 767 const char* status;
571 const char* headers; 768 const char* headers;
572 const char* body; 769 const char* body;
573 }; 770 };
574 771
575 struct Context {
576 Context() : result(ERR_IO_PENDING) {}
577
578 int result;
579 TestCompletionCallback callback;
580 std::unique_ptr<HttpTransaction> trans;
581 };
582
583 class FakeWebSocketHandshakeStreamCreateHelper 772 class FakeWebSocketHandshakeStreamCreateHelper
584 : public WebSocketHandshakeStreamBase::CreateHelper { 773 : public WebSocketHandshakeStreamBase::CreateHelper {
585 public: 774 public:
586 ~FakeWebSocketHandshakeStreamCreateHelper() override {} 775 ~FakeWebSocketHandshakeStreamCreateHelper() override {}
587 WebSocketHandshakeStreamBase* CreateBasicStream( 776 WebSocketHandshakeStreamBase* CreateBasicStream(
588 std::unique_ptr<ClientSocketHandle> connect, 777 std::unique_ptr<ClientSocketHandle> connect,
589 bool using_proxy) override { 778 bool using_proxy) override {
590 return NULL; 779 return NULL;
591 } 780 }
592 WebSocketHandshakeStreamBase* CreateSpdyStream( 781 WebSocketHandshakeStreamBase* CreateSpdyStream(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 823
635 } // namespace 824 } // namespace
636 825
637 826
638 //----------------------------------------------------------------------------- 827 //-----------------------------------------------------------------------------
639 // Tests. 828 // Tests.
640 829
641 TEST(HttpCache, CreateThenDestroy) { 830 TEST(HttpCache, CreateThenDestroy) {
642 MockHttpCache cache; 831 MockHttpCache cache;
643 832
644 std::unique_ptr<HttpTransaction> trans; 833 Context* c = new Context();
645 EXPECT_THAT(cache.CreateTransaction(&trans), IsOk()); 834 EXPECT_THAT(cache.CreateTransaction(&c->trans), IsOk());
646 ASSERT_TRUE(trans.get()); 835 ASSERT_TRUE(c->trans.get());
836 delete c;
647 } 837 }
648 838
649 TEST(HttpCache, GetBackend) { 839 TEST(HttpCache, GetBackend) {
650 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0)); 840 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0));
651 841
652 disk_cache::Backend* backend; 842 disk_cache::Backend* backend;
653 TestCompletionCallback cb; 843 TestCompletionCallback cb;
654 // This will lazily initialize the backend. 844 // This will lazily initialize the backend.
655 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); 845 int rv = cache.http_cache()->GetBackend(&backend, cb.callback());
656 EXPECT_THAT(cb.GetResult(rv), IsOk()); 846 EXPECT_THAT(cb.GetResult(rv), IsOk());
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 } 915 }
726 916
727 // Tests that IOBuffers are not referenced after IO completes. 917 // Tests that IOBuffers are not referenced after IO completes.
728 TEST(HttpCache, ReleaseBuffer) { 918 TEST(HttpCache, ReleaseBuffer) {
729 MockHttpCache cache; 919 MockHttpCache cache;
730 920
731 // Write to the cache. 921 // Write to the cache.
732 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 922 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
733 923
734 MockHttpRequest request(kSimpleGET_Transaction); 924 MockHttpRequest request(kSimpleGET_Transaction);
735 std::unique_ptr<HttpTransaction> trans; 925 Context* c = new Context();
736 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 926 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
737 927
738 const int kBufferSize = 10; 928 const int kBufferSize = 10;
739 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); 929 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize));
740 ReleaseBufferCompletionCallback cb(buffer.get()); 930 ReleaseBufferCompletionCallback cb(buffer.get());
741 931
742 int rv = trans->Start(&request, cb.callback(), NetLogWithSource()); 932 int rv = c->trans->Start(&request, cb.callback(), NetLogWithSource());
743 EXPECT_THAT(cb.GetResult(rv), IsOk()); 933 EXPECT_THAT(cb.GetResult(rv), IsOk());
744 934
745 rv = trans->Read(buffer.get(), kBufferSize, cb.callback()); 935 rv = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
746 EXPECT_EQ(kBufferSize, cb.GetResult(rv)); 936 EXPECT_EQ(kBufferSize, cb.GetResult(rv));
937
938 delete c;
747 } 939 }
748 940
749 TEST(HttpCache, SimpleGETWithDiskFailures) { 941 TEST(HttpCache, SimpleGETWithDiskFailures) {
750 MockHttpCache cache; 942 MockHttpCache cache;
751 943
752 cache.disk_cache()->set_soft_failures(true); 944 cache.disk_cache()->set_soft_failures(true);
753 945
754 // Read from the network, and fail to write to the cache. 946 // Read from the network, and fail to write to the cache.
755 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 947 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
756 948
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
913 } 1105 }
914 1106
915 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { 1107 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) {
916 MockHttpCache cache; 1108 MockHttpCache cache;
917 1109
918 // force this transaction to read from the cache 1110 // force this transaction to read from the cache
919 MockTransaction transaction(kSimpleGET_Transaction); 1111 MockTransaction transaction(kSimpleGET_Transaction);
920 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 1112 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
921 1113
922 MockHttpRequest request(transaction); 1114 MockHttpRequest request(transaction);
923 TestCompletionCallback callback;
924 1115
925 std::unique_ptr<HttpTransaction> trans; 1116 std::unique_ptr<Context> c(new Context());
926 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 1117 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
927 1118
928 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 1119 int rv =
1120 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
929 if (rv == ERR_IO_PENDING) 1121 if (rv == ERR_IO_PENDING)
930 rv = callback.WaitForResult(); 1122 rv = c->callback.WaitForResult();
931 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); 1123 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS));
932 1124
933 trans.reset();
934
935 EXPECT_EQ(0, cache.network_layer()->transaction_count()); 1125 EXPECT_EQ(0, cache.network_layer()->transaction_count());
936 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1126 EXPECT_EQ(0, cache.disk_cache()->open_count());
937 EXPECT_EQ(0, cache.disk_cache()->create_count()); 1127 EXPECT_EQ(0, cache.disk_cache()->create_count());
938 } 1128 }
939 1129
940 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { 1130 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) {
941 MockHttpCache cache; 1131 MockHttpCache cache;
942 1132
943 // write to the cache 1133 // write to the cache
944 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1134 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1031 RunTransactionTest(cache.http_cache(), transaction); 1221 RunTransactionTest(cache.http_cache(), transaction);
1032 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1222 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1033 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1223 EXPECT_EQ(1, cache.disk_cache()->create_count());
1034 RemoveMockTransaction(&transaction); 1224 RemoveMockTransaction(&transaction);
1035 1225
1036 // Network failure with error; should fail but have was_cached set. 1226 // Network failure with error; should fail but have was_cached set.
1037 transaction.return_code = ERR_FAILED; 1227 transaction.return_code = ERR_FAILED;
1038 AddMockTransaction(&transaction); 1228 AddMockTransaction(&transaction);
1039 1229
1040 MockHttpRequest request(transaction); 1230 MockHttpRequest request(transaction);
1041 TestCompletionCallback callback; 1231 Context* c = new Context();
1042 std::unique_ptr<HttpTransaction> trans; 1232 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans);
1043 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
1044 EXPECT_THAT(rv, IsOk()); 1233 EXPECT_THAT(rv, IsOk());
1045 ASSERT_TRUE(trans.get()); 1234 ASSERT_TRUE(c->trans.get());
1046 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 1235 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1047 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED)); 1236 EXPECT_THAT(c->callback.GetResult(rv), IsError(ERR_FAILED));
1048 1237
1049 const HttpResponseInfo* response_info = trans->GetResponseInfo(); 1238 const HttpResponseInfo* response_info = c->trans->GetResponseInfo();
1050 ASSERT_TRUE(response_info); 1239 ASSERT_TRUE(response_info);
1051 EXPECT_TRUE(response_info->was_cached); 1240 EXPECT_TRUE(response_info->was_cached);
1052 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1241 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1053 1242
1054 RemoveMockTransaction(&transaction); 1243 RemoveMockTransaction(&transaction);
1244
1245 delete c;
1055 } 1246 }
1056 1247
1057 // Confirm if we have an empty cache, a read is marked as network verified. 1248 // Confirm if we have an empty cache, a read is marked as network verified.
1058 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { 1249 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) {
1059 MockHttpCache cache; 1250 MockHttpCache cache;
1060 1251
1061 // write to the cache 1252 // write to the cache
1062 HttpResponseInfo response_info; 1253 HttpResponseInfo response_info;
1063 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 1254 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
1064 &response_info); 1255 &response_info);
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
1335 1526
1336 // All requests are waiting for the active entry. 1527 // All requests are waiting for the active entry.
1337 for (int i = 0; i < kNumTransactions; ++i) { 1528 for (int i = 0; i < kNumTransactions; ++i) {
1338 Context* c = context_list[i]; 1529 Context* c = context_list[i];
1339 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); 1530 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1340 } 1531 }
1341 1532
1342 // Allow all requests to move from the Create queue to the active entry. 1533 // Allow all requests to move from the Create queue to the active entry.
1343 base::RunLoop().RunUntilIdle(); 1534 base::RunLoop().RunUntilIdle();
1344 1535
1345 // The first request should be a writer at this point, and the subsequent 1536 // At the end of Start: [0] should be in shared_writers->all_writers_,
1346 // requests should be pending. 1537 // [3] and [4] should also be in all_writers_ since its a case of validation
1538 // match/skip, [1] and [2] should be in pending_queue.
1347 1539
1348 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1540 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1349 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1541 EXPECT_EQ(0, cache.disk_cache()->open_count());
1350 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1542 EXPECT_EQ(1, cache.disk_cache()->create_count());
1351 1543
1352 // All requests depend on the writer, and the writer is between Start and 1544 // All requests depend on the writer, and the writer is between Start and
1353 // Read, i.e. idle. 1545 // Read, i.e. idle.
1354 for (int i = 0; i < kNumTransactions; ++i) { 1546 for (int i = 0; i < kNumTransactions; ++i) {
1355 Context* c = context_list[i]; 1547 Context* c = context_list[i];
1356 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); 1548 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1412 1604
1413 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1605 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1414 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1606 EXPECT_EQ(0, cache.disk_cache()->open_count());
1415 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1607 EXPECT_EQ(1, cache.disk_cache()->create_count());
1416 1608
1417 Context* c = context_list[0]; 1609 Context* c = context_list[0];
1418 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1610 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1419 c->result = c->callback.WaitForResult(); 1611 c->result = c->callback.WaitForResult();
1420 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1612 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1421 1613
1422 // Now we have 2 active readers and two queued transactions. 1614 // after [0] completes reading and writing, [3] and [4] should be added to
1423 1615 // entry->readers from shared writers and pending queue should be
1616 // processed to add [1] and [2] to readers as well. They are all waiting for
1617 // their consumer to invoke Read.
1424 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); 1618 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState());
1425 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, 1619 EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState());
1426 context_list[3]->trans->GetLoadState());
1427 1620
1428 c = context_list[1]; 1621 c = context_list[1];
1429 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1622 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1430 c->result = c->callback.WaitForResult(); 1623 c->result = c->callback.WaitForResult();
1431 if (c->result == OK) 1624 if (c->result == OK)
1432 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1625 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1433 1626
1434 // At this point we have one reader, two pending transactions and a task on 1627 // At this point we have one reader, two pending transactions and a task on
1435 // the queue to move to the next transaction. Now we cancel the request that 1628 // the queue to move to the next transaction. Now we cancel the request that
1436 // is the current reader, and expect the queued task to be able to start the 1629 // is the current reader, and expect the queued task to be able to start the
1437 // next request. 1630 // next request.
1438 1631
1439 c = context_list[2]; 1632 c = context_list[2];
1440 c->trans.reset(); 1633 if (c->trans) {
1634 c->trans->Orphan(std::move(c->trans));
1635 }
1441 1636
1442 for (int i = 3; i < kNumTransactions; ++i) { 1637 for (int i = 3; i < kNumTransactions; ++i) {
1443 Context* c = context_list[i]; 1638 Context* c = context_list[i];
1444 if (c->result == ERR_IO_PENDING) 1639 if (c->result == ERR_IO_PENDING)
1445 c->result = c->callback.WaitForResult(); 1640 c->result = c->callback.WaitForResult();
1446 if (c->result == OK) 1641 if (c->result == OK)
1447 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1642 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1448 } 1643 }
1449 1644
1450 // We should not have had to re-open the disk entry. 1645 // We should not have had to re-open the disk entry.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1530 c->result = 1725 c->result =
1531 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 1726 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1532 } 1727 }
1533 1728
1534 // Allow all requests to move from the Create queue to the active entry. 1729 // Allow all requests to move from the Create queue to the active entry.
1535 base::RunLoop().RunUntilIdle(); 1730 base::RunLoop().RunUntilIdle();
1536 1731
1537 // The first request should be a writer at this point, and the subsequent 1732 // The first request should be a writer at this point, and the subsequent
1538 // requests should be pending. 1733 // requests should be pending.
1539 1734
1540 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1735 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1541 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1736 EXPECT_EQ(0, cache.disk_cache()->open_count());
1542 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1737 EXPECT_EQ(2, cache.disk_cache()->create_count());
1543 1738
1544 // Now, make sure that the second request asks for the entry not to be stored. 1739 // Now, make sure that the second request asks for the entry not to be stored.
1545 request_handler.set_no_store(true); 1740 request_handler.set_no_store(true);
1546 1741
1547 for (int i = 0; i < kNumTransactions; ++i) { 1742 for (int i = 0; i < kNumTransactions; ++i) {
1548 Context* c = context_list[i]; 1743 Context* c = context_list[i];
1549 if (c->result == ERR_IO_PENDING) 1744 if (c->result == ERR_IO_PENDING)
1550 c->result = c->callback.WaitForResult(); 1745 c->result = c->callback.WaitForResult();
1551 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); 1746 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction);
1552 delete c; 1747 delete c;
(...skipping 22 matching lines...) Expand all
1575 ASSERT_THAT(c->result, IsOk()); 1770 ASSERT_THAT(c->result, IsOk());
1576 1771
1577 c->result = 1772 c->result =
1578 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 1773 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1579 } 1774 }
1580 1775
1581 // Allow all requests to move from the Create queue to the active entry. 1776 // Allow all requests to move from the Create queue to the active entry.
1582 base::RunLoop().RunUntilIdle(); 1777 base::RunLoop().RunUntilIdle();
1583 1778
1584 // The first request should be a writer at this point, and the subsequent 1779 // The first request should be a writer at this point, and the subsequent
1585 // requests should be pending. 1780 // requests should have been added to SharedWriters, skipped validation and
1781 // completed the start state machine of HttpCache::Transaction.
1586 1782
1587 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1783 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1588 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1784 EXPECT_EQ(0, cache.disk_cache()->open_count());
1589 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1785 EXPECT_EQ(1, cache.disk_cache()->create_count());
1590 1786
1591 for (int i = 0; i < kNumTransactions; ++i) { 1787 for (int i = 0; i < kNumTransactions; ++i) {
1592 Context* c = context_list[i]; 1788 Context* c = context_list[i];
1593 if (c->result == ERR_IO_PENDING) 1789 if (c->result == ERR_IO_PENDING)
1594 c->result = c->callback.WaitForResult(); 1790 c->result = c->callback.WaitForResult();
1595 // Destroy only the first transaction. 1791 // Destroy only the first transaction.
1596 if (i == 0) { 1792 if (i == 0) {
1597 delete c; 1793 delete c;
1598 context_list[i] = NULL; 1794 context_list[i] = NULL;
1599 } 1795 }
1600 } 1796 }
1601 1797
1602 // Complete the rest of the transactions. 1798 // Complete the rest of the transactions.
1603 for (int i = 1; i < kNumTransactions; ++i) { 1799 for (int i = 1; i < kNumTransactions; ++i) {
1604 Context* c = context_list[i]; 1800 Context* c = context_list[i];
1605 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1801 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1606 } 1802 }
1607 1803
1608 // We should have had to re-open the disk entry. 1804 // 2nd transaction will reuse the network transaction as they are both part of
1609 1805 // SharedWriters. Similarly it will be part of the same entry.
1610 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1806 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1611 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1807 EXPECT_EQ(0, cache.disk_cache()->open_count());
1612 EXPECT_EQ(2, cache.disk_cache()->create_count()); 1808 EXPECT_EQ(1, cache.disk_cache()->create_count());
1613 1809
1614 for (int i = 1; i < kNumTransactions; ++i) { 1810 for (int i = 1; i < kNumTransactions; ++i) {
1615 Context* c = context_list[i]; 1811 Context* c = context_list[i];
1616 delete c; 1812 delete c;
1617 } 1813 }
1618 } 1814 }
1619 1815
1620 // Tests that we can cancel requests that are queued waiting to open the disk 1816 // Tests that we can cancel requests that are queued waiting to open the disk
1621 // cache entry. 1817 // cache entry.
1622 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { 1818 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) {
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
1784 if (rv == ERR_IO_PENDING) 1980 if (rv == ERR_IO_PENDING)
1785 rv = callback.WaitForResult(); 1981 rv = callback.WaitForResult();
1786 ASSERT_THAT(rv, IsOk()); 1982 ASSERT_THAT(rv, IsOk());
1787 1983
1788 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); 1984 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
1789 rv = trans->Read(buf.get(), 256, callback.callback()); 1985 rv = trans->Read(buf.get(), 256, callback.callback());
1790 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 1986 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
1791 1987
1792 // Test that destroying the transaction while it is reading from the cache 1988 // Test that destroying the transaction while it is reading from the cache
1793 // works properly. 1989 // works properly.
1794 trans.reset(); 1990 if (trans) {
1991 trans->Orphan(std::move(trans));
1992 }
1795 1993
1796 // Make sure we pump any pending events, which should include a call to 1994 // Make sure we pump any pending events, which should include a call to
1797 // HttpCache::Transaction::OnCacheReadCompleted. 1995 // HttpCache::Transaction::OnCacheReadCompleted.
1798 base::RunLoop().RunUntilIdle(); 1996 base::RunLoop().RunUntilIdle();
1799 } 1997 }
1800 1998
1801 // Tests that we can delete the HttpCache and deal with queued transactions 1999 // Tests that we can delete the HttpCache and deal with queued transactions
1802 // ("waiting for the backend" as opposed to Active or Doomed entries). 2000 // ("waiting for the backend" as opposed to Active or Doomed entries).
1803 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { 2001 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) {
1804 std::unique_ptr<MockHttpCache> cache( 2002 std::unique_ptr<MockHttpCache> cache(
(...skipping 1148 matching lines...) Expand 10 before | Expand all | Expand 10 after
2953 3151
2954 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { 3152 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) {
2955 MockHttpCache cache; 3153 MockHttpCache cache;
2956 3154
2957 MockTransaction transaction(kSimplePOST_Transaction); 3155 MockTransaction transaction(kSimplePOST_Transaction);
2958 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3156 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
2959 3157
2960 MockHttpRequest request(transaction); 3158 MockHttpRequest request(transaction);
2961 TestCompletionCallback callback; 3159 TestCompletionCallback callback;
2962 3160
2963 std::unique_ptr<HttpTransaction> trans; 3161 std::unique_ptr<Context> c(new Context());
2964 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 3162 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
2965 ASSERT_TRUE(trans.get()); 3163 ASSERT_TRUE(c->trans.get());
2966 3164
2967 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 3165 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
2968 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); 3166 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS));
2969 3167
2970 trans.reset();
2971
2972 EXPECT_EQ(0, cache.network_layer()->transaction_count()); 3168 EXPECT_EQ(0, cache.network_layer()->transaction_count());
2973 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3169 EXPECT_EQ(0, cache.disk_cache()->open_count());
2974 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3170 EXPECT_EQ(0, cache.disk_cache()->create_count());
2975 } 3171 }
2976 3172
2977 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { 3173 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) {
2978 MockHttpCache cache; 3174 MockHttpCache cache;
2979 3175
2980 // Test that we hit the cache for POST requests. 3176 // Test that we hit the cache for POST requests.
2981 3177
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
3208 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { 3404 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) {
3209 MockHttpCache cache; 3405 MockHttpCache cache;
3210 MockTransaction transaction(kSimplePOST_Transaction); 3406 MockTransaction transaction(kSimplePOST_Transaction);
3211 AddMockTransaction(&transaction); 3407 AddMockTransaction(&transaction);
3212 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3408 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3213 transaction.method = "HEAD"; 3409 transaction.method = "HEAD";
3214 3410
3215 MockHttpRequest request(transaction); 3411 MockHttpRequest request(transaction);
3216 TestCompletionCallback callback; 3412 TestCompletionCallback callback;
3217 3413
3218 std::unique_ptr<HttpTransaction> trans; 3414 std::unique_ptr<Context> c(new Context());
3219 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 3415 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
3220 ASSERT_TRUE(trans.get()); 3416 ASSERT_TRUE(c->trans.get());
3221 3417
3222 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 3418 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
3223 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); 3419 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS));
3224 3420
3225 trans.reset();
3226
3227 EXPECT_EQ(0, cache.network_layer()->transaction_count()); 3421 EXPECT_EQ(0, cache.network_layer()->transaction_count());
3228 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3422 EXPECT_EQ(0, cache.disk_cache()->open_count());
3229 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3423 EXPECT_EQ(0, cache.disk_cache()->create_count());
3230 RemoveMockTransaction(&transaction); 3424 RemoveMockTransaction(&transaction);
3231 } 3425 }
3232 3426
3233 // Tests that a HEAD request is served from a cached GET. 3427 // Tests that a HEAD request is served from a cached GET.
3234 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) { 3428 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) {
3235 MockHttpCache cache; 3429 MockHttpCache cache;
3236 MockTransaction transaction(kSimpleGET_Transaction); 3430 MockTransaction transaction(kSimpleGET_Transaction);
(...skipping 2366 matching lines...) Expand 10 before | Expand all | Expand 10 after
5603 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5797 EXPECT_EQ(0, cache.disk_cache()->open_count());
5604 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5798 EXPECT_EQ(1, cache.disk_cache()->create_count());
5605 5799
5606 // Force this transaction to read from the cache. 5800 // Force this transaction to read from the cache.
5607 MockTransaction transaction(kRangeGET_TransactionOK); 5801 MockTransaction transaction(kRangeGET_TransactionOK);
5608 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 5802 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
5609 5803
5610 MockHttpRequest request(transaction); 5804 MockHttpRequest request(transaction);
5611 TestCompletionCallback callback; 5805 TestCompletionCallback callback;
5612 5806
5613 std::unique_ptr<HttpTransaction> trans; 5807 std::unique_ptr<Context> c(new Context());
5614 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 5808 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans);
5615 EXPECT_THAT(rv, IsOk()); 5809 EXPECT_THAT(rv, IsOk());
5616 ASSERT_TRUE(trans.get()); 5810 ASSERT_TRUE(c->trans.get());
5617 5811
5618 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 5812 rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
5619 if (rv == ERR_IO_PENDING) 5813 if (rv == ERR_IO_PENDING)
5620 rv = callback.WaitForResult(); 5814 rv = callback.WaitForResult();
5621 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); 5815 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS));
5622 5816
5623 trans.reset(); 5817 c.reset();
5624 5818
5625 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5819 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5626 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5820 EXPECT_EQ(1, cache.disk_cache()->open_count());
5627 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5821 EXPECT_EQ(1, cache.disk_cache()->create_count());
5628 5822
5629 RemoveMockTransaction(&kRangeGET_TransactionOK); 5823 RemoveMockTransaction(&kRangeGET_TransactionOK);
5630 } 5824 }
5631 #endif 5825 #endif
5632 5826
5633 // Tests the handling of the "truncation" flag. 5827 // Tests the handling of the "truncation" flag.
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
5824 EXPECT_EQ(buf->size(), rv); 6018 EXPECT_EQ(buf->size(), rv);
5825 6019
5826 // We want to cancel the request when the transaction is busy. 6020 // We want to cancel the request when the transaction is busy.
5827 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); 6021 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5828 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 6022 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
5829 EXPECT_FALSE(c->callback.have_result()); 6023 EXPECT_FALSE(c->callback.have_result());
5830 6024
5831 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); 6025 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL);
5832 6026
5833 // Destroy the transaction. 6027 // Destroy the transaction.
5834 c->trans.reset(); 6028 if (c->trans) {
6029 c->trans->Orphan(std::move(c->trans));
6030 }
5835 MockHttpCache::SetTestMode(0); 6031 MockHttpCache::SetTestMode(0);
5836 6032
5837 6033
5838 // Make sure that we don't invoke the callback. We may have an issue if the 6034 // Make sure that we don't invoke the callback. We may have an issue if the
5839 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we 6035 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we
5840 // could end up with the transaction being deleted twice if we send any 6036 // could end up with the transaction being deleted twice if we send any
5841 // notification from the transaction destructor (see http://crbug.com/31723). 6037 // notification from the transaction destructor (see http://crbug.com/31723).
5842 EXPECT_FALSE(c->callback.have_result()); 6038 EXPECT_FALSE(c->callback.have_result());
5843 6039
5844 // Verify that the entry is marked as incomplete. 6040 // Verify that the entry is not marked as incomplete since SharedWriters
5845 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); 6041 // enables response writing to complete even if transaction is destroyed
6042 // mid-way.
6043 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0);
5846 } 6044 }
5847 6045
5848 // Tests that we don't mark an entry as truncated when we read everything. 6046 // Tests that we don't mark an entry as truncated when we read everything.
5849 TEST(HttpCache, DontSetTruncatedFlag) { 6047 TEST(HttpCache, DontSetTruncatedFlag) {
5850 MockHttpCache cache; 6048 MockHttpCache cache;
5851 6049
5852 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6050 ScopedMockTransaction transaction(kSimpleGET_Transaction);
5853 transaction.response_headers = 6051 transaction.response_headers =
5854 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 6052 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5855 "Content-Length: 22\n" 6053 "Content-Length: 22\n"
5856 "Etag: \"foopy\"\n"; 6054 "Etag: \"foopy\"\n";
5857 MockHttpRequest request(transaction); 6055 MockHttpRequest request(transaction);
5858 6056
5859 std::unique_ptr<Context> c(new Context()); 6057 std::unique_ptr<Context> c(new Context());
5860 int rv = cache.CreateTransaction(&c->trans); 6058 int rv = cache.CreateTransaction(&c->trans);
5861 ASSERT_THAT(rv, IsOk()); 6059 ASSERT_THAT(rv, IsOk());
5862 6060
5863 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 6061 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
5864 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); 6062 EXPECT_THAT(c->callback.GetResult(rv), IsOk());
5865 6063
5866 // Read everything. 6064 // Read everything.
5867 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); 6065 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22));
5868 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); 6066 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5869 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); 6067 EXPECT_EQ(buf->size(), c->callback.GetResult(rv));
5870 6068
5871 // Destroy the transaction. 6069 // Destroy the transaction.
5872 c->trans.reset(); 6070 if (c->trans) {
6071 c->trans->Orphan(std::move(c->trans));
6072 }
5873 6073
5874 // Verify that the entry is not marked as truncated. 6074 // Verify that the entry is not marked as truncated.
5875 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); 6075 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0);
5876 } 6076 }
5877 6077
5878 // Tests that sparse entries don't set the truncate flag. 6078 // Tests that sparse entries don't set the truncate flag.
5879 TEST(HttpCache, RangeGET_DontTruncate) { 6079 TEST(HttpCache, RangeGET_DontTruncate) {
5880 MockHttpCache cache; 6080 MockHttpCache cache;
5881 6081
5882 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 6082 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5883 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; 6083 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER;
5884 6084
5885 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); 6085 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction));
5886 std::unique_ptr<HttpTransaction> trans; 6086 std::unique_ptr<Context> c(new Context());
5887 6087
5888 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 6088 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans);
5889 EXPECT_THAT(rv, IsOk()); 6089 EXPECT_THAT(rv, IsOk());
5890 6090
5891 TestCompletionCallback cb; 6091 TestCompletionCallback cb;
5892 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); 6092 rv = c->trans->Start(request.get(), cb.callback(), NetLogWithSource());
5893 EXPECT_EQ(0, cb.GetResult(rv)); 6093 EXPECT_EQ(0, cb.GetResult(rv));
5894 6094
5895 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); 6095 scoped_refptr<IOBuffer> buf(new IOBuffer(10));
5896 rv = trans->Read(buf.get(), 10, cb.callback()); 6096 rv = c->trans->Read(buf.get(), 10, cb.callback());
5897 EXPECT_EQ(10, cb.GetResult(rv)); 6097 EXPECT_EQ(10, cb.GetResult(rv));
5898 6098
5899 // Should not trigger any DCHECK.
5900 trans.reset();
5901 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); 6099 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0);
5902 } 6100 }
5903 6101
5904 // Tests that sparse entries don't set the truncate flag (when the byte range 6102 // Tests that sparse entries don't set the truncate flag (when the byte range
5905 // starts after 0). 6103 // starts after 0).
5906 TEST(HttpCache, RangeGET_DontTruncate2) { 6104 TEST(HttpCache, RangeGET_DontTruncate2) {
5907 MockHttpCache cache; 6105 MockHttpCache cache;
5908 6106
5909 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 6107 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5910 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; 6108 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER;
5911 6109
5912 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); 6110 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction));
5913 std::unique_ptr<HttpTransaction> trans; 6111 std::unique_ptr<Context> c(new Context());
5914 6112
5915 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 6113 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans);
5916 EXPECT_THAT(rv, IsOk()); 6114 EXPECT_THAT(rv, IsOk());
5917 6115
5918 TestCompletionCallback cb; 6116 rv = c->trans->Start(request.get(), c->callback.callback(),
5919 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); 6117 NetLogWithSource());
5920 EXPECT_EQ(0, cb.GetResult(rv)); 6118 EXPECT_EQ(0, c->callback.GetResult(rv));
5921 6119
5922 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); 6120 scoped_refptr<IOBuffer> buf(new IOBuffer(10));
5923 rv = trans->Read(buf.get(), 10, cb.callback()); 6121 rv = c->trans->Read(buf.get(), 10, c->callback.callback());
5924 EXPECT_EQ(10, cb.GetResult(rv)); 6122 EXPECT_EQ(10, c->callback.GetResult(rv));
5925 6123
5926 // Should not trigger any DCHECK. 6124 // Should not trigger any DCHECK.
5927 trans.reset();
5928 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); 6125 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0);
5929 } 6126 }
5930 6127
5931 // Tests that we can continue with a request that was interrupted. 6128 // Tests that we can continue with a request that was interrupted.
5932 TEST(HttpCache, GET_IncompleteResource) { 6129 TEST(HttpCache, GET_IncompleteResource) {
5933 MockHttpCache cache; 6130 MockHttpCache cache;
5934 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 6131 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5935 6132
5936 std::string raw_headers("HTTP/1.1 200 OK\n" 6133 std::string raw_headers("HTTP/1.1 200 OK\n"
5937 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 6134 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
6381 6578
6382 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); 6579 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction);
6383 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; 6580 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently";
6384 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; 6581 kTestTransaction.response_headers = "Location: http://www.bar.com/\n";
6385 6582
6386 MockHttpRequest request(kTestTransaction); 6583 MockHttpRequest request(kTestTransaction);
6387 TestCompletionCallback callback; 6584 TestCompletionCallback callback;
6388 6585
6389 // Write to the cache. 6586 // Write to the cache.
6390 { 6587 {
6391 std::unique_ptr<HttpTransaction> trans; 6588 std::unique_ptr<Context> c(new Context());
6392 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 6589 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
6393 6590
6394 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 6591 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
6395 if (rv == ERR_IO_PENDING) 6592 if (rv == ERR_IO_PENDING)
6396 rv = callback.WaitForResult(); 6593 rv = callback.WaitForResult();
6397 ASSERT_THAT(rv, IsOk()); 6594 ASSERT_THAT(rv, IsOk());
6398 6595
6399 const HttpResponseInfo* info = trans->GetResponseInfo(); 6596 const HttpResponseInfo* info = c->trans->GetResponseInfo();
6400 ASSERT_TRUE(info); 6597 ASSERT_TRUE(info);
6401 6598
6402 EXPECT_EQ(info->headers->response_code(), 301); 6599 EXPECT_EQ(info->headers->response_code(), 301);
6403 6600
6404 std::string location; 6601 std::string location;
6405 info->headers->EnumerateHeader(NULL, "Location", &location); 6602 info->headers->EnumerateHeader(NULL, "Location", &location);
6406 EXPECT_EQ(location, "http://www.bar.com/"); 6603 EXPECT_EQ(location, "http://www.bar.com/");
6407 6604
6408 // Mark the transaction as completed so it is cached. 6605 // Mark the transaction as completed so it is cached.
6409 trans->DoneReading(); 6606 c->trans->DoneReading();
6410 6607
6411 // Destroy transaction when going out of scope. We have not actually 6608 // Destroy transaction when going out of scope. We have not actually
6412 // read the response body -- want to test that it is still getting cached. 6609 // read the response body -- want to test that it is still getting cached.
6413 } 6610 }
6414 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6611 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6415 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6612 EXPECT_EQ(0, cache.disk_cache()->open_count());
6416 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6613 EXPECT_EQ(1, cache.disk_cache()->create_count());
6417 6614
6418 // Active entries in the cache are not retired synchronously. Make 6615 // Active entries in the cache are not retired synchronously. Make
6419 // sure the next run hits the MockHttpCache and open_count is 6616 // sure the next run hits the MockHttpCache and open_count is
6420 // correct. 6617 // correct.
6421 base::RunLoop().RunUntilIdle(); 6618 base::RunLoop().RunUntilIdle();
6422 6619
6423 // Read from the cache. 6620 // Read from the cache.
6424 { 6621 {
6425 std::unique_ptr<HttpTransaction> trans; 6622 std::unique_ptr<Context> c(new Context());
6426 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 6623 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
6427 6624
6428 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 6625 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
6429 if (rv == ERR_IO_PENDING) 6626 if (rv == ERR_IO_PENDING)
6430 rv = callback.WaitForResult(); 6627 rv = callback.WaitForResult();
6431 ASSERT_THAT(rv, IsOk()); 6628 ASSERT_THAT(rv, IsOk());
6432 6629
6433 const HttpResponseInfo* info = trans->GetResponseInfo(); 6630 const HttpResponseInfo* info = c->trans->GetResponseInfo();
6434 ASSERT_TRUE(info); 6631 ASSERT_TRUE(info);
6435 6632
6436 EXPECT_EQ(info->headers->response_code(), 301); 6633 EXPECT_EQ(info->headers->response_code(), 301);
6437 6634
6438 std::string location; 6635 std::string location;
6439 info->headers->EnumerateHeader(NULL, "Location", &location); 6636 info->headers->EnumerateHeader(NULL, "Location", &location);
6440 EXPECT_EQ(location, "http://www.bar.com/"); 6637 EXPECT_EQ(location, "http://www.bar.com/");
6441 6638
6442 // Mark the transaction as completed so it is cached. 6639 // Mark the transaction as completed so it is cached.
6443 trans->DoneReading(); 6640 c->trans->DoneReading();
6444 6641
6445 // Destroy transaction when going out of scope. We have not actually 6642 // Destroy transaction when going out of scope. We have not actually
6446 // read the response body -- want to test that it is still getting cached. 6643 // read the response body -- want to test that it is still getting cached.
6447 } 6644 }
6448 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6645 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6449 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6646 EXPECT_EQ(1, cache.disk_cache()->open_count());
6450 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6647 EXPECT_EQ(1, cache.disk_cache()->create_count());
6451 } 6648 }
6452 6649
6453 // Verify that no-cache resources are stored in cache, but are not fetched from 6650 // Verify that no-cache resources are stored in cache, but are not fetched from
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
6594 transaction.cert_status = CERT_STATUS_REVOKED; 6791 transaction.cert_status = CERT_STATUS_REVOKED;
6595 ScopedMockTransaction scoped_transaction(transaction); 6792 ScopedMockTransaction scoped_transaction(transaction);
6596 6793
6597 // write to the cache 6794 // write to the cache
6598 RunTransactionTest(cache.http_cache(), transaction); 6795 RunTransactionTest(cache.http_cache(), transaction);
6599 6796
6600 // Test that it was not cached. 6797 // Test that it was not cached.
6601 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 6798 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
6602 6799
6603 MockHttpRequest request(transaction); 6800 MockHttpRequest request(transaction);
6604 TestCompletionCallback callback;
6605 6801
6606 std::unique_ptr<HttpTransaction> trans; 6802 std::unique_ptr<Context> c(new Context());
6607 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 6803 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
6608 6804
6609 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 6805 int rv =
6806 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
6610 if (rv == ERR_IO_PENDING) 6807 if (rv == ERR_IO_PENDING)
6611 rv = callback.WaitForResult(); 6808 rv = c->callback.WaitForResult();
6612 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); 6809 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS));
6613 } 6810 }
6614 6811
6615 // Ensure that we don't crash by if left-behind transactions. 6812 // Ensure that we don't crash by if left-behind transactions.
6616 TEST(HttpCache, OutlivedTransactions) { 6813 TEST(HttpCache, OutlivedTransactions) {
6617 MockHttpCache* cache = new MockHttpCache; 6814 MockHttpCache* cache = new MockHttpCache;
6618 6815
6619 std::unique_ptr<HttpTransaction> trans; 6816 std::unique_ptr<Context> c(new Context());
6620 EXPECT_THAT(cache->CreateTransaction(&trans), IsOk()); 6817 EXPECT_THAT(cache->CreateTransaction(&c->trans), IsOk());
6621 6818
6622 delete cache; 6819 delete cache;
6623 trans.reset();
6624 } 6820 }
6625 6821
6626 // Test that the disabled mode works. 6822 // Test that the disabled mode works.
6627 TEST(HttpCache, CacheDisabledMode) { 6823 TEST(HttpCache, CacheDisabledMode) {
6628 MockHttpCache cache; 6824 MockHttpCache cache;
6629 6825
6630 // write to the cache 6826 // write to the cache
6631 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 6827 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6632 6828
6633 // go into disabled mode 6829 // go into disabled mode
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
6892 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7088 EXPECT_EQ(1, cache.disk_cache()->create_count());
6893 } 7089 }
6894 7090
6895 // Tests that we don't mark entries as truncated when a filter detects the end 7091 // Tests that we don't mark entries as truncated when a filter detects the end
6896 // of the stream. 7092 // of the stream.
6897 TEST(HttpCache, FilterCompletion) { 7093 TEST(HttpCache, FilterCompletion) {
6898 MockHttpCache cache; 7094 MockHttpCache cache;
6899 TestCompletionCallback callback; 7095 TestCompletionCallback callback;
6900 7096
6901 { 7097 {
6902 std::unique_ptr<HttpTransaction> trans; 7098 std::unique_ptr<Context> c(new Context());
6903 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7099 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
6904 7100
6905 MockHttpRequest request(kSimpleGET_Transaction); 7101 MockHttpRequest request(kSimpleGET_Transaction);
6906 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7102 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
6907 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7103 EXPECT_THAT(callback.GetResult(rv), IsOk());
6908 7104
6909 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); 7105 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
6910 rv = trans->Read(buf.get(), 256, callback.callback()); 7106 rv = c->trans->Read(buf.get(), 256, callback.callback());
6911 EXPECT_GT(callback.GetResult(rv), 0); 7107 EXPECT_GT(callback.GetResult(rv), 0);
6912 7108
6913 // Now make sure that the entry is preserved. 7109 // Now make sure that the entry is preserved.
6914 trans->DoneReading(); 7110 c->trans->DoneReading();
6915 } 7111 }
6916 7112
6917 // Make sure that the ActiveEntry is gone. 7113 // Make sure that the ActiveEntry is gone.
6918 base::RunLoop().RunUntilIdle(); 7114 base::RunLoop().RunUntilIdle();
6919 7115
6920 // Read from the cache. 7116 // Read from the cache.
6921 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7117 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6922 7118
6923 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7119 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6924 EXPECT_EQ(1, cache.disk_cache()->open_count()); 7120 EXPECT_EQ(1, cache.disk_cache()->open_count());
6925 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7121 EXPECT_EQ(1, cache.disk_cache()->create_count());
6926 } 7122 }
6927 7123
6928 // Tests that we don't mark entries as truncated and release the cache 7124 // Tests that we don't mark entries as truncated and release the cache
6929 // entry when DoneReading() is called before any Read() calls, such as 7125 // entry when DoneReading() is called before any Read() calls, such as
6930 // for a redirect. 7126 // for a redirect.
6931 TEST(HttpCache, DoneReading) { 7127 TEST(HttpCache, DoneReading) {
6932 MockHttpCache cache; 7128 MockHttpCache cache;
6933 TestCompletionCallback callback; 7129 TestCompletionCallback callback;
6934 7130
6935 ScopedMockTransaction transaction(kSimpleGET_Transaction); 7131 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6936 transaction.data = ""; 7132 transaction.data = "";
6937 7133
6938 std::unique_ptr<HttpTransaction> trans; 7134 std::unique_ptr<Context> c(new Context());
6939 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7135 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
6940 7136
6941 MockHttpRequest request(transaction); 7137 MockHttpRequest request(transaction);
6942 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7138 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
6943 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7139 EXPECT_THAT(callback.GetResult(rv), IsOk());
6944 7140
6945 trans->DoneReading(); 7141 c->trans->DoneReading();
6946 // Leave the transaction around. 7142 // Leave the transaction around.
6947 7143
6948 // Make sure that the ActiveEntry is gone. 7144 // Make sure that the ActiveEntry is gone.
6949 base::RunLoop().RunUntilIdle(); 7145 base::RunLoop().RunUntilIdle();
6950 7146
6951 // Read from the cache. This should not deadlock. 7147 // Read from the cache. This should not deadlock.
6952 RunTransactionTest(cache.http_cache(), transaction); 7148 RunTransactionTest(cache.http_cache(), transaction);
6953 7149
6954 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7150 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6955 EXPECT_EQ(1, cache.disk_cache()->open_count()); 7151 EXPECT_EQ(1, cache.disk_cache()->open_count());
6956 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7152 EXPECT_EQ(1, cache.disk_cache()->create_count());
6957 } 7153 }
6958 7154
6959 // Tests that we stop caching when told. 7155 // Tests that we stop caching when told.
6960 TEST(HttpCache, StopCachingDeletesEntry) { 7156 TEST(HttpCache, StopCachingDeletesEntry) {
6961 MockHttpCache cache; 7157 MockHttpCache cache;
6962 TestCompletionCallback callback;
6963 MockHttpRequest request(kSimpleGET_Transaction); 7158 MockHttpRequest request(kSimpleGET_Transaction);
6964 7159
6965 { 7160 {
6966 std::unique_ptr<HttpTransaction> trans; 7161 std::unique_ptr<Context> c(new Context());
6967 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7162 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
6968 7163
6969 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7164 int rv =
6970 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7165 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
7166 EXPECT_THAT(c->callback.GetResult(rv), IsOk());
6971 7167
6972 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); 7168 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
6973 rv = trans->Read(buf.get(), 10, callback.callback()); 7169 rv = c->trans->Read(buf.get(), 10, c->callback.callback());
6974 EXPECT_EQ(10, callback.GetResult(rv)); 7170 EXPECT_EQ(10, c->callback.GetResult(rv));
6975 7171
6976 trans->StopCaching(); 7172 c->trans->StopCaching();
6977 7173
6978 // We should be able to keep reading. 7174 // We should be able to keep reading.
6979 rv = trans->Read(buf.get(), 256, callback.callback()); 7175 rv = c->trans->Read(buf.get(), 256, c->callback.callback());
6980 EXPECT_GT(callback.GetResult(rv), 0); 7176 EXPECT_GT(c->callback.GetResult(rv), 0);
6981 rv = trans->Read(buf.get(), 256, callback.callback()); 7177 rv = c->trans->Read(buf.get(), 256, c->callback.callback());
6982 EXPECT_EQ(0, callback.GetResult(rv)); 7178 EXPECT_EQ(0, c->callback.GetResult(rv));
6983 } 7179 }
6984 7180
6985 // Make sure that the ActiveEntry is gone. 7181 // Make sure that the ActiveEntry is gone.
6986 base::RunLoop().RunUntilIdle(); 7182 base::RunLoop().RunUntilIdle();
6987 7183
6988 // Verify that the entry is gone. 7184 // Verify that the entry is gone.
6989 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7185 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6990 7186
6991 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7187 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6992 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7188 EXPECT_EQ(0, cache.disk_cache()->open_count());
6993 EXPECT_EQ(2, cache.disk_cache()->create_count()); 7189 EXPECT_EQ(2, cache.disk_cache()->create_count());
6994 } 7190 }
6995 7191
6996 // Tests that we stop caching when told, even if DoneReading is called 7192 // Tests that we stop caching when told, even if DoneReading is called
6997 // after StopCaching. 7193 // after StopCaching.
6998 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { 7194 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) {
6999 MockHttpCache cache; 7195 MockHttpCache cache;
7000 TestCompletionCallback callback; 7196 TestCompletionCallback callback;
7001 MockHttpRequest request(kSimpleGET_Transaction); 7197 MockHttpRequest request(kSimpleGET_Transaction);
7002 7198
7003 { 7199 {
7004 std::unique_ptr<HttpTransaction> trans; 7200 std::unique_ptr<Context> c(new Context());
7005 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7201 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
7006 7202
7007 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7203 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
7008 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7204 EXPECT_THAT(callback.GetResult(rv), IsOk());
7009 7205
7010 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); 7206 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
7011 rv = trans->Read(buf.get(), 10, callback.callback()); 7207 rv = c->trans->Read(buf.get(), 10, callback.callback());
7012 EXPECT_EQ(10, callback.GetResult(rv)); 7208 EXPECT_EQ(10, callback.GetResult(rv));
7013 7209
7014 trans->StopCaching(); 7210 c->trans->StopCaching();
7015 7211
7016 // We should be able to keep reading. 7212 // We should be able to keep reading.
7017 rv = trans->Read(buf.get(), 256, callback.callback()); 7213 rv = c->trans->Read(buf.get(), 256, callback.callback());
7018 EXPECT_GT(callback.GetResult(rv), 0); 7214 EXPECT_GT(callback.GetResult(rv), 0);
7019 rv = trans->Read(buf.get(), 256, callback.callback()); 7215 rv = c->trans->Read(buf.get(), 256, callback.callback());
7020 EXPECT_EQ(0, callback.GetResult(rv)); 7216 EXPECT_EQ(0, callback.GetResult(rv));
7021 7217
7022 // We should be able to call DoneReading. 7218 // We should be able to call DoneReading.
7023 trans->DoneReading(); 7219 c->trans->DoneReading();
7024 } 7220 }
7025 7221
7026 // Make sure that the ActiveEntry is gone. 7222 // Make sure that the ActiveEntry is gone.
7027 base::RunLoop().RunUntilIdle(); 7223 base::RunLoop().RunUntilIdle();
7028 7224
7029 // Verify that the entry is gone. 7225 // Verify that the entry is gone.
7030 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7226 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7031 7227
7032 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7228 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7033 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7229 EXPECT_EQ(0, cache.disk_cache()->open_count());
7034 EXPECT_EQ(2, cache.disk_cache()->create_count()); 7230 EXPECT_EQ(2, cache.disk_cache()->create_count());
7035 } 7231 }
7036 7232
7037 // Tests that we stop caching when told, when using auth. 7233 // Tests that we stop caching when told, when using auth.
7038 TEST(HttpCache, StopCachingWithAuthDeletesEntry) { 7234 TEST(HttpCache, StopCachingWithAuthDeletesEntry) {
7039 MockHttpCache cache; 7235 MockHttpCache cache;
7040 TestCompletionCallback callback; 7236 TestCompletionCallback callback;
7041 MockTransaction mock_transaction(kSimpleGET_Transaction); 7237 MockTransaction mock_transaction(kSimpleGET_Transaction);
7042 mock_transaction.status = "HTTP/1.1 401 Unauthorized"; 7238 mock_transaction.status = "HTTP/1.1 401 Unauthorized";
7043 AddMockTransaction(&mock_transaction); 7239 AddMockTransaction(&mock_transaction);
7044 MockHttpRequest request(mock_transaction); 7240 MockHttpRequest request(mock_transaction);
7045 7241
7046 { 7242 {
7047 std::unique_ptr<HttpTransaction> trans; 7243 std::unique_ptr<Context> c(new Context());
7048 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7244 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
7049 7245
7050 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7246 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
7051 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7247 EXPECT_THAT(callback.GetResult(rv), IsOk());
7052 7248
7053 trans->StopCaching(); 7249 c->trans->StopCaching();
7054 7250
7055 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); 7251 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
7056 rv = trans->Read(buf.get(), 10, callback.callback()); 7252 rv = c->trans->Read(buf.get(), 10, callback.callback());
7057 EXPECT_EQ(callback.GetResult(rv), 10); 7253 EXPECT_EQ(callback.GetResult(rv), 10);
7058 } 7254 }
7059 RemoveMockTransaction(&mock_transaction); 7255 RemoveMockTransaction(&mock_transaction);
7060 7256
7061 // Make sure that the ActiveEntry is gone. 7257 // Make sure that the ActiveEntry is gone.
7062 base::RunLoop().RunUntilIdle(); 7258 base::RunLoop().RunUntilIdle();
7063 7259
7064 // Verify that the entry is gone. 7260 // Verify that the entry is gone.
7065 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7261 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7066 7262
7067 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7263 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7068 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7264 EXPECT_EQ(0, cache.disk_cache()->open_count());
7069 EXPECT_EQ(2, cache.disk_cache()->create_count()); 7265 EXPECT_EQ(2, cache.disk_cache()->create_count());
7070 } 7266 }
7071 7267
7072 // Tests that when we are told to stop caching we don't throw away valid data. 7268 // Tests that when we are told to stop caching we don't throw away valid data.
7073 TEST(HttpCache, StopCachingSavesEntry) { 7269 TEST(HttpCache, StopCachingSavesEntry) {
7074 MockHttpCache cache; 7270 MockHttpCache cache;
7075 TestCompletionCallback callback; 7271 TestCompletionCallback callback;
7076 MockHttpRequest request(kSimpleGET_Transaction); 7272 MockHttpRequest request(kSimpleGET_Transaction);
7077 7273
7078 { 7274 {
7079 std::unique_ptr<HttpTransaction> trans; 7275 std::unique_ptr<Context> c(new Context());
7080 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7276 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
7081 7277
7082 // Force a response that can be resumed. 7278 // Force a response that can be resumed.
7083 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); 7279 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
7084 AddMockTransaction(&mock_transaction); 7280 AddMockTransaction(&mock_transaction);
7085 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" 7281 mock_transaction.response_headers = "Cache-Control: max-age=10000\n"
7086 "Content-Length: 42\n" 7282 "Content-Length: 42\n"
7087 "Etag: \"foo\"\n"; 7283 "Etag: \"foo\"\n";
7088 7284
7089 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7285 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
7090 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7286 EXPECT_THAT(callback.GetResult(rv), IsOk());
7091 7287
7092 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); 7288 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
7093 rv = trans->Read(buf.get(), 10, callback.callback()); 7289 rv = c->trans->Read(buf.get(), 10, callback.callback());
7094 EXPECT_EQ(callback.GetResult(rv), 10); 7290 EXPECT_EQ(callback.GetResult(rv), 10);
7095 7291
7096 trans->StopCaching(); 7292 c->trans->StopCaching();
7097 7293
7098 // We should be able to keep reading. 7294 // We should be able to keep reading.
7099 rv = trans->Read(buf.get(), 256, callback.callback()); 7295 rv = c->trans->Read(buf.get(), 256, callback.callback());
7100 EXPECT_GT(callback.GetResult(rv), 0); 7296 EXPECT_GT(callback.GetResult(rv), 0);
7101 rv = trans->Read(buf.get(), 256, callback.callback()); 7297 rv = c->trans->Read(buf.get(), 256, callback.callback());
7102 EXPECT_EQ(callback.GetResult(rv), 0); 7298 EXPECT_EQ(callback.GetResult(rv), 0);
7103 } 7299 }
7104 7300
7105 // Verify that the entry is marked as incomplete. 7301 // Verify that the entry is marked as incomplete.
7106 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); 7302 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
7107 } 7303 }
7108 7304
7109 // Tests that we handle truncated enries when StopCaching is called. 7305 // Tests that we handle truncated enries when StopCaching is called.
7110 TEST(HttpCache, StopCachingTruncatedEntry) { 7306 TEST(HttpCache, StopCachingTruncatedEntry) {
7111 MockHttpCache cache; 7307 MockHttpCache cache;
7112 TestCompletionCallback callback; 7308 TestCompletionCallback callback;
7113 MockHttpRequest request(kRangeGET_TransactionOK); 7309 MockHttpRequest request(kRangeGET_TransactionOK);
7114 request.extra_headers.Clear(); 7310 request.extra_headers.Clear();
7115 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); 7311 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE);
7116 AddMockTransaction(&kRangeGET_TransactionOK); 7312 AddMockTransaction(&kRangeGET_TransactionOK);
7117 7313
7118 std::string raw_headers("HTTP/1.1 200 OK\n" 7314 std::string raw_headers("HTTP/1.1 200 OK\n"
7119 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7315 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7120 "ETag: \"foo\"\n" 7316 "ETag: \"foo\"\n"
7121 "Accept-Ranges: bytes\n" 7317 "Accept-Ranges: bytes\n"
7122 "Content-Length: 80\n"); 7318 "Content-Length: 80\n");
7123 CreateTruncatedEntry(raw_headers, &cache); 7319 CreateTruncatedEntry(raw_headers, &cache);
7124 7320
7125 { 7321 {
7126 // Now make a regular request. 7322 // Now make a regular request.
7127 std::unique_ptr<HttpTransaction> trans; 7323 std::unique_ptr<Context> c(new Context());
7128 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7324 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk());
7129 7325
7130 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7326 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
7131 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7327 EXPECT_THAT(callback.GetResult(rv), IsOk());
7132 7328
7133 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); 7329 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
7134 rv = trans->Read(buf.get(), 10, callback.callback()); 7330 rv = c->trans->Read(buf.get(), 10, callback.callback());
7135 EXPECT_EQ(callback.GetResult(rv), 10); 7331 EXPECT_EQ(callback.GetResult(rv), 10);
7136 7332
7137 // This is actually going to do nothing. 7333 // This is actually going to do nothing.
7138 trans->StopCaching(); 7334 c->trans->StopCaching();
7139 7335
7140 // We should be able to keep reading. 7336 // We should be able to keep reading.
7141 rv = trans->Read(buf.get(), 256, callback.callback()); 7337 rv = c->trans->Read(buf.get(), 256, callback.callback());
7142 EXPECT_GT(callback.GetResult(rv), 0); 7338 EXPECT_GT(callback.GetResult(rv), 0);
7143 rv = trans->Read(buf.get(), 256, callback.callback()); 7339 rv = c->trans->Read(buf.get(), 256, callback.callback());
7144 EXPECT_GT(callback.GetResult(rv), 0); 7340 EXPECT_GT(callback.GetResult(rv), 0);
7145 rv = trans->Read(buf.get(), 256, callback.callback()); 7341 rv = c->trans->Read(buf.get(), 256, callback.callback());
7146 EXPECT_EQ(callback.GetResult(rv), 0); 7342 EXPECT_EQ(callback.GetResult(rv), 0);
7147 } 7343 }
7148 7344
7149 // Verify that the disk entry was updated. 7345 // Verify that the disk entry was updated.
7150 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); 7346 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80);
7151 RemoveMockTransaction(&kRangeGET_TransactionOK); 7347 RemoveMockTransaction(&kRangeGET_TransactionOK);
7152 } 7348 }
7153 7349
7154 namespace { 7350 namespace {
7155 7351
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
7362 (*cache_initializer)(&cache); 7558 (*cache_initializer)(&cache);
7363 7559
7364 MockTransaction transaction(kSimpleGET_Transaction); 7560 MockTransaction transaction(kSimpleGET_Transaction);
7365 transaction.url = kRangeGET_TransactionOK.url; 7561 transaction.url = kRangeGET_TransactionOK.url;
7366 transaction.handler = &LargeResourceTransactionHandler; 7562 transaction.handler = &LargeResourceTransactionHandler;
7367 transaction.read_handler = &LargeBufferReader; 7563 transaction.read_handler = &LargeBufferReader;
7368 ScopedMockTransaction scoped_transaction(transaction); 7564 ScopedMockTransaction scoped_transaction(transaction);
7369 7565
7370 MockHttpRequest request(transaction); 7566 MockHttpRequest request(transaction);
7371 net::TestCompletionCallback callback; 7567 net::TestCompletionCallback callback;
7372 std::unique_ptr<net::HttpTransaction> http_transaction; 7568 std::unique_ptr<Context> c(new Context());
7373 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, 7569 int rv =
7374 &http_transaction); 7570 cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &c->trans);
7375 ASSERT_EQ(net::OK, rv); 7571 ASSERT_EQ(net::OK, rv);
7376 ASSERT_TRUE(http_transaction.get()); 7572 ASSERT_TRUE(c->trans.get());
7377 7573
7378 bool network_transaction_started = false; 7574 bool network_transaction_started = false;
7379 if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { 7575 if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) {
7380 http_transaction->SetBeforeNetworkStartCallback( 7576 c->trans->SetBeforeNetworkStartCallback(
7381 base::Bind(&SetFlagOnBeforeNetworkStart, &network_transaction_started)); 7577 base::Bind(&SetFlagOnBeforeNetworkStart, &network_transaction_started));
7382 } 7578 }
7383 7579
7384 rv = http_transaction->Start(&request, callback.callback(), 7580 rv = c->trans->Start(&request, callback.callback(), NetLogWithSource());
7385 NetLogWithSource());
7386 rv = callback.GetResult(rv); 7581 rv = callback.GetResult(rv);
7387 ASSERT_EQ(net::OK, rv); 7582 ASSERT_EQ(net::OK, rv);
7388 7583
7389 if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ) 7584 if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ)
7390 http_transaction->StopCaching(); 7585 c->trans->StopCaching();
7391 7586
7392 int64_t total_bytes_received = 0; 7587 int64_t total_bytes_received = 0;
7393 7588
7394 EXPECT_EQ(kTotalSize, 7589 EXPECT_EQ(kTotalSize,
7395 http_transaction->GetResponseInfo()->headers->GetContentLength()); 7590 c->trans->GetResponseInfo()->headers->GetContentLength());
7396 do { 7591 do {
7397 // This test simulates reading gigabytes of data. Buffer size is set to 10MB 7592 // This test simulates reading gigabytes of data. Buffer size is set to 10MB
7398 // to reduce the number of reads and speed up the test. 7593 // to reduce the number of reads and speed up the test.
7399 const int kBufferSize = 1024 * 1024 * 10; 7594 const int kBufferSize = 1024 * 1024 * 10;
7400 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); 7595 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize));
7401 rv = http_transaction->Read(buf.get(), kBufferSize, callback.callback()); 7596 rv = c->trans->Read(buf.get(), kBufferSize, callback.callback());
7402 rv = callback.GetResult(rv); 7597 rv = callback.GetResult(rv);
7403 7598
7404 if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ && 7599 if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ &&
7405 total_bytes_received == 0) { 7600 total_bytes_received == 0) {
7406 http_transaction->StopCaching(); 7601 c->trans->StopCaching();
7407 } 7602 }
7408 7603
7409 if (rv > 0) 7604 if (rv > 0)
7410 total_bytes_received += rv; 7605 total_bytes_received += rv;
7411 7606
7412 if (network_transaction_started && 7607 if (network_transaction_started &&
7413 stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { 7608 stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) {
7414 http_transaction->StopCaching(); 7609 c->trans->StopCaching();
7415 network_transaction_started = false; 7610 network_transaction_started = false;
7416 } 7611 }
7417 } while (rv > 0); 7612 } while (rv > 0);
7418 7613
7419 // The only verification we are going to do is that the received resource has 7614 // The only verification we are going to do is that the received resource has
7420 // the correct size. This is sufficient to verify that the state machine 7615 // the correct size. This is sufficient to verify that the state machine
7421 // didn't terminate abruptly due to the StopCaching() call. 7616 // didn't terminate abruptly due to the StopCaching() call.
7422 EXPECT_EQ(kTotalSize, total_bytes_received); 7617 EXPECT_EQ(kTotalSize, total_bytes_received);
7423 } 7618 }
7424 7619
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
7459 7654
7460 // Verify that the entry is marked as incomplete. 7655 // Verify that the entry is marked as incomplete.
7461 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); 7656 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
7462 } 7657 }
7463 7658
7464 // Make sure that calling SetPriority on a cache transaction passes on 7659 // Make sure that calling SetPriority on a cache transaction passes on
7465 // its priority updates to its underlying network transaction. 7660 // its priority updates to its underlying network transaction.
7466 TEST(HttpCache, SetPriority) { 7661 TEST(HttpCache, SetPriority) {
7467 MockHttpCache cache; 7662 MockHttpCache cache;
7468 7663
7469 std::unique_ptr<HttpTransaction> trans; 7664 std::unique_ptr<Context> c(new Context);
7470 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); 7665 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &c->trans), IsOk());
7471 7666
7472 // Shouldn't crash, but doesn't do anything either. 7667 // Shouldn't crash, but doesn't do anything either.
7473 trans->SetPriority(LOW); 7668 c->trans->SetPriority(LOW);
7474 7669
7475 EXPECT_FALSE(cache.network_layer()->last_transaction()); 7670 EXPECT_FALSE(cache.network_layer()->last_transaction());
7476 EXPECT_EQ(DEFAULT_PRIORITY, 7671 EXPECT_EQ(DEFAULT_PRIORITY,
7477 cache.network_layer()->last_create_transaction_priority()); 7672 cache.network_layer()->last_create_transaction_priority());
7478 7673
7479 HttpRequestInfo info; 7674 HttpRequestInfo info;
7480 info.url = GURL(kSimpleGET_Transaction.url); 7675 info.url = GURL(kSimpleGET_Transaction.url);
7481 TestCompletionCallback callback; 7676 TestCompletionCallback callback;
7482 EXPECT_EQ(ERR_IO_PENDING, 7677 EXPECT_EQ(ERR_IO_PENDING,
7483 trans->Start(&info, callback.callback(), NetLogWithSource())); 7678 c->trans->Start(&info, callback.callback(), NetLogWithSource()));
7484 7679
7485 EXPECT_TRUE(cache.network_layer()->last_transaction()); 7680 EXPECT_TRUE(cache.network_layer()->last_transaction());
7486 if (cache.network_layer()->last_transaction()) { 7681 if (cache.network_layer()->last_transaction()) {
7487 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); 7682 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority());
7488 EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority()); 7683 EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority());
7489 } 7684 }
7490 7685
7491 trans->SetPriority(HIGHEST); 7686 c->trans->SetPriority(HIGHEST);
7492 7687
7493 if (cache.network_layer()->last_transaction()) { 7688 if (cache.network_layer()->last_transaction()) {
7494 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); 7689 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority());
7495 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority()); 7690 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority());
7496 } 7691 }
7497 7692
7498 EXPECT_THAT(callback.WaitForResult(), IsOk()); 7693 EXPECT_THAT(callback.WaitForResult(), IsOk());
7499 } 7694 }
7500 7695
7501 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache 7696 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache
7502 // transaction passes on its argument to the underlying network transaction. 7697 // transaction passes on its argument to the underlying network transaction.
7503 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { 7698 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) {
7504 MockHttpCache cache; 7699 MockHttpCache cache;
7505 7700
7506 FakeWebSocketHandshakeStreamCreateHelper create_helper; 7701 FakeWebSocketHandshakeStreamCreateHelper create_helper;
7507 std::unique_ptr<HttpTransaction> trans; 7702 std::unique_ptr<Context> c(new Context);
7508 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); 7703 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &c->trans), IsOk());
7509 7704
7510 EXPECT_FALSE(cache.network_layer()->last_transaction()); 7705 EXPECT_FALSE(cache.network_layer()->last_transaction());
7511 7706
7512 HttpRequestInfo info; 7707 HttpRequestInfo info;
7513 info.url = GURL(kSimpleGET_Transaction.url); 7708 info.url = GURL(kSimpleGET_Transaction.url);
7514 TestCompletionCallback callback; 7709 TestCompletionCallback callback;
7515 EXPECT_EQ(ERR_IO_PENDING, 7710 EXPECT_EQ(ERR_IO_PENDING,
7516 trans->Start(&info, callback.callback(), NetLogWithSource())); 7711 c->trans->Start(&info, callback.callback(), NetLogWithSource()));
7517 7712
7518 ASSERT_TRUE(cache.network_layer()->last_transaction()); 7713 ASSERT_TRUE(cache.network_layer()->last_transaction());
7519 EXPECT_FALSE(cache.network_layer()->last_transaction()-> 7714 EXPECT_FALSE(cache.network_layer()->last_transaction()->
7520 websocket_handshake_stream_create_helper()); 7715 websocket_handshake_stream_create_helper());
7521 trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); 7716 c->trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper);
7522 EXPECT_EQ(&create_helper, 7717 EXPECT_EQ(&create_helper,
7523 cache.network_layer()->last_transaction()-> 7718 cache.network_layer()->last_transaction()->
7524 websocket_handshake_stream_create_helper()); 7719 websocket_handshake_stream_create_helper());
7525 EXPECT_THAT(callback.WaitForResult(), IsOk()); 7720 EXPECT_THAT(callback.WaitForResult(), IsOk());
7526 } 7721 }
7527 7722
7528 // Make sure that a cache transaction passes on its priority to 7723 // Make sure that a cache transaction passes on its priority to
7529 // newly-created network transactions. 7724 // newly-created network transactions.
7530 TEST(HttpCache, SetPriorityNewTransaction) { 7725 TEST(HttpCache, SetPriorityNewTransaction) {
7531 MockHttpCache cache; 7726 MockHttpCache cache;
7532 AddMockTransaction(&kRangeGET_TransactionOK); 7727 AddMockTransaction(&kRangeGET_TransactionOK);
7533 7728
7534 std::string raw_headers("HTTP/1.1 200 OK\n" 7729 std::string raw_headers("HTTP/1.1 200 OK\n"
7535 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7730 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7536 "ETag: \"foo\"\n" 7731 "ETag: \"foo\"\n"
7537 "Accept-Ranges: bytes\n" 7732 "Accept-Ranges: bytes\n"
7538 "Content-Length: 80\n"); 7733 "Content-Length: 80\n");
7539 CreateTruncatedEntry(raw_headers, &cache); 7734 CreateTruncatedEntry(raw_headers, &cache);
7540 7735
7541 // Now make a regular request. 7736 // Now make a regular request.
7542 std::string headers; 7737 std::string headers;
7543 MockTransaction transaction(kRangeGET_TransactionOK); 7738 MockTransaction transaction(kRangeGET_TransactionOK);
7544 transaction.request_headers = EXTRA_HEADER; 7739 transaction.request_headers = EXTRA_HEADER;
7545 transaction.data = kFullRangeData; 7740 transaction.data = kFullRangeData;
7546 7741
7547 std::unique_ptr<HttpTransaction> trans; 7742 std::unique_ptr<Context> c(new Context);
7548 ASSERT_THAT(cache.http_cache()->CreateTransaction(MEDIUM, &trans), IsOk()); 7743 ASSERT_THAT(cache.http_cache()->CreateTransaction(MEDIUM, &c->trans), IsOk());
7549 EXPECT_EQ(DEFAULT_PRIORITY, 7744 EXPECT_EQ(DEFAULT_PRIORITY,
7550 cache.network_layer()->last_create_transaction_priority()); 7745 cache.network_layer()->last_create_transaction_priority());
7551 7746
7552 MockHttpRequest info(transaction); 7747 MockHttpRequest info(transaction);
7553 TestCompletionCallback callback; 7748 TestCompletionCallback callback;
7554 EXPECT_EQ(ERR_IO_PENDING, 7749 EXPECT_EQ(ERR_IO_PENDING,
7555 trans->Start(&info, callback.callback(), NetLogWithSource())); 7750 c->trans->Start(&info, callback.callback(), NetLogWithSource()));
7556 EXPECT_THAT(callback.WaitForResult(), IsOk()); 7751 EXPECT_THAT(callback.WaitForResult(), IsOk());
7557 7752
7558 EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority()); 7753 EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority());
7559 7754
7560 trans->SetPriority(HIGHEST); 7755 c->trans->SetPriority(HIGHEST);
7561 // Should trigger a new network transaction and pick up the new 7756 // Should trigger a new network transaction and pick up the new
7562 // priority. 7757 // priority.
7563 ReadAndVerifyTransaction(trans.get(), transaction); 7758 ReadAndVerifyTransaction(c->trans.get(), transaction);
7564 7759
7565 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); 7760 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority());
7566 7761
7567 RemoveMockTransaction(&kRangeGET_TransactionOK); 7762 RemoveMockTransaction(&kRangeGET_TransactionOK);
7568 } 7763 }
7569 7764
7570 namespace { 7765 namespace {
7571 7766
7572 void RunTransactionAndGetNetworkBytes(MockHttpCache& cache, 7767 void RunTransactionAndGetNetworkBytes(MockHttpCache& cache,
7573 const MockTransaction& trans_info, 7768 const MockTransaction& trans_info,
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
7932 MockHttpCache cache; 8127 MockHttpCache cache;
7933 8128
7934 // Create a transaction for bytes 0-9. 8129 // Create a transaction for bytes 0-9.
7935 MockHttpRequest request(kRangeGET_TransactionOK); 8130 MockHttpRequest request(kRangeGET_TransactionOK);
7936 MockTransaction transaction(kRangeGET_TransactionOK); 8131 MockTransaction transaction(kRangeGET_TransactionOK);
7937 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; 8132 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
7938 transaction.data = "rg: 00-09 "; 8133 transaction.data = "rg: 00-09 ";
7939 AddMockTransaction(&transaction); 8134 AddMockTransaction(&transaction);
7940 8135
7941 TestCompletionCallback callback; 8136 TestCompletionCallback callback;
7942 std::unique_ptr<HttpTransaction> trans; 8137 std::unique_ptr<Context> c(new Context);
7943 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 8138 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans);
7944 EXPECT_THAT(rv, IsOk()); 8139 EXPECT_THAT(rv, IsOk());
7945 ASSERT_TRUE(trans.get()); 8140 ASSERT_TRUE(c->trans.get());
7946 8141
7947 // Start our transaction. 8142 // Start our transaction.
7948 trans->Start(&request, callback.callback(), NetLogWithSource()); 8143 c->trans->Start(&request, callback.callback(), NetLogWithSource());
7949 8144
7950 // A second transaction on a different part of the file (the default 8145 // A second transaction on a different part of the file (the default
7951 // kRangeGET_TransactionOK requests 40-49) should not be blocked by 8146 // kRangeGET_TransactionOK requests 40-49) should not be blocked by
7952 // the already pending transaction. 8147 // the already pending transaction.
7953 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 8148 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
7954 8149
7955 // Let the first transaction complete. 8150 // Let the first transaction complete.
7956 callback.WaitForResult(); 8151 callback.WaitForResult();
7957 8152
7958 RemoveMockTransaction(&transaction); 8153 RemoveMockTransaction(&transaction);
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
8189 HttpResponseInfo response_info; 8384 HttpResponseInfo response_info;
8190 RunTransactionTestWithResponseInfo(cache.http_cache(), 8385 RunTransactionTestWithResponseInfo(cache.http_cache(),
8191 kTypicalGET_Transaction, &response_info); 8386 kTypicalGET_Transaction, &response_info);
8192 8387
8193 EXPECT_FALSE(response_info.was_cached); 8388 EXPECT_FALSE(response_info.was_cached);
8194 EXPECT_TRUE(response_info.network_accessed); 8389 EXPECT_TRUE(response_info.network_accessed);
8195 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, 8390 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE,
8196 response_info.cache_entry_status); 8391 response_info.cache_entry_status);
8197 } 8392 }
8198 8393
8394 TEST(HttpCache, SimpleGET_SharedWritingCacheRead) {
8395 MockHttpCache cache;
8396
8397 MockHttpRequest request(kSimpleGET_Transaction);
8398
8399 std::vector<Context*> context_list;
8400 const int kNumTransactions = 5;
8401
8402 for (int i = 0; i < kNumTransactions; ++i) {
8403 context_list.push_back(new Context());
8404 Context* c = context_list[i];
8405
8406 c->result = cache.CreateTransaction(&c->trans);
8407 ASSERT_THAT(c->result, IsOk());
8408 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8409
8410 c->result =
8411 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
8412 }
8413
8414 // All requests are waiting for the active entry.
8415 for (int i = 0; i < kNumTransactions; ++i) {
8416 Context* c = context_list[i];
8417 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8418 }
8419
8420 // Allow all requests to move from the Create queue to the active entry.
8421 base::RunLoop().RunUntilIdle();
8422
8423 // The first request should be a writer at this point, and the subsequent
8424 // requests should be pending.
8425
8426 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8427 EXPECT_EQ(0, cache.disk_cache()->open_count());
8428 EXPECT_EQ(1, cache.disk_cache()->create_count());
8429
8430 // All requests depend on the writer, and the writer is between Start and
8431 // Read, i.e. idle.
8432 for (int i = 0; i < kNumTransactions; ++i) {
8433 Context* c = context_list[i];
8434 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8435 }
8436
8437 for (int i = 0; i < kNumTransactions; ++i) {
8438 Context* c = context_list[i];
8439 if (c->result == ERR_IO_PENDING)
8440 c->result = c->callback.WaitForResult();
8441 }
8442
8443 ReadAndVerifySharedWritersCacheRead(context_list, kSimpleGET_Transaction);
8444
8445 // We should not have had to re-open the disk entry
8446
8447 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8448 EXPECT_EQ(0, cache.disk_cache()->open_count());
8449 EXPECT_EQ(1, cache.disk_cache()->create_count());
8450
8451 for (int i = 0; i < kNumTransactions; ++i) {
8452 Context* c = context_list[i];
8453 delete c;
8454 }
8455 }
8456
8457 TEST(HttpCache, SimpleGET_SharedWritingJoinedRead) {
8458 MockHttpCache cache;
8459
8460 MockHttpRequest request(kSimpleGET_Transaction);
8461
8462 std::vector<Context*> context_list;
8463 const int kNumTransactions = 5;
8464
8465 for (int i = 0; i < kNumTransactions; ++i) {
8466 context_list.push_back(new Context());
8467 Context* c = context_list[i];
8468
8469 c->result = cache.CreateTransaction(&c->trans);
8470 ASSERT_THAT(c->result, IsOk());
8471 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8472
8473 c->result =
8474 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
8475 }
8476
8477 // All requests are waiting for the active entry.
8478 for (int i = 0; i < kNumTransactions; ++i) {
8479 Context* c = context_list[i];
8480 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8481 }
8482
8483 // Allow all requests to move from the Create queue to the active entry.
8484 base::RunLoop().RunUntilIdle();
8485
8486 // The first request should be a writer at this point, and the subsequent
8487 // requests should be pending.
8488
8489 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8490 EXPECT_EQ(0, cache.disk_cache()->open_count());
8491 EXPECT_EQ(1, cache.disk_cache()->create_count());
8492
8493 // All requests depend on the writer, and the writer is between Start and
8494 // Read, i.e. idle.
8495 for (int i = 0; i < kNumTransactions; ++i) {
8496 Context* c = context_list[i];
8497 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8498 }
8499
8500 std::vector<HttpTransaction*> transactions;
8501 for (int i = 0; i < kNumTransactions; ++i) {
8502 Context* c = context_list[i];
8503 if (c->result == ERR_IO_PENDING)
8504 c->result = c->callback.WaitForResult();
8505 transactions.push_back(c->trans.get());
8506 }
8507
8508 ReadAndVerifySharedWritersJoinedRead(kSimpleGET_Transaction, context_list);
8509
8510 // We should not have had to re-open the disk entry
8511
8512 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8513 EXPECT_EQ(0, cache.disk_cache()->open_count());
8514 EXPECT_EQ(1, cache.disk_cache()->create_count());
8515
8516 for (int i = 0; i < kNumTransactions; ++i) {
8517 Context* c = context_list[i];
8518 delete c;
8519 }
8520 }
8521
8522 TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDoneReading) {
8523 MockHttpCache cache;
8524
8525 MockHttpRequest request(kSimpleGET_Transaction);
8526
8527 std::vector<Context*> context_list;
8528 const int kNumTransactions = 5;
8529
8530 for (int i = 0; i < kNumTransactions; ++i) {
8531 context_list.push_back(new Context());
8532 Context* c = context_list[i];
8533
8534 c->result = cache.CreateTransaction(&c->trans);
8535 ASSERT_THAT(c->result, IsOk());
8536 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8537
8538 c->result =
8539 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
8540 }
8541
8542 // All requests are waiting for the active entry.
8543 for (int i = 0; i < kNumTransactions; ++i) {
8544 Context* c = context_list[i];
8545 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8546 }
8547
8548 // Allow all requests to move from the Create queue to the active entry.
8549 base::RunLoop().RunUntilIdle();
8550
8551 // The first request should be a writer at this point, and the subsequent
8552 // requests should be pending.
8553
8554 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8555 EXPECT_EQ(0, cache.disk_cache()->open_count());
8556 EXPECT_EQ(1, cache.disk_cache()->create_count());
8557
8558 // All requests depend on the writer, and the writer is between Start and
8559 // Read, i.e. idle.
8560 for (int i = 0; i < kNumTransactions; ++i) {
8561 Context* c = context_list[i];
8562 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8563 }
8564
8565 std::vector<HttpTransaction*> transactions;
8566 for (int i = 0; i < kNumTransactions; ++i) {
8567 Context* c = context_list[i];
8568 if (c->result == ERR_IO_PENDING)
8569 c->result = c->callback.WaitForResult();
8570 transactions.push_back(c->trans.get());
8571 }
8572
8573 ReadAndVerifySharedWritersJoinedReadDoneReading(kSimpleGET_Transaction,
8574 context_list);
8575
8576 // We should not have had to re-open the disk entry
8577
8578 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8579 EXPECT_EQ(0, cache.disk_cache()->open_count());
8580 EXPECT_EQ(1, cache.disk_cache()->create_count());
8581
8582 for (int i = 0; i < kNumTransactions; ++i) {
8583 Context* c = context_list[i];
8584 delete c;
8585 }
8586 }
8587 // Tests the following:
8588 // - A 200 will doom the entry and the transaction will continue to read from
8589 // the network.
8590 // - Doomed entry's SharedWriters will continue to read and write to the cache.
8591 // - Doomed entry's SharedWriters' waiting_for_validation_ will create another
8592 // entry.
8593 // - The new entry will also have another SharedWriters for subsequent
8594 // transactions.
8595 TEST(HttpCache, SharedWritingValidation200) {
8596 MockHttpCache cache;
8597
8598 MockHttpRequest request(kSimpleGET_Transaction);
8599 request.load_flags |= LOAD_VALIDATE_CACHE;
8600 MockHttpRequest reader_request(kSimpleGET_Transaction);
8601 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
8602 MockHttpRequest request_no_validate(kSimpleGET_Transaction);
8603
8604 std::vector<Context*> context_list;
8605 const int kNumTransactions = 6;
8606
8607 for (int i = 0; i < kNumTransactions; ++i) {
8608 context_list.push_back(new Context());
8609 Context* c = context_list[i];
8610
8611 c->result = cache.CreateTransaction(&c->trans);
8612 ASSERT_THAT(c->result, IsOk());
8613 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8614
8615 MockHttpRequest* this_request = &request;
8616 if (i == 1 || i == 2)
8617 this_request = &reader_request;
8618
8619 if (i == 5)
8620 this_request = &request_no_validate;
8621
8622 c->result = c->trans->Start(this_request, c->callback.callback(),
8623 NetLogWithSource());
8624 }
8625
8626 // All requests are waiting for the active entry.
8627 for (int i = 0; i < kNumTransactions; ++i) {
8628 Context* c = context_list[i];
8629 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8630 }
8631
8632 // Allow all requests to move from the Create queue to the active entry.
8633 base::RunLoop().RunUntilIdle();
8634
8635 // The first [0] request should be a writer at this point, and should have
8636 // created
8637 // SharedWriters, [1] and [2] will be in pending_queue and [3] and [4] should
8638 // have validated and [5] will have skipped validation and be ready to read.
8639
8640 EXPECT_EQ(3, cache.network_layer()->transaction_count());
8641 EXPECT_EQ(0, cache.disk_cache()->open_count());
8642 EXPECT_EQ(2, cache.disk_cache()->create_count());
8643
8644 Context* c = context_list[0];
8645 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
8646 c->result = c->callback.WaitForResult();
8647
8648 // First request is done with the headers, now [3] will start validation.
8649 Context* c3 = context_list[3];
8650 ASSERT_THAT(c3->result, IsError(ERR_IO_PENDING));
8651
8652 // Let [3] get a 200 , thus leading to dooming the entry, [4] and [5] will
8653 // move to entry's pending_queue.
8654 c3->result = c3->callback.WaitForResult();
8655
8656 // [3] will complete reading the response using its own network transaction
8657 // and not writing to the cache.
8658 ReadAndVerifyTransaction(c3->trans.get(), kSimpleGET_Transaction);
8659
8660 // [0] will complete reading the response using SharedWriter's network
8661 // transaction and then the entry should be finalized.
8662 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
8663
8664 // [4] should be added to a new entry and should contiue reading and writing
8665 // to the cache (also create SharedWriters)
8666 for (int i = 4; i <= 5; i++) {
8667 Context* c = context_list[i];
8668 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
8669 c->result = c->callback.WaitForResult();
8670 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8671 // [5] will join SharedWriters at this point when [4] completes validation.
8672 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
8673 }
8674 // [1] and [2] will become readers and read from the cache.
8675 for (int i = 1; i <= 2; i++) {
8676 Context* c = context_list[i];
8677 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
8678 c->result = c->callback.WaitForResult();
8679 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8680 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
8681 }
8682
8683 // We should not have had to re-open the disk entry
8684 EXPECT_EQ(3, cache.network_layer()->transaction_count());
8685 EXPECT_EQ(0, cache.disk_cache()->open_count());
8686 EXPECT_EQ(2, cache.disk_cache()->create_count());
8687
8688 for (int i = 0; i < kNumTransactions; ++i) {
8689 Context* c = context_list[i];
8690 delete c;
8691 }
8692 }
8693
8694 // Tests a validating transaction deletion while it is waiting for its callback
8695 // to be invoked.
8696 TEST(HttpCache, SharedWritingDeleteValidationTrans) {
8697 MockHttpCache cache;
8698
8699 MockHttpRequest request(kSimpleGET_Transaction);
8700 request.load_flags |= LOAD_VALIDATE_CACHE;
8701 MockHttpRequest reader_request(kSimpleGET_Transaction);
8702 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
8703
8704 std::vector<Context*> context_list;
8705 const int kNumTransactions = 5;
8706
8707 for (int i = 0; i < kNumTransactions; ++i) {
8708 context_list.push_back(new Context());
8709 Context* c = context_list[i];
8710
8711 c->result = cache.CreateTransaction(&c->trans);
8712 ASSERT_THAT(c->result, IsOk());
8713 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8714
8715 MockHttpRequest* this_request = &request;
8716 if (i == 1 || i == 2)
8717 this_request = &reader_request;
8718
8719 if (i == 3 || i == 4)
8720 continue;
8721
8722 c->result = c->trans->Start(this_request, c->callback.callback(),
8723 NetLogWithSource());
8724 }
8725
8726 // All requests are waiting for the active entry.
8727 for (int i = 0; i < kNumTransactions; ++i) {
8728 if (i == 3 || i == 4)
8729 continue;
8730 Context* c = context_list[i];
8731 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8732 }
8733
8734 // Allow all requests to move from the Create queue to the active entry.
8735 base::RunLoop().RunUntilIdle();
8736
8737 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8738 EXPECT_EQ(0, cache.disk_cache()->open_count());
8739 EXPECT_EQ(1, cache.disk_cache()->create_count());
8740
8741 // Start [3], [4].
8742 for (size_t i = 3; i <= 4; i++) {
8743 Context* c = context_list[i];
8744 c->result =
8745 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
8746 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
8747 }
8748
8749 // Delete [3] as it is the current validating trans.
8750 Context* c3 = context_list[3];
8751 for (auto it = context_list.begin(); it != context_list.end(); it++) {
8752 if (*it == c3) {
8753 context_list.erase(it);
8754 delete c3;
8755 break;
8756 }
8757 }
8758
8759 // Complete start state machine for [4].
8760 base::RunLoop().RunUntilIdle();
8761
8762 // [0] will complete reading the response using SharedWriter's network
8763 // transaction and then the entry should be finalized.
8764 Context* c0 = context_list[0];
8765 ReadAndVerifyTransaction(c0->trans.get(), kSimpleGET_Transaction);
8766
8767 // [4] (now [3]) should doom the entry and continue reading with its network
8768 // transaction.
8769 Context* c4 = context_list[3];
8770 ReadAndVerifyTransaction(c4->trans.get(), kSimpleGET_Transaction);
8771
8772 // 1st entry will get destroyed and [1] and [2] will being readers will get a
8773 // ERR_CACHE_MISS.
8774 for (int i = 1; i <= 2; i++) {
8775 Context* c = context_list[i];
8776 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
8777 c->result = c->callback.WaitForResult();
8778 ASSERT_THAT(c->result, IsError(ERR_CACHE_MISS));
8779 }
8780
8781 // We should not have had to re-open the disk entry
8782 EXPECT_EQ(2, cache.network_layer()->transaction_count());
8783 EXPECT_EQ(0, cache.disk_cache()->open_count());
8784 EXPECT_EQ(1, cache.disk_cache()->create_count());
8785
8786 for (auto i = context_list.begin(); i != context_list.end(); ++i) {
8787 Context* c = *i;
8788 delete c;
8789 }
8790 }
8791
8792 // Tests a transaction deletion while it is waiting for validation.
8793 TEST(HttpCache, SharedWritingDeleteWaitingForValidation) {
8794 MockHttpCache cache;
8795
8796 MockHttpRequest request(kSimpleGET_Transaction);
8797 request.load_flags |= LOAD_VALIDATE_CACHE;
8798 MockHttpRequest reader_request(kSimpleGET_Transaction);
8799 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
8800
8801 std::vector<Context*> context_list;
8802 const int kNumTransactions = 5;
8803
8804 for (int i = 0; i < kNumTransactions; ++i) {
8805 context_list.push_back(new Context());
8806 Context* c = context_list[i];
8807
8808 c->result = cache.CreateTransaction(&c->trans);
8809 ASSERT_THAT(c->result, IsOk());
8810 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8811
8812 MockHttpRequest* this_request = &request;
8813 if (i == 1 || i == 2)
8814 this_request = &reader_request;
8815
8816 if (i == 3 || i == 4)
8817 continue;
8818
8819 c->result = c->trans->Start(this_request, c->callback.callback(),
8820 NetLogWithSource());
8821 }
8822
8823 // All requests are waiting for the active entry.
8824 for (int i = 0; i < kNumTransactions; ++i) {
8825 if (i == 3 || i == 4)
8826 continue;
8827 Context* c = context_list[i];
8828 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8829 }
8830
8831 // Allow all requests to move from the Create queue to the active entry.
8832 base::RunLoop().RunUntilIdle();
8833
8834 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8835 EXPECT_EQ(0, cache.disk_cache()->open_count());
8836 EXPECT_EQ(1, cache.disk_cache()->create_count());
8837
8838 // Start [3], [4].
8839 for (size_t i = 3; i <= 4; i++) {
8840 Context* c = context_list[i];
8841 c->result =
8842 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
8843 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
8844 }
8845
8846 // Delete [4] as it is the waiting for validation trans.
8847 Context* c4 = context_list[4];
8848 for (auto it = context_list.begin(); it != context_list.end(); it++) {
8849 if (*it == c4) {
8850 context_list.erase(it);
8851 delete c4;
8852 break;
8853 }
8854 }
8855
8856 // Complete start state machine for [3].
8857 base::RunLoop().RunUntilIdle();
8858
8859 // [0] will complete reading the response using SharedWriter's network
8860 // transaction and then the entry should be finalized.
8861 Context* c0 = context_list[0];
8862 ReadAndVerifyTransaction(c0->trans.get(), kSimpleGET_Transaction);
8863
8864 // [3] should doom the entry and continue reading with its network
8865 // transaction.
8866 Context* c3 = context_list[3];
8867 ReadAndVerifyTransaction(c3->trans.get(), kSimpleGET_Transaction);
8868
8869 // 1st entry will get destroyed and [1] and [2] will being readers will get a
8870 // ERR_CACHE_MISS.
8871 for (int i = 1; i <= 2; i++) {
8872 Context* c = context_list[i];
8873 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
8874 c->result = c->callback.WaitForResult();
8875 ASSERT_THAT(c->result, IsError(ERR_CACHE_MISS));
8876 }
8877
8878 // We should not have had to re-open the disk entry
8879 EXPECT_EQ(2, cache.network_layer()->transaction_count());
8880 EXPECT_EQ(0, cache.disk_cache()->open_count());
8881 EXPECT_EQ(1, cache.disk_cache()->create_count());
8882
8883 for (auto i = context_list.begin(); i != context_list.end(); ++i) {
8884 Context* c = *i;
8885 delete c;
8886 }
8887 }
8888
8889 // Tests the impact of cache write failure on Shared Writing.
8890 TEST(HttpCache, SharedWritingCacheWriteFailure) {
8891 MockHttpCache cache;
8892
8893 MockHttpRequest request(kSimpleGET_Transaction);
8894 request.load_flags |= LOAD_VALIDATE_CACHE;
8895 MockHttpRequest reader_request(kSimpleGET_Transaction);
8896 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
8897 MockHttpRequest request_no_validate(kSimpleGET_Transaction);
8898
8899 std::vector<Context*> context_list;
8900 const int kNumTransactions = 7;
8901
8902 for (int i = 0; i < kNumTransactions; ++i) {
8903 context_list.push_back(new Context());
8904 Context* c = context_list[i];
8905
8906 c->result = cache.CreateTransaction(&c->trans);
8907 ASSERT_THAT(c->result, IsOk());
8908 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8909
8910 MockHttpRequest* this_request = &request;
8911 if (i == 1 || i == 2)
8912 this_request = &reader_request;
8913
8914 if (i == 3 || i == 6)
8915 this_request = &request_no_validate;
8916
8917 if (i == 4 || i == 5)
8918 continue;
8919
8920 c->result = c->trans->Start(this_request, c->callback.callback(),
8921 NetLogWithSource());
8922 }
8923
8924 // All requests are waiting for the active entry.
8925 for (int i = 0; i < kNumTransactions; ++i) {
8926 if (i == 4 || i == 5)
8927 continue;
8928 Context* c = context_list[i];
8929 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8930 }
8931
8932 // Allow all requests to move from the Create queue to the active entry.
8933 base::RunLoop().RunUntilIdle();
8934
8935 // At this point, 0,5 and 3 are idle writers, 1 and 2 are pending readers and
8936 // 4
8937 // will now become the validating_trans_.
8938
8939 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8940 EXPECT_EQ(0, cache.disk_cache()->open_count());
8941 EXPECT_EQ(1, cache.disk_cache()->create_count());
8942
8943 ReadAndVerifySharedWritersJoinedReadCacheWriteFailure(kSimpleGET_Transaction,
8944 context_list, cache);
8945
8946 // We should not have had to re-open the disk entry
8947 EXPECT_EQ(3, cache.network_layer()->transaction_count());
8948 EXPECT_EQ(1, cache.disk_cache()->open_count());
8949 EXPECT_EQ(3, cache.disk_cache()->create_count());
8950
8951 for (auto i = context_list.begin(); i != context_list.end(); ++i) {
8952 Context* c = *i;
8953 delete c;
8954 }
8955 }
8956
8957 TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDoomCurrentWriter) {
8958 MockHttpCache cache;
8959
8960 MockHttpRequest request(kSimpleGET_Transaction);
8961
8962 std::vector<Context*> context_list;
8963 const int kNumTransactions = 5;
8964
8965 for (int i = 0; i < kNumTransactions; ++i) {
8966 context_list.push_back(new Context());
8967 Context* c = context_list[i];
8968
8969 c->result = cache.CreateTransaction(&c->trans);
8970 ASSERT_THAT(c->result, IsOk());
8971 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8972
8973 c->result =
8974 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
8975 }
8976
8977 // All requests are waiting for the active entry.
8978 for (int i = 0; i < kNumTransactions; ++i) {
8979 Context* c = context_list[i];
8980 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
8981 }
8982
8983 // Allow all requests to move from the Create queue to the active entry.
8984 base::RunLoop().RunUntilIdle();
8985
8986 // The first request should be a writer at this point, and the subsequent
8987 // requests should be pending.
8988
8989 EXPECT_EQ(1, cache.network_layer()->transaction_count());
8990 EXPECT_EQ(0, cache.disk_cache()->open_count());
8991 EXPECT_EQ(1, cache.disk_cache()->create_count());
8992
8993 // All requests depend on the writer, and the writer is between Start and
8994 // Read, i.e. idle.
8995 for (int i = 0; i < kNumTransactions; ++i) {
8996 Context* c = context_list[i];
8997 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
8998 }
8999
9000 std::vector<HttpTransaction*> transactions;
9001 for (int i = 0; i < kNumTransactions; ++i) {
9002 Context* c = context_list[i];
9003 if (c->result == ERR_IO_PENDING)
9004 c->result = c->callback.WaitForResult();
9005 transactions.push_back(c->trans.get());
9006 }
9007
9008 ReadAndVerifySharedWritersJoinedReadDoomCurrentWriter(kSimpleGET_Transaction,
9009 context_list);
9010
9011 // We should not have had to re-open the disk entry
9012 EXPECT_EQ(1, cache.network_layer()->transaction_count());
9013 EXPECT_EQ(0, cache.disk_cache()->open_count());
9014 EXPECT_EQ(1, cache.disk_cache()->create_count());
9015
9016 for (auto i = context_list.begin(); i != context_list.end(); ++i) {
9017 Context* c = *i;
9018 delete c;
9019 }
9020 }
9021
9022 TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDeleteWaitingWriter) {
9023 MockHttpCache cache;
9024
9025 MockHttpRequest request(kSimpleGET_Transaction);
9026
9027 std::vector<Context*> context_list;
9028 const int kNumTransactions = 5;
9029
9030 for (int i = 0; i < kNumTransactions; ++i) {
9031 context_list.push_back(new Context());
9032 Context* c = context_list[i];
9033
9034 c->result = cache.CreateTransaction(&c->trans);
9035 ASSERT_THAT(c->result, IsOk());
9036 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
9037
9038 c->result =
9039 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
9040 }
9041
9042 // All requests are waiting for the active entry.
9043 for (int i = 0; i < kNumTransactions; ++i) {
9044 Context* c = context_list[i];
9045 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
9046 }
9047
9048 // Allow all requests to move from the Create queue to the active entry.
9049 base::RunLoop().RunUntilIdle();
9050
9051 // The first request should be a writer at this point, and the subsequent
9052 // requests should be pending.
9053
9054 EXPECT_EQ(1, cache.network_layer()->transaction_count());
9055 EXPECT_EQ(0, cache.disk_cache()->open_count());
9056 EXPECT_EQ(1, cache.disk_cache()->create_count());
9057
9058 // All requests depend on the writer, and the writer is between Start and
9059 // Read, i.e. idle.
9060 for (int i = 0; i < kNumTransactions; ++i) {
9061 Context* c = context_list[i];
9062 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
9063 }
9064
9065 std::vector<HttpTransaction*> transactions;
9066 for (int i = 0; i < kNumTransactions; ++i) {
9067 Context* c = context_list[i];
9068 if (c->result == ERR_IO_PENDING)
9069 c->result = c->callback.WaitForResult();
9070 transactions.push_back(c->trans.get());
9071 }
9072
9073 ReadAndVerifySharedWritersDeleteWaitingWriter(kSimpleGET_Transaction,
9074 context_list);
9075
9076 // We should not have had to re-open the disk entry
9077 EXPECT_EQ(1, cache.network_layer()->transaction_count());
9078 EXPECT_EQ(0, cache.disk_cache()->open_count());
9079 EXPECT_EQ(1, cache.disk_cache()->create_count());
9080
9081 for (auto i = context_list.begin(); i != context_list.end(); ++i) {
9082 Context* c = *i;
9083 delete c;
9084 }
9085 }
9086
9087 TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDeleteIdleWriter) {
9088 MockHttpCache cache;
9089
9090 MockHttpRequest request(kSimpleGET_Transaction);
9091
9092 std::vector<Context*> context_list;
9093 const int kNumTransactions = 5;
9094
9095 for (int i = 0; i < kNumTransactions; ++i) {
9096 context_list.push_back(new Context());
9097 Context* c = context_list[i];
9098
9099 c->result = cache.CreateTransaction(&c->trans);
9100 ASSERT_THAT(c->result, IsOk());
9101 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
9102
9103 c->result =
9104 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
9105 }
9106
9107 // All requests are waiting for the active entry.
9108 for (int i = 0; i < kNumTransactions; ++i) {
9109 Context* c = context_list[i];
9110 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
9111 }
9112
9113 // Allow all requests to move from the Create queue to the active entry.
9114 base::RunLoop().RunUntilIdle();
9115
9116 // The first request should be a writer at this point, and the subsequent
9117 // requests should be pending.
9118
9119 EXPECT_EQ(1, cache.network_layer()->transaction_count());
9120 EXPECT_EQ(0, cache.disk_cache()->open_count());
9121 EXPECT_EQ(1, cache.disk_cache()->create_count());
9122
9123 // All requests depend on the writer, and the writer is between Start and
9124 // Read, i.e. idle.
9125 for (int i = 0; i < kNumTransactions; ++i) {
9126 Context* c = context_list[i];
9127 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
9128 }
9129
9130 std::vector<HttpTransaction*> transactions;
9131 for (int i = 0; i < kNumTransactions; ++i) {
9132 Context* c = context_list[i];
9133 if (c->result == ERR_IO_PENDING)
9134 c->result = c->callback.WaitForResult();
9135 transactions.push_back(c->trans.get());
9136 }
9137
9138 ReadAndVerifySharedWritersDeleteIdleWriter(kSimpleGET_Transaction,
9139 context_list);
9140
9141 // We should not have had to re-open the disk entry
9142 EXPECT_EQ(1, cache.network_layer()->transaction_count());
9143 EXPECT_EQ(0, cache.disk_cache()->open_count());
9144 EXPECT_EQ(1, cache.disk_cache()->create_count());
9145
9146 for (auto i = context_list.begin(); i != context_list.end(); ++i) {
9147 Context* c = *i;
9148 delete c;
9149 }
9150 }
9151
8199 } // namespace net 9152 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698