Index: net/http/http_cache_writers_unittest.cc |
diff --git a/net/http/http_cache_writers_unittest.cc b/net/http/http_cache_writers_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..43e2b4f4813657978d0045497948736581315838 |
--- /dev/null |
+++ b/net/http/http_cache_writers_unittest.cc |
@@ -0,0 +1,217 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/http/http_cache_writers.h" |
+ |
+#include "base/run_loop.h" |
+#include "net/http/http_cache.h" |
+#include "net/http/http_cache_transaction.h" |
+#include "net/http/http_transaction.h" |
+#include "net/http/http_transaction_test_util.h" |
+#include "net/http/mock_http_cache.h" |
+#include "net/test/gtest_util.h" |
+ |
+using net::test::IsError; |
+using net::test::IsOk; |
+ |
+namespace net { |
+ |
+class HttpCache::WritersTest { |
+ public: |
+ WritersTest() {} |
+ |
+ // Since ActiveEntry and Transaction are private classes of HttpCache, |
+ // accessing those only in this class instead of in tests. |
+ |
+ void CreateWriters(const std::string& url) { |
+ HttpCache* cache = cache_.http_cache(); |
+ ActiveEntry* entry = cache->FindActiveEntry(url); |
+ writers_ = base::MakeUnique<Writers>(cache, entry); |
+ } |
+ |
+ void CreateNetworkTransaction(std::unique_ptr<HttpTransaction>* transaction) { |
+ MockNetworkLayer* network_layer = cache_.network_layer(); |
+ network_layer->CreateTransaction(DEFAULT_PRIORITY, transaction); |
+ } |
+ |
+ void CreateAddTransactionToWriters(MockTransaction transaction_info) { |
+ MockHttpRequest request(transaction_info); |
+ std::unique_ptr<HttpTransaction> transaction; |
+ int rv = cache_.CreateTransaction(&transaction); |
+ DCHECK_EQ(rv, OK); |
+ DCHECK(transaction.get()); |
+ |
+ // Create an ActiveEntry. |
+ TestCompletionCallback callback; |
+ rv = transaction->Start(&request, callback.callback(), NetLogWithSource()); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ CreateWriters(kSimpleGET_Transaction.url); |
+ |
+ // Create and Start a mock network transaction. |
+ std::unique_ptr<HttpTransaction> network_transaction; |
+ CreateNetworkTransaction(&network_transaction); |
+ network_transaction->Start(&request, callback.callback(), |
+ NetLogWithSource()); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ DCHECK(writers_->IsEmpty()); |
+ writers_->AddTransaction((Transaction*)(transaction.get()), |
+ std::move(network_transaction)); |
+ transactions_.insert(std::move(transaction)); |
+ } |
+ |
+ void AddTransactionToExistingWriters() { |
+ std::unique_ptr<HttpTransaction> transaction; |
+ int rv = cache_.CreateTransaction(&transaction); |
+ DCHECK_EQ(rv, OK); |
+ DCHECK(transaction.get()); |
+ |
+ DCHECK(writers_); |
+ writers_->AddTransaction((Transaction*)(transaction.get()), nullptr); |
+ transactions_.insert(std::move(transaction)); |
+ } |
+ |
+ int Read(std::string* result) { |
+ Transaction* transaction = (Transaction*)(transactions_.begin()->get()); |
+ TestCompletionCallback callback; |
+ |
+ std::string content; |
+ int rv; |
+ do { |
+ scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
+ rv = writers_->Read(buf.get(), 256, callback.callback(), transaction); |
+ if (rv == ERR_IO_PENDING) { |
+ rv = callback.WaitForResult(); |
+ base::RunLoop().RunUntilIdle(); |
+ } |
+ |
+ if (rv > 0) |
+ content.append(buf->data(), rv); |
+ else if (rv < 0) |
+ return rv; |
+ } while (rv > 0); |
+ |
+ result->swap(content); |
+ return OK; |
+ } |
+ |
+ bool StopCaching() { |
+ Transaction* transaction = (Transaction*)(transactions_.begin()->get()); |
+ return writers_->StopCaching(transaction); |
+ } |
+ |
+ void RemoveTransaction() { |
+ Transaction* transaction = (Transaction*)(transactions_.begin()->get()); |
+ writers_->RemoveTransaction(transaction); |
+ } |
+ |
+ MockHttpCache cache_; |
+ std::unique_ptr<Writers> writers_; |
+ std::unordered_set<std::unique_ptr<HttpTransaction>> transactions_; |
+}; |
+ |
+// Tests successful addition of a transaction. |
+TEST(HttpCacheWriters, AddTransaction) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+} |
+ |
+// Tests successful addition of multiple transaction. |
+TEST(HttpCacheWriters, AddManyTransactions) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+ |
+ for (int i = 0; i < 5; i++) |
+ writersTest.AddTransactionToExistingWriters(); |
+ |
+ EXPECT_EQ(writersTest.writers_->CountTransactionsForTesting(), 6); |
+} |
+ |
+// Tests that CanAddWriters should return false if it is exclusive writing. |
+TEST(HttpCacheWriters, AddTransactionsExclusive) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+ |
+ writersTest.writers_->SetExclusive(); |
+ EXPECT_FALSE(writersTest.writers_->CanAddWriters()); |
+} |
+ |
+// Tests StopCaching should not stop caching if there are multiple writers. |
+TEST(HttpCacheWriters, StopCachingMultipleWriters) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writersTest.writers_->CanAddWriters()); |
+ writersTest.AddTransactionToExistingWriters(); |
+ |
+ EXPECT_FALSE(writersTest.StopCaching()); |
+ EXPECT_TRUE(writersTest.writers_->CanAddWriters()); |
+} |
+ |
+// Tests StopCaching should stop caching if there is a single writer. |
+TEST(HttpCacheWriters, StopCaching) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writersTest.StopCaching()); |
+ EXPECT_FALSE(writersTest.writers_->CanAddWriters()); |
+} |
+ |
+// Tests removing of an idle transaction. |
+TEST(HttpCacheWriters, RemoveIdleTransaction) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+ |
+ writersTest.RemoveTransaction(); |
+ EXPECT_TRUE(writersTest.writers_->IsEmpty()); |
+} |
+ |
+// Tests that Read is successful. |
+TEST(HttpCacheWriters, Read) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+ |
+ std::string content; |
+ int rv = writersTest.Read(&content); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ EXPECT_EQ(expected, content); |
+} |
+ |
+// Tests truncating the entry via Writers. |
+TEST(HttpCacheWriters, TruncateEntry) { |
+ HttpCache::WritersTest writersTest; |
+ |
+ writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction); |
+ EXPECT_FALSE(writersTest.writers_->IsEmpty()); |
+ |
+ std::string content; |
+ int rv = writersTest.Read(&content); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ EXPECT_EQ(expected, content); |
+ |
+ writersTest.writers_->TruncateEntry(); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(writersTest.writers_->IsTruncatedForTesting()); |
+} |
+ |
+} // namespace net |