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_TRUE(response.metadata.get() == NULL); | |
6829 | |
6830 // Trivial call. | |
6831 cache.http_cache()->WriteMetadata(GURL("foo"), DEFAULT_PRIORITY, Time::Now(), | |
6832 NULL, 0); | |
asanka
2017/02/09 02:39:53
this doesn't do anything?
shivanisha
2017/02/09 16:46:41
Removed.
| |
6833 | |
6834 MockHttpRequest request(kSimpleGET_Transaction); | |
6835 Context c1; | |
6836 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk()); | |
6837 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(), | |
6838 NetLogWithSource())); | |
6839 | |
6840 cache.SimulateCacheLockTimeout(); | |
6841 | |
6842 // Write meta data to the same entry. | |
6843 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); | |
6844 memset(buf->data(), 0, buf->size()); | |
6845 base::strlcpy(buf->data(), "Hi there", buf->size()); | |
6846 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), | |
6847 DEFAULT_PRIORITY, response.response_time, | |
6848 buf.get(), buf->size()); | |
6849 | |
6850 // Release the buffer before the operation takes place. | |
6851 buf = NULL; | |
6852 | |
6853 // Makes sure we finish pending operations. | |
6854 base::RunLoop().RunUntilIdle(); | |
6855 | |
6856 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
6857 &response); | |
6858 | |
6859 // The writer transaction should fail due to cache lock timeout. | |
6860 ASSERT_TRUE(response.metadata.get() == NULL); | |
asanka
2017/02/09 02:39:53
nullptr or just ASSERT_FALSE(response.metadata.get
shivanisha
2017/02/09 16:46:41
Changed to ASSERT_FALSE.
| |
6861 } | |
6862 | |
6791 // Tests that we ignore VARY checks when writing metadata since the request | 6863 // Tests that we ignore VARY checks when writing metadata since the request |
6792 // headers for the WriteMetadata transaction are made up. | 6864 // headers for the WriteMetadata transaction are made up. |
6793 TEST(HttpCache, WriteMetadata_IgnoreVary) { | 6865 TEST(HttpCache, WriteMetadata_IgnoreVary) { |
6794 MockHttpCache cache; | 6866 MockHttpCache cache; |
6795 | 6867 |
6796 // Write to the cache | 6868 // Write to the cache |
6797 HttpResponseInfo response; | 6869 HttpResponseInfo response; |
6798 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 6870 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
6799 transaction.request_headers = "accept-encoding: gzip\r\n"; | 6871 transaction.request_headers = "accept-encoding: gzip\r\n"; |
6800 transaction.response_headers = | 6872 transaction.response_headers = |
(...skipping 1453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8254 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8326 RunTransactionTestWithResponseInfo(cache.http_cache(), |
8255 kTypicalGET_Transaction, &response_info); | 8327 kTypicalGET_Transaction, &response_info); |
8256 | 8328 |
8257 EXPECT_FALSE(response_info.was_cached); | 8329 EXPECT_FALSE(response_info.was_cached); |
8258 EXPECT_TRUE(response_info.network_accessed); | 8330 EXPECT_TRUE(response_info.network_accessed); |
8259 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8331 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
8260 response_info.cache_entry_status); | 8332 response_info.cache_entry_status); |
8261 } | 8333 } |
8262 | 8334 |
8263 } // namespace net | 8335 } // namespace net |
OLD | NEW |