OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_vector.h" | 9 #include "base/scoped_vector.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 return static_cast<MockNetworkLayer*>(http_cache_.network_layer()); | 563 return static_cast<MockNetworkLayer*>(http_cache_.network_layer()); |
564 } | 564 } |
565 MockDiskCache* disk_cache() { | 565 MockDiskCache* disk_cache() { |
566 return static_cast<MockDiskCache*>(http_cache_.GetBackend()); | 566 return static_cast<MockDiskCache*>(http_cache_.GetBackend()); |
567 } | 567 } |
568 | 568 |
569 private: | 569 private: |
570 net::HttpCache http_cache_; | 570 net::HttpCache http_cache_; |
571 }; | 571 }; |
572 | 572 |
| 573 // This version of the disk cache doesn't invoke CreateEntry callbacks. |
| 574 class MockDiskCacheNoCB : public MockDiskCache { |
| 575 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
| 576 net::CompletionCallback* callback) { |
| 577 return net::ERR_IO_PENDING; |
| 578 } |
| 579 }; |
573 | 580 |
574 //----------------------------------------------------------------------------- | 581 //----------------------------------------------------------------------------- |
575 // helpers | 582 // helpers |
576 | 583 |
577 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | 584 void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
578 const MockTransaction& trans_info) { | 585 const MockTransaction& trans_info) { |
579 std::string content; | 586 std::string content; |
580 int rv = ReadTransaction(trans, &content); | 587 int rv = ReadTransaction(trans, &content); |
581 | 588 |
582 EXPECT_EQ(net::OK, rv); | 589 EXPECT_EQ(net::OK, rv); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
862 TestCompletionCallback callback; | 869 TestCompletionCallback callback; |
863 scoped_ptr<net::HttpTransaction> trans; | 870 scoped_ptr<net::HttpTransaction> trans; |
864 }; | 871 }; |
865 | 872 |
866 } // namespace | 873 } // namespace |
867 | 874 |
868 | 875 |
869 //----------------------------------------------------------------------------- | 876 //----------------------------------------------------------------------------- |
870 // tests | 877 // tests |
871 | 878 |
| 879 namespace net { |
| 880 |
| 881 // This is a friend class of the HttpCache so that we can interact with it to |
| 882 // perform a few internal tests. We override the backend factory method so that |
| 883 // we can test the logic of backend instantiation. |
| 884 class HttpCacheTest : public testing::Test { |
| 885 public: |
| 886 // Controls whether to block the backend instantiation or not. If |block| is |
| 887 // true, any blocked call will be notified via the provided callback. |
| 888 void BlockCacheCreation(bool block) { |
| 889 block_ = block; |
| 890 if (!block_ && callback_) { |
| 891 *backend_ = new MockDiskCache(); |
| 892 callback_->Run(OK); |
| 893 callback_ = NULL; |
| 894 } |
| 895 } |
| 896 |
| 897 protected: |
| 898 virtual void SetUp() { |
| 899 current_test_ = this; |
| 900 backend_ = NULL; |
| 901 callback_ = NULL; |
| 902 block_ = false; |
| 903 } |
| 904 |
| 905 virtual void TearDown() { current_test_ = NULL; } |
| 906 |
| 907 // Implements disk_cache::CreateCacheBackend(). |
| 908 static int MockCreateBackend(CacheType type, const FilePath& path, |
| 909 int max_bytes, bool force, MessageLoop* thread, |
| 910 disk_cache::Backend** backend, |
| 911 CompletionCallback* callback) { |
| 912 if (current_test_ == NULL) { |
| 913 EXPECT_TRUE(current_test_); |
| 914 return ERR_FAILED; |
| 915 } |
| 916 if (!current_test_->block_) { |
| 917 *backend = new MockDiskCache(); |
| 918 return OK; |
| 919 } |
| 920 |
| 921 current_test_->backend_ = backend; |
| 922 current_test_->callback_ = callback; |
| 923 return ERR_IO_PENDING; |
| 924 } |
| 925 |
| 926 private: |
| 927 static HttpCacheTest* current_test_; |
| 928 disk_cache::Backend** backend_; |
| 929 CompletionCallback* callback_; |
| 930 bool block_; |
| 931 }; |
| 932 |
| 933 // Static. |
| 934 HttpCacheTest* HttpCacheTest::current_test_ = NULL; |
| 935 |
| 936 // Tests that we queue requests when initializing the backend. |
| 937 TEST_F(HttpCacheTest, SimpleGET_WaitForBackend) { |
| 938 MockHttpCache cache(NULL); |
| 939 cache.http_cache()->create_backend_fn_ = MockCreateBackend; |
| 940 cache.http_cache()->set_type(MEMORY_CACHE); |
| 941 BlockCacheCreation(true); |
| 942 |
| 943 MockHttpRequest request0(kSimpleGET_Transaction); |
| 944 MockHttpRequest request1(kTypicalGET_Transaction); |
| 945 MockHttpRequest request2(kETagGET_Transaction); |
| 946 |
| 947 std::vector<Context*> context_list; |
| 948 const int kNumTransactions = 3; |
| 949 |
| 950 for (int i = 0; i < kNumTransactions; i++) { |
| 951 context_list.push_back(new Context()); |
| 952 Context* c = context_list[i]; |
| 953 |
| 954 c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 955 EXPECT_EQ(OK, c->result); |
| 956 } |
| 957 |
| 958 context_list[0]->result = context_list[0]->trans->Start( |
| 959 &request0, &context_list[0]->callback, BoundNetLog()); |
| 960 context_list[1]->result = context_list[1]->trans->Start( |
| 961 &request1, &context_list[1]->callback, BoundNetLog()); |
| 962 context_list[2]->result = context_list[2]->trans->Start( |
| 963 &request2, &context_list[2]->callback, BoundNetLog()); |
| 964 |
| 965 // Just to make sure that everything is still pending. |
| 966 MessageLoop::current()->RunAllPending(); |
| 967 |
| 968 // The first request should be creating the disk cache. |
| 969 EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 970 |
| 971 BlockCacheCreation(false); |
| 972 |
| 973 MessageLoop::current()->RunAllPending(); |
| 974 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 975 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 976 |
| 977 for (int i = 0; i < kNumTransactions; ++i) { |
| 978 EXPECT_TRUE(context_list[i]->callback.have_result()); |
| 979 delete context_list[i]; |
| 980 } |
| 981 } |
| 982 |
| 983 // Tests that we can cancel requests that are queued waiting for the backend |
| 984 // to be initialized. |
| 985 TEST_F(HttpCacheTest, SimpleGET_WaitForBackend_CancelCreate) { |
| 986 MockHttpCache cache(NULL); |
| 987 cache.http_cache()->create_backend_fn_ = MockCreateBackend; |
| 988 cache.http_cache()->set_type(MEMORY_CACHE); |
| 989 BlockCacheCreation(true); |
| 990 |
| 991 MockHttpRequest request0(kSimpleGET_Transaction); |
| 992 MockHttpRequest request1(kTypicalGET_Transaction); |
| 993 MockHttpRequest request2(kETagGET_Transaction); |
| 994 |
| 995 std::vector<Context*> context_list; |
| 996 const int kNumTransactions = 3; |
| 997 |
| 998 for (int i = 0; i < kNumTransactions; i++) { |
| 999 context_list.push_back(new Context()); |
| 1000 Context* c = context_list[i]; |
| 1001 |
| 1002 c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1003 EXPECT_EQ(OK, c->result); |
| 1004 } |
| 1005 |
| 1006 context_list[0]->result = context_list[0]->trans->Start( |
| 1007 &request0, &context_list[0]->callback, BoundNetLog()); |
| 1008 context_list[1]->result = context_list[1]->trans->Start( |
| 1009 &request1, &context_list[1]->callback, BoundNetLog()); |
| 1010 context_list[2]->result = context_list[2]->trans->Start( |
| 1011 &request2, &context_list[2]->callback, BoundNetLog()); |
| 1012 |
| 1013 // Just to make sure that everything is still pending. |
| 1014 MessageLoop::current()->RunAllPending(); |
| 1015 |
| 1016 // The first request should be creating the disk cache. |
| 1017 EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 1018 |
| 1019 // Cancel a request from the pending queue. |
| 1020 delete context_list[1]; |
| 1021 context_list[1] = NULL; |
| 1022 |
| 1023 // Cancel the request that is creating the entry. |
| 1024 delete context_list[0]; |
| 1025 context_list[0] = NULL; |
| 1026 |
| 1027 // Complete the last transaction. |
| 1028 BlockCacheCreation(false); |
| 1029 |
| 1030 context_list[2]->result = |
| 1031 context_list[2]->callback.GetResult(context_list[2]->result); |
| 1032 ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction); |
| 1033 |
| 1034 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1035 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1036 |
| 1037 delete context_list[2]; |
| 1038 } |
| 1039 |
| 1040 } // net namespace. |
872 | 1041 |
873 TEST(HttpCache, CreateThenDestroy) { | 1042 TEST(HttpCache, CreateThenDestroy) { |
874 MockHttpCache cache; | 1043 MockHttpCache cache; |
875 | 1044 |
876 scoped_ptr<net::HttpTransaction> trans; | 1045 scoped_ptr<net::HttpTransaction> trans; |
877 int rv = cache.http_cache()->CreateTransaction(&trans); | 1046 int rv = cache.http_cache()->CreateTransaction(&trans); |
878 EXPECT_EQ(net::OK, rv); | 1047 EXPECT_EQ(net::OK, rv); |
879 ASSERT_TRUE(trans.get()); | 1048 ASSERT_TRUE(trans.get()); |
880 } | 1049 } |
881 | 1050 |
(...skipping 23 matching lines...) Expand all Loading... |
905 cache.disk_cache()->set_fail_requests(); | 1074 cache.disk_cache()->set_fail_requests(); |
906 | 1075 |
907 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); | 1076 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
908 | 1077 |
909 // Read from the network, and don't use the cache. | 1078 // Read from the network, and don't use the cache. |
910 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, | 1079 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
911 log.bound()); | 1080 log.bound()); |
912 | 1081 |
913 // Check that the NetLog was filled as expected. | 1082 // Check that the NetLog was filled as expected. |
914 // (We attempted to both Open and Create entries, but both failed). | 1083 // (We attempted to both Open and Create entries, but both failed). |
915 EXPECT_EQ(4u, log.entries().size()); | 1084 EXPECT_EQ(6u, log.entries().size()); |
916 EXPECT_TRUE(net::LogContainsBeginEvent( | 1085 EXPECT_TRUE(net::LogContainsBeginEvent( |
917 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1086 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
918 EXPECT_TRUE(net::LogContainsEndEvent( | 1087 EXPECT_TRUE(net::LogContainsEndEvent( |
919 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1088 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
920 EXPECT_TRUE(net::LogContainsBeginEvent( | 1089 EXPECT_TRUE(net::LogContainsBeginEvent( |
921 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1090 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
922 EXPECT_TRUE(net::LogContainsEndEvent( | 1091 EXPECT_TRUE(net::LogContainsEndEvent( |
923 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1092 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 1093 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1094 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1095 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1096 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
924 | 1097 |
925 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1098 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
926 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1099 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
927 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 1100 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
928 } | 1101 } |
929 | 1102 |
| 1103 TEST(HttpCache, SimpleGETNoDiskCache2) { |
| 1104 // This will initialize a cache object with NULL backend. |
| 1105 MockHttpCache cache(NULL); |
| 1106 |
| 1107 // Read from the network, and don't use the cache. |
| 1108 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1109 |
| 1110 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1111 EXPECT_FALSE(cache.http_cache()->GetBackend()); |
| 1112 } |
| 1113 |
930 TEST(HttpCache, SimpleGETWithDiskFailures) { | 1114 TEST(HttpCache, SimpleGETWithDiskFailures) { |
931 MockHttpCache cache; | 1115 MockHttpCache cache; |
932 | 1116 |
933 cache.disk_cache()->set_soft_failures(true); | 1117 cache.disk_cache()->set_soft_failures(true); |
934 | 1118 |
935 // Read from the network, and fail to write to the cache. | 1119 // Read from the network, and fail to write to the cache. |
936 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1120 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
937 | 1121 |
938 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1122 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
939 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1123 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
989 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { | 1173 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { |
990 MockHttpCache cache; | 1174 MockHttpCache cache; |
991 | 1175 |
992 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); | 1176 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
993 | 1177 |
994 // write to the cache | 1178 // write to the cache |
995 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, | 1179 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
996 log.bound()); | 1180 log.bound()); |
997 | 1181 |
998 // Check that the NetLog was filled as expected. | 1182 // Check that the NetLog was filled as expected. |
999 EXPECT_EQ(6u, log.entries().size()); | 1183 EXPECT_EQ(8u, log.entries().size()); |
1000 EXPECT_TRUE(net::LogContainsBeginEvent( | 1184 EXPECT_TRUE(net::LogContainsBeginEvent( |
1001 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1185 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1002 EXPECT_TRUE(net::LogContainsEndEvent( | 1186 EXPECT_TRUE(net::LogContainsEndEvent( |
1003 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1187 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1004 EXPECT_TRUE(net::LogContainsBeginEvent( | 1188 EXPECT_TRUE(net::LogContainsBeginEvent( |
1005 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1189 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
1006 EXPECT_TRUE(net::LogContainsEndEvent( | 1190 EXPECT_TRUE(net::LogContainsEndEvent( |
1007 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1191 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
1008 EXPECT_TRUE(net::LogContainsBeginEvent( | 1192 EXPECT_TRUE(net::LogContainsBeginEvent( |
1009 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1193 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
1010 EXPECT_TRUE(net::LogContainsEndEvent( | 1194 EXPECT_TRUE(net::LogContainsEndEvent( |
1011 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1195 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1196 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1197 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1198 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1199 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1012 | 1200 |
1013 // force this transaction to read from the cache | 1201 // force this transaction to read from the cache |
1014 MockTransaction transaction(kSimpleGET_Transaction); | 1202 MockTransaction transaction(kSimpleGET_Transaction); |
1015 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 1203 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
1016 | 1204 |
1017 log.Clear(); | 1205 log.Clear(); |
1018 | 1206 |
1019 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); | 1207 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
1020 | 1208 |
1021 // Check that the NetLog was filled as expected. | 1209 // Check that the NetLog was filled as expected. |
1022 EXPECT_EQ(6u, log.entries().size()); | 1210 EXPECT_EQ(8u, log.entries().size()); |
1023 EXPECT_TRUE(net::LogContainsBeginEvent( | 1211 EXPECT_TRUE(net::LogContainsBeginEvent( |
1024 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1212 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1025 EXPECT_TRUE(net::LogContainsEndEvent( | 1213 EXPECT_TRUE(net::LogContainsEndEvent( |
1026 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1214 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1027 EXPECT_TRUE(net::LogContainsBeginEvent( | 1215 EXPECT_TRUE(net::LogContainsBeginEvent( |
1028 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1216 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
1029 EXPECT_TRUE(net::LogContainsEndEvent( | 1217 EXPECT_TRUE(net::LogContainsEndEvent( |
1030 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1218 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
1031 EXPECT_TRUE(net::LogContainsBeginEvent( | 1219 EXPECT_TRUE(net::LogContainsBeginEvent( |
1032 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); | 1220 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1033 EXPECT_TRUE(net::LogContainsEndEvent( | 1221 EXPECT_TRUE(net::LogContainsEndEvent( |
1034 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); | 1222 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1223 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1224 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
| 1225 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1226 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
1035 | 1227 |
1036 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1228 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1037 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1229 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
1038 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1230 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1039 } | 1231 } |
1040 | 1232 |
1041 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { | 1233 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
1042 MockHttpCache cache; | 1234 MockHttpCache cache; |
1043 | 1235 |
1044 // force this transaction to read from the cache | 1236 // force this transaction to read from the cache |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1104 | 1296 |
1105 // Force this transaction to write to the cache again. | 1297 // Force this transaction to write to the cache again. |
1106 MockTransaction transaction(kSimpleGET_Transaction); | 1298 MockTransaction transaction(kSimpleGET_Transaction); |
1107 transaction.load_flags |= net::LOAD_BYPASS_CACHE; | 1299 transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
1108 | 1300 |
1109 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); | 1301 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
1110 | 1302 |
1111 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); | 1303 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
1112 | 1304 |
1113 // Check that the NetLog was filled as expected. | 1305 // Check that the NetLog was filled as expected. |
1114 EXPECT_EQ(6u, log.entries().size()); | 1306 EXPECT_EQ(8u, log.entries().size()); |
1115 EXPECT_TRUE(net::LogContainsBeginEvent( | 1307 EXPECT_TRUE(net::LogContainsBeginEvent( |
1116 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); | 1308 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1117 EXPECT_TRUE(net::LogContainsEndEvent( | 1309 EXPECT_TRUE(net::LogContainsEndEvent( |
1118 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); | 1310 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1119 EXPECT_TRUE(net::LogContainsBeginEvent( | 1311 EXPECT_TRUE(net::LogContainsBeginEvent( |
1120 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1312 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
1121 EXPECT_TRUE(net::LogContainsEndEvent( | 1313 EXPECT_TRUE(net::LogContainsEndEvent( |
1122 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1314 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
1123 EXPECT_TRUE(net::LogContainsBeginEvent( | 1315 EXPECT_TRUE(net::LogContainsBeginEvent( |
1124 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1316 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
1125 EXPECT_TRUE(net::LogContainsEndEvent( | 1317 EXPECT_TRUE(net::LogContainsEndEvent( |
1126 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1318 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1319 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1320 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1321 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1322 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
1127 | 1323 |
1128 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1324 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
1129 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1325 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1130 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1326 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1131 } | 1327 } |
1132 | 1328 |
1133 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { | 1329 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { |
1134 MockHttpCache cache; | 1330 MockHttpCache cache; |
1135 | 1331 |
1136 // write to the cache | 1332 // write to the cache |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1661 | 1857 |
1662 // Test that destroying the transaction while it is reading from the cache | 1858 // Test that destroying the transaction while it is reading from the cache |
1663 // works properly. | 1859 // works properly. |
1664 trans.reset(); | 1860 trans.reset(); |
1665 | 1861 |
1666 // Make sure we pump any pending events, which should include a call to | 1862 // Make sure we pump any pending events, which should include a call to |
1667 // HttpCache::Transaction::OnCacheReadCompleted. | 1863 // HttpCache::Transaction::OnCacheReadCompleted. |
1668 MessageLoop::current()->RunAllPending(); | 1864 MessageLoop::current()->RunAllPending(); |
1669 } | 1865 } |
1670 | 1866 |
| 1867 // Tests that we can delete the HttpCache and deal with queued transactions |
| 1868 // ("waiting for the backend" as opposed to Active or Doomed entries). |
| 1869 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { |
| 1870 scoped_ptr<MockHttpCache> cache(new MockHttpCache(new MockDiskCacheNoCB())); |
| 1871 |
| 1872 MockHttpRequest request(kSimpleGET_Transaction); |
| 1873 |
| 1874 std::vector<Context*> context_list; |
| 1875 const int kNumTransactions = 5; |
| 1876 |
| 1877 for (int i = 0; i < kNumTransactions; i++) { |
| 1878 context_list.push_back(new Context()); |
| 1879 Context* c = context_list[i]; |
| 1880 |
| 1881 c->result = cache->http_cache()->CreateTransaction(&c->trans); |
| 1882 EXPECT_EQ(net::OK, c->result); |
| 1883 |
| 1884 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
| 1885 } |
| 1886 |
| 1887 // The first request should be creating the disk cache entry and the others |
| 1888 // should be pending. |
| 1889 |
| 1890 EXPECT_EQ(0, cache->network_layer()->transaction_count()); |
| 1891 EXPECT_EQ(0, cache->disk_cache()->open_count()); |
| 1892 EXPECT_EQ(0, cache->disk_cache()->create_count()); |
| 1893 |
| 1894 cache.reset(); |
| 1895 |
| 1896 // There is not much to do with the transactions at this point... they are |
| 1897 // waiting for a callback that will not fire. |
| 1898 for (int i = 0; i < kNumTransactions; ++i) { |
| 1899 delete context_list[i]; |
| 1900 } |
| 1901 } |
| 1902 |
1671 TEST(HttpCache, TypicalGET_ConditionalRequest) { | 1903 TEST(HttpCache, TypicalGET_ConditionalRequest) { |
1672 MockHttpCache cache; | 1904 MockHttpCache cache; |
1673 | 1905 |
1674 // write to the cache | 1906 // write to the cache |
1675 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); | 1907 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); |
1676 | 1908 |
1677 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1909 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1678 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1910 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1679 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1911 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1680 | 1912 |
(...skipping 2197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3878 | 4110 |
3879 // This test ensures that a read that completes synchronously does not cause | 4111 // This test ensures that a read that completes synchronously does not cause |
3880 // any problems. | 4112 // any problems. |
3881 | 4113 |
3882 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 4114 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
3883 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | | 4115 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | |
3884 TEST_MODE_SYNC_CACHE_READ | | 4116 TEST_MODE_SYNC_CACHE_READ | |
3885 TEST_MODE_SYNC_CACHE_WRITE); | 4117 TEST_MODE_SYNC_CACHE_WRITE); |
3886 | 4118 |
3887 MockHttpRequest r1(transaction), | 4119 MockHttpRequest r1(transaction), |
3888 r2(transaction), | 4120 r2(transaction), |
3889 r3(transaction); | 4121 r3(transaction); |
3890 | 4122 |
3891 TestTransactionConsumer c1(cache.http_cache()), | 4123 TestTransactionConsumer c1(cache.http_cache()), |
3892 c2(cache.http_cache()), | 4124 c2(cache.http_cache()), |
3893 c3(cache.http_cache()); | 4125 c3(cache.http_cache()); |
3894 | 4126 |
3895 c1.Start(&r1, net::BoundNetLog()); | 4127 c1.Start(&r1, net::BoundNetLog()); |
3896 | 4128 |
3897 r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 4129 r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
3898 c2.Start(&r2, net::BoundNetLog()); | 4130 c2.Start(&r2, net::BoundNetLog()); |
3899 | 4131 |
3900 r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 4132 r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
3901 c3.Start(&r3, net::BoundNetLog()); | 4133 c3.Start(&r3, net::BoundNetLog()); |
3902 | 4134 |
3903 MessageLoop::current()->Run(); | 4135 MessageLoop::current()->Run(); |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4335 // Now return 200 when validating the entry so the metadata will be lost. | 4567 // Now return 200 when validating the entry so the metadata will be lost. |
4336 MockTransaction trans2(kTypicalGET_Transaction); | 4568 MockTransaction trans2(kTypicalGET_Transaction); |
4337 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4569 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
4338 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4570 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
4339 EXPECT_TRUE(response.metadata.get() == NULL); | 4571 EXPECT_TRUE(response.metadata.get() == NULL); |
4340 | 4572 |
4341 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4573 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
4342 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4574 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
4343 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4575 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4344 } | 4576 } |
OLD | NEW |