| OLD | NEW |
| 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 150 |
| 151 void RunTransactionTestBase(HttpCache* cache, | 151 void RunTransactionTestBase(HttpCache* cache, |
| 152 const MockTransaction& trans_info, | 152 const MockTransaction& trans_info, |
| 153 const MockHttpRequest& request, | 153 const MockHttpRequest& request, |
| 154 HttpResponseInfo* response_info, | 154 HttpResponseInfo* response_info, |
| 155 const NetLogWithSource& net_log, | 155 const NetLogWithSource& net_log, |
| 156 LoadTimingInfo* load_timing_info, | 156 LoadTimingInfo* load_timing_info, |
| 157 int64_t* sent_bytes, | 157 int64_t* sent_bytes, |
| 158 int64_t* received_bytes, | 158 int64_t* received_bytes, |
| 159 IPEndPoint* remote_endpoint) { | 159 IPEndPoint* remote_endpoint) { |
| 160 TestCompletionCallback callback; | 160 // write to the cache |
| 161 std::unique_ptr<Context> c; |
| 162 c.reset(new Context()); |
| 163 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 164 EXPECT_THAT(rv, IsOk()); |
| 165 ASSERT_TRUE(c->trans.get()); |
| 161 | 166 |
| 162 // write to the cache | 167 rv = c->trans->Start(&request, c->callback.callback(), net_log); |
| 163 | |
| 164 std::unique_ptr<HttpTransaction> trans; | |
| 165 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); | |
| 166 EXPECT_THAT(rv, IsOk()); | |
| 167 ASSERT_TRUE(trans.get()); | |
| 168 | |
| 169 rv = trans->Start(&request, callback.callback(), net_log); | |
| 170 if (rv == ERR_IO_PENDING) | 168 if (rv == ERR_IO_PENDING) |
| 171 rv = callback.WaitForResult(); | 169 rv = c->callback.WaitForResult(); |
| 172 ASSERT_EQ(trans_info.return_code, rv); | 170 ASSERT_EQ(trans_info.return_code, rv); |
| 173 | 171 |
| 174 if (OK != rv) | 172 if (OK != rv) |
| 175 return; | 173 return; |
| 176 | 174 |
| 177 const HttpResponseInfo* response = trans->GetResponseInfo(); | 175 const HttpResponseInfo* response = c->trans->GetResponseInfo(); |
| 178 ASSERT_TRUE(response); | 176 ASSERT_TRUE(response); |
| 179 | 177 |
| 180 if (response_info) | 178 if (response_info) |
| 181 *response_info = *response; | 179 *response_info = *response; |
| 182 | 180 |
| 183 if (load_timing_info) { | 181 if (load_timing_info) { |
| 184 // If a fake network connection is used, need a NetLog to get a fake socket | 182 // If a fake network connection is used, need a NetLog to get a fake socket |
| 185 // ID. | 183 // ID. |
| 186 EXPECT_TRUE(net_log.net_log()); | 184 EXPECT_TRUE(net_log.net_log()); |
| 187 *load_timing_info = LoadTimingInfo(); | 185 *load_timing_info = LoadTimingInfo(); |
| 188 trans->GetLoadTimingInfo(load_timing_info); | 186 c->trans->GetLoadTimingInfo(load_timing_info); |
| 189 } | 187 } |
| 190 | 188 |
| 191 if (remote_endpoint) | 189 if (remote_endpoint) |
| 192 ASSERT_TRUE(trans->GetRemoteEndpoint(remote_endpoint)); | 190 ASSERT_TRUE(c->trans->GetRemoteEndpoint(remote_endpoint)); |
| 193 | 191 |
| 194 ReadAndVerifyTransaction(trans.get(), trans_info); | 192 ReadAndVerifyTransaction(c->trans.get(), trans_info); |
| 195 | 193 |
| 196 if (sent_bytes) | 194 if (sent_bytes) |
| 197 *sent_bytes = trans->GetTotalSentBytes(); | 195 *sent_bytes = c->trans->GetTotalSentBytes(); |
| 198 if (received_bytes) | 196 if (received_bytes) |
| 199 *received_bytes = trans->GetTotalReceivedBytes(); | 197 *received_bytes = c->trans->GetTotalReceivedBytes(); |
| 200 } | 198 } |
| 201 | 199 |
| 202 void RunTransactionTestWithRequest(HttpCache* cache, | 200 void RunTransactionTestWithRequest(HttpCache* cache, |
| 203 const MockTransaction& trans_info, | 201 const MockTransaction& trans_info, |
| 204 const MockHttpRequest& request, | 202 const MockHttpRequest& request, |
| 205 HttpResponseInfo* response_info) { | 203 HttpResponseInfo* response_info) { |
| 206 RunTransactionTestBase(cache, trans_info, request, response_info, | 204 RunTransactionTestBase(cache, trans_info, request, response_info, |
| 207 NetLogWithSource(), nullptr, nullptr, nullptr, | 205 NetLogWithSource(), nullptr, nullptr, nullptr, |
| 208 nullptr); | 206 nullptr); |
| 209 } | 207 } |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 | 563 |
| 566 std::string status_and_headers() const { | 564 std::string status_and_headers() const { |
| 567 return std::string(status) + "\n" + std::string(headers); | 565 return std::string(status) + "\n" + std::string(headers); |
| 568 } | 566 } |
| 569 | 567 |
| 570 const char* status; | 568 const char* status; |
| 571 const char* headers; | 569 const char* headers; |
| 572 const char* body; | 570 const char* body; |
| 573 }; | 571 }; |
| 574 | 572 |
| 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 | 573 class FakeWebSocketHandshakeStreamCreateHelper |
| 584 : public WebSocketHandshakeStreamBase::CreateHelper { | 574 : public WebSocketHandshakeStreamBase::CreateHelper { |
| 585 public: | 575 public: |
| 586 ~FakeWebSocketHandshakeStreamCreateHelper() override {} | 576 ~FakeWebSocketHandshakeStreamCreateHelper() override {} |
| 587 WebSocketHandshakeStreamBase* CreateBasicStream( | 577 WebSocketHandshakeStreamBase* CreateBasicStream( |
| 588 std::unique_ptr<ClientSocketHandle> connect, | 578 std::unique_ptr<ClientSocketHandle> connect, |
| 589 bool using_proxy) override { | 579 bool using_proxy) override { |
| 590 return NULL; | 580 return NULL; |
| 591 } | 581 } |
| 592 WebSocketHandshakeStreamBase* CreateSpdyStream( | 582 WebSocketHandshakeStreamBase* CreateSpdyStream( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 | 624 |
| 635 } // namespace | 625 } // namespace |
| 636 | 626 |
| 637 | 627 |
| 638 //----------------------------------------------------------------------------- | 628 //----------------------------------------------------------------------------- |
| 639 // Tests. | 629 // Tests. |
| 640 | 630 |
| 641 TEST(HttpCache, CreateThenDestroy) { | 631 TEST(HttpCache, CreateThenDestroy) { |
| 642 MockHttpCache cache; | 632 MockHttpCache cache; |
| 643 | 633 |
| 644 std::unique_ptr<HttpTransaction> trans; | 634 Context* c = new Context(); |
| 645 EXPECT_THAT(cache.CreateTransaction(&trans), IsOk()); | 635 EXPECT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 646 ASSERT_TRUE(trans.get()); | 636 ASSERT_TRUE(c->trans.get()); |
| 637 delete c; |
| 647 } | 638 } |
| 648 | 639 |
| 649 TEST(HttpCache, GetBackend) { | 640 TEST(HttpCache, GetBackend) { |
| 650 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0)); | 641 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0)); |
| 651 | 642 |
| 652 disk_cache::Backend* backend; | 643 disk_cache::Backend* backend; |
| 653 TestCompletionCallback cb; | 644 TestCompletionCallback cb; |
| 654 // This will lazily initialize the backend. | 645 // This will lazily initialize the backend. |
| 655 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); | 646 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); |
| 656 EXPECT_THAT(cb.GetResult(rv), IsOk()); | 647 EXPECT_THAT(cb.GetResult(rv), IsOk()); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 } | 716 } |
| 726 | 717 |
| 727 // Tests that IOBuffers are not referenced after IO completes. | 718 // Tests that IOBuffers are not referenced after IO completes. |
| 728 TEST(HttpCache, ReleaseBuffer) { | 719 TEST(HttpCache, ReleaseBuffer) { |
| 729 MockHttpCache cache; | 720 MockHttpCache cache; |
| 730 | 721 |
| 731 // Write to the cache. | 722 // Write to the cache. |
| 732 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 723 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 733 | 724 |
| 734 MockHttpRequest request(kSimpleGET_Transaction); | 725 MockHttpRequest request(kSimpleGET_Transaction); |
| 735 std::unique_ptr<HttpTransaction> trans; | 726 Context* c = new Context(); |
| 736 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 727 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 737 | 728 |
| 738 const int kBufferSize = 10; | 729 const int kBufferSize = 10; |
| 739 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); | 730 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); |
| 740 ReleaseBufferCompletionCallback cb(buffer.get()); | 731 ReleaseBufferCompletionCallback cb(buffer.get()); |
| 741 | 732 |
| 742 int rv = trans->Start(&request, cb.callback(), NetLogWithSource()); | 733 int rv = c->trans->Start(&request, cb.callback(), NetLogWithSource()); |
| 743 EXPECT_THAT(cb.GetResult(rv), IsOk()); | 734 EXPECT_THAT(cb.GetResult(rv), IsOk()); |
| 744 | 735 |
| 745 rv = trans->Read(buffer.get(), kBufferSize, cb.callback()); | 736 rv = c->trans->Read(buffer.get(), kBufferSize, cb.callback()); |
| 746 EXPECT_EQ(kBufferSize, cb.GetResult(rv)); | 737 EXPECT_EQ(kBufferSize, cb.GetResult(rv)); |
| 738 |
| 739 delete c; |
| 747 } | 740 } |
| 748 | 741 |
| 749 TEST(HttpCache, SimpleGETWithDiskFailures) { | 742 TEST(HttpCache, SimpleGETWithDiskFailures) { |
| 750 MockHttpCache cache; | 743 MockHttpCache cache; |
| 751 | 744 |
| 752 cache.disk_cache()->set_soft_failures(true); | 745 cache.disk_cache()->set_soft_failures(true); |
| 753 | 746 |
| 754 // Read from the network, and fail to write to the cache. | 747 // Read from the network, and fail to write to the cache. |
| 755 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 748 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 756 | 749 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 } | 906 } |
| 914 | 907 |
| 915 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { | 908 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 916 MockHttpCache cache; | 909 MockHttpCache cache; |
| 917 | 910 |
| 918 // force this transaction to read from the cache | 911 // force this transaction to read from the cache |
| 919 MockTransaction transaction(kSimpleGET_Transaction); | 912 MockTransaction transaction(kSimpleGET_Transaction); |
| 920 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 913 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 921 | 914 |
| 922 MockHttpRequest request(transaction); | 915 MockHttpRequest request(transaction); |
| 923 TestCompletionCallback callback; | |
| 924 | 916 |
| 925 std::unique_ptr<HttpTransaction> trans; | 917 std::unique_ptr<Context> c(new Context()); |
| 926 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 918 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 927 | 919 |
| 928 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 920 int rv = |
| 921 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 929 if (rv == ERR_IO_PENDING) | 922 if (rv == ERR_IO_PENDING) |
| 930 rv = callback.WaitForResult(); | 923 rv = c->callback.WaitForResult(); |
| 931 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); | 924 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 932 | 925 |
| 933 trans.reset(); | |
| 934 | |
| 935 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 926 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 936 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 927 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 937 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 928 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 938 } | 929 } |
| 939 | 930 |
| 940 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { | 931 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { |
| 941 MockHttpCache cache; | 932 MockHttpCache cache; |
| 942 | 933 |
| 943 // write to the cache | 934 // write to the cache |
| 944 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 935 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1031 RunTransactionTest(cache.http_cache(), transaction); | 1022 RunTransactionTest(cache.http_cache(), transaction); |
| 1032 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1023 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1033 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1024 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1034 RemoveMockTransaction(&transaction); | 1025 RemoveMockTransaction(&transaction); |
| 1035 | 1026 |
| 1036 // Network failure with error; should fail but have was_cached set. | 1027 // Network failure with error; should fail but have was_cached set. |
| 1037 transaction.return_code = ERR_FAILED; | 1028 transaction.return_code = ERR_FAILED; |
| 1038 AddMockTransaction(&transaction); | 1029 AddMockTransaction(&transaction); |
| 1039 | 1030 |
| 1040 MockHttpRequest request(transaction); | 1031 MockHttpRequest request(transaction); |
| 1041 TestCompletionCallback callback; | 1032 Context* c = new Context(); |
| 1042 std::unique_ptr<HttpTransaction> trans; | 1033 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()); | 1034 EXPECT_THAT(rv, IsOk()); |
| 1045 ASSERT_TRUE(trans.get()); | 1035 ASSERT_TRUE(c->trans.get()); |
| 1046 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 1036 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1047 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED)); | 1037 EXPECT_THAT(c->callback.GetResult(rv), IsError(ERR_FAILED)); |
| 1048 | 1038 |
| 1049 const HttpResponseInfo* response_info = trans->GetResponseInfo(); | 1039 const HttpResponseInfo* response_info = c->trans->GetResponseInfo(); |
| 1050 ASSERT_TRUE(response_info); | 1040 ASSERT_TRUE(response_info); |
| 1051 EXPECT_TRUE(response_info->was_cached); | 1041 EXPECT_TRUE(response_info->was_cached); |
| 1052 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1042 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1053 | 1043 |
| 1054 RemoveMockTransaction(&transaction); | 1044 RemoveMockTransaction(&transaction); |
| 1045 |
| 1046 delete c; |
| 1055 } | 1047 } |
| 1056 | 1048 |
| 1057 // Confirm if we have an empty cache, a read is marked as network verified. | 1049 // Confirm if we have an empty cache, a read is marked as network verified. |
| 1058 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { | 1050 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { |
| 1059 MockHttpCache cache; | 1051 MockHttpCache cache; |
| 1060 | 1052 |
| 1061 // write to the cache | 1053 // write to the cache |
| 1062 HttpResponseInfo response_info; | 1054 HttpResponseInfo response_info; |
| 1063 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 1055 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 1064 &response_info); | 1056 &response_info); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1335 | 1327 |
| 1336 // All requests are waiting for the active entry. | 1328 // All requests are waiting for the active entry. |
| 1337 for (int i = 0; i < kNumTransactions; ++i) { | 1329 for (int i = 0; i < kNumTransactions; ++i) { |
| 1338 Context* c = context_list[i]; | 1330 Context* c = context_list[i]; |
| 1339 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); | 1331 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
| 1340 } | 1332 } |
| 1341 | 1333 |
| 1342 // Allow all requests to move from the Create queue to the active entry. | 1334 // Allow all requests to move from the Create queue to the active entry. |
| 1343 base::RunLoop().RunUntilIdle(); | 1335 base::RunLoop().RunUntilIdle(); |
| 1344 | 1336 |
| 1345 // The first request should be a writer at this point, and the subsequent | 1337 // At the end of Start: [0] should be in shared_writers->all_writers_, |
| 1346 // requests should be pending. | 1338 // [3] and [4] should also be in all_writers_ since its a case of validation |
| 1339 // match/skip, [1] and [2] should be in pending_queue. |
| 1347 | 1340 |
| 1348 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1341 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1349 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1342 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1350 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1343 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1351 | 1344 |
| 1352 // All requests depend on the writer, and the writer is between Start and | 1345 // All requests depend on the writer, and the writer is between Start and |
| 1353 // Read, i.e. idle. | 1346 // Read, i.e. idle. |
| 1354 for (int i = 0; i < kNumTransactions; ++i) { | 1347 for (int i = 0; i < kNumTransactions; ++i) { |
| 1355 Context* c = context_list[i]; | 1348 Context* c = context_list[i]; |
| 1356 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | 1349 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1412 | 1405 |
| 1413 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1406 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1414 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1407 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1415 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1408 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1416 | 1409 |
| 1417 Context* c = context_list[0]; | 1410 Context* c = context_list[0]; |
| 1418 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1411 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
| 1419 c->result = c->callback.WaitForResult(); | 1412 c->result = c->callback.WaitForResult(); |
| 1420 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1413 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1421 | 1414 |
| 1422 // Now we have 2 active readers and two queued transactions. | 1415 // after [0] completes reading and writing, [3] and [4] should be added to |
| 1423 | 1416 // entry->readers from shared writers and pending queue should be |
| 1417 // processed to add [1] and [2] to readers as well. They are all waiting for |
| 1418 // their consumer to invoke Read. |
| 1424 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); | 1419 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); |
| 1425 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, | 1420 EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState()); |
| 1426 context_list[3]->trans->GetLoadState()); | |
| 1427 | 1421 |
| 1428 c = context_list[1]; | 1422 c = context_list[1]; |
| 1429 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1423 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
| 1430 c->result = c->callback.WaitForResult(); | 1424 c->result = c->callback.WaitForResult(); |
| 1431 if (c->result == OK) | 1425 if (c->result == OK) |
| 1432 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1426 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1433 | 1427 |
| 1434 // At this point we have one reader, two pending transactions and a task on | 1428 // 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 | 1429 // 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 | 1430 // is the current reader, and expect the queued task to be able to start the |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1530 c->result = | 1524 c->result = |
| 1531 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1525 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1532 } | 1526 } |
| 1533 | 1527 |
| 1534 // Allow all requests to move from the Create queue to the active entry. | 1528 // Allow all requests to move from the Create queue to the active entry. |
| 1535 base::RunLoop().RunUntilIdle(); | 1529 base::RunLoop().RunUntilIdle(); |
| 1536 | 1530 |
| 1537 // The first request should be a writer at this point, and the subsequent | 1531 // The first request should be a writer at this point, and the subsequent |
| 1538 // requests should be pending. | 1532 // requests should be pending. |
| 1539 | 1533 |
| 1540 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1534 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1541 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1535 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1542 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1536 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1543 | 1537 |
| 1544 // Now, make sure that the second request asks for the entry not to be stored. | 1538 // Now, make sure that the second request asks for the entry not to be stored. |
| 1545 request_handler.set_no_store(true); | 1539 request_handler.set_no_store(true); |
| 1546 | 1540 |
| 1547 for (int i = 0; i < kNumTransactions; ++i) { | 1541 for (int i = 0; i < kNumTransactions; ++i) { |
| 1548 Context* c = context_list[i]; | 1542 Context* c = context_list[i]; |
| 1549 if (c->result == ERR_IO_PENDING) | 1543 if (c->result == ERR_IO_PENDING) |
| 1550 c->result = c->callback.WaitForResult(); | 1544 c->result = c->callback.WaitForResult(); |
| 1551 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 1545 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
| 1552 delete c; | 1546 delete c; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1575 ASSERT_THAT(c->result, IsOk()); | 1569 ASSERT_THAT(c->result, IsOk()); |
| 1576 | 1570 |
| 1577 c->result = | 1571 c->result = |
| 1578 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1572 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1579 } | 1573 } |
| 1580 | 1574 |
| 1581 // Allow all requests to move from the Create queue to the active entry. | 1575 // Allow all requests to move from the Create queue to the active entry. |
| 1582 base::RunLoop().RunUntilIdle(); | 1576 base::RunLoop().RunUntilIdle(); |
| 1583 | 1577 |
| 1584 // The first request should be a writer at this point, and the subsequent | 1578 // The first request should be a writer at this point, and the subsequent |
| 1585 // requests should be pending. | 1579 // requests should have been added to SharedWriters, skipped validation and |
| 1580 // completed the start state machine of HttpCache::Transaction. |
| 1586 | 1581 |
| 1587 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1582 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1588 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1583 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1589 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1584 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1590 | 1585 |
| 1591 for (int i = 0; i < kNumTransactions; ++i) { | 1586 for (int i = 0; i < kNumTransactions; ++i) { |
| 1592 Context* c = context_list[i]; | 1587 Context* c = context_list[i]; |
| 1593 if (c->result == ERR_IO_PENDING) | 1588 if (c->result == ERR_IO_PENDING) |
| 1594 c->result = c->callback.WaitForResult(); | 1589 c->result = c->callback.WaitForResult(); |
| 1595 // Destroy only the first transaction. | 1590 // Destroy only the first transaction. |
| 1596 if (i == 0) { | 1591 if (i == 0) { |
| 1597 delete c; | 1592 delete c; |
| 1598 context_list[i] = NULL; | 1593 context_list[i] = NULL; |
| 1599 } | 1594 } |
| 1600 } | 1595 } |
| 1601 | 1596 |
| 1602 // Complete the rest of the transactions. | 1597 // Complete the rest of the transactions. |
| 1603 for (int i = 1; i < kNumTransactions; ++i) { | 1598 for (int i = 1; i < kNumTransactions; ++i) { |
| 1604 Context* c = context_list[i]; | 1599 Context* c = context_list[i]; |
| 1605 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1600 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1606 } | 1601 } |
| 1607 | 1602 |
| 1608 // We should have had to re-open the disk entry. | 1603 // 2nd transaction will reuse the network transaction as they are both part of |
| 1609 | 1604 // SharedWriters. Similarly it will be part of the same entry. |
| 1610 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1605 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1611 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1606 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1612 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1607 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1613 | 1608 |
| 1614 for (int i = 1; i < kNumTransactions; ++i) { | 1609 for (int i = 1; i < kNumTransactions; ++i) { |
| 1615 Context* c = context_list[i]; | 1610 Context* c = context_list[i]; |
| 1616 delete c; | 1611 delete c; |
| 1617 } | 1612 } |
| 1618 } | 1613 } |
| 1619 | 1614 |
| 1620 // Tests that we can cancel requests that are queued waiting to open the disk | 1615 // Tests that we can cancel requests that are queued waiting to open the disk |
| 1621 // cache entry. | 1616 // cache entry. |
| 1622 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { | 1617 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { |
| (...skipping 1330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2953 | 2948 |
| 2954 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { | 2949 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { |
| 2955 MockHttpCache cache; | 2950 MockHttpCache cache; |
| 2956 | 2951 |
| 2957 MockTransaction transaction(kSimplePOST_Transaction); | 2952 MockTransaction transaction(kSimplePOST_Transaction); |
| 2958 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 2953 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 2959 | 2954 |
| 2960 MockHttpRequest request(transaction); | 2955 MockHttpRequest request(transaction); |
| 2961 TestCompletionCallback callback; | 2956 TestCompletionCallback callback; |
| 2962 | 2957 |
| 2963 std::unique_ptr<HttpTransaction> trans; | 2958 std::unique_ptr<Context> c(new Context()); |
| 2964 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 2959 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 2965 ASSERT_TRUE(trans.get()); | 2960 ASSERT_TRUE(c->trans.get()); |
| 2966 | 2961 |
| 2967 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 2962 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 2968 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); | 2963 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); |
| 2969 | 2964 |
| 2970 trans.reset(); | |
| 2971 | |
| 2972 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 2965 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 2973 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2966 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2974 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 2967 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2975 } | 2968 } |
| 2976 | 2969 |
| 2977 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { | 2970 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { |
| 2978 MockHttpCache cache; | 2971 MockHttpCache cache; |
| 2979 | 2972 |
| 2980 // Test that we hit the cache for POST requests. | 2973 // Test that we hit the cache for POST requests. |
| 2981 | 2974 |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3208 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { | 3201 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { |
| 3209 MockHttpCache cache; | 3202 MockHttpCache cache; |
| 3210 MockTransaction transaction(kSimplePOST_Transaction); | 3203 MockTransaction transaction(kSimplePOST_Transaction); |
| 3211 AddMockTransaction(&transaction); | 3204 AddMockTransaction(&transaction); |
| 3212 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 3205 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3213 transaction.method = "HEAD"; | 3206 transaction.method = "HEAD"; |
| 3214 | 3207 |
| 3215 MockHttpRequest request(transaction); | 3208 MockHttpRequest request(transaction); |
| 3216 TestCompletionCallback callback; | 3209 TestCompletionCallback callback; |
| 3217 | 3210 |
| 3218 std::unique_ptr<HttpTransaction> trans; | 3211 std::unique_ptr<Context> c(new Context()); |
| 3219 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 3212 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 3220 ASSERT_TRUE(trans.get()); | 3213 ASSERT_TRUE(c->trans.get()); |
| 3221 | 3214 |
| 3222 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 3215 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 3223 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); | 3216 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); |
| 3224 | 3217 |
| 3225 trans.reset(); | |
| 3226 | |
| 3227 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 3218 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 3228 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3219 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3229 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 3220 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3230 RemoveMockTransaction(&transaction); | 3221 RemoveMockTransaction(&transaction); |
| 3231 } | 3222 } |
| 3232 | 3223 |
| 3233 // Tests that a HEAD request is served from a cached GET. | 3224 // Tests that a HEAD request is served from a cached GET. |
| 3234 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) { | 3225 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) { |
| 3235 MockHttpCache cache; | 3226 MockHttpCache cache; |
| 3236 MockTransaction transaction(kSimpleGET_Transaction); | 3227 MockTransaction transaction(kSimpleGET_Transaction); |
| (...skipping 2366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5603 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5594 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5604 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5595 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5605 | 5596 |
| 5606 // Force this transaction to read from the cache. | 5597 // Force this transaction to read from the cache. |
| 5607 MockTransaction transaction(kRangeGET_TransactionOK); | 5598 MockTransaction transaction(kRangeGET_TransactionOK); |
| 5608 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 5599 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 5609 | 5600 |
| 5610 MockHttpRequest request(transaction); | 5601 MockHttpRequest request(transaction); |
| 5611 TestCompletionCallback callback; | 5602 TestCompletionCallback callback; |
| 5612 | 5603 |
| 5613 std::unique_ptr<HttpTransaction> trans; | 5604 std::unique_ptr<Context> c(new Context()); |
| 5614 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5605 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 5615 EXPECT_THAT(rv, IsOk()); | 5606 EXPECT_THAT(rv, IsOk()); |
| 5616 ASSERT_TRUE(trans.get()); | 5607 ASSERT_TRUE(c->trans.get()); |
| 5617 | 5608 |
| 5618 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 5609 rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 5619 if (rv == ERR_IO_PENDING) | 5610 if (rv == ERR_IO_PENDING) |
| 5620 rv = callback.WaitForResult(); | 5611 rv = callback.WaitForResult(); |
| 5621 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); | 5612 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 5622 | 5613 |
| 5623 trans.reset(); | 5614 c.reset(); |
| 5624 | 5615 |
| 5625 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5616 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5626 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5617 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5627 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5618 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5628 | 5619 |
| 5629 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5620 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5630 } | 5621 } |
| 5631 #endif | 5622 #endif |
| 5632 | 5623 |
| 5633 // Tests the handling of the "truncation" flag. | 5624 // Tests the handling of the "truncation" flag. |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5834 c->trans.reset(); | 5825 c->trans.reset(); |
| 5835 MockHttpCache::SetTestMode(0); | 5826 MockHttpCache::SetTestMode(0); |
| 5836 | 5827 |
| 5837 | 5828 |
| 5838 // Make sure that we don't invoke the callback. We may have an issue if the | 5829 // 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 | 5830 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 5840 // could end up with the transaction being deleted twice if we send any | 5831 // could end up with the transaction being deleted twice if we send any |
| 5841 // notification from the transaction destructor (see http://crbug.com/31723). | 5832 // notification from the transaction destructor (see http://crbug.com/31723). |
| 5842 EXPECT_FALSE(c->callback.have_result()); | 5833 EXPECT_FALSE(c->callback.have_result()); |
| 5843 | 5834 |
| 5844 // Verify that the entry is marked as incomplete. | |
| 5845 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); | 5835 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 5846 } | 5836 } |
| 5847 | 5837 |
| 5838 // Tests that we mark an entry as incomplete when the request is cancelled, |
| 5839 // but not if there are multiple transactions reading. |
| 5840 TEST(HttpCache, SetTruncatedFlagShared) { |
| 5841 MockHttpCache cache; |
| 5842 |
| 5843 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5844 transaction.response_headers = |
| 5845 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5846 "Content-Length: 22\n" |
| 5847 "Etag: \"foopy\"\n"; |
| 5848 MockHttpRequest request(transaction); |
| 5849 |
| 5850 std::vector<std::unique_ptr<Context>> contexts(2); |
| 5851 |
| 5852 for (auto& c : contexts) { |
| 5853 c.reset(new Context()); |
| 5854 } |
| 5855 |
| 5856 for (auto& c : contexts) { |
| 5857 int rv = cache.CreateTransaction(&c->trans); |
| 5858 ASSERT_THAT(rv, IsOk()); |
| 5859 } |
| 5860 |
| 5861 for (auto& c : contexts) { |
| 5862 int rv = |
| 5863 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5864 if (rv == ERR_IO_PENDING) |
| 5865 rv = c->callback.WaitForResult(); |
| 5866 } |
| 5867 |
| 5868 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5869 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5870 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5871 |
| 5872 // Make sure that the entry has some data stored. |
| 5873 for (auto& c : contexts) { |
| 5874 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
| 5875 int rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5876 if (rv == ERR_IO_PENDING) |
| 5877 rv = c->callback.WaitForResult(); |
| 5878 EXPECT_EQ(buf->size(), rv); |
| 5879 } |
| 5880 |
| 5881 // We want to cancel the request when the transaction is busy. |
| 5882 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
| 5883 Context* c = contexts[0].get(); |
| 5884 int rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5885 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 5886 EXPECT_FALSE(c->callback.have_result()); |
| 5887 |
| 5888 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); |
| 5889 |
| 5890 // Destroy the transaction. |
| 5891 c->trans.reset(); |
| 5892 MockHttpCache::SetTestMode(0); |
| 5893 |
| 5894 // Make sure that we don't invoke the callback. We may have an issue if the |
| 5895 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 5896 // could end up with the transaction being deleted twice if we send any |
| 5897 // notification from the transaction destructor (see http://crbug.com/31723). |
| 5898 EXPECT_FALSE(c->callback.have_result()); |
| 5899 |
| 5900 // Verify that the entry is not marked as incomplete since SharedWriters |
| 5901 // enables response writing to complete even if transaction is destroyed |
| 5902 // mid-way. |
| 5903 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); |
| 5904 } |
| 5905 |
| 5848 // Tests that we don't mark an entry as truncated when we read everything. | 5906 // Tests that we don't mark an entry as truncated when we read everything. |
| 5849 TEST(HttpCache, DontSetTruncatedFlag) { | 5907 TEST(HttpCache, DontSetTruncatedFlag) { |
| 5850 MockHttpCache cache; | 5908 MockHttpCache cache; |
| 5851 | 5909 |
| 5852 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 5910 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5853 transaction.response_headers = | 5911 transaction.response_headers = |
| 5854 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5912 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5855 "Content-Length: 22\n" | 5913 "Content-Length: 22\n" |
| 5856 "Etag: \"foopy\"\n"; | 5914 "Etag: \"foopy\"\n"; |
| 5857 MockHttpRequest request(transaction); | 5915 MockHttpRequest request(transaction); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 5876 } | 5934 } |
| 5877 | 5935 |
| 5878 // Tests that sparse entries don't set the truncate flag. | 5936 // Tests that sparse entries don't set the truncate flag. |
| 5879 TEST(HttpCache, RangeGET_DontTruncate) { | 5937 TEST(HttpCache, RangeGET_DontTruncate) { |
| 5880 MockHttpCache cache; | 5938 MockHttpCache cache; |
| 5881 | 5939 |
| 5882 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5940 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5883 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; | 5941 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; |
| 5884 | 5942 |
| 5885 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | 5943 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5886 std::unique_ptr<HttpTransaction> trans; | 5944 std::unique_ptr<Context> c(new Context()); |
| 5887 | 5945 |
| 5888 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5946 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 5889 EXPECT_THAT(rv, IsOk()); | 5947 EXPECT_THAT(rv, IsOk()); |
| 5890 | 5948 |
| 5891 TestCompletionCallback cb; | 5949 TestCompletionCallback cb; |
| 5892 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); | 5950 rv = c->trans->Start(request.get(), cb.callback(), NetLogWithSource()); |
| 5893 EXPECT_EQ(0, cb.GetResult(rv)); | 5951 EXPECT_EQ(0, cb.GetResult(rv)); |
| 5894 | 5952 |
| 5895 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | 5953 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5896 rv = trans->Read(buf.get(), 10, cb.callback()); | 5954 rv = c->trans->Read(buf.get(), 10, cb.callback()); |
| 5897 EXPECT_EQ(10, cb.GetResult(rv)); | 5955 EXPECT_EQ(10, cb.GetResult(rv)); |
| 5898 | 5956 |
| 5899 // Should not trigger any DCHECK. | |
| 5900 trans.reset(); | |
| 5901 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); | 5957 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5902 } | 5958 } |
| 5903 | 5959 |
| 5904 // Tests that sparse entries don't set the truncate flag (when the byte range | 5960 // Tests that sparse entries don't set the truncate flag (when the byte range |
| 5905 // starts after 0). | 5961 // starts after 0). |
| 5906 TEST(HttpCache, RangeGET_DontTruncate2) { | 5962 TEST(HttpCache, RangeGET_DontTruncate2) { |
| 5907 MockHttpCache cache; | 5963 MockHttpCache cache; |
| 5908 | 5964 |
| 5909 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5965 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5910 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; | 5966 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; |
| 5911 | 5967 |
| 5912 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | 5968 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5913 std::unique_ptr<HttpTransaction> trans; | 5969 std::unique_ptr<Context> c(new Context()); |
| 5914 | 5970 |
| 5915 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5971 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 5916 EXPECT_THAT(rv, IsOk()); | 5972 EXPECT_THAT(rv, IsOk()); |
| 5917 | 5973 |
| 5918 TestCompletionCallback cb; | 5974 rv = c->trans->Start(request.get(), c->callback.callback(), |
| 5919 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); | 5975 NetLogWithSource()); |
| 5920 EXPECT_EQ(0, cb.GetResult(rv)); | 5976 EXPECT_EQ(0, c->callback.GetResult(rv)); |
| 5921 | 5977 |
| 5922 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | 5978 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5923 rv = trans->Read(buf.get(), 10, cb.callback()); | 5979 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
| 5924 EXPECT_EQ(10, cb.GetResult(rv)); | 5980 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 5925 | 5981 |
| 5926 // Should not trigger any DCHECK. | 5982 // Should not trigger any DCHECK. |
| 5927 trans.reset(); | |
| 5928 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); | 5983 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5929 } | 5984 } |
| 5930 | 5985 |
| 5931 // Tests that we can continue with a request that was interrupted. | 5986 // Tests that we can continue with a request that was interrupted. |
| 5932 TEST(HttpCache, GET_IncompleteResource) { | 5987 TEST(HttpCache, GET_IncompleteResource) { |
| 5933 MockHttpCache cache; | 5988 MockHttpCache cache; |
| 5934 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5989 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5935 | 5990 |
| 5936 std::string raw_headers("HTTP/1.1 200 OK\n" | 5991 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5937 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5992 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6381 | 6436 |
| 6382 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); | 6437 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); |
| 6383 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; | 6438 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 6384 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; | 6439 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; |
| 6385 | 6440 |
| 6386 MockHttpRequest request(kTestTransaction); | 6441 MockHttpRequest request(kTestTransaction); |
| 6387 TestCompletionCallback callback; | 6442 TestCompletionCallback callback; |
| 6388 | 6443 |
| 6389 // Write to the cache. | 6444 // Write to the cache. |
| 6390 { | 6445 { |
| 6391 std::unique_ptr<HttpTransaction> trans; | 6446 std::unique_ptr<Context> c(new Context()); |
| 6392 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6447 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6393 | 6448 |
| 6394 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6449 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6395 if (rv == ERR_IO_PENDING) | 6450 if (rv == ERR_IO_PENDING) |
| 6396 rv = callback.WaitForResult(); | 6451 rv = callback.WaitForResult(); |
| 6397 ASSERT_THAT(rv, IsOk()); | 6452 ASSERT_THAT(rv, IsOk()); |
| 6398 | 6453 |
| 6399 const HttpResponseInfo* info = trans->GetResponseInfo(); | 6454 const HttpResponseInfo* info = c->trans->GetResponseInfo(); |
| 6400 ASSERT_TRUE(info); | 6455 ASSERT_TRUE(info); |
| 6401 | 6456 |
| 6402 EXPECT_EQ(info->headers->response_code(), 301); | 6457 EXPECT_EQ(info->headers->response_code(), 301); |
| 6403 | 6458 |
| 6404 std::string location; | 6459 std::string location; |
| 6405 info->headers->EnumerateHeader(NULL, "Location", &location); | 6460 info->headers->EnumerateHeader(NULL, "Location", &location); |
| 6406 EXPECT_EQ(location, "http://www.bar.com/"); | 6461 EXPECT_EQ(location, "http://www.bar.com/"); |
| 6407 | 6462 |
| 6408 // Mark the transaction as completed so it is cached. | 6463 // Mark the transaction as completed so it is cached. |
| 6409 trans->DoneReading(); | 6464 c->trans->DoneReading(); |
| 6410 | 6465 |
| 6411 // Destroy transaction when going out of scope. We have not actually | 6466 // 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. | 6467 // read the response body -- want to test that it is still getting cached. |
| 6413 } | 6468 } |
| 6414 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6469 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6415 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 6470 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6416 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6471 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6417 | 6472 |
| 6418 // Active entries in the cache are not retired synchronously. Make | 6473 // Active entries in the cache are not retired synchronously. Make |
| 6419 // sure the next run hits the MockHttpCache and open_count is | 6474 // sure the next run hits the MockHttpCache and open_count is |
| 6420 // correct. | 6475 // correct. |
| 6421 base::RunLoop().RunUntilIdle(); | 6476 base::RunLoop().RunUntilIdle(); |
| 6422 | 6477 |
| 6423 // Read from the cache. | 6478 // Read from the cache. |
| 6424 { | 6479 { |
| 6425 std::unique_ptr<HttpTransaction> trans; | 6480 std::unique_ptr<Context> c(new Context()); |
| 6426 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6481 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6427 | 6482 |
| 6428 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6483 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6429 if (rv == ERR_IO_PENDING) | 6484 if (rv == ERR_IO_PENDING) |
| 6430 rv = callback.WaitForResult(); | 6485 rv = callback.WaitForResult(); |
| 6431 ASSERT_THAT(rv, IsOk()); | 6486 ASSERT_THAT(rv, IsOk()); |
| 6432 | 6487 |
| 6433 const HttpResponseInfo* info = trans->GetResponseInfo(); | 6488 const HttpResponseInfo* info = c->trans->GetResponseInfo(); |
| 6434 ASSERT_TRUE(info); | 6489 ASSERT_TRUE(info); |
| 6435 | 6490 |
| 6436 EXPECT_EQ(info->headers->response_code(), 301); | 6491 EXPECT_EQ(info->headers->response_code(), 301); |
| 6437 | 6492 |
| 6438 std::string location; | 6493 std::string location; |
| 6439 info->headers->EnumerateHeader(NULL, "Location", &location); | 6494 info->headers->EnumerateHeader(NULL, "Location", &location); |
| 6440 EXPECT_EQ(location, "http://www.bar.com/"); | 6495 EXPECT_EQ(location, "http://www.bar.com/"); |
| 6441 | 6496 |
| 6442 // Mark the transaction as completed so it is cached. | 6497 // Mark the transaction as completed so it is cached. |
| 6443 trans->DoneReading(); | 6498 c->trans->DoneReading(); |
| 6444 | 6499 |
| 6445 // Destroy transaction when going out of scope. We have not actually | 6500 // 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. | 6501 // read the response body -- want to test that it is still getting cached. |
| 6447 } | 6502 } |
| 6448 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6503 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6449 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6504 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6450 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6505 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6451 } | 6506 } |
| 6452 | 6507 |
| 6453 // Verify that no-cache resources are stored in cache, but are not fetched from | 6508 // 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 Loading... |
| 6594 transaction.cert_status = CERT_STATUS_REVOKED; | 6649 transaction.cert_status = CERT_STATUS_REVOKED; |
| 6595 ScopedMockTransaction scoped_transaction(transaction); | 6650 ScopedMockTransaction scoped_transaction(transaction); |
| 6596 | 6651 |
| 6597 // write to the cache | 6652 // write to the cache |
| 6598 RunTransactionTest(cache.http_cache(), transaction); | 6653 RunTransactionTest(cache.http_cache(), transaction); |
| 6599 | 6654 |
| 6600 // Test that it was not cached. | 6655 // Test that it was not cached. |
| 6601 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 6656 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 6602 | 6657 |
| 6603 MockHttpRequest request(transaction); | 6658 MockHttpRequest request(transaction); |
| 6604 TestCompletionCallback callback; | |
| 6605 | 6659 |
| 6606 std::unique_ptr<HttpTransaction> trans; | 6660 std::unique_ptr<Context> c(new Context()); |
| 6607 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6661 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6608 | 6662 |
| 6609 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6663 int rv = |
| 6664 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 6610 if (rv == ERR_IO_PENDING) | 6665 if (rv == ERR_IO_PENDING) |
| 6611 rv = callback.WaitForResult(); | 6666 rv = c->callback.WaitForResult(); |
| 6612 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); | 6667 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 6613 } | 6668 } |
| 6614 | 6669 |
| 6615 // Ensure that we don't crash by if left-behind transactions. | 6670 // Ensure that we don't crash by if left-behind transactions. |
| 6616 TEST(HttpCache, OutlivedTransactions) { | 6671 TEST(HttpCache, OutlivedTransactions) { |
| 6617 MockHttpCache* cache = new MockHttpCache; | 6672 MockHttpCache* cache = new MockHttpCache; |
| 6618 | 6673 |
| 6619 std::unique_ptr<HttpTransaction> trans; | 6674 std::unique_ptr<Context> c(new Context()); |
| 6620 EXPECT_THAT(cache->CreateTransaction(&trans), IsOk()); | 6675 EXPECT_THAT(cache->CreateTransaction(&c->trans), IsOk()); |
| 6621 | 6676 |
| 6622 delete cache; | 6677 delete cache; |
| 6623 trans.reset(); | |
| 6624 } | 6678 } |
| 6625 | 6679 |
| 6626 // Test that the disabled mode works. | 6680 // Test that the disabled mode works. |
| 6627 TEST(HttpCache, CacheDisabledMode) { | 6681 TEST(HttpCache, CacheDisabledMode) { |
| 6628 MockHttpCache cache; | 6682 MockHttpCache cache; |
| 6629 | 6683 |
| 6630 // write to the cache | 6684 // write to the cache |
| 6631 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 6685 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6632 | 6686 |
| 6633 // go into disabled mode | 6687 // go into disabled mode |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6956 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7010 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6957 } | 7011 } |
| 6958 | 7012 |
| 6959 // Tests that we don't mark entries as truncated when a filter detects the end | 7013 // Tests that we don't mark entries as truncated when a filter detects the end |
| 6960 // of the stream. | 7014 // of the stream. |
| 6961 TEST(HttpCache, FilterCompletion) { | 7015 TEST(HttpCache, FilterCompletion) { |
| 6962 MockHttpCache cache; | 7016 MockHttpCache cache; |
| 6963 TestCompletionCallback callback; | 7017 TestCompletionCallback callback; |
| 6964 | 7018 |
| 6965 { | 7019 { |
| 6966 std::unique_ptr<HttpTransaction> trans; | 7020 std::unique_ptr<Context> c(new Context()); |
| 6967 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7021 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6968 | 7022 |
| 6969 MockHttpRequest request(kSimpleGET_Transaction); | 7023 MockHttpRequest request(kSimpleGET_Transaction); |
| 6970 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7024 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6971 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7025 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6972 | 7026 |
| 6973 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7027 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 6974 rv = trans->Read(buf.get(), 256, callback.callback()); | 7028 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 6975 EXPECT_GT(callback.GetResult(rv), 0); | 7029 EXPECT_GT(callback.GetResult(rv), 0); |
| 6976 | 7030 |
| 6977 // Now make sure that the entry is preserved. | 7031 // Now make sure that the entry is preserved. |
| 6978 trans->DoneReading(); | 7032 c->trans->DoneReading(); |
| 6979 } | 7033 } |
| 6980 | 7034 |
| 6981 // Make sure that the ActiveEntry is gone. | 7035 // Make sure that the ActiveEntry is gone. |
| 6982 base::RunLoop().RunUntilIdle(); | 7036 base::RunLoop().RunUntilIdle(); |
| 6983 | 7037 |
| 6984 // Read from the cache. | 7038 // Read from the cache. |
| 6985 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7039 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6986 | 7040 |
| 6987 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 7041 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6988 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 7042 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6989 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7043 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6990 } | 7044 } |
| 6991 | 7045 |
| 6992 // Tests that we don't mark entries as truncated and release the cache | 7046 // Tests that we don't mark entries as truncated and release the cache |
| 6993 // entry when DoneReading() is called before any Read() calls, such as | 7047 // entry when DoneReading() is called before any Read() calls, such as |
| 6994 // for a redirect. | 7048 // for a redirect. |
| 6995 TEST(HttpCache, DoneReading) { | 7049 TEST(HttpCache, DoneReading) { |
| 6996 MockHttpCache cache; | 7050 MockHttpCache cache; |
| 6997 TestCompletionCallback callback; | 7051 TestCompletionCallback callback; |
| 6998 | 7052 |
| 6999 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 7053 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 7000 transaction.data = ""; | 7054 transaction.data = ""; |
| 7001 | 7055 |
| 7002 std::unique_ptr<HttpTransaction> trans; | 7056 std::unique_ptr<Context> c(new Context()); |
| 7003 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7057 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7004 | 7058 |
| 7005 MockHttpRequest request(transaction); | 7059 MockHttpRequest request(transaction); |
| 7006 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7060 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7007 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7061 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7008 | 7062 |
| 7009 trans->DoneReading(); | 7063 c->trans->DoneReading(); |
| 7010 // Leave the transaction around. | 7064 // Leave the transaction around. |
| 7011 | 7065 |
| 7012 // Make sure that the ActiveEntry is gone. | 7066 // Make sure that the ActiveEntry is gone. |
| 7013 base::RunLoop().RunUntilIdle(); | 7067 base::RunLoop().RunUntilIdle(); |
| 7014 | 7068 |
| 7015 // Read from the cache. This should not deadlock. | 7069 // Read from the cache. This should not deadlock. |
| 7016 RunTransactionTest(cache.http_cache(), transaction); | 7070 RunTransactionTest(cache.http_cache(), transaction); |
| 7017 | 7071 |
| 7018 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 7072 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7019 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 7073 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7020 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7074 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7021 } | 7075 } |
| 7022 | 7076 |
| 7023 // Tests that we stop caching when told. | 7077 // Tests that we stop caching when told. |
| 7024 TEST(HttpCache, StopCachingDeletesEntry) { | 7078 TEST(HttpCache, StopCachingDeletesEntry) { |
| 7025 MockHttpCache cache; | 7079 MockHttpCache cache; |
| 7026 TestCompletionCallback callback; | |
| 7027 MockHttpRequest request(kSimpleGET_Transaction); | 7080 MockHttpRequest request(kSimpleGET_Transaction); |
| 7028 | 7081 |
| 7029 { | 7082 { |
| 7030 std::unique_ptr<HttpTransaction> trans; | 7083 std::unique_ptr<Context> c(new Context()); |
| 7031 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7084 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7032 | 7085 |
| 7033 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7086 int rv = |
| 7034 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7087 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 7088 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 7035 | 7089 |
| 7036 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7090 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7037 rv = trans->Read(buf.get(), 10, callback.callback()); | 7091 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
| 7038 EXPECT_EQ(10, callback.GetResult(rv)); | 7092 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 7039 | 7093 |
| 7040 trans->StopCaching(); | 7094 c->trans->StopCaching(); |
| 7041 | 7095 |
| 7042 // We should be able to keep reading. | 7096 // We should be able to keep reading. |
| 7043 rv = trans->Read(buf.get(), 256, callback.callback()); | 7097 rv = c->trans->Read(buf.get(), 256, c->callback.callback()); |
| 7044 EXPECT_GT(callback.GetResult(rv), 0); | 7098 EXPECT_GT(c->callback.GetResult(rv), 0); |
| 7045 rv = trans->Read(buf.get(), 256, callback.callback()); | 7099 rv = c->trans->Read(buf.get(), 256, c->callback.callback()); |
| 7046 EXPECT_EQ(0, callback.GetResult(rv)); | 7100 EXPECT_EQ(0, c->callback.GetResult(rv)); |
| 7047 } | 7101 } |
| 7048 | 7102 |
| 7049 // Make sure that the ActiveEntry is gone. | 7103 // Make sure that the ActiveEntry is gone. |
| 7050 base::RunLoop().RunUntilIdle(); | 7104 base::RunLoop().RunUntilIdle(); |
| 7051 | 7105 |
| 7052 // Verify that the entry is gone. | 7106 // Verify that the entry is gone. |
| 7053 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7107 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 7054 | 7108 |
| 7055 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7109 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7056 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 7110 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7057 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 7111 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7058 } | 7112 } |
| 7059 | 7113 |
| 7060 // Tests that we stop caching when told, even if DoneReading is called | 7114 // Tests that we stop caching when told, even if DoneReading is called |
| 7061 // after StopCaching. | 7115 // after StopCaching. |
| 7062 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { | 7116 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { |
| 7063 MockHttpCache cache; | 7117 MockHttpCache cache; |
| 7064 TestCompletionCallback callback; | 7118 TestCompletionCallback callback; |
| 7065 MockHttpRequest request(kSimpleGET_Transaction); | 7119 MockHttpRequest request(kSimpleGET_Transaction); |
| 7066 | 7120 |
| 7067 { | 7121 { |
| 7068 std::unique_ptr<HttpTransaction> trans; | 7122 std::unique_ptr<Context> c(new Context()); |
| 7069 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7123 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7070 | 7124 |
| 7071 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7125 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7072 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7126 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7073 | 7127 |
| 7074 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7128 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7075 rv = trans->Read(buf.get(), 10, callback.callback()); | 7129 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7076 EXPECT_EQ(10, callback.GetResult(rv)); | 7130 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7077 | 7131 |
| 7078 trans->StopCaching(); | 7132 c->trans->StopCaching(); |
| 7079 | 7133 |
| 7080 // We should be able to keep reading. | 7134 // We should be able to keep reading. |
| 7081 rv = trans->Read(buf.get(), 256, callback.callback()); | 7135 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7082 EXPECT_GT(callback.GetResult(rv), 0); | 7136 EXPECT_GT(callback.GetResult(rv), 0); |
| 7083 rv = trans->Read(buf.get(), 256, callback.callback()); | 7137 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7084 EXPECT_EQ(0, callback.GetResult(rv)); | 7138 EXPECT_EQ(0, callback.GetResult(rv)); |
| 7085 | 7139 |
| 7086 // We should be able to call DoneReading. | 7140 // We should be able to call DoneReading. |
| 7087 trans->DoneReading(); | 7141 c->trans->DoneReading(); |
| 7088 } | 7142 } |
| 7089 | 7143 |
| 7090 // Make sure that the ActiveEntry is gone. | 7144 // Make sure that the ActiveEntry is gone. |
| 7091 base::RunLoop().RunUntilIdle(); | 7145 base::RunLoop().RunUntilIdle(); |
| 7092 | 7146 |
| 7093 // Verify that the entry is gone. | 7147 // Verify that the entry is gone. |
| 7094 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7148 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 7095 | 7149 |
| 7096 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7150 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7097 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 7151 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7098 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 7152 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7099 } | 7153 } |
| 7100 | 7154 |
| 7101 // Tests that we stop caching when told, when using auth. | 7155 // Tests that we stop caching when told, when using auth. |
| 7102 TEST(HttpCache, StopCachingWithAuthDeletesEntry) { | 7156 TEST(HttpCache, StopCachingWithAuthDeletesEntry) { |
| 7103 MockHttpCache cache; | 7157 MockHttpCache cache; |
| 7104 TestCompletionCallback callback; | 7158 TestCompletionCallback callback; |
| 7105 MockTransaction mock_transaction(kSimpleGET_Transaction); | 7159 MockTransaction mock_transaction(kSimpleGET_Transaction); |
| 7106 mock_transaction.status = "HTTP/1.1 401 Unauthorized"; | 7160 mock_transaction.status = "HTTP/1.1 401 Unauthorized"; |
| 7107 AddMockTransaction(&mock_transaction); | 7161 AddMockTransaction(&mock_transaction); |
| 7108 MockHttpRequest request(mock_transaction); | 7162 MockHttpRequest request(mock_transaction); |
| 7109 | 7163 |
| 7110 { | 7164 { |
| 7111 std::unique_ptr<HttpTransaction> trans; | 7165 std::unique_ptr<Context> c(new Context()); |
| 7112 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7166 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7113 | 7167 |
| 7114 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7168 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7115 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7169 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7116 | 7170 |
| 7117 trans->StopCaching(); | 7171 c->trans->StopCaching(); |
| 7118 | 7172 |
| 7119 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7173 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7120 rv = trans->Read(buf.get(), 10, callback.callback()); | 7174 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7121 EXPECT_EQ(callback.GetResult(rv), 10); | 7175 EXPECT_EQ(callback.GetResult(rv), 10); |
| 7122 } | 7176 } |
| 7123 RemoveMockTransaction(&mock_transaction); | 7177 RemoveMockTransaction(&mock_transaction); |
| 7124 | 7178 |
| 7125 // Make sure that the ActiveEntry is gone. | 7179 // Make sure that the ActiveEntry is gone. |
| 7126 base::RunLoop().RunUntilIdle(); | 7180 base::RunLoop().RunUntilIdle(); |
| 7127 | 7181 |
| 7128 // Verify that the entry is gone. | 7182 // Verify that the entry is gone. |
| 7129 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7183 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 7130 | 7184 |
| 7131 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7185 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7132 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 7186 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7133 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 7187 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7134 } | 7188 } |
| 7135 | 7189 |
| 7136 // Tests that when we are told to stop caching we don't throw away valid data. | 7190 // Tests that when we are told to stop caching we don't throw away valid data. |
| 7137 TEST(HttpCache, StopCachingSavesEntry) { | 7191 TEST(HttpCache, StopCachingSavesEntry) { |
| 7138 MockHttpCache cache; | 7192 MockHttpCache cache; |
| 7139 TestCompletionCallback callback; | 7193 TestCompletionCallback callback; |
| 7140 MockHttpRequest request(kSimpleGET_Transaction); | 7194 MockHttpRequest request(kSimpleGET_Transaction); |
| 7141 | 7195 |
| 7142 { | 7196 { |
| 7143 std::unique_ptr<HttpTransaction> trans; | 7197 std::unique_ptr<Context> c(new Context()); |
| 7144 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7198 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7145 | 7199 |
| 7146 // Force a response that can be resumed. | 7200 // Force a response that can be resumed. |
| 7147 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); | 7201 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); |
| 7148 AddMockTransaction(&mock_transaction); | 7202 AddMockTransaction(&mock_transaction); |
| 7149 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" | 7203 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 7150 "Content-Length: 42\n" | 7204 "Content-Length: 42\n" |
| 7151 "Etag: \"foo\"\n"; | 7205 "Etag: \"foo\"\n"; |
| 7152 | 7206 |
| 7153 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7207 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7154 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7208 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7155 | 7209 |
| 7156 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7210 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7157 rv = trans->Read(buf.get(), 10, callback.callback()); | 7211 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7158 EXPECT_EQ(callback.GetResult(rv), 10); | 7212 EXPECT_EQ(callback.GetResult(rv), 10); |
| 7159 | 7213 |
| 7160 trans->StopCaching(); | 7214 c->trans->StopCaching(); |
| 7161 | 7215 |
| 7162 // We should be able to keep reading. | 7216 // We should be able to keep reading. |
| 7163 rv = trans->Read(buf.get(), 256, callback.callback()); | 7217 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7164 EXPECT_GT(callback.GetResult(rv), 0); | 7218 EXPECT_GT(callback.GetResult(rv), 0); |
| 7165 rv = trans->Read(buf.get(), 256, callback.callback()); | 7219 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7166 EXPECT_EQ(callback.GetResult(rv), 0); | 7220 EXPECT_EQ(callback.GetResult(rv), 0); |
| 7167 } | 7221 } |
| 7168 | 7222 |
| 7169 // Verify that the entry is marked as incomplete. | 7223 // Verify that the entry is marked as incomplete. |
| 7170 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); | 7224 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 7171 } | 7225 } |
| 7172 | 7226 |
| 7173 // Tests that we handle truncated enries when StopCaching is called. | 7227 // Tests that we handle truncated enries when StopCaching is called. |
| 7174 TEST(HttpCache, StopCachingTruncatedEntry) { | 7228 TEST(HttpCache, StopCachingTruncatedEntry) { |
| 7175 MockHttpCache cache; | 7229 MockHttpCache cache; |
| 7176 TestCompletionCallback callback; | 7230 TestCompletionCallback callback; |
| 7177 MockHttpRequest request(kRangeGET_TransactionOK); | 7231 MockHttpRequest request(kRangeGET_TransactionOK); |
| 7178 request.extra_headers.Clear(); | 7232 request.extra_headers.Clear(); |
| 7179 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); | 7233 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); |
| 7180 AddMockTransaction(&kRangeGET_TransactionOK); | 7234 AddMockTransaction(&kRangeGET_TransactionOK); |
| 7181 | 7235 |
| 7182 std::string raw_headers("HTTP/1.1 200 OK\n" | 7236 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 7183 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 7237 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7184 "ETag: \"foo\"\n" | 7238 "ETag: \"foo\"\n" |
| 7185 "Accept-Ranges: bytes\n" | 7239 "Accept-Ranges: bytes\n" |
| 7186 "Content-Length: 80\n"); | 7240 "Content-Length: 80\n"); |
| 7187 CreateTruncatedEntry(raw_headers, &cache); | 7241 CreateTruncatedEntry(raw_headers, &cache); |
| 7188 | 7242 |
| 7189 { | 7243 { |
| 7190 // Now make a regular request. | 7244 // Now make a regular request. |
| 7191 std::unique_ptr<HttpTransaction> trans; | 7245 std::unique_ptr<Context> c(new Context()); |
| 7192 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7246 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7193 | 7247 |
| 7194 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7248 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7195 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7249 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7196 | 7250 |
| 7197 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7251 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7198 rv = trans->Read(buf.get(), 10, callback.callback()); | 7252 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7199 EXPECT_EQ(callback.GetResult(rv), 10); | 7253 EXPECT_EQ(callback.GetResult(rv), 10); |
| 7200 | 7254 |
| 7201 // This is actually going to do nothing. | 7255 // This is actually going to do nothing. |
| 7202 trans->StopCaching(); | 7256 c->trans->StopCaching(); |
| 7203 | 7257 |
| 7204 // We should be able to keep reading. | 7258 // We should be able to keep reading. |
| 7205 rv = trans->Read(buf.get(), 256, callback.callback()); | 7259 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7206 EXPECT_GT(callback.GetResult(rv), 0); | 7260 EXPECT_GT(callback.GetResult(rv), 0); |
| 7207 rv = trans->Read(buf.get(), 256, callback.callback()); | 7261 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7208 EXPECT_GT(callback.GetResult(rv), 0); | 7262 EXPECT_GT(callback.GetResult(rv), 0); |
| 7209 rv = trans->Read(buf.get(), 256, callback.callback()); | 7263 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7210 EXPECT_EQ(callback.GetResult(rv), 0); | 7264 EXPECT_EQ(callback.GetResult(rv), 0); |
| 7211 } | 7265 } |
| 7212 | 7266 |
| 7213 // Verify that the disk entry was updated. | 7267 // Verify that the disk entry was updated. |
| 7214 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); | 7268 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
| 7215 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7269 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 7216 } | 7270 } |
| 7217 | 7271 |
| 7218 namespace { | 7272 namespace { |
| 7219 | 7273 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7426 (*cache_initializer)(&cache); | 7480 (*cache_initializer)(&cache); |
| 7427 | 7481 |
| 7428 MockTransaction transaction(kSimpleGET_Transaction); | 7482 MockTransaction transaction(kSimpleGET_Transaction); |
| 7429 transaction.url = kRangeGET_TransactionOK.url; | 7483 transaction.url = kRangeGET_TransactionOK.url; |
| 7430 transaction.handler = &LargeResourceTransactionHandler; | 7484 transaction.handler = &LargeResourceTransactionHandler; |
| 7431 transaction.read_handler = &LargeBufferReader; | 7485 transaction.read_handler = &LargeBufferReader; |
| 7432 ScopedMockTransaction scoped_transaction(transaction); | 7486 ScopedMockTransaction scoped_transaction(transaction); |
| 7433 | 7487 |
| 7434 MockHttpRequest request(transaction); | 7488 MockHttpRequest request(transaction); |
| 7435 net::TestCompletionCallback callback; | 7489 net::TestCompletionCallback callback; |
| 7436 std::unique_ptr<net::HttpTransaction> http_transaction; | 7490 std::unique_ptr<Context> c(new Context()); |
| 7437 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, | 7491 int rv = |
| 7438 &http_transaction); | 7492 cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &c->trans); |
| 7439 ASSERT_EQ(net::OK, rv); | 7493 ASSERT_EQ(net::OK, rv); |
| 7440 ASSERT_TRUE(http_transaction.get()); | 7494 ASSERT_TRUE(c->trans.get()); |
| 7441 | 7495 |
| 7442 bool network_transaction_started = false; | 7496 bool network_transaction_started = false; |
| 7443 if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { | 7497 if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { |
| 7444 http_transaction->SetBeforeNetworkStartCallback( | 7498 c->trans->SetBeforeNetworkStartCallback( |
| 7445 base::Bind(&SetFlagOnBeforeNetworkStart, &network_transaction_started)); | 7499 base::Bind(&SetFlagOnBeforeNetworkStart, &network_transaction_started)); |
| 7446 } | 7500 } |
| 7447 | 7501 |
| 7448 rv = http_transaction->Start(&request, callback.callback(), | 7502 rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7449 NetLogWithSource()); | |
| 7450 rv = callback.GetResult(rv); | 7503 rv = callback.GetResult(rv); |
| 7451 ASSERT_EQ(net::OK, rv); | 7504 ASSERT_EQ(net::OK, rv); |
| 7452 | 7505 |
| 7453 if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ) | 7506 if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ) |
| 7454 http_transaction->StopCaching(); | 7507 c->trans->StopCaching(); |
| 7455 | 7508 |
| 7456 int64_t total_bytes_received = 0; | 7509 int64_t total_bytes_received = 0; |
| 7457 | 7510 |
| 7458 EXPECT_EQ(kTotalSize, | 7511 EXPECT_EQ(kTotalSize, |
| 7459 http_transaction->GetResponseInfo()->headers->GetContentLength()); | 7512 c->trans->GetResponseInfo()->headers->GetContentLength()); |
| 7460 do { | 7513 do { |
| 7461 // This test simulates reading gigabytes of data. Buffer size is set to 10MB | 7514 // This test simulates reading gigabytes of data. Buffer size is set to 10MB |
| 7462 // to reduce the number of reads and speed up the test. | 7515 // to reduce the number of reads and speed up the test. |
| 7463 const int kBufferSize = 1024 * 1024 * 10; | 7516 const int kBufferSize = 1024 * 1024 * 10; |
| 7464 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); | 7517 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); |
| 7465 rv = http_transaction->Read(buf.get(), kBufferSize, callback.callback()); | 7518 rv = c->trans->Read(buf.get(), kBufferSize, callback.callback()); |
| 7466 rv = callback.GetResult(rv); | 7519 rv = callback.GetResult(rv); |
| 7467 | 7520 |
| 7468 if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ && | 7521 if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ && |
| 7469 total_bytes_received == 0) { | 7522 total_bytes_received == 0) { |
| 7470 http_transaction->StopCaching(); | 7523 c->trans->StopCaching(); |
| 7471 } | 7524 } |
| 7472 | 7525 |
| 7473 if (rv > 0) | 7526 if (rv > 0) |
| 7474 total_bytes_received += rv; | 7527 total_bytes_received += rv; |
| 7475 | 7528 |
| 7476 if (network_transaction_started && | 7529 if (network_transaction_started && |
| 7477 stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { | 7530 stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { |
| 7478 http_transaction->StopCaching(); | 7531 c->trans->StopCaching(); |
| 7479 network_transaction_started = false; | 7532 network_transaction_started = false; |
| 7480 } | 7533 } |
| 7481 } while (rv > 0); | 7534 } while (rv > 0); |
| 7482 | 7535 |
| 7483 // The only verification we are going to do is that the received resource has | 7536 // The only verification we are going to do is that the received resource has |
| 7484 // the correct size. This is sufficient to verify that the state machine | 7537 // the correct size. This is sufficient to verify that the state machine |
| 7485 // didn't terminate abruptly due to the StopCaching() call. | 7538 // didn't terminate abruptly due to the StopCaching() call. |
| 7486 EXPECT_EQ(kTotalSize, total_bytes_received); | 7539 EXPECT_EQ(kTotalSize, total_bytes_received); |
| 7487 } | 7540 } |
| 7488 | 7541 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7523 | 7576 |
| 7524 // Verify that the entry is marked as incomplete. | 7577 // Verify that the entry is marked as incomplete. |
| 7525 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); | 7578 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 7526 } | 7579 } |
| 7527 | 7580 |
| 7528 // Make sure that calling SetPriority on a cache transaction passes on | 7581 // Make sure that calling SetPriority on a cache transaction passes on |
| 7529 // its priority updates to its underlying network transaction. | 7582 // its priority updates to its underlying network transaction. |
| 7530 TEST(HttpCache, SetPriority) { | 7583 TEST(HttpCache, SetPriority) { |
| 7531 MockHttpCache cache; | 7584 MockHttpCache cache; |
| 7532 | 7585 |
| 7533 std::unique_ptr<HttpTransaction> trans; | 7586 std::unique_ptr<Context> c(new Context); |
| 7534 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); | 7587 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &c->trans), IsOk()); |
| 7535 | 7588 |
| 7536 // Shouldn't crash, but doesn't do anything either. | 7589 // Shouldn't crash, but doesn't do anything either. |
| 7537 trans->SetPriority(LOW); | 7590 c->trans->SetPriority(LOW); |
| 7538 | 7591 |
| 7539 EXPECT_FALSE(cache.network_layer()->last_transaction()); | 7592 EXPECT_FALSE(cache.network_layer()->last_transaction()); |
| 7540 EXPECT_EQ(DEFAULT_PRIORITY, | 7593 EXPECT_EQ(DEFAULT_PRIORITY, |
| 7541 cache.network_layer()->last_create_transaction_priority()); | 7594 cache.network_layer()->last_create_transaction_priority()); |
| 7542 | 7595 |
| 7543 HttpRequestInfo info; | 7596 HttpRequestInfo info; |
| 7544 info.url = GURL(kSimpleGET_Transaction.url); | 7597 info.url = GURL(kSimpleGET_Transaction.url); |
| 7545 TestCompletionCallback callback; | 7598 TestCompletionCallback callback; |
| 7546 EXPECT_EQ(ERR_IO_PENDING, | 7599 EXPECT_EQ(ERR_IO_PENDING, |
| 7547 trans->Start(&info, callback.callback(), NetLogWithSource())); | 7600 c->trans->Start(&info, callback.callback(), NetLogWithSource())); |
| 7548 | 7601 |
| 7549 EXPECT_TRUE(cache.network_layer()->last_transaction()); | 7602 EXPECT_TRUE(cache.network_layer()->last_transaction()); |
| 7550 if (cache.network_layer()->last_transaction()) { | 7603 if (cache.network_layer()->last_transaction()) { |
| 7551 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); | 7604 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); |
| 7552 EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority()); | 7605 EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority()); |
| 7553 } | 7606 } |
| 7554 | 7607 |
| 7555 trans->SetPriority(HIGHEST); | 7608 c->trans->SetPriority(HIGHEST); |
| 7556 | 7609 |
| 7557 if (cache.network_layer()->last_transaction()) { | 7610 if (cache.network_layer()->last_transaction()) { |
| 7558 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); | 7611 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); |
| 7559 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority()); | 7612 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority()); |
| 7560 } | 7613 } |
| 7561 | 7614 |
| 7562 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 7615 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 7563 } | 7616 } |
| 7564 | 7617 |
| 7565 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache | 7618 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache |
| 7566 // transaction passes on its argument to the underlying network transaction. | 7619 // transaction passes on its argument to the underlying network transaction. |
| 7567 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { | 7620 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { |
| 7568 MockHttpCache cache; | 7621 MockHttpCache cache; |
| 7569 | 7622 |
| 7570 FakeWebSocketHandshakeStreamCreateHelper create_helper; | 7623 FakeWebSocketHandshakeStreamCreateHelper create_helper; |
| 7571 std::unique_ptr<HttpTransaction> trans; | 7624 std::unique_ptr<Context> c(new Context); |
| 7572 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); | 7625 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &c->trans), IsOk()); |
| 7573 | 7626 |
| 7574 EXPECT_FALSE(cache.network_layer()->last_transaction()); | 7627 EXPECT_FALSE(cache.network_layer()->last_transaction()); |
| 7575 | 7628 |
| 7576 HttpRequestInfo info; | 7629 HttpRequestInfo info; |
| 7577 info.url = GURL(kSimpleGET_Transaction.url); | 7630 info.url = GURL(kSimpleGET_Transaction.url); |
| 7578 TestCompletionCallback callback; | 7631 TestCompletionCallback callback; |
| 7579 EXPECT_EQ(ERR_IO_PENDING, | 7632 EXPECT_EQ(ERR_IO_PENDING, |
| 7580 trans->Start(&info, callback.callback(), NetLogWithSource())); | 7633 c->trans->Start(&info, callback.callback(), NetLogWithSource())); |
| 7581 | 7634 |
| 7582 ASSERT_TRUE(cache.network_layer()->last_transaction()); | 7635 ASSERT_TRUE(cache.network_layer()->last_transaction()); |
| 7583 EXPECT_FALSE(cache.network_layer()->last_transaction()-> | 7636 EXPECT_FALSE(cache.network_layer()->last_transaction()-> |
| 7584 websocket_handshake_stream_create_helper()); | 7637 websocket_handshake_stream_create_helper()); |
| 7585 trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); | 7638 c->trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); |
| 7586 EXPECT_EQ(&create_helper, | 7639 EXPECT_EQ(&create_helper, |
| 7587 cache.network_layer()->last_transaction()-> | 7640 cache.network_layer()->last_transaction()-> |
| 7588 websocket_handshake_stream_create_helper()); | 7641 websocket_handshake_stream_create_helper()); |
| 7589 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 7642 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 7590 } | 7643 } |
| 7591 | 7644 |
| 7592 // Make sure that a cache transaction passes on its priority to | 7645 // Make sure that a cache transaction passes on its priority to |
| 7593 // newly-created network transactions. | 7646 // newly-created network transactions. |
| 7594 TEST(HttpCache, SetPriorityNewTransaction) { | 7647 TEST(HttpCache, SetPriorityNewTransaction) { |
| 7595 MockHttpCache cache; | 7648 MockHttpCache cache; |
| 7596 AddMockTransaction(&kRangeGET_TransactionOK); | 7649 AddMockTransaction(&kRangeGET_TransactionOK); |
| 7597 | 7650 |
| 7598 std::string raw_headers("HTTP/1.1 200 OK\n" | 7651 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 7599 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 7652 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7600 "ETag: \"foo\"\n" | 7653 "ETag: \"foo\"\n" |
| 7601 "Accept-Ranges: bytes\n" | 7654 "Accept-Ranges: bytes\n" |
| 7602 "Content-Length: 80\n"); | 7655 "Content-Length: 80\n"); |
| 7603 CreateTruncatedEntry(raw_headers, &cache); | 7656 CreateTruncatedEntry(raw_headers, &cache); |
| 7604 | 7657 |
| 7605 // Now make a regular request. | 7658 // Now make a regular request. |
| 7606 std::string headers; | 7659 std::string headers; |
| 7607 MockTransaction transaction(kRangeGET_TransactionOK); | 7660 MockTransaction transaction(kRangeGET_TransactionOK); |
| 7608 transaction.request_headers = EXTRA_HEADER; | 7661 transaction.request_headers = EXTRA_HEADER; |
| 7609 transaction.data = kFullRangeData; | 7662 transaction.data = kFullRangeData; |
| 7610 | 7663 |
| 7611 std::unique_ptr<HttpTransaction> trans; | 7664 std::unique_ptr<Context> c(new Context); |
| 7612 ASSERT_THAT(cache.http_cache()->CreateTransaction(MEDIUM, &trans), IsOk()); | 7665 ASSERT_THAT(cache.http_cache()->CreateTransaction(MEDIUM, &c->trans), IsOk()); |
| 7613 EXPECT_EQ(DEFAULT_PRIORITY, | 7666 EXPECT_EQ(DEFAULT_PRIORITY, |
| 7614 cache.network_layer()->last_create_transaction_priority()); | 7667 cache.network_layer()->last_create_transaction_priority()); |
| 7615 | 7668 |
| 7616 MockHttpRequest info(transaction); | 7669 MockHttpRequest info(transaction); |
| 7617 TestCompletionCallback callback; | 7670 TestCompletionCallback callback; |
| 7618 EXPECT_EQ(ERR_IO_PENDING, | 7671 EXPECT_EQ(ERR_IO_PENDING, |
| 7619 trans->Start(&info, callback.callback(), NetLogWithSource())); | 7672 c->trans->Start(&info, callback.callback(), NetLogWithSource())); |
| 7620 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 7673 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 7621 | 7674 |
| 7622 EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority()); | 7675 EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority()); |
| 7623 | 7676 |
| 7624 trans->SetPriority(HIGHEST); | 7677 c->trans->SetPriority(HIGHEST); |
| 7625 // Should trigger a new network transaction and pick up the new | 7678 // Should trigger a new network transaction and pick up the new |
| 7626 // priority. | 7679 // priority. |
| 7627 ReadAndVerifyTransaction(trans.get(), transaction); | 7680 ReadAndVerifyTransaction(c->trans.get(), transaction); |
| 7628 | 7681 |
| 7629 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); | 7682 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); |
| 7630 | 7683 |
| 7631 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7684 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 7632 } | 7685 } |
| 7633 | 7686 |
| 7634 namespace { | 7687 namespace { |
| 7635 | 7688 |
| 7636 void RunTransactionAndGetNetworkBytes(MockHttpCache& cache, | 7689 void RunTransactionAndGetNetworkBytes(MockHttpCache& cache, |
| 7637 const MockTransaction& trans_info, | 7690 const MockTransaction& trans_info, |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7996 MockHttpCache cache; | 8049 MockHttpCache cache; |
| 7997 | 8050 |
| 7998 // Create a transaction for bytes 0-9. | 8051 // Create a transaction for bytes 0-9. |
| 7999 MockHttpRequest request(kRangeGET_TransactionOK); | 8052 MockHttpRequest request(kRangeGET_TransactionOK); |
| 8000 MockTransaction transaction(kRangeGET_TransactionOK); | 8053 MockTransaction transaction(kRangeGET_TransactionOK); |
| 8001 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; | 8054 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 8002 transaction.data = "rg: 00-09 "; | 8055 transaction.data = "rg: 00-09 "; |
| 8003 AddMockTransaction(&transaction); | 8056 AddMockTransaction(&transaction); |
| 8004 | 8057 |
| 8005 TestCompletionCallback callback; | 8058 TestCompletionCallback callback; |
| 8006 std::unique_ptr<HttpTransaction> trans; | 8059 std::unique_ptr<Context> c(new Context); |
| 8007 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 8060 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 8008 EXPECT_THAT(rv, IsOk()); | 8061 EXPECT_THAT(rv, IsOk()); |
| 8009 ASSERT_TRUE(trans.get()); | 8062 ASSERT_TRUE(c->trans.get()); |
| 8010 | 8063 |
| 8011 // Start our transaction. | 8064 // Start our transaction. |
| 8012 trans->Start(&request, callback.callback(), NetLogWithSource()); | 8065 c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 8013 | 8066 |
| 8014 // A second transaction on a different part of the file (the default | 8067 // A second transaction on a different part of the file (the default |
| 8015 // kRangeGET_TransactionOK requests 40-49) should not be blocked by | 8068 // kRangeGET_TransactionOK requests 40-49) should not be blocked by |
| 8016 // the already pending transaction. | 8069 // the already pending transaction. |
| 8017 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 8070 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 8018 | 8071 |
| 8019 // Let the first transaction complete. | 8072 // Let the first transaction complete. |
| 8020 callback.WaitForResult(); | 8073 callback.WaitForResult(); |
| 8021 | 8074 |
| 8022 RemoveMockTransaction(&transaction); | 8075 RemoveMockTransaction(&transaction); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8254 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8307 RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 8255 kTypicalGET_Transaction, &response_info); | 8308 kTypicalGET_Transaction, &response_info); |
| 8256 | 8309 |
| 8257 EXPECT_FALSE(response_info.was_cached); | 8310 EXPECT_FALSE(response_info.was_cached); |
| 8258 EXPECT_TRUE(response_info.network_accessed); | 8311 EXPECT_TRUE(response_info.network_accessed); |
| 8259 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8312 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
| 8260 response_info.cache_entry_status); | 8313 response_info.cache_entry_status); |
| 8261 } | 8314 } |
| 8262 | 8315 |
| 8263 } // namespace net | 8316 } // namespace net |
| OLD | NEW |