| 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. | 5837 // Verify that the entry is not marked as incomplete since SharedWriters |
| 5845 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); | 5838 // enables response writing to complete even if transaction is destroyed |
| 5839 // mid-way. |
| 5840 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); |
| 5846 } | 5841 } |
| 5847 | 5842 |
| 5848 // Tests that we don't mark an entry as truncated when we read everything. | 5843 // Tests that we don't mark an entry as truncated when we read everything. |
| 5849 TEST(HttpCache, DontSetTruncatedFlag) { | 5844 TEST(HttpCache, DontSetTruncatedFlag) { |
| 5850 MockHttpCache cache; | 5845 MockHttpCache cache; |
| 5851 | 5846 |
| 5852 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 5847 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5853 transaction.response_headers = | 5848 transaction.response_headers = |
| 5854 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5849 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5855 "Content-Length: 22\n" | 5850 "Content-Length: 22\n" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 5876 } | 5871 } |
| 5877 | 5872 |
| 5878 // Tests that sparse entries don't set the truncate flag. | 5873 // Tests that sparse entries don't set the truncate flag. |
| 5879 TEST(HttpCache, RangeGET_DontTruncate) { | 5874 TEST(HttpCache, RangeGET_DontTruncate) { |
| 5880 MockHttpCache cache; | 5875 MockHttpCache cache; |
| 5881 | 5876 |
| 5882 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5877 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5883 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; | 5878 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; |
| 5884 | 5879 |
| 5885 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | 5880 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5886 std::unique_ptr<HttpTransaction> trans; | 5881 std::unique_ptr<Context> c(new Context()); |
| 5887 | 5882 |
| 5888 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5883 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 5889 EXPECT_THAT(rv, IsOk()); | 5884 EXPECT_THAT(rv, IsOk()); |
| 5890 | 5885 |
| 5891 TestCompletionCallback cb; | 5886 TestCompletionCallback cb; |
| 5892 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); | 5887 rv = c->trans->Start(request.get(), cb.callback(), NetLogWithSource()); |
| 5893 EXPECT_EQ(0, cb.GetResult(rv)); | 5888 EXPECT_EQ(0, cb.GetResult(rv)); |
| 5894 | 5889 |
| 5895 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | 5890 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5896 rv = trans->Read(buf.get(), 10, cb.callback()); | 5891 rv = c->trans->Read(buf.get(), 10, cb.callback()); |
| 5897 EXPECT_EQ(10, cb.GetResult(rv)); | 5892 EXPECT_EQ(10, cb.GetResult(rv)); |
| 5898 | 5893 |
| 5899 // Should not trigger any DCHECK. | |
| 5900 trans.reset(); | |
| 5901 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); | 5894 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5902 } | 5895 } |
| 5903 | 5896 |
| 5904 // Tests that sparse entries don't set the truncate flag (when the byte range | 5897 // Tests that sparse entries don't set the truncate flag (when the byte range |
| 5905 // starts after 0). | 5898 // starts after 0). |
| 5906 TEST(HttpCache, RangeGET_DontTruncate2) { | 5899 TEST(HttpCache, RangeGET_DontTruncate2) { |
| 5907 MockHttpCache cache; | 5900 MockHttpCache cache; |
| 5908 | 5901 |
| 5909 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5902 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5910 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; | 5903 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; |
| 5911 | 5904 |
| 5912 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | 5905 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5913 std::unique_ptr<HttpTransaction> trans; | 5906 std::unique_ptr<Context> c(new Context()); |
| 5914 | 5907 |
| 5915 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5908 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 5916 EXPECT_THAT(rv, IsOk()); | 5909 EXPECT_THAT(rv, IsOk()); |
| 5917 | 5910 |
| 5918 TestCompletionCallback cb; | 5911 rv = c->trans->Start(request.get(), c->callback.callback(), |
| 5919 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); | 5912 NetLogWithSource()); |
| 5920 EXPECT_EQ(0, cb.GetResult(rv)); | 5913 EXPECT_EQ(0, c->callback.GetResult(rv)); |
| 5921 | 5914 |
| 5922 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | 5915 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5923 rv = trans->Read(buf.get(), 10, cb.callback()); | 5916 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
| 5924 EXPECT_EQ(10, cb.GetResult(rv)); | 5917 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 5925 | 5918 |
| 5926 // Should not trigger any DCHECK. | 5919 // Should not trigger any DCHECK. |
| 5927 trans.reset(); | |
| 5928 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); | 5920 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5929 } | 5921 } |
| 5930 | 5922 |
| 5931 // Tests that we can continue with a request that was interrupted. | 5923 // Tests that we can continue with a request that was interrupted. |
| 5932 TEST(HttpCache, GET_IncompleteResource) { | 5924 TEST(HttpCache, GET_IncompleteResource) { |
| 5933 MockHttpCache cache; | 5925 MockHttpCache cache; |
| 5934 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5926 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5935 | 5927 |
| 5936 std::string raw_headers("HTTP/1.1 200 OK\n" | 5928 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5937 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5929 "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 | 6373 |
| 6382 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); | 6374 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); |
| 6383 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; | 6375 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 6384 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; | 6376 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; |
| 6385 | 6377 |
| 6386 MockHttpRequest request(kTestTransaction); | 6378 MockHttpRequest request(kTestTransaction); |
| 6387 TestCompletionCallback callback; | 6379 TestCompletionCallback callback; |
| 6388 | 6380 |
| 6389 // Write to the cache. | 6381 // Write to the cache. |
| 6390 { | 6382 { |
| 6391 std::unique_ptr<HttpTransaction> trans; | 6383 std::unique_ptr<Context> c(new Context()); |
| 6392 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6384 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6393 | 6385 |
| 6394 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6386 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6395 if (rv == ERR_IO_PENDING) | 6387 if (rv == ERR_IO_PENDING) |
| 6396 rv = callback.WaitForResult(); | 6388 rv = callback.WaitForResult(); |
| 6397 ASSERT_THAT(rv, IsOk()); | 6389 ASSERT_THAT(rv, IsOk()); |
| 6398 | 6390 |
| 6399 const HttpResponseInfo* info = trans->GetResponseInfo(); | 6391 const HttpResponseInfo* info = c->trans->GetResponseInfo(); |
| 6400 ASSERT_TRUE(info); | 6392 ASSERT_TRUE(info); |
| 6401 | 6393 |
| 6402 EXPECT_EQ(info->headers->response_code(), 301); | 6394 EXPECT_EQ(info->headers->response_code(), 301); |
| 6403 | 6395 |
| 6404 std::string location; | 6396 std::string location; |
| 6405 info->headers->EnumerateHeader(NULL, "Location", &location); | 6397 info->headers->EnumerateHeader(NULL, "Location", &location); |
| 6406 EXPECT_EQ(location, "http://www.bar.com/"); | 6398 EXPECT_EQ(location, "http://www.bar.com/"); |
| 6407 | 6399 |
| 6408 // Mark the transaction as completed so it is cached. | 6400 // Mark the transaction as completed so it is cached. |
| 6409 trans->DoneReading(); | 6401 c->trans->DoneReading(); |
| 6410 | 6402 |
| 6411 // Destroy transaction when going out of scope. We have not actually | 6403 // 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. | 6404 // read the response body -- want to test that it is still getting cached. |
| 6413 } | 6405 } |
| 6414 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6406 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6415 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 6407 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6416 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6408 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6417 | 6409 |
| 6418 // Active entries in the cache are not retired synchronously. Make | 6410 // Active entries in the cache are not retired synchronously. Make |
| 6419 // sure the next run hits the MockHttpCache and open_count is | 6411 // sure the next run hits the MockHttpCache and open_count is |
| 6420 // correct. | 6412 // correct. |
| 6421 base::RunLoop().RunUntilIdle(); | 6413 base::RunLoop().RunUntilIdle(); |
| 6422 | 6414 |
| 6423 // Read from the cache. | 6415 // Read from the cache. |
| 6424 { | 6416 { |
| 6425 std::unique_ptr<HttpTransaction> trans; | 6417 std::unique_ptr<Context> c(new Context()); |
| 6426 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6418 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6427 | 6419 |
| 6428 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6420 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6429 if (rv == ERR_IO_PENDING) | 6421 if (rv == ERR_IO_PENDING) |
| 6430 rv = callback.WaitForResult(); | 6422 rv = callback.WaitForResult(); |
| 6431 ASSERT_THAT(rv, IsOk()); | 6423 ASSERT_THAT(rv, IsOk()); |
| 6432 | 6424 |
| 6433 const HttpResponseInfo* info = trans->GetResponseInfo(); | 6425 const HttpResponseInfo* info = c->trans->GetResponseInfo(); |
| 6434 ASSERT_TRUE(info); | 6426 ASSERT_TRUE(info); |
| 6435 | 6427 |
| 6436 EXPECT_EQ(info->headers->response_code(), 301); | 6428 EXPECT_EQ(info->headers->response_code(), 301); |
| 6437 | 6429 |
| 6438 std::string location; | 6430 std::string location; |
| 6439 info->headers->EnumerateHeader(NULL, "Location", &location); | 6431 info->headers->EnumerateHeader(NULL, "Location", &location); |
| 6440 EXPECT_EQ(location, "http://www.bar.com/"); | 6432 EXPECT_EQ(location, "http://www.bar.com/"); |
| 6441 | 6433 |
| 6442 // Mark the transaction as completed so it is cached. | 6434 // Mark the transaction as completed so it is cached. |
| 6443 trans->DoneReading(); | 6435 c->trans->DoneReading(); |
| 6444 | 6436 |
| 6445 // Destroy transaction when going out of scope. We have not actually | 6437 // 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. | 6438 // read the response body -- want to test that it is still getting cached. |
| 6447 } | 6439 } |
| 6448 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6440 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6449 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6441 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6450 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6442 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6451 } | 6443 } |
| 6452 | 6444 |
| 6453 // Verify that no-cache resources are stored in cache, but are not fetched from | 6445 // 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; | 6586 transaction.cert_status = CERT_STATUS_REVOKED; |
| 6595 ScopedMockTransaction scoped_transaction(transaction); | 6587 ScopedMockTransaction scoped_transaction(transaction); |
| 6596 | 6588 |
| 6597 // write to the cache | 6589 // write to the cache |
| 6598 RunTransactionTest(cache.http_cache(), transaction); | 6590 RunTransactionTest(cache.http_cache(), transaction); |
| 6599 | 6591 |
| 6600 // Test that it was not cached. | 6592 // Test that it was not cached. |
| 6601 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 6593 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 6602 | 6594 |
| 6603 MockHttpRequest request(transaction); | 6595 MockHttpRequest request(transaction); |
| 6604 TestCompletionCallback callback; | |
| 6605 | 6596 |
| 6606 std::unique_ptr<HttpTransaction> trans; | 6597 std::unique_ptr<Context> c(new Context()); |
| 6607 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6598 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6608 | 6599 |
| 6609 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6600 int rv = |
| 6601 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 6610 if (rv == ERR_IO_PENDING) | 6602 if (rv == ERR_IO_PENDING) |
| 6611 rv = callback.WaitForResult(); | 6603 rv = c->callback.WaitForResult(); |
| 6612 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); | 6604 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 6613 } | 6605 } |
| 6614 | 6606 |
| 6615 // Ensure that we don't crash by if left-behind transactions. | 6607 // Ensure that we don't crash by if left-behind transactions. |
| 6616 TEST(HttpCache, OutlivedTransactions) { | 6608 TEST(HttpCache, OutlivedTransactions) { |
| 6617 MockHttpCache* cache = new MockHttpCache; | 6609 MockHttpCache* cache = new MockHttpCache; |
| 6618 | 6610 |
| 6619 std::unique_ptr<HttpTransaction> trans; | 6611 std::unique_ptr<Context> c(new Context()); |
| 6620 EXPECT_THAT(cache->CreateTransaction(&trans), IsOk()); | 6612 EXPECT_THAT(cache->CreateTransaction(&c->trans), IsOk()); |
| 6621 | 6613 |
| 6622 delete cache; | 6614 delete cache; |
| 6623 trans.reset(); | |
| 6624 } | 6615 } |
| 6625 | 6616 |
| 6626 // Test that the disabled mode works. | 6617 // Test that the disabled mode works. |
| 6627 TEST(HttpCache, CacheDisabledMode) { | 6618 TEST(HttpCache, CacheDisabledMode) { |
| 6628 MockHttpCache cache; | 6619 MockHttpCache cache; |
| 6629 | 6620 |
| 6630 // write to the cache | 6621 // write to the cache |
| 6631 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 6622 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6632 | 6623 |
| 6633 // go into disabled mode | 6624 // 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()); | 6947 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6957 } | 6948 } |
| 6958 | 6949 |
| 6959 // Tests that we don't mark entries as truncated when a filter detects the end | 6950 // Tests that we don't mark entries as truncated when a filter detects the end |
| 6960 // of the stream. | 6951 // of the stream. |
| 6961 TEST(HttpCache, FilterCompletion) { | 6952 TEST(HttpCache, FilterCompletion) { |
| 6962 MockHttpCache cache; | 6953 MockHttpCache cache; |
| 6963 TestCompletionCallback callback; | 6954 TestCompletionCallback callback; |
| 6964 | 6955 |
| 6965 { | 6956 { |
| 6966 std::unique_ptr<HttpTransaction> trans; | 6957 std::unique_ptr<Context> c(new Context()); |
| 6967 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6958 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 6968 | 6959 |
| 6969 MockHttpRequest request(kSimpleGET_Transaction); | 6960 MockHttpRequest request(kSimpleGET_Transaction); |
| 6970 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6961 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6971 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 6962 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6972 | 6963 |
| 6973 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 6964 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 6974 rv = trans->Read(buf.get(), 256, callback.callback()); | 6965 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 6975 EXPECT_GT(callback.GetResult(rv), 0); | 6966 EXPECT_GT(callback.GetResult(rv), 0); |
| 6976 | 6967 |
| 6977 // Now make sure that the entry is preserved. | 6968 // Now make sure that the entry is preserved. |
| 6978 trans->DoneReading(); | 6969 c->trans->DoneReading(); |
| 6979 } | 6970 } |
| 6980 | 6971 |
| 6981 // Make sure that the ActiveEntry is gone. | 6972 // Make sure that the ActiveEntry is gone. |
| 6982 base::RunLoop().RunUntilIdle(); | 6973 base::RunLoop().RunUntilIdle(); |
| 6983 | 6974 |
| 6984 // Read from the cache. | 6975 // Read from the cache. |
| 6985 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 6976 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6986 | 6977 |
| 6987 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6978 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6988 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6979 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6989 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6980 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6990 } | 6981 } |
| 6991 | 6982 |
| 6992 // Tests that we don't mark entries as truncated and release the cache | 6983 // 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 | 6984 // entry when DoneReading() is called before any Read() calls, such as |
| 6994 // for a redirect. | 6985 // for a redirect. |
| 6995 TEST(HttpCache, DoneReading) { | 6986 TEST(HttpCache, DoneReading) { |
| 6996 MockHttpCache cache; | 6987 MockHttpCache cache; |
| 6997 TestCompletionCallback callback; | 6988 TestCompletionCallback callback; |
| 6998 | 6989 |
| 6999 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 6990 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 7000 transaction.data = ""; | 6991 transaction.data = ""; |
| 7001 | 6992 |
| 7002 std::unique_ptr<HttpTransaction> trans; | 6993 std::unique_ptr<Context> c(new Context()); |
| 7003 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6994 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7004 | 6995 |
| 7005 MockHttpRequest request(transaction); | 6996 MockHttpRequest request(transaction); |
| 7006 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6997 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7007 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 6998 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7008 | 6999 |
| 7009 trans->DoneReading(); | 7000 c->trans->DoneReading(); |
| 7010 // Leave the transaction around. | 7001 // Leave the transaction around. |
| 7011 | 7002 |
| 7012 // Make sure that the ActiveEntry is gone. | 7003 // Make sure that the ActiveEntry is gone. |
| 7013 base::RunLoop().RunUntilIdle(); | 7004 base::RunLoop().RunUntilIdle(); |
| 7014 | 7005 |
| 7015 // Read from the cache. This should not deadlock. | 7006 // Read from the cache. This should not deadlock. |
| 7016 RunTransactionTest(cache.http_cache(), transaction); | 7007 RunTransactionTest(cache.http_cache(), transaction); |
| 7017 | 7008 |
| 7018 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 7009 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7019 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 7010 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7020 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7011 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7021 } | 7012 } |
| 7022 | 7013 |
| 7023 // Tests that we stop caching when told. | 7014 // Tests that we stop caching when told. |
| 7024 TEST(HttpCache, StopCachingDeletesEntry) { | 7015 TEST(HttpCache, StopCachingDeletesEntry) { |
| 7025 MockHttpCache cache; | 7016 MockHttpCache cache; |
| 7026 TestCompletionCallback callback; | |
| 7027 MockHttpRequest request(kSimpleGET_Transaction); | 7017 MockHttpRequest request(kSimpleGET_Transaction); |
| 7028 | 7018 |
| 7029 { | 7019 { |
| 7030 std::unique_ptr<HttpTransaction> trans; | 7020 std::unique_ptr<Context> c(new Context()); |
| 7031 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7021 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7032 | 7022 |
| 7033 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7023 int rv = |
| 7034 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7024 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 7025 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 7035 | 7026 |
| 7036 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7027 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7037 rv = trans->Read(buf.get(), 10, callback.callback()); | 7028 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
| 7038 EXPECT_EQ(10, callback.GetResult(rv)); | 7029 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 7039 | 7030 |
| 7040 trans->StopCaching(); | 7031 c->trans->StopCaching(); |
| 7041 | 7032 |
| 7042 // We should be able to keep reading. | 7033 // We should be able to keep reading. |
| 7043 rv = trans->Read(buf.get(), 256, callback.callback()); | 7034 rv = c->trans->Read(buf.get(), 256, c->callback.callback()); |
| 7044 EXPECT_GT(callback.GetResult(rv), 0); | 7035 EXPECT_GT(c->callback.GetResult(rv), 0); |
| 7045 rv = trans->Read(buf.get(), 256, callback.callback()); | 7036 rv = c->trans->Read(buf.get(), 256, c->callback.callback()); |
| 7046 EXPECT_EQ(0, callback.GetResult(rv)); | 7037 EXPECT_EQ(0, c->callback.GetResult(rv)); |
| 7047 } | 7038 } |
| 7048 | 7039 |
| 7049 // Make sure that the ActiveEntry is gone. | 7040 // Make sure that the ActiveEntry is gone. |
| 7050 base::RunLoop().RunUntilIdle(); | 7041 base::RunLoop().RunUntilIdle(); |
| 7051 | 7042 |
| 7052 // Verify that the entry is gone. | 7043 // Verify that the entry is gone. |
| 7053 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7044 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 7054 | 7045 |
| 7055 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7046 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7056 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 7047 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7057 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 7048 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7058 } | 7049 } |
| 7059 | 7050 |
| 7060 // Tests that we stop caching when told, even if DoneReading is called | 7051 // Tests that we stop caching when told, even if DoneReading is called |
| 7061 // after StopCaching. | 7052 // after StopCaching. |
| 7062 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { | 7053 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { |
| 7063 MockHttpCache cache; | 7054 MockHttpCache cache; |
| 7064 TestCompletionCallback callback; | 7055 TestCompletionCallback callback; |
| 7065 MockHttpRequest request(kSimpleGET_Transaction); | 7056 MockHttpRequest request(kSimpleGET_Transaction); |
| 7066 | 7057 |
| 7067 { | 7058 { |
| 7068 std::unique_ptr<HttpTransaction> trans; | 7059 std::unique_ptr<Context> c(new Context()); |
| 7069 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7060 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7070 | 7061 |
| 7071 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7062 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7072 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7063 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7073 | 7064 |
| 7074 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7065 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7075 rv = trans->Read(buf.get(), 10, callback.callback()); | 7066 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7076 EXPECT_EQ(10, callback.GetResult(rv)); | 7067 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7077 | 7068 |
| 7078 trans->StopCaching(); | 7069 c->trans->StopCaching(); |
| 7079 | 7070 |
| 7080 // We should be able to keep reading. | 7071 // We should be able to keep reading. |
| 7081 rv = trans->Read(buf.get(), 256, callback.callback()); | 7072 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7082 EXPECT_GT(callback.GetResult(rv), 0); | 7073 EXPECT_GT(callback.GetResult(rv), 0); |
| 7083 rv = trans->Read(buf.get(), 256, callback.callback()); | 7074 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7084 EXPECT_EQ(0, callback.GetResult(rv)); | 7075 EXPECT_EQ(0, callback.GetResult(rv)); |
| 7085 | 7076 |
| 7086 // We should be able to call DoneReading. | 7077 // We should be able to call DoneReading. |
| 7087 trans->DoneReading(); | 7078 c->trans->DoneReading(); |
| 7088 } | 7079 } |
| 7089 | 7080 |
| 7090 // Make sure that the ActiveEntry is gone. | 7081 // Make sure that the ActiveEntry is gone. |
| 7091 base::RunLoop().RunUntilIdle(); | 7082 base::RunLoop().RunUntilIdle(); |
| 7092 | 7083 |
| 7093 // Verify that the entry is gone. | 7084 // Verify that the entry is gone. |
| 7094 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7085 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 7095 | 7086 |
| 7096 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7087 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7097 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 7088 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7098 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 7089 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7099 } | 7090 } |
| 7100 | 7091 |
| 7101 // Tests that we stop caching when told, when using auth. | 7092 // Tests that we stop caching when told, when using auth. |
| 7102 TEST(HttpCache, StopCachingWithAuthDeletesEntry) { | 7093 TEST(HttpCache, StopCachingWithAuthDeletesEntry) { |
| 7103 MockHttpCache cache; | 7094 MockHttpCache cache; |
| 7104 TestCompletionCallback callback; | 7095 TestCompletionCallback callback; |
| 7105 MockTransaction mock_transaction(kSimpleGET_Transaction); | 7096 MockTransaction mock_transaction(kSimpleGET_Transaction); |
| 7106 mock_transaction.status = "HTTP/1.1 401 Unauthorized"; | 7097 mock_transaction.status = "HTTP/1.1 401 Unauthorized"; |
| 7107 AddMockTransaction(&mock_transaction); | 7098 AddMockTransaction(&mock_transaction); |
| 7108 MockHttpRequest request(mock_transaction); | 7099 MockHttpRequest request(mock_transaction); |
| 7109 | 7100 |
| 7110 { | 7101 { |
| 7111 std::unique_ptr<HttpTransaction> trans; | 7102 std::unique_ptr<Context> c(new Context()); |
| 7112 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7103 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7113 | 7104 |
| 7114 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7105 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7115 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7106 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7116 | 7107 |
| 7117 trans->StopCaching(); | 7108 c->trans->StopCaching(); |
| 7118 | 7109 |
| 7119 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7110 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7120 rv = trans->Read(buf.get(), 10, callback.callback()); | 7111 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7121 EXPECT_EQ(callback.GetResult(rv), 10); | 7112 EXPECT_EQ(callback.GetResult(rv), 10); |
| 7122 } | 7113 } |
| 7123 RemoveMockTransaction(&mock_transaction); | 7114 RemoveMockTransaction(&mock_transaction); |
| 7124 | 7115 |
| 7125 // Make sure that the ActiveEntry is gone. | 7116 // Make sure that the ActiveEntry is gone. |
| 7126 base::RunLoop().RunUntilIdle(); | 7117 base::RunLoop().RunUntilIdle(); |
| 7127 | 7118 |
| 7128 // Verify that the entry is gone. | 7119 // Verify that the entry is gone. |
| 7129 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7120 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 7130 | 7121 |
| 7131 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7122 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7132 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 7123 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7133 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 7124 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7134 } | 7125 } |
| 7135 | 7126 |
| 7136 // Tests that when we are told to stop caching we don't throw away valid data. | 7127 // Tests that when we are told to stop caching we don't throw away valid data. |
| 7137 TEST(HttpCache, StopCachingSavesEntry) { | 7128 TEST(HttpCache, StopCachingSavesEntry) { |
| 7138 MockHttpCache cache; | 7129 MockHttpCache cache; |
| 7139 TestCompletionCallback callback; | 7130 TestCompletionCallback callback; |
| 7140 MockHttpRequest request(kSimpleGET_Transaction); | 7131 MockHttpRequest request(kSimpleGET_Transaction); |
| 7141 | 7132 |
| 7142 { | 7133 { |
| 7143 std::unique_ptr<HttpTransaction> trans; | 7134 std::unique_ptr<Context> c(new Context()); |
| 7144 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7135 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7145 | 7136 |
| 7146 // Force a response that can be resumed. | 7137 // Force a response that can be resumed. |
| 7147 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); | 7138 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); |
| 7148 AddMockTransaction(&mock_transaction); | 7139 AddMockTransaction(&mock_transaction); |
| 7149 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" | 7140 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 7150 "Content-Length: 42\n" | 7141 "Content-Length: 42\n" |
| 7151 "Etag: \"foo\"\n"; | 7142 "Etag: \"foo\"\n"; |
| 7152 | 7143 |
| 7153 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7144 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7154 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7145 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7155 | 7146 |
| 7156 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7147 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7157 rv = trans->Read(buf.get(), 10, callback.callback()); | 7148 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7158 EXPECT_EQ(callback.GetResult(rv), 10); | 7149 EXPECT_EQ(callback.GetResult(rv), 10); |
| 7159 | 7150 |
| 7160 trans->StopCaching(); | 7151 c->trans->StopCaching(); |
| 7161 | 7152 |
| 7162 // We should be able to keep reading. | 7153 // We should be able to keep reading. |
| 7163 rv = trans->Read(buf.get(), 256, callback.callback()); | 7154 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7164 EXPECT_GT(callback.GetResult(rv), 0); | 7155 EXPECT_GT(callback.GetResult(rv), 0); |
| 7165 rv = trans->Read(buf.get(), 256, callback.callback()); | 7156 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7166 EXPECT_EQ(callback.GetResult(rv), 0); | 7157 EXPECT_EQ(callback.GetResult(rv), 0); |
| 7167 } | 7158 } |
| 7168 | 7159 |
| 7169 // Verify that the entry is marked as incomplete. | 7160 // Verify that the entry is marked as incomplete. |
| 7170 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); | 7161 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 7171 } | 7162 } |
| 7172 | 7163 |
| 7173 // Tests that we handle truncated enries when StopCaching is called. | 7164 // Tests that we handle truncated enries when StopCaching is called. |
| 7174 TEST(HttpCache, StopCachingTruncatedEntry) { | 7165 TEST(HttpCache, StopCachingTruncatedEntry) { |
| 7175 MockHttpCache cache; | 7166 MockHttpCache cache; |
| 7176 TestCompletionCallback callback; | 7167 TestCompletionCallback callback; |
| 7177 MockHttpRequest request(kRangeGET_TransactionOK); | 7168 MockHttpRequest request(kRangeGET_TransactionOK); |
| 7178 request.extra_headers.Clear(); | 7169 request.extra_headers.Clear(); |
| 7179 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); | 7170 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); |
| 7180 AddMockTransaction(&kRangeGET_TransactionOK); | 7171 AddMockTransaction(&kRangeGET_TransactionOK); |
| 7181 | 7172 |
| 7182 std::string raw_headers("HTTP/1.1 200 OK\n" | 7173 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 7183 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 7174 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7184 "ETag: \"foo\"\n" | 7175 "ETag: \"foo\"\n" |
| 7185 "Accept-Ranges: bytes\n" | 7176 "Accept-Ranges: bytes\n" |
| 7186 "Content-Length: 80\n"); | 7177 "Content-Length: 80\n"); |
| 7187 CreateTruncatedEntry(raw_headers, &cache); | 7178 CreateTruncatedEntry(raw_headers, &cache); |
| 7188 | 7179 |
| 7189 { | 7180 { |
| 7190 // Now make a regular request. | 7181 // Now make a regular request. |
| 7191 std::unique_ptr<HttpTransaction> trans; | 7182 std::unique_ptr<Context> c(new Context()); |
| 7192 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7183 ASSERT_THAT(cache.CreateTransaction(&c->trans), IsOk()); |
| 7193 | 7184 |
| 7194 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7185 int rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7195 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7186 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 7196 | 7187 |
| 7197 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 7188 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 7198 rv = trans->Read(buf.get(), 10, callback.callback()); | 7189 rv = c->trans->Read(buf.get(), 10, callback.callback()); |
| 7199 EXPECT_EQ(callback.GetResult(rv), 10); | 7190 EXPECT_EQ(callback.GetResult(rv), 10); |
| 7200 | 7191 |
| 7201 // This is actually going to do nothing. | 7192 // This is actually going to do nothing. |
| 7202 trans->StopCaching(); | 7193 c->trans->StopCaching(); |
| 7203 | 7194 |
| 7204 // We should be able to keep reading. | 7195 // We should be able to keep reading. |
| 7205 rv = trans->Read(buf.get(), 256, callback.callback()); | 7196 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7206 EXPECT_GT(callback.GetResult(rv), 0); | 7197 EXPECT_GT(callback.GetResult(rv), 0); |
| 7207 rv = trans->Read(buf.get(), 256, callback.callback()); | 7198 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7208 EXPECT_GT(callback.GetResult(rv), 0); | 7199 EXPECT_GT(callback.GetResult(rv), 0); |
| 7209 rv = trans->Read(buf.get(), 256, callback.callback()); | 7200 rv = c->trans->Read(buf.get(), 256, callback.callback()); |
| 7210 EXPECT_EQ(callback.GetResult(rv), 0); | 7201 EXPECT_EQ(callback.GetResult(rv), 0); |
| 7211 } | 7202 } |
| 7212 | 7203 |
| 7213 // Verify that the disk entry was updated. | 7204 // Verify that the disk entry was updated. |
| 7214 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); | 7205 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
| 7215 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7206 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 7216 } | 7207 } |
| 7217 | 7208 |
| 7218 namespace { | 7209 namespace { |
| 7219 | 7210 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7426 (*cache_initializer)(&cache); | 7417 (*cache_initializer)(&cache); |
| 7427 | 7418 |
| 7428 MockTransaction transaction(kSimpleGET_Transaction); | 7419 MockTransaction transaction(kSimpleGET_Transaction); |
| 7429 transaction.url = kRangeGET_TransactionOK.url; | 7420 transaction.url = kRangeGET_TransactionOK.url; |
| 7430 transaction.handler = &LargeResourceTransactionHandler; | 7421 transaction.handler = &LargeResourceTransactionHandler; |
| 7431 transaction.read_handler = &LargeBufferReader; | 7422 transaction.read_handler = &LargeBufferReader; |
| 7432 ScopedMockTransaction scoped_transaction(transaction); | 7423 ScopedMockTransaction scoped_transaction(transaction); |
| 7433 | 7424 |
| 7434 MockHttpRequest request(transaction); | 7425 MockHttpRequest request(transaction); |
| 7435 net::TestCompletionCallback callback; | 7426 net::TestCompletionCallback callback; |
| 7436 std::unique_ptr<net::HttpTransaction> http_transaction; | 7427 std::unique_ptr<Context> c(new Context()); |
| 7437 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, | 7428 int rv = |
| 7438 &http_transaction); | 7429 cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &c->trans); |
| 7439 ASSERT_EQ(net::OK, rv); | 7430 ASSERT_EQ(net::OK, rv); |
| 7440 ASSERT_TRUE(http_transaction.get()); | 7431 ASSERT_TRUE(c->trans.get()); |
| 7441 | 7432 |
| 7442 bool network_transaction_started = false; | 7433 bool network_transaction_started = false; |
| 7443 if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { | 7434 if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { |
| 7444 http_transaction->SetBeforeNetworkStartCallback( | 7435 c->trans->SetBeforeNetworkStartCallback( |
| 7445 base::Bind(&SetFlagOnBeforeNetworkStart, &network_transaction_started)); | 7436 base::Bind(&SetFlagOnBeforeNetworkStart, &network_transaction_started)); |
| 7446 } | 7437 } |
| 7447 | 7438 |
| 7448 rv = http_transaction->Start(&request, callback.callback(), | 7439 rv = c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 7449 NetLogWithSource()); | |
| 7450 rv = callback.GetResult(rv); | 7440 rv = callback.GetResult(rv); |
| 7451 ASSERT_EQ(net::OK, rv); | 7441 ASSERT_EQ(net::OK, rv); |
| 7452 | 7442 |
| 7453 if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ) | 7443 if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ) |
| 7454 http_transaction->StopCaching(); | 7444 c->trans->StopCaching(); |
| 7455 | 7445 |
| 7456 int64_t total_bytes_received = 0; | 7446 int64_t total_bytes_received = 0; |
| 7457 | 7447 |
| 7458 EXPECT_EQ(kTotalSize, | 7448 EXPECT_EQ(kTotalSize, |
| 7459 http_transaction->GetResponseInfo()->headers->GetContentLength()); | 7449 c->trans->GetResponseInfo()->headers->GetContentLength()); |
| 7460 do { | 7450 do { |
| 7461 // This test simulates reading gigabytes of data. Buffer size is set to 10MB | 7451 // 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. | 7452 // to reduce the number of reads and speed up the test. |
| 7463 const int kBufferSize = 1024 * 1024 * 10; | 7453 const int kBufferSize = 1024 * 1024 * 10; |
| 7464 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); | 7454 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); |
| 7465 rv = http_transaction->Read(buf.get(), kBufferSize, callback.callback()); | 7455 rv = c->trans->Read(buf.get(), kBufferSize, callback.callback()); |
| 7466 rv = callback.GetResult(rv); | 7456 rv = callback.GetResult(rv); |
| 7467 | 7457 |
| 7468 if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ && | 7458 if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ && |
| 7469 total_bytes_received == 0) { | 7459 total_bytes_received == 0) { |
| 7470 http_transaction->StopCaching(); | 7460 c->trans->StopCaching(); |
| 7471 } | 7461 } |
| 7472 | 7462 |
| 7473 if (rv > 0) | 7463 if (rv > 0) |
| 7474 total_bytes_received += rv; | 7464 total_bytes_received += rv; |
| 7475 | 7465 |
| 7476 if (network_transaction_started && | 7466 if (network_transaction_started && |
| 7477 stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { | 7467 stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { |
| 7478 http_transaction->StopCaching(); | 7468 c->trans->StopCaching(); |
| 7479 network_transaction_started = false; | 7469 network_transaction_started = false; |
| 7480 } | 7470 } |
| 7481 } while (rv > 0); | 7471 } while (rv > 0); |
| 7482 | 7472 |
| 7483 // The only verification we are going to do is that the received resource has | 7473 // 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 | 7474 // the correct size. This is sufficient to verify that the state machine |
| 7485 // didn't terminate abruptly due to the StopCaching() call. | 7475 // didn't terminate abruptly due to the StopCaching() call. |
| 7486 EXPECT_EQ(kTotalSize, total_bytes_received); | 7476 EXPECT_EQ(kTotalSize, total_bytes_received); |
| 7487 } | 7477 } |
| 7488 | 7478 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7523 | 7513 |
| 7524 // Verify that the entry is marked as incomplete. | 7514 // Verify that the entry is marked as incomplete. |
| 7525 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); | 7515 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 7526 } | 7516 } |
| 7527 | 7517 |
| 7528 // Make sure that calling SetPriority on a cache transaction passes on | 7518 // Make sure that calling SetPriority on a cache transaction passes on |
| 7529 // its priority updates to its underlying network transaction. | 7519 // its priority updates to its underlying network transaction. |
| 7530 TEST(HttpCache, SetPriority) { | 7520 TEST(HttpCache, SetPriority) { |
| 7531 MockHttpCache cache; | 7521 MockHttpCache cache; |
| 7532 | 7522 |
| 7533 std::unique_ptr<HttpTransaction> trans; | 7523 std::unique_ptr<Context> c(new Context); |
| 7534 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); | 7524 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &c->trans), IsOk()); |
| 7535 | 7525 |
| 7536 // Shouldn't crash, but doesn't do anything either. | 7526 // Shouldn't crash, but doesn't do anything either. |
| 7537 trans->SetPriority(LOW); | 7527 c->trans->SetPriority(LOW); |
| 7538 | 7528 |
| 7539 EXPECT_FALSE(cache.network_layer()->last_transaction()); | 7529 EXPECT_FALSE(cache.network_layer()->last_transaction()); |
| 7540 EXPECT_EQ(DEFAULT_PRIORITY, | 7530 EXPECT_EQ(DEFAULT_PRIORITY, |
| 7541 cache.network_layer()->last_create_transaction_priority()); | 7531 cache.network_layer()->last_create_transaction_priority()); |
| 7542 | 7532 |
| 7543 HttpRequestInfo info; | 7533 HttpRequestInfo info; |
| 7544 info.url = GURL(kSimpleGET_Transaction.url); | 7534 info.url = GURL(kSimpleGET_Transaction.url); |
| 7545 TestCompletionCallback callback; | 7535 TestCompletionCallback callback; |
| 7546 EXPECT_EQ(ERR_IO_PENDING, | 7536 EXPECT_EQ(ERR_IO_PENDING, |
| 7547 trans->Start(&info, callback.callback(), NetLogWithSource())); | 7537 c->trans->Start(&info, callback.callback(), NetLogWithSource())); |
| 7548 | 7538 |
| 7549 EXPECT_TRUE(cache.network_layer()->last_transaction()); | 7539 EXPECT_TRUE(cache.network_layer()->last_transaction()); |
| 7550 if (cache.network_layer()->last_transaction()) { | 7540 if (cache.network_layer()->last_transaction()) { |
| 7551 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); | 7541 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); |
| 7552 EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority()); | 7542 EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority()); |
| 7553 } | 7543 } |
| 7554 | 7544 |
| 7555 trans->SetPriority(HIGHEST); | 7545 c->trans->SetPriority(HIGHEST); |
| 7556 | 7546 |
| 7557 if (cache.network_layer()->last_transaction()) { | 7547 if (cache.network_layer()->last_transaction()) { |
| 7558 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); | 7548 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); |
| 7559 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority()); | 7549 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority()); |
| 7560 } | 7550 } |
| 7561 | 7551 |
| 7562 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 7552 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 7563 } | 7553 } |
| 7564 | 7554 |
| 7565 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache | 7555 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache |
| 7566 // transaction passes on its argument to the underlying network transaction. | 7556 // transaction passes on its argument to the underlying network transaction. |
| 7567 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { | 7557 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { |
| 7568 MockHttpCache cache; | 7558 MockHttpCache cache; |
| 7569 | 7559 |
| 7570 FakeWebSocketHandshakeStreamCreateHelper create_helper; | 7560 FakeWebSocketHandshakeStreamCreateHelper create_helper; |
| 7571 std::unique_ptr<HttpTransaction> trans; | 7561 std::unique_ptr<Context> c(new Context); |
| 7572 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); | 7562 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &c->trans), IsOk()); |
| 7573 | 7563 |
| 7574 EXPECT_FALSE(cache.network_layer()->last_transaction()); | 7564 EXPECT_FALSE(cache.network_layer()->last_transaction()); |
| 7575 | 7565 |
| 7576 HttpRequestInfo info; | 7566 HttpRequestInfo info; |
| 7577 info.url = GURL(kSimpleGET_Transaction.url); | 7567 info.url = GURL(kSimpleGET_Transaction.url); |
| 7578 TestCompletionCallback callback; | 7568 TestCompletionCallback callback; |
| 7579 EXPECT_EQ(ERR_IO_PENDING, | 7569 EXPECT_EQ(ERR_IO_PENDING, |
| 7580 trans->Start(&info, callback.callback(), NetLogWithSource())); | 7570 c->trans->Start(&info, callback.callback(), NetLogWithSource())); |
| 7581 | 7571 |
| 7582 ASSERT_TRUE(cache.network_layer()->last_transaction()); | 7572 ASSERT_TRUE(cache.network_layer()->last_transaction()); |
| 7583 EXPECT_FALSE(cache.network_layer()->last_transaction()-> | 7573 EXPECT_FALSE(cache.network_layer()->last_transaction()-> |
| 7584 websocket_handshake_stream_create_helper()); | 7574 websocket_handshake_stream_create_helper()); |
| 7585 trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); | 7575 c->trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); |
| 7586 EXPECT_EQ(&create_helper, | 7576 EXPECT_EQ(&create_helper, |
| 7587 cache.network_layer()->last_transaction()-> | 7577 cache.network_layer()->last_transaction()-> |
| 7588 websocket_handshake_stream_create_helper()); | 7578 websocket_handshake_stream_create_helper()); |
| 7589 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 7579 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 7590 } | 7580 } |
| 7591 | 7581 |
| 7592 // Make sure that a cache transaction passes on its priority to | 7582 // Make sure that a cache transaction passes on its priority to |
| 7593 // newly-created network transactions. | 7583 // newly-created network transactions. |
| 7594 TEST(HttpCache, SetPriorityNewTransaction) { | 7584 TEST(HttpCache, SetPriorityNewTransaction) { |
| 7595 MockHttpCache cache; | 7585 MockHttpCache cache; |
| 7596 AddMockTransaction(&kRangeGET_TransactionOK); | 7586 AddMockTransaction(&kRangeGET_TransactionOK); |
| 7597 | 7587 |
| 7598 std::string raw_headers("HTTP/1.1 200 OK\n" | 7588 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 7599 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 7589 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7600 "ETag: \"foo\"\n" | 7590 "ETag: \"foo\"\n" |
| 7601 "Accept-Ranges: bytes\n" | 7591 "Accept-Ranges: bytes\n" |
| 7602 "Content-Length: 80\n"); | 7592 "Content-Length: 80\n"); |
| 7603 CreateTruncatedEntry(raw_headers, &cache); | 7593 CreateTruncatedEntry(raw_headers, &cache); |
| 7604 | 7594 |
| 7605 // Now make a regular request. | 7595 // Now make a regular request. |
| 7606 std::string headers; | 7596 std::string headers; |
| 7607 MockTransaction transaction(kRangeGET_TransactionOK); | 7597 MockTransaction transaction(kRangeGET_TransactionOK); |
| 7608 transaction.request_headers = EXTRA_HEADER; | 7598 transaction.request_headers = EXTRA_HEADER; |
| 7609 transaction.data = kFullRangeData; | 7599 transaction.data = kFullRangeData; |
| 7610 | 7600 |
| 7611 std::unique_ptr<HttpTransaction> trans; | 7601 std::unique_ptr<Context> c(new Context); |
| 7612 ASSERT_THAT(cache.http_cache()->CreateTransaction(MEDIUM, &trans), IsOk()); | 7602 ASSERT_THAT(cache.http_cache()->CreateTransaction(MEDIUM, &c->trans), IsOk()); |
| 7613 EXPECT_EQ(DEFAULT_PRIORITY, | 7603 EXPECT_EQ(DEFAULT_PRIORITY, |
| 7614 cache.network_layer()->last_create_transaction_priority()); | 7604 cache.network_layer()->last_create_transaction_priority()); |
| 7615 | 7605 |
| 7616 MockHttpRequest info(transaction); | 7606 MockHttpRequest info(transaction); |
| 7617 TestCompletionCallback callback; | 7607 TestCompletionCallback callback; |
| 7618 EXPECT_EQ(ERR_IO_PENDING, | 7608 EXPECT_EQ(ERR_IO_PENDING, |
| 7619 trans->Start(&info, callback.callback(), NetLogWithSource())); | 7609 c->trans->Start(&info, callback.callback(), NetLogWithSource())); |
| 7620 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 7610 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 7621 | 7611 |
| 7622 EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority()); | 7612 EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority()); |
| 7623 | 7613 |
| 7624 trans->SetPriority(HIGHEST); | 7614 c->trans->SetPriority(HIGHEST); |
| 7625 // Should trigger a new network transaction and pick up the new | 7615 // Should trigger a new network transaction and pick up the new |
| 7626 // priority. | 7616 // priority. |
| 7627 ReadAndVerifyTransaction(trans.get(), transaction); | 7617 ReadAndVerifyTransaction(c->trans.get(), transaction); |
| 7628 | 7618 |
| 7629 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); | 7619 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); |
| 7630 | 7620 |
| 7631 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7621 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 7632 } | 7622 } |
| 7633 | 7623 |
| 7634 namespace { | 7624 namespace { |
| 7635 | 7625 |
| 7636 void RunTransactionAndGetNetworkBytes(MockHttpCache& cache, | 7626 void RunTransactionAndGetNetworkBytes(MockHttpCache& cache, |
| 7637 const MockTransaction& trans_info, | 7627 const MockTransaction& trans_info, |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7996 MockHttpCache cache; | 7986 MockHttpCache cache; |
| 7997 | 7987 |
| 7998 // Create a transaction for bytes 0-9. | 7988 // Create a transaction for bytes 0-9. |
| 7999 MockHttpRequest request(kRangeGET_TransactionOK); | 7989 MockHttpRequest request(kRangeGET_TransactionOK); |
| 8000 MockTransaction transaction(kRangeGET_TransactionOK); | 7990 MockTransaction transaction(kRangeGET_TransactionOK); |
| 8001 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; | 7991 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 8002 transaction.data = "rg: 00-09 "; | 7992 transaction.data = "rg: 00-09 "; |
| 8003 AddMockTransaction(&transaction); | 7993 AddMockTransaction(&transaction); |
| 8004 | 7994 |
| 8005 TestCompletionCallback callback; | 7995 TestCompletionCallback callback; |
| 8006 std::unique_ptr<HttpTransaction> trans; | 7996 std::unique_ptr<Context> c(new Context); |
| 8007 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 7997 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &c->trans); |
| 8008 EXPECT_THAT(rv, IsOk()); | 7998 EXPECT_THAT(rv, IsOk()); |
| 8009 ASSERT_TRUE(trans.get()); | 7999 ASSERT_TRUE(c->trans.get()); |
| 8010 | 8000 |
| 8011 // Start our transaction. | 8001 // Start our transaction. |
| 8012 trans->Start(&request, callback.callback(), NetLogWithSource()); | 8002 c->trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 8013 | 8003 |
| 8014 // A second transaction on a different part of the file (the default | 8004 // A second transaction on a different part of the file (the default |
| 8015 // kRangeGET_TransactionOK requests 40-49) should not be blocked by | 8005 // kRangeGET_TransactionOK requests 40-49) should not be blocked by |
| 8016 // the already pending transaction. | 8006 // the already pending transaction. |
| 8017 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 8007 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 8018 | 8008 |
| 8019 // Let the first transaction complete. | 8009 // Let the first transaction complete. |
| 8020 callback.WaitForResult(); | 8010 callback.WaitForResult(); |
| 8021 | 8011 |
| 8022 RemoveMockTransaction(&transaction); | 8012 RemoveMockTransaction(&transaction); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8254 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8244 RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 8255 kTypicalGET_Transaction, &response_info); | 8245 kTypicalGET_Transaction, &response_info); |
| 8256 | 8246 |
| 8257 EXPECT_FALSE(response_info.was_cached); | 8247 EXPECT_FALSE(response_info.was_cached); |
| 8258 EXPECT_TRUE(response_info.network_accessed); | 8248 EXPECT_TRUE(response_info.network_accessed); |
| 8259 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8249 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
| 8260 response_info.cache_entry_status); | 8250 response_info.cache_entry_status); |
| 8261 } | 8251 } |
| 8262 | 8252 |
| 8263 } // namespace net | 8253 } // namespace net |
| OLD | NEW |