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