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 1730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1741 | 1741 |
1742 for (int i = 0; i < kNumTransactions; ++i) { | 1742 for (int i = 0; i < kNumTransactions; ++i) { |
1743 delete context_list[i]; | 1743 delete context_list[i]; |
1744 } | 1744 } |
1745 } | 1745 } |
1746 | 1746 |
1747 // Tests that a (simulated) timeout allows transactions waiting on the cache | 1747 // Tests that a (simulated) timeout allows transactions waiting on the cache |
1748 // lock to continue. | 1748 // lock to continue. |
1749 TEST(HttpCache, SimpleGET_WriterTimeout) { | 1749 TEST(HttpCache, SimpleGET_WriterTimeout) { |
1750 MockHttpCache cache; | 1750 MockHttpCache cache; |
1751 cache.BypassCacheLock(); | 1751 cache.SimulateCacheLockTimeout(); |
1752 | 1752 |
1753 MockHttpRequest request(kSimpleGET_Transaction); | 1753 MockHttpRequest request(kSimpleGET_Transaction); |
1754 Context c1, c2; | 1754 Context c1, c2; |
1755 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk()); | 1755 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk()); |
1756 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(), | 1756 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(), |
1757 NetLogWithSource())); | 1757 NetLogWithSource())); |
1758 ASSERT_THAT(cache.CreateTransaction(&c2.trans), IsOk()); | 1758 ASSERT_THAT(cache.CreateTransaction(&c2.trans), IsOk()); |
1759 ASSERT_EQ(ERR_IO_PENDING, c2.trans->Start(&request, c2.callback.callback(), | 1759 ASSERT_EQ(ERR_IO_PENDING, c2.trans->Start(&request, c2.callback.callback(), |
1760 NetLogWithSource())); | 1760 NetLogWithSource())); |
1761 | 1761 |
1762 // The second request is queued after the first one. | 1762 // The second request is queued after the first one. |
1763 | 1763 |
1764 c2.callback.WaitForResult(); | 1764 c2.callback.WaitForResult(); |
1765 ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction); | 1765 ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction); |
1766 | 1766 |
1767 // Complete the first transaction. | 1767 // Complete the first transaction. |
1768 c1.callback.WaitForResult(); | 1768 c1.callback.WaitForResult(); |
1769 ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction); | 1769 ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction); |
1770 } | 1770 } |
1771 | 1771 |
| 1772 // Tests that a (simulated) timeout allows transactions waiting on the cache |
| 1773 // lock to continue but read only transactions to error out. |
| 1774 TEST(HttpCache, SimpleGET_WriterTimeoutReadOnlyError) { |
| 1775 MockHttpCache cache; |
| 1776 |
| 1777 // Simulate timeout. |
| 1778 cache.SimulateCacheLockTimeout(); |
| 1779 |
| 1780 MockHttpRequest request(kSimpleGET_Transaction); |
| 1781 Context c1, c2; |
| 1782 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk()); |
| 1783 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(), |
| 1784 NetLogWithSource())); |
| 1785 |
| 1786 request.load_flags = LOAD_ONLY_FROM_CACHE; |
| 1787 ASSERT_THAT(cache.CreateTransaction(&c2.trans), IsOk()); |
| 1788 ASSERT_EQ(ERR_IO_PENDING, c2.trans->Start(&request, c2.callback.callback(), |
| 1789 NetLogWithSource())); |
| 1790 |
| 1791 // The second request is queued after the first one. |
| 1792 int res = c2.callback.WaitForResult(); |
| 1793 ASSERT_EQ(ERR_CACHE_MISS, res); |
| 1794 |
| 1795 // Complete the first transaction. |
| 1796 c1.callback.WaitForResult(); |
| 1797 ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction); |
| 1798 } |
| 1799 |
1772 TEST(HttpCache, SimpleGET_AbandonedCacheRead) { | 1800 TEST(HttpCache, SimpleGET_AbandonedCacheRead) { |
1773 MockHttpCache cache; | 1801 MockHttpCache cache; |
1774 | 1802 |
1775 // write to the cache | 1803 // write to the cache |
1776 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1804 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
1777 | 1805 |
1778 MockHttpRequest request(kSimpleGET_Transaction); | 1806 MockHttpRequest request(kSimpleGET_Transaction); |
1779 TestCompletionCallback callback; | 1807 TestCompletionCallback callback; |
1780 | 1808 |
1781 std::unique_ptr<HttpTransaction> trans; | 1809 std::unique_ptr<HttpTransaction> trans; |
(...skipping 4999 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6781 | 6809 |
6782 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 6810 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6783 &response); | 6811 &response); |
6784 EXPECT_TRUE(response.metadata.get() == NULL); | 6812 EXPECT_TRUE(response.metadata.get() == NULL); |
6785 | 6813 |
6786 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6814 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6787 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 6815 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
6788 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6816 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6789 } | 6817 } |
6790 | 6818 |
| 6819 // Tests that if a metadata writer transaction hits cache lock timeout, it will |
| 6820 // error out. |
| 6821 TEST(HttpCache, WriteMetadata_CacheLockTimeout) { |
| 6822 MockHttpCache cache; |
| 6823 |
| 6824 // Write to the cache |
| 6825 HttpResponseInfo response; |
| 6826 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 6827 &response); |
| 6828 EXPECT_FALSE(response.metadata.get()); |
| 6829 |
| 6830 MockHttpRequest request(kSimpleGET_Transaction); |
| 6831 Context c1; |
| 6832 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk()); |
| 6833 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(), |
| 6834 NetLogWithSource())); |
| 6835 |
| 6836 cache.SimulateCacheLockTimeout(); |
| 6837 |
| 6838 // Write meta data to the same entry. |
| 6839 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); |
| 6840 memset(buf->data(), 0, buf->size()); |
| 6841 base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 6842 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), |
| 6843 DEFAULT_PRIORITY, response.response_time, |
| 6844 buf.get(), buf->size()); |
| 6845 |
| 6846 // Release the buffer before the operation takes place. |
| 6847 buf = NULL; |
| 6848 |
| 6849 // Makes sure we finish pending operations. |
| 6850 base::RunLoop().RunUntilIdle(); |
| 6851 |
| 6852 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 6853 &response); |
| 6854 |
| 6855 // The writer transaction should fail due to cache lock timeout. |
| 6856 ASSERT_FALSE(response.metadata.get()); |
| 6857 } |
| 6858 |
6791 // Tests that we ignore VARY checks when writing metadata since the request | 6859 // Tests that we ignore VARY checks when writing metadata since the request |
6792 // headers for the WriteMetadata transaction are made up. | 6860 // headers for the WriteMetadata transaction are made up. |
6793 TEST(HttpCache, WriteMetadata_IgnoreVary) { | 6861 TEST(HttpCache, WriteMetadata_IgnoreVary) { |
6794 MockHttpCache cache; | 6862 MockHttpCache cache; |
6795 | 6863 |
6796 // Write to the cache | 6864 // Write to the cache |
6797 HttpResponseInfo response; | 6865 HttpResponseInfo response; |
6798 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 6866 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
6799 transaction.request_headers = "accept-encoding: gzip\r\n"; | 6867 transaction.request_headers = "accept-encoding: gzip\r\n"; |
6800 transaction.response_headers = | 6868 transaction.response_headers = |
(...skipping 1453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8254 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8322 RunTransactionTestWithResponseInfo(cache.http_cache(), |
8255 kTypicalGET_Transaction, &response_info); | 8323 kTypicalGET_Transaction, &response_info); |
8256 | 8324 |
8257 EXPECT_FALSE(response_info.was_cached); | 8325 EXPECT_FALSE(response_info.was_cached); |
8258 EXPECT_TRUE(response_info.network_accessed); | 8326 EXPECT_TRUE(response_info.network_accessed); |
8259 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8327 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
8260 response_info.cache_entry_status); | 8328 response_info.cache_entry_status); |
8261 } | 8329 } |
8262 | 8330 |
8263 } // namespace net | 8331 } // namespace net |
OLD | NEW |