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..c1c668d672a147c99a22effbfbfc4ae0a1e8eb01 |
--- /dev/null |
+++ b/net/http/http_cache_writers_unittest.cc |
@@ -0,0 +1,695 @@ |
+// 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 <memory> |
+#include <string> |
+#include <vector> |
+ |
+#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 WritersTest : public testing::Test { |
+ public: |
+ WritersTest() : disk_entry_(nullptr), request_(kSimpleGET_Transaction) {} |
+ |
+ ~WritersTest() override { |
+ if (disk_entry_) |
+ disk_entry_->Close(); |
+ } |
+ |
+ void CreateWriters(const std::string& url) { |
+ cache_.CreateBackendEntry(kSimpleGET_Transaction.url, &disk_entry_, |
+ nullptr); |
+ writers_ = base::MakeUnique<HttpCache::Writers>(disk_entry_); |
+ } |
+ |
+ std::unique_ptr<HttpTransaction> CreateNetworkTransaction() { |
+ std::unique_ptr<HttpTransaction> transaction; |
+ MockNetworkLayer* network_layer = cache_.network_layer(); |
+ network_layer->CreateTransaction(DEFAULT_PRIORITY, &transaction); |
+ return transaction; |
+ } |
+ |
+ void CreateWritersAddTransaction( |
+ bool is_exclusive = false, |
+ net::RequestPriority priority = DEFAULT_PRIORITY) { |
Randy Smith (Not in Mondays)
2017/06/29 00:53:49
The google style guide discourages default argumen
shivanisha
2017/07/05 17:44:24
Created CreateWritersAddTransactionPriority which
|
+ std::unique_ptr<HttpTransaction> transaction; |
+ int rv = cache_.CreateTransaction(&transaction); |
+ EXPECT_EQ(rv, OK); |
+ EXPECT_TRUE(transaction); |
+ |
+ TestCompletionCallback callback; |
+ |
+ CreateWriters(kSimpleGET_Transaction.url); |
+ |
+ // Create and Start a mock network transaction. |
+ std::unique_ptr<HttpTransaction> network_transaction; |
+ network_transaction = CreateNetworkTransaction(); |
+ network_transaction->Start(&request_, callback.callback(), |
+ NetLogWithSource()); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ EXPECT_TRUE(writers_->IsEmpty()); |
+ writers_->AddTransaction( |
+ static_cast<HttpCache::Transaction*>(transaction.get()), |
+ std::move(network_transaction), is_exclusive); |
+ transaction->SetPriority(priority); |
+ EXPECT_TRUE(writers_->HasTransaction( |
+ static_cast<HttpCache::Transaction*>(transaction.get()))); |
+ transactions_.push_back(std::move(transaction)); |
+ } |
+ |
+ void AddTransactionToExistingWriters() { |
+ std::unique_ptr<HttpTransaction> transaction; |
+ int rv = cache_.CreateTransaction(&transaction); |
+ EXPECT_EQ(rv, OK); |
+ EXPECT_TRUE(transaction); |
+ |
+ EXPECT_TRUE(writers_); |
+ writers_->AddTransaction( |
+ static_cast<HttpCache::Transaction*>(transaction.get()), nullptr, |
+ false); |
+ transactions_.push_back(std::move(transaction)); |
+ } |
+ |
+ int Read(std::string* result) { |
+ EXPECT_TRUE(transactions_.size() >= (size_t)1); |
+ HttpCache::Transaction* transaction = |
+ static_cast<HttpCache::Transaction*>(transactions_.begin()->get()); |
+ TestCompletionCallback callback; |
+ |
+ std::string content; |
+ int rv = 0; |
+ 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; |
+ } |
+ |
+ int ReadAll(std::vector<std::string>* results) { |
+ int rv = 0; |
+ do { |
+ std::vector<scoped_refptr<IOBuffer>> bufs; |
+ std::vector<TestCompletionCallback> callbacks(results->size()); |
+ |
+ // Multiple transactions should be able to read. |
+ for (size_t i = 0; i < transactions_.size(); i++) { |
+ bufs.push_back(new IOBuffer(256)); |
+ rv = writers_->Read( |
+ bufs[i].get(), 256, callbacks[i].callback(), |
+ static_cast<HttpCache::Transaction*>(transactions_[i].get())); |
+ EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. |
+ } |
+ |
+ if (rv == ERR_IO_PENDING) { |
Randy Smith (Not in Mondays)
2017/06/29 00:53:50
I don't think this test makes sense. If any of th
shivanisha
2017/07/05 17:44:25
Since the EXPECT_EQ above asserts all rvs to be ER
|
+ int prev_rv = callbacks[0].WaitForResult(); |
+ for (size_t i = 1; i < callbacks.size(); i++) { |
Randy Smith (Not in Mondays)
2017/06/29 00:53:49
nit: I think this fails if *results is of length 1
shivanisha
2017/07/05 17:44:24
Changed the logic to have rv = prev_rv before the
|
+ rv = callbacks[i].WaitForResult(); |
+ EXPECT_EQ(rv, prev_rv); |
+ prev_rv = rv; |
+ } |
+ } |
+ |
+ if (rv > 0) { |
+ for (size_t i = 0; i < results->size(); i++) |
+ results->at(i).append(bufs[i]->data(), rv); |
+ } else if (rv <= 0) { |
+ return rv; |
+ } |
+ } while (rv > 0); |
+ |
+ return OK; |
+ } |
+ |
+ void ReadVerifyTwoDifferentBufferSizes(std::vector<std::string>* results) { |
+ EXPECT_EQ(2u, results->size()); |
+ |
+ // Check only the 1st Read and not the complete response because the smaller |
+ // buffer transaction will need to read the remaining response from the |
+ // cache which will be tested when integrated with HttpCache::Transaction |
+ // layer. |
+ |
+ int rv = 0; |
+ |
+ std::vector<scoped_refptr<IOBuffer>> bufs; |
+ std::vector<TestCompletionCallback> callbacks(results->size()); |
+ |
+ int larger_buffer_size = 20; |
+ int smaller_buffer_size = 10; |
Randy Smith (Not in Mondays)
2017/06/29 00:53:49
Reasonable to also test the case where the smaller
shivanisha
2017/07/05 17:44:24
Added, also changed this function to work in both
|
+ bufs.push_back(new IOBuffer(larger_buffer_size)); |
+ bufs.push_back(new IOBuffer(smaller_buffer_size)); |
+ |
+ // Multiple transactions should be able to read with different sized |
+ // buffers. |
+ for (size_t i = 0; i < transactions_.size(); i++) { |
+ int buffer_size = i == 0 ? larger_buffer_size : smaller_buffer_size; |
+ rv = writers_->Read( |
+ bufs[i].get(), buffer_size, callbacks[i].callback(), |
+ static_cast<HttpCache::Transaction*>(transactions_[i].get())); |
+ EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. |
+ } |
+ |
+ for (size_t i = 0; i < callbacks.size(); i++) { |
+ int buffer_size = i == 0 ? larger_buffer_size : smaller_buffer_size; |
+ rv = callbacks[i].WaitForResult(); |
+ EXPECT_EQ(rv, buffer_size); |
+ } |
+ |
+ for (size_t i = 0; i < results->size(); i++) { |
+ int buffer_size = i == 0 ? larger_buffer_size : smaller_buffer_size; |
+ results->at(i).append(bufs[i]->data(), buffer_size); |
+ } |
+ |
+ EXPECT_EQ(results->at(0).substr(0, smaller_buffer_size), results->at(1)); |
+ } |
+ |
+ int ReadAllDeleteActiveTransaction(std::vector<std::string>* results) { |
+ int rv = 0; |
+ bool first_iter = true; |
+ do { |
+ std::vector<scoped_refptr<IOBuffer>> bufs; |
+ std::vector<TestCompletionCallback> callbacks(results->size()); |
+ |
+ // Multiple transactions should be able to read. |
+ for (size_t i = 0; i < transactions_.size(); i++) { |
+ bufs.push_back(new IOBuffer(256)); |
+ if (!first_iter && i == 0) |
+ continue; |
+ |
+ rv = writers_->Read( |
+ bufs[i].get(), 256, callbacks[i].callback(), |
+ static_cast<HttpCache::Transaction*>(transactions_[i].get())); |
+ EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. |
+ } |
+ |
+ if (rv == ERR_IO_PENDING) { |
Randy Smith (Not in Mondays)
2017/06/29 00:53:49
Same comment as above--I don't think this makes se
shivanisha
2017/07/05 17:44:24
Changed.
|
+ if (first_iter) { |
+ // Delete active transaction. |
+ RemoveFirstTransaction(); |
+ } |
+ int prev_rv = callbacks[1].WaitForResult(); |
+ for (size_t i = 2; i < callbacks.size(); i++) { |
Randy Smith (Not in Mondays)
2017/06/29 00:53:49
Same comment as above--I think this fails (doesn't
shivanisha
2017/07/05 17:44:24
Changed
|
+ rv = callbacks[i].WaitForResult(); |
+ EXPECT_EQ(rv, prev_rv); |
+ prev_rv = rv; |
+ } |
+ } |
+ |
+ if (rv > 0) { |
+ for (size_t i = 0; i < results->size(); i++) |
+ results->at(i).append(bufs[i]->data(), rv); |
+ } |
+ first_iter = false; |
+ } while (rv > 0); |
+ |
+ return rv; |
+ } |
+ |
+ int ReadAllDeleteWaitingTransaction(size_t waiting_index, |
+ std::vector<std::string>* results) { |
+ int rv = 0; |
+ bool first_iter = true; |
+ do { |
+ std::vector<scoped_refptr<IOBuffer>> bufs; |
+ std::vector<TestCompletionCallback> callbacks(results->size()); |
+ |
+ // Multiple transactions should be able to read. |
+ for (size_t i = 0; i < transactions_.size(); i++) { |
+ bufs.push_back(new IOBuffer(256)); |
+ if (!first_iter && i == waiting_index) |
+ continue; |
+ |
+ rv = writers_->Read( |
+ bufs[i].get(), 256, callbacks[i].callback(), |
+ static_cast<HttpCache::Transaction*>(transactions_[i].get())); |
+ EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. |
+ } |
+ |
+ if (rv == ERR_IO_PENDING) { |
+ if (first_iter) { |
+ // Delete waiting transaction. |
+ writers_->RemoveTransaction(static_cast<HttpCache::Transaction*>( |
+ transactions_.at(waiting_index).get())); |
+ } |
+ int prev_rv = callbacks[0].WaitForResult(); |
+ for (size_t i = 1; i < callbacks.size(); i++) { |
+ if (i == waiting_index) |
+ continue; |
+ rv = callbacks[i].WaitForResult(); |
+ EXPECT_EQ(rv, prev_rv); |
+ prev_rv = rv; |
+ } |
+ } |
+ |
+ if (rv > 0) { |
+ for (size_t i = 0; i < results->size(); i++) { |
+ if (i == waiting_index) |
+ continue; |
+ results->at(i).append(bufs[i]->data(), rv); |
+ } |
+ } |
+ first_iter = false; |
+ } while (rv > 0); |
+ |
+ return rv; |
+ } |
+ |
+ int ReadAllDeleteIdleTransaction(size_t idle_index, |
+ std::vector<std::string>* results) { |
+ int rv = 0; |
+ bool first_iter = true; |
+ do { |
+ std::vector<scoped_refptr<IOBuffer>> bufs; |
+ std::vector<TestCompletionCallback> callbacks(results->size()); |
+ |
+ // Multiple transactions should be able to read. |
+ for (size_t i = 0; i < transactions_.size(); i++) { |
+ bufs.push_back(new IOBuffer(256)); |
+ if (i == idle_index) |
+ continue; |
+ |
+ rv = writers_->Read( |
+ bufs[i].get(), 256, callbacks[i].callback(), |
+ static_cast<HttpCache::Transaction*>(transactions_[i].get())); |
+ EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. |
+ } |
+ |
+ if (rv == ERR_IO_PENDING) { |
+ if (first_iter) { |
+ // Delete idle transaction. |
+ writers_->RemoveTransaction(static_cast<HttpCache::Transaction*>( |
+ transactions_.at(idle_index).get())); |
+ } |
+ int prev_rv = callbacks[0].WaitForResult(); |
+ for (size_t i = 1; i < callbacks.size(); i++) { |
+ if (i == idle_index) |
+ continue; |
+ rv = callbacks[i].WaitForResult(); |
+ EXPECT_EQ(rv, prev_rv); |
+ prev_rv = rv; |
+ } |
+ } |
+ |
+ if (rv > 0) { |
+ for (size_t i = 0; i < results->size(); i++) { |
+ if (i == idle_index) |
+ continue; |
+ results->at(i).append(bufs[i]->data(), rv); |
+ } |
+ } |
+ first_iter = false; |
+ } while (rv > 0); |
+ |
+ return rv; |
+ } |
+ |
+ int ReadCacheWriteFailure(std::vector<std::string>* results) { |
+ int rv = 0; |
+ int active_transaction_rv = 0; |
+ bool first_iter = true; |
+ do { |
+ std::vector<scoped_refptr<IOBuffer>> bufs; |
+ std::vector<TestCompletionCallback> callbacks(results->size()); |
+ |
+ // Fail the request. |
+ cache_.disk_cache()->set_soft_failures(true); |
+ |
+ // We have to open the entry again to propagate the failure flag. |
+ disk_cache::Entry* en; |
+ cache_.OpenBackendEntry(kSimpleGET_Transaction.url, &en); |
+ en->Close(); |
+ |
+ for (size_t i = 0; i < transactions_.size(); i++) { |
+ bufs.push_back(new IOBuffer(30)); |
+ |
+ if (!first_iter && i > 0) |
+ break; |
+ rv = writers_->Read( |
+ bufs[i].get(), 30, callbacks[i].callback(), |
+ static_cast<HttpCache::Transaction*>(transactions_[i].get())); |
+ EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. |
+ } |
+ |
+ if (rv == ERR_IO_PENDING) { |
+ for (size_t i = 0; i < callbacks.size(); i++) { |
+ // Only active transaction should succeed. |
+ if (i == 0) { |
+ active_transaction_rv = callbacks[i].WaitForResult(); |
+ EXPECT_TRUE(active_transaction_rv >= 0); |
+ results->at(0).append(bufs[i]->data(), active_transaction_rv); |
+ } else if (first_iter) { |
+ rv = callbacks[i].WaitForResult(); |
+ EXPECT_EQ(ERR_CACHE_WRITE_FAILURE, rv); |
+ } |
+ } |
+ } |
+ |
+ first_iter = false; |
+ } while (active_transaction_rv > 0); |
+ |
+ return active_transaction_rv; |
+ } |
+ |
+ int ReadNetworkFailure(std::vector<std::string>* results, Error error) { |
+ int rv = 0; |
+ std::vector<scoped_refptr<IOBuffer>> bufs; |
+ std::vector<TestCompletionCallback> callbacks(results->size()); |
+ |
+ for (size_t i = 0; i < transactions_.size(); i++) { |
+ bufs.push_back(new IOBuffer(30)); |
+ |
+ rv = writers_->Read( |
+ bufs[i].get(), 30, callbacks[i].callback(), |
+ static_cast<HttpCache::Transaction*>(transactions_[i].get())); |
+ EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. |
+ } |
+ |
+ if (rv == ERR_IO_PENDING) { |
+ for (auto& callback : callbacks) { |
+ rv = callback.WaitForResult(); |
+ EXPECT_EQ(error, rv); |
+ } |
+ } |
+ |
+ return error; |
+ } |
+ |
+ bool StopCaching() { |
+ HttpCache::Transaction* transaction = |
+ static_cast<HttpCache::Transaction*>(transactions_.begin()->get()); |
+ EXPECT_TRUE(transaction); |
+ return writers_->StopCaching(transaction); |
+ } |
+ |
+ void RemoveFirstTransaction() { |
+ HttpCache::Transaction* transaction = |
+ static_cast<HttpCache::Transaction*>(transactions_.begin()->get()); |
+ EXPECT_TRUE(transaction); |
+ writers_->RemoveTransaction(transaction); |
+ } |
+ |
+ void UpdateAndVerifyPriority(RequestPriority priority) { |
+ writers_->UpdatePriority(); |
+ EXPECT_EQ(writers_->priority_, priority); |
+ } |
+ |
+ MockHttpCache cache_; |
+ std::unique_ptr<HttpCache::Writers> writers_; |
+ disk_cache::Entry* disk_entry_; |
+ |
+ // Should be before transactions_ since it is accessed in the network |
+ // transaction's destructor. |
+ MockHttpRequest request_; |
+ |
+ std::vector<std::unique_ptr<HttpTransaction>> transactions_; |
+}; |
+ |
+// Tests successful addition of a transaction. |
+TEST_F(WritersTest, AddTransaction) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+} |
+ |
+// Tests successful addition of multiple transaction. |
+TEST_F(WritersTest, AddManyTransactions) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ for (int i = 0; i < 5; i++) |
+ AddTransactionToExistingWriters(); |
+ |
+ EXPECT_EQ(writers_->CountTransactionsForTesting(), 6); |
+} |
+ |
+// Tests that CanAddWriters should return false if it is exclusive writing. |
+TEST_F(WritersTest, AddTransactionsExclusive) { |
+ CreateWritersAddTransaction(true /* is_exclusive */); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_FALSE(writers_->CanAddWriters()); |
+} |
+ |
+// Tests StopCaching should not stop caching if there are multiple writers. |
+TEST_F(WritersTest, StopCachingMultipleWriters) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ |
+ EXPECT_FALSE(StopCaching()); |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+} |
+ |
+// Tests StopCaching should stop caching if there is a single writer. |
+TEST_F(WritersTest, StopCaching) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(StopCaching()); |
+ EXPECT_FALSE(writers_->CanAddWriters()); |
+} |
+ |
+// Tests removing of an idle transaction and change in priority. |
+TEST_F(WritersTest, RemoveIdleTransaction) { |
+ CreateWritersAddTransaction(false, HIGHEST); |
+ UpdateAndVerifyPriority(HIGHEST); |
+ |
+ AddTransactionToExistingWriters(); |
+ UpdateAndVerifyPriority(HIGHEST); |
+ |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ EXPECT_EQ(2, writers_->CountTransactionsForTesting()); |
+ |
+ RemoveFirstTransaction(); |
+ EXPECT_EQ(1, writers_->CountTransactionsForTesting()); |
+ |
+ UpdateAndVerifyPriority(DEFAULT_PRIORITY); |
+} |
+ |
+// Tests that Read is successful. |
+TEST_F(WritersTest, Read) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ std::string content; |
+ int rv = Read(&content); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ EXPECT_EQ(expected, content); |
+} |
+ |
+// Tests that multiple transactions can read the same data simultaneously. |
+TEST_F(WritersTest, ReadMultiple) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ |
+ std::vector<std::string> contents(2); |
+ int rv = ReadAll(&contents); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ |
+ for (auto content : contents) |
+ EXPECT_EQ(expected, content); |
+} |
+ |
+// Tests that multiple transactions can read the same data simultaneously. |
+TEST_F(WritersTest, ReadMultipleDifferentBufferSizes) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ |
+ std::vector<std::string> contents(2); |
+ ReadVerifyTwoDifferentBufferSizes(&contents); |
+} |
+ |
+// Tests that ongoing Read completes even when active transaction is deleted |
+// mid-read. Any transactions waiting should be able to get the read buffer. |
+TEST_F(WritersTest, ReadMultipleDeleteActiveTransaction) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ AddTransactionToExistingWriters(); |
+ |
+ std::vector<std::string> contents(3); |
+ int rv = ReadAllDeleteActiveTransaction(&contents); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ |
+ for (auto content : contents) |
+ EXPECT_EQ(expected, content); |
+} |
+ |
+// Tests that removing a waiting for read transaction does not impact other |
+// transactions. |
+TEST_F(WritersTest, ReadMultipleDeleteWaitingTransaction) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ AddTransactionToExistingWriters(); |
+ AddTransactionToExistingWriters(); |
+ |
+ std::vector<std::string> contents(4); |
+ size_t waiting_index = 1; |
+ int rv = ReadAllDeleteWaitingTransaction(waiting_index, &contents); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ |
+ size_t i = 0; |
+ for (auto content : contents) { |
+ if (i == waiting_index) |
+ EXPECT_EQ("", content); |
+ else |
+ EXPECT_EQ(expected, content); |
+ i++; |
+ } |
+} |
+ |
+// Tests that removing an idle transaction does not impact other transactions. |
+TEST_F(WritersTest, ReadMultipleDeleteIdleTransaction) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ AddTransactionToExistingWriters(); |
+ |
+ std::vector<std::string> contents(3); |
+ size_t idle_index = 1; |
+ int rv = ReadAllDeleteIdleTransaction(idle_index, &contents); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ |
+ size_t i = 0; |
+ for (auto content : contents) { |
+ if (i == idle_index) { |
+ i++; |
+ continue; |
+ } |
+ EXPECT_EQ(expected, content); |
+ i++; |
+ } |
+} |
+ |
+// Tests cache write failure. |
+TEST_F(WritersTest, ReadMultipleCacheWriteFailed) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ AddTransactionToExistingWriters(); |
+ |
+ std::vector<std::string> contents(3); |
+ int rv = ReadCacheWriteFailure(&contents); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ |
+ // Only active_transaction_ should succeed. |
+ EXPECT_EQ(expected, contents.at(0)); |
+ |
+ // No new transactions should now be added. |
+ EXPECT_FALSE(writers_->CanAddWriters()); |
+} |
+ |
+// Tests that network read failure fails all transactions: active, waiting and |
+// idle. |
+TEST_F(WritersTest, ReadMultipleNetworkReadFailed) { |
+ ScopedMockTransaction transaction(kSimpleGET_Transaction); |
+ transaction.read_return_code = ERR_INTERNET_DISCONNECTED; |
+ MockHttpRequest request(transaction); |
+ request_ = request; |
+ |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ AddTransactionToExistingWriters(); |
+ |
+ std::vector<std::string> contents(3); |
+ int rv = ReadNetworkFailure(&contents, ERR_INTERNET_DISCONNECTED); |
+ |
+ EXPECT_EQ(rv, ERR_INTERNET_DISCONNECTED); |
+} |
+ |
+// Tests moving idle writers to readers. |
+TEST_F(WritersTest, MoveIdleWritersToReaders) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_TRUE(writers_->CanAddWriters()); |
+ AddTransactionToExistingWriters(); |
+ AddTransactionToExistingWriters(); |
+ |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ writers_->RemoveAllIdleWriters(); |
+ EXPECT_TRUE(writers_->IsEmpty()); |
+} |
+ |
+// Tests GetWriterLoadState. |
+TEST_F(WritersTest, GetWriterLoadState) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ EXPECT_EQ(LOAD_STATE_IDLE, writers_->GetWriterLoadState()); |
+} |
+ |
+// Tests truncating the entry via Writers. |
+TEST_F(WritersTest, TruncateEntry) { |
+ CreateWritersAddTransaction(); |
+ EXPECT_FALSE(writers_->IsEmpty()); |
+ |
+ std::string content; |
+ int rv = Read(&content); |
+ |
+ EXPECT_THAT(rv, IsOk()); |
+ std::string expected(kSimpleGET_Transaction.data); |
+ EXPECT_EQ(expected, content); |
+ |
+ writers_->TruncateEntry(); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(writers_->IsTruncatedForTesting()); |
+} |
+ |
+} // namespace net |