| 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 1994 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2005 // We cannot call FinishCreation because the factory itself will go away with | 2005 // We cannot call FinishCreation because the factory itself will go away with |
| 2006 // the cache, so grab the callback and attempt to use it. | 2006 // the cache, so grab the callback and attempt to use it. |
| 2007 net::CompletionCallback* callback = factory->callback(); | 2007 net::CompletionCallback* callback = factory->callback(); |
| 2008 | 2008 |
| 2009 cache.reset(); | 2009 cache.reset(); |
| 2010 MessageLoop::current()->RunAllPending(); | 2010 MessageLoop::current()->RunAllPending(); |
| 2011 | 2011 |
| 2012 callback->Run(net::ERR_ABORTED); | 2012 callback->Run(net::ERR_ABORTED); |
| 2013 } | 2013 } |
| 2014 | 2014 |
| 2015 class DeleteCacheCompletionCallback : public TestCompletionCallback { |
| 2016 public: |
| 2017 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) |
| 2018 : cache_(cache) {} |
| 2019 |
| 2020 virtual void RunWithParams(const Tuple1<int>& params) { |
| 2021 delete cache_; |
| 2022 TestCompletionCallback::RunWithParams(params); |
| 2023 } |
| 2024 |
| 2025 private: |
| 2026 MockHttpCache* cache_; |
| 2027 }; |
| 2028 |
| 2029 // Tests that we can delete the cache while creating the backend, from within |
| 2030 // one of the callbacks. |
| 2031 TEST(HttpCache, DeleteCacheWaitingForBackend2) { |
| 2032 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 2033 MockHttpCache* cache = new MockHttpCache(factory); |
| 2034 |
| 2035 DeleteCacheCompletionCallback cb(cache); |
| 2036 disk_cache::Backend* backend; |
| 2037 int rv = cache->http_cache()->GetBackend(&backend, &cb); |
| 2038 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 2039 |
| 2040 // Now let's queue a regular transaction |
| 2041 MockHttpRequest request(kSimpleGET_Transaction); |
| 2042 |
| 2043 scoped_ptr<Context> c(new Context()); |
| 2044 c->result = cache->http_cache()->CreateTransaction(&c->trans); |
| 2045 EXPECT_EQ(net::OK, c->result); |
| 2046 |
| 2047 c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
| 2048 |
| 2049 // And another direct backend request. |
| 2050 TestCompletionCallback cb2; |
| 2051 rv = cache->http_cache()->GetBackend(&backend, &cb2); |
| 2052 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 2053 |
| 2054 // Just to make sure that everything is still pending. |
| 2055 MessageLoop::current()->RunAllPending(); |
| 2056 |
| 2057 // The request should be queued. |
| 2058 EXPECT_FALSE(c->callback.have_result()); |
| 2059 |
| 2060 // Generate the callback. |
| 2061 factory->FinishCreation(); |
| 2062 rv = cb.WaitForResult(); |
| 2063 |
| 2064 // The cache should be gone by now. |
| 2065 MessageLoop::current()->RunAllPending(); |
| 2066 EXPECT_EQ(net::OK, c->callback.GetResult(c->result)); |
| 2067 EXPECT_FALSE(cb2.have_result()); |
| 2068 } |
| 2069 |
| 2015 TEST(HttpCache, TypicalGET_ConditionalRequest) { | 2070 TEST(HttpCache, TypicalGET_ConditionalRequest) { |
| 2016 MockHttpCache cache; | 2071 MockHttpCache cache; |
| 2017 | 2072 |
| 2018 // write to the cache | 2073 // write to the cache |
| 2019 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); | 2074 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); |
| 2020 | 2075 |
| 2021 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2076 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2022 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2077 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2023 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2078 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2024 | 2079 |
| (...skipping 2732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4757 // Now return 200 when validating the entry so the metadata will be lost. | 4812 // Now return 200 when validating the entry so the metadata will be lost. |
| 4758 MockTransaction trans2(kTypicalGET_Transaction); | 4813 MockTransaction trans2(kTypicalGET_Transaction); |
| 4759 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4814 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4760 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4815 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 4761 EXPECT_TRUE(response.metadata.get() == NULL); | 4816 EXPECT_TRUE(response.metadata.get() == NULL); |
| 4762 | 4817 |
| 4763 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4818 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4764 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4819 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 4765 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4820 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4766 } | 4821 } |
| OLD | NEW |