Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Unified Diff: net/http/http_cache_shared_writers_unittest.cc

Issue 2519473002: Fixes the cache lock issue. (Closed)
Patch Set: Feedback addressed Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/http/http_cache_shared_writers.cc ('k') | net/http/http_cache_transaction.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/http/http_cache_shared_writers_unittest.cc
diff --git a/net/http/http_cache_shared_writers_unittest.cc b/net/http/http_cache_shared_writers_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c7028b0cc819dc6a13b181061ed90f3e8b91bc67
--- /dev/null
+++ b/net/http/http_cache_shared_writers_unittest.cc
@@ -0,0 +1,1244 @@
+// Copyright (c) 2016 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_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 {
+
+namespace {
+
+// helpers
+void ReadAndVerifyTransaction(HttpTransaction* transaction,
+ const MockTransaction& transaction_info) {
+ std::string content;
+ int rv = ReadTransaction(transaction, &content);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ EXPECT_EQ(expected, content);
+}
+
+// The 1st transaction reads from the network and the subsequent transactions
+// are able to read from the cache even before the complete response is written
+// to the cache.
+int ReadSharedWritersCacheRead(std::vector<Context*>& context_list,
+ std::vector<std::string>& results) {
+ int rv;
+
+ size_t i = 0;
+ HttpTransaction* transaction = nullptr;
+ do {
+ Context* c = context_list.at(i);
+ transaction = c->trans.get();
+ scoped_refptr<IOBuffer> buf(new IOBuffer(256));
+ rv = transaction->Read(buf.get(), 256, c->callback.callback());
+ if (rv == ERR_IO_PENDING)
+ rv = c->callback.WaitForResult();
+
+ if (rv > 0)
+ results.at(i).append(buf->data(), rv);
+ else if (rv < 0)
+ return rv;
+
+ i = (i == context_list.size() - 1) ? 0 : i + 1;
+ } while (rv > 0);
+
+ return OK;
+}
+
+void ReadAndVerifySharedWritersCacheRead(
+ std::vector<Context*>& context_list,
+ const MockTransaction& transaction_info) {
+ std::vector<std::string> contents(context_list.size());
+ int rv = ReadSharedWritersCacheRead(context_list, contents);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ for (size_t i = 0; i < contents.size(); i++) {
+ EXPECT_EQ(expected, contents.at(i));
+ }
+}
+
+// The 1st transaction reads from the network and read is invoked on other
+// transactions even before the 1st one's io callback is invoked. The other
+// transactions should have data written in their read buffers as part
+// of the 1st transaction's callback.
+int ReadSharedWritersJoinedRead(std::vector<Context*>& context_list,
+ std::vector<std::string>& results) {
+ int rv = 0;
+ do {
+ // Invoke Read for all transactions.
+ std::vector<scoped_refptr<IOBuffer>> buffers(context_list.size());
+ for (size_t i = 0; i < context_list.size(); i++) {
+ HttpTransaction* transaction = context_list[i]->trans.get();
+ buffers[i] = new IOBuffer(30);
+ rv = transaction->Read(buffers[i].get(), 30,
+ context_list[i]->callback.callback());
+ // Invoking stop caching here should not have any impact since there are
+ // multiple transactions waiting for this resource.
+ if (i == 0)
+ transaction->StopCaching();
+ }
+ if (rv == ERR_IO_PENDING) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ Context* ct = context_list[i];
+ rv = ct->callback.WaitForResult();
+ }
+ }
+
+ if (rv > 0) {
+ for (size_t i = 0; i < results.size(); i++) {
+ results[i].append(buffers[i]->data(), rv);
+ }
+ } else if (rv < 0) {
+ return rv;
+ }
+ } while (rv > 0);
+
+ return OK;
+}
+
+void ReadAndVerifySharedWritersJoinedRead(
+ const MockTransaction& transaction_info,
+ std::vector<Context*>& context_list) {
+ std::vector<std::string> results(context_list.size());
+ int rv = ReadSharedWritersJoinedRead(context_list, results);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ for (size_t i = 0; i < results.size(); i++) {
+ EXPECT_EQ(expected, results[i]);
+ }
+}
+
+int ReadSharedWritersJoinedReadDoneReading(std::vector<Context*>& context_list,
+ std::vector<std::string>& results) {
+ int rv = 0;
+ int cnt = 0;
+ do {
+ // Invoke Read for all transactions.
+ std::vector<scoped_refptr<IOBuffer>> buffers(context_list.size());
+ for (size_t i = 0; i < context_list.size(); i++) {
+ HttpTransaction* transaction = context_list[i]->trans.get();
+ buffers[i] = new IOBuffer(30);
+ rv = transaction->Read(buffers[i].get(), 30,
+ context_list[i]->callback.callback());
+ }
+ if (rv == ERR_IO_PENDING) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ Context* ct = context_list[i];
+ rv = ct->callback.WaitForResult();
+ }
+ }
+
+ if (rv > 0) {
+ for (size_t i = 0; i < results.size(); i++) {
+ results[i].append(buffers[i]->data(), rv);
+ }
+ } else if (rv < 0) {
+ return rv;
+ }
+
+ cnt++;
+ if (cnt == 2) {
+ context_list[0]->trans.get()->DoneReading();
+ }
+ } while (cnt < 2);
+
+ return OK;
+}
+
+void ReadAndVerifySharedWritersJoinedReadDoneReading(
+ const MockTransaction& transaction_info,
+ std::vector<Context*>& context_list) {
+ std::vector<std::string> results(context_list.size());
+ int rv = ReadSharedWritersJoinedReadDoneReading(context_list, results);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ for (size_t i = 0; i < results.size(); i++) {
+ EXPECT_EQ(expected, results[i]);
+ }
+}
+
+// The 1st transaction reads from the network and read is invoked on other
+// transactions even before the 1st one's io callback is invoked. Also, the
+// first transaction is deleted before the io callback is invoked. The other
+// transactions should have data written in their read buffers as part
+// of the 1st transaction's callback.
+int ReadSharedWritersJoinedReadDeleteCurrentWriter(
+ std::vector<Context*>& context_list,
+ std::vector<std::string>& results) {
+ int rv = 0;
+ bool first_iter = true;
+ do {
+ // Invoke Read for all transactions.
+ std::vector<scoped_refptr<IOBuffer>> buffers(context_list.size());
+ for (size_t i = 0; i < context_list.size(); i++) {
+ HttpTransaction* transaction = context_list[i]->trans.get();
+ buffers[i] = new IOBuffer(30);
+ rv = transaction->Read(buffers[i].get(), 30,
+ context_list[i]->callback.callback());
+ }
+
+ // Delete the 1st transaction.
+ if (first_iter) {
+ Context* c = context_list[0];
+ context_list.erase(context_list.begin());
+ delete c;
+ buffers.erase(buffers.begin());
+ }
+
+ if (rv == ERR_IO_PENDING) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ Context* ct = context_list[i];
+ rv = ct->callback.WaitForResult();
+ }
+ }
+
+ if (rv > 0) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ results[i].append(buffers[i]->data(), rv);
+ }
+ } else if (rv < 0) {
+ return rv;
+ }
+
+ first_iter = false;
+ } while (rv > 0);
+
+ return OK;
+}
+
+void ReadAndVerifySharedWritersJoinedReadDeleteCurrentWriter(
+ const MockTransaction& transaction_info,
+ std::vector<Context*>& context_list) {
+ std::vector<std::string> results(context_list.size() - 1);
+ int rv =
+ ReadSharedWritersJoinedReadDeleteCurrentWriter(context_list, results);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ for (size_t i = 0; i < results.size(); i++) {
+ EXPECT_EQ(expected, results[i]);
+ }
+}
+
+int ReadSharedWritersJoinedReadDeleteWaitingWriter(
+ std::vector<Context*>& context_list,
+ std::vector<std::string>& results) {
+ int rv = 0;
+ bool first_iter = true;
+ do {
+ // Invoke Read for all transactions.
+ std::vector<scoped_refptr<IOBuffer>> buffers(context_list.size());
+ for (size_t i = 0; i < context_list.size(); i++) {
+ HttpTransaction* transaction = context_list[i]->trans.get();
+ buffers[i] = new IOBuffer(30);
+ rv = transaction->Read(buffers[i].get(), 30,
+ context_list[i]->callback.callback());
+ }
+
+ // Remove a waiting writer transaction.
+ if (first_iter) {
+ auto it = context_list.begin();
+ it++;
+ if (it != context_list.end()) {
+ Context* c = *it;
+ context_list.erase(it);
+ delete c;
+ auto it1 = buffers.begin();
+ it1++;
+ buffers.erase(it1);
+ }
+ }
+
+ if (rv == ERR_IO_PENDING) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ Context* ct = context_list[i];
+ rv = ct->callback.WaitForResult();
+ }
+ }
+
+ if (rv > 0) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ results[i].append(buffers[i]->data(), rv);
+ }
+ } else if (rv < 0) {
+ return rv;
+ }
+
+ first_iter = false;
+ } while (rv > 0);
+
+ return OK;
+}
+
+void ReadAndVerifySharedWritersDeleteWaitingWriter(
+ const MockTransaction& transaction_info,
+ std::vector<Context*>& context_list) {
+ std::vector<std::string> results(context_list.size() - 1);
+ int rv =
+ ReadSharedWritersJoinedReadDeleteWaitingWriter(context_list, results);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ for (size_t i = 0; i < results.size(); i++) {
+ EXPECT_EQ(expected, results[i]);
+ }
+}
+
+int ReadSharedWritersJoinedReadDeleteIdleWriter(
+ std::vector<Context*>& context_list,
+ std::vector<std::string>& results) {
+ int rv = 0;
+ bool first_iter = true;
+ do {
+ // Invoke Read for all transactions.
+ std::vector<scoped_refptr<IOBuffer>> buffers(context_list.size());
+ for (size_t i = 0; i < context_list.size(); i++) {
+ HttpTransaction* transaction = context_list[i]->trans.get();
+ buffers[i] = new IOBuffer(30);
+ // Do not invoke Read on the first transaction.
+ if (first_iter && i == 0)
+ continue;
+ rv = transaction->Read(buffers[i].get(), 30,
+ context_list[i]->callback.callback());
+ }
+
+ // Remove the idle writer transaction.
+ if (first_iter) {
+ auto it = context_list.begin();
+ Context* c = *it;
+ context_list.erase(it);
+ delete c;
+ auto it1 = buffers.begin();
+ buffers.erase(it1);
+ }
+
+ if (rv == ERR_IO_PENDING) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ Context* ct = context_list[i];
+ rv = ct->callback.WaitForResult();
+ }
+ }
+
+ if (rv > 0) {
+ for (size_t i = 0; i < context_list.size(); i++) {
+ results[i].append(buffers[i]->data(), rv);
+ }
+ } else if (rv < 0) {
+ return rv;
+ }
+
+ first_iter = false;
+ } while (rv > 0);
+
+ return OK;
+}
+
+void ReadAndVerifySharedWritersDeleteIdleWriter(
+ const MockTransaction& transaction_info,
+ std::vector<Context*>& context_list) {
+ std::vector<std::string> results(context_list.size() - 1);
+ int rv = ReadSharedWritersJoinedReadDeleteIdleWriter(context_list, results);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ for (size_t i = 0; i < results.size(); i++) {
+ EXPECT_EQ(expected, results[i]);
+ }
+}
+
+int ReadSharedWritersJoinedReadCacheWriteFailure(
+ std::vector<Context*>& context_list,
+ std::vector<std::string>& results,
+ MockHttpCache& cache) {
+ int rv = 0;
+ MockHttpRequest request(kSimpleGET_Transaction);
+
+ // 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();
+
+ // Invoke Read for two transactions so they become current_writer_ and
+ // waiting_writers_ respectively.
+ std::vector<scoped_refptr<IOBuffer>> buf(2);
+ size_t i = 0, j = 0;
+ do {
+ HttpTransaction* transaction = context_list[i]->trans.get();
+ buf[j] = new IOBuffer(30);
+ rv = transaction->Read(buf[j].get(), 30,
+ context_list[i]->callback.callback());
+ i = context_list.size() - 1;
+ j++;
+ } while (j < buf.size());
+
+ // current_writer_ which is transaction [0] should continue without writing
+ // to the cache, idle writer ([3]) should fail on its next Read call and
+ // waiting_writers_ ([6]) should return a failure back.
+ if (rv == ERR_IO_PENDING) {
+ Context* ct = *(context_list.begin());
+ rv = ct->callback.WaitForResult();
+ }
+
+ // Only [0] should succeed in the Read.
+ results[0].append(buf[0]->data(), 30);
+
+ // Invoke Start on 4 and 5. 4 will fail to read from the cache, doom the
+ // entry and go on to create a new entry. 5 will also get added to that
+ // entry.
+ for (size_t i = 4; i <= 5; i++) {
+ Context* c = context_list[i];
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ }
+
+ base::RunLoop().RunUntilIdle();
+
+ // 3 and 6 failed, delete them.
+ Context* c = context_list[3];
+ scoped_refptr<IOBuffer> buf1 = new IOBuffer(30);
+ int rv_fail = c->trans->Read(buf1.get(), 30, c->callback.callback());
+ EXPECT_THAT(rv_fail, ERR_CACHE_WRITE_FAILURE);
+
+ Context* c6 = context_list[6];
+ for (auto itr = context_list.begin(); itr != context_list.end();) {
+ if (*itr == c || *itr == c6) {
+ Context* ct = *itr;
+ itr = context_list.erase(itr);
+ delete ct;
+ } else {
+ itr++;
+ }
+ }
+
+ cache.disk_cache()->set_soft_failures(false);
+
+ base::RunLoop().RunUntilIdle();
+
+ // Read for the rest of the transactions.
+ Context* c0 = context_list[0];
+ Context* c3 = context_list[3];
+ for (size_t i = 0; i < context_list.size(); i++) {
+ if (i == 1 || i == 2 || i == 4)
+ continue;
+ std::string res;
+ ReadTransaction(context_list[i]->trans.get(), &res);
+ results[i].append(res);
+ }
+
+ base::RunLoop().RunUntilIdle();
+
+ // Delete the done transactions so that pending queue can be processed.
+ // The reader transactions will error out with ERR_CACHE_MISS because of cache
+ // failures.
+ Context* c1 = context_list[1];
+ Context* c2 = context_list[2];
+ auto itr = context_list.begin();
+ for (; itr != context_list.end();) {
+ Context* c = *itr;
+ if (c == c0 || c == c1 || c == c2 || c == c3) {
+ itr = context_list.erase(itr);
+ delete c;
+ } else {
+ itr++;
+ }
+ }
+
+ std::string res;
+ ReadTransaction(context_list[0]->trans.get(), &res);
+ results[4].append(res);
+
+ return OK;
+}
+
+void ReadAndVerifySharedWritersJoinedReadCacheWriteFailure(
+ const MockTransaction& transaction_info,
+ std::vector<Context*>& context_list,
+ MockHttpCache& cache) {
+ std::vector<std::string> results(context_list.size());
+ int rv = ReadSharedWritersJoinedReadCacheWriteFailure(context_list, results,
+ cache);
+
+ EXPECT_THAT(rv, IsOk());
+ std::string expected(transaction_info.data);
+ for (size_t i = 0; i < context_list.size(); i++) {
+ EXPECT_EQ(expected, results[i]);
+ }
+}
+
+} // namespace
+
+// Tests.
+
+TEST(HttpCache, SimpleGET_SharedWritingCacheRead) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // The first request should be a writer at this point, and the subsequent
+ // requests should be pending.
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // All requests depend on the writer, and the writer is between Start and
+ // Read, i.e. idle.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ }
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ if (c->result == ERR_IO_PENDING)
+ c->result = c->callback.WaitForResult();
+ }
+
+ ReadAndVerifySharedWritersCacheRead(context_list, kSimpleGET_Transaction);
+
+ // We should not have had to re-open the disk entry
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ delete c;
+ }
+}
+
+TEST(HttpCache, SimpleGET_SharedWritingJoinedRead) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // The first request should be a writer at this point, and the subsequent
+ // requests should be pending.
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+ // All requests depend on the writer, and the writer is between Start and
+ // Read, i.e. idle.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ }
+
+ std::vector<HttpTransaction*> transactions;
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ if (c->result == ERR_IO_PENDING)
+ c->result = c->callback.WaitForResult();
+ transactions.push_back(c->trans.get());
+ }
+
+ ReadAndVerifySharedWritersJoinedRead(kSimpleGET_Transaction, context_list);
+
+ // We should not have had to re-open the disk entry
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ delete c;
+ }
+}
+
+TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDoneReading) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // The first request should be a writer at this point, and the subsequent
+ // requests should be pending.
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // All requests depend on the writer, and the writer is between Start and
+ // Read, i.e. idle.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ }
+
+ std::vector<HttpTransaction*> transactions;
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ if (c->result == ERR_IO_PENDING)
+ c->result = c->callback.WaitForResult();
+ transactions.push_back(c->trans.get());
+ }
+
+ ReadAndVerifySharedWritersJoinedReadDoneReading(kSimpleGET_Transaction,
+ context_list);
+
+ // We should not have had to re-open the disk entry
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ delete c;
+ }
+}
+
+// Tests the following:
+// - A 200 will doom the entry and the transaction will continue to read from
+// the network.
+// - Doomed entry's SharedWriters will continue to read and write to the cache.
+// - Doomed entry's SharedWriters' waiting_for_validation_ will create another
+// entry.
+// - The new entry will also have another SharedWriters for subsequent
+// transactions.
+TEST(HttpCache, SharedWritingValidation200) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+ request.load_flags |= LOAD_VALIDATE_CACHE;
+ MockHttpRequest reader_request(kSimpleGET_Transaction);
+ reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
+ MockHttpRequest request_no_validate(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 6;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ MockHttpRequest* this_request = &request;
+ if (i == 1 || i == 2)
+ this_request = &reader_request;
+
+ if (i == 5)
+ this_request = &request_no_validate;
+
+ c->result = c->trans->Start(this_request, c->callback.callback(),
+ NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // The first [0] request should be a writer at this point, and should have
+ // created
+ // SharedWriters, [1] and [2] will be in pending_queue and [3] and [4] should
+ // have validated and [5] will have skipped validation and be ready to read.
+
+ EXPECT_EQ(3, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(2, cache.disk_cache()->create_count());
+
+ Context* c = context_list[0];
+ ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
+ c->result = c->callback.WaitForResult();
+
+ // First request is done with the headers, now [3] will start validation.
+ Context* c3 = context_list[3];
+ ASSERT_THAT(c3->result, IsError(ERR_IO_PENDING));
+
+ // Let [3] get a 200 , thus leading to dooming the entry, [4] and [5] will
+ // move to entry's pending_queue.
+ c3->result = c3->callback.WaitForResult();
+
+ // [3] will complete reading the response using its own network transaction
+ // and not writing to the cache.
+ ReadAndVerifyTransaction(c3->trans.get(), kSimpleGET_Transaction);
+
+ // [0] will complete reading the response using SharedWriter's network
+ // transaction and then the entry should be finalized.
+ ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
+
+ // [4] should be added to a new entry and should contiue reading and writing
+ // to the cache (also create SharedWriters)
+ for (int i = 4; i <= 5; i++) {
+ Context* c = context_list[i];
+ ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
+ c->result = c->callback.WaitForResult();
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ // [5] will join SharedWriters at this point when [4] completes validation.
+ ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
+ }
+ // [1] and [2] will become readers and read from the cache.
+ for (int i = 1; i <= 2; i++) {
+ Context* c = context_list[i];
+ ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
+ c->result = c->callback.WaitForResult();
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
+ }
+
+ // We should not have had to re-open the disk entry
+ EXPECT_EQ(3, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(2, cache.disk_cache()->create_count());
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ delete c;
+ }
+}
+
+// Tests a validating transaction deletion while it is waiting for its callback
+// to be invoked.
+TEST(HttpCache, SharedWritingDeleteValidationTrans) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+ request.load_flags |= LOAD_VALIDATE_CACHE;
+ MockHttpRequest reader_request(kSimpleGET_Transaction);
+ reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ MockHttpRequest* this_request = &request;
+ if (i == 1 || i == 2)
+ this_request = &reader_request;
+
+ if (i == 3 || i == 4)
+ continue;
+
+ c->result = c->trans->Start(this_request, c->callback.callback(),
+ NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ if (i == 3 || i == 4)
+ continue;
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // Start [3], [4].
+ for (size_t i = 3; i <= 4; i++) {
+ Context* c = context_list[i];
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
+ }
+
+ // Delete [3] as it is the current validating trans.
+ Context* c3 = context_list[3];
+ for (auto it = context_list.begin(); it != context_list.end(); it++) {
+ if (*it == c3) {
+ context_list.erase(it);
+ delete c3;
+ break;
+ }
+ }
+
+ // Complete start state machine for [4].
+ base::RunLoop().RunUntilIdle();
+
+ // [0] will complete reading the response using SharedWriter's network
+ // transaction and then the entry should be finalized.
+ Context* c0 = context_list[0];
+ ReadAndVerifyTransaction(c0->trans.get(), kSimpleGET_Transaction);
+
+ // [4] (now [3]) should doom the entry and continue reading with its network
+ // transaction.
+ Context* c4 = context_list[3];
+ ReadAndVerifyTransaction(c4->trans.get(), kSimpleGET_Transaction);
+
+ // 1st entry will get destroyed and [1] and [2] will being readers will get a
+ // ERR_CACHE_MISS.
+ for (int i = 1; i <= 2; i++) {
+ Context* c = context_list[i];
+ ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
+ c->result = c->callback.WaitForResult();
+ ASSERT_THAT(c->result, IsError(ERR_CACHE_MISS));
+ }
+
+ // We should not have had to re-open the disk entry
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (auto i = context_list.begin(); i != context_list.end(); ++i) {
+ Context* c = *i;
+ delete c;
+ }
+}
+
+// Tests a transaction deletion while it is waiting for validation.
+TEST(HttpCache, SharedWritingDeleteWaitingForValidation) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+ request.load_flags |= LOAD_VALIDATE_CACHE;
+ MockHttpRequest reader_request(kSimpleGET_Transaction);
+ reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ MockHttpRequest* this_request = &request;
+ if (i == 1 || i == 2)
+ this_request = &reader_request;
+
+ if (i == 3 || i == 4)
+ continue;
+
+ c->result = c->trans->Start(this_request, c->callback.callback(),
+ NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ if (i == 3 || i == 4)
+ continue;
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // Start [3], [4].
+ for (size_t i = 3; i <= 4; i++) {
+ Context* c = context_list[i];
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
+ }
+
+ // Delete [4] as it is the waiting for validation trans.
+ Context* c4 = context_list[4];
+ for (auto it = context_list.begin(); it != context_list.end(); it++) {
+ if (*it == c4) {
+ context_list.erase(it);
+ delete c4;
+ break;
+ }
+ }
+
+ // Complete start state machine for [3].
+ base::RunLoop().RunUntilIdle();
+
+ // [0] will complete reading the response using SharedWriter's network
+ // transaction and then the entry should be finalized.
+ Context* c0 = context_list[0];
+ ReadAndVerifyTransaction(c0->trans.get(), kSimpleGET_Transaction);
+
+ // [3] should doom the entry and continue reading with its network
+ // transaction.
+ Context* c3 = context_list[3];
+ ReadAndVerifyTransaction(c3->trans.get(), kSimpleGET_Transaction);
+
+ // 1st entry will get destroyed and [1] and [2] will being readers will get a
+ // ERR_CACHE_MISS.
+ for (int i = 1; i <= 2; i++) {
+ Context* c = context_list[i];
+ ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
+ c->result = c->callback.WaitForResult();
+ ASSERT_THAT(c->result, IsError(ERR_CACHE_MISS));
+ }
+
+ // We should not have had to re-open the disk entry
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (auto i = context_list.begin(); i != context_list.end(); ++i) {
+ Context* c = *i;
+ delete c;
+ }
+}
+
+// Tests the impact of cache write failure on Shared Writing.
+TEST(HttpCache, SharedWritingCacheWriteFailure) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+ request.load_flags |= LOAD_VALIDATE_CACHE;
+ MockHttpRequest reader_request(kSimpleGET_Transaction);
+ reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
+ MockHttpRequest request_no_validate(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 7;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ MockHttpRequest* this_request = &request;
+ if (i == 1 || i == 2)
+ this_request = &reader_request;
+
+ if (i == 3 || i == 6)
+ this_request = &request_no_validate;
+
+ if (i == 4 || i == 5)
+ continue;
+
+ c->result = c->trans->Start(this_request, c->callback.callback(),
+ NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ if (i == 4 || i == 5)
+ continue;
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // At this point, 0,5 and 3 are idle writers, 1 and 2 are pending readers and
+ // 4
+ // will now become the validating_trans_.
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ ReadAndVerifySharedWritersJoinedReadCacheWriteFailure(kSimpleGET_Transaction,
+ context_list, cache);
+
+ // We should not have had to re-open the disk entry
+ EXPECT_EQ(3, cache.network_layer()->transaction_count());
+ EXPECT_EQ(1, cache.disk_cache()->open_count());
+ EXPECT_EQ(3, cache.disk_cache()->create_count());
+
+ for (auto i = context_list.begin(); i != context_list.end(); ++i) {
+ Context* c = *i;
+ delete c;
+ }
+}
+
+TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDeleteCurrentWriter) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // The first request should be a writer at this point, and the subsequent
+ // requests should be pending.
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // All requests depend on the writer, and the writer is between Start and
+ // Read, i.e. idle.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ }
+
+ std::vector<HttpTransaction*> transactions;
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ if (c->result == ERR_IO_PENDING)
+ c->result = c->callback.WaitForResult();
+ transactions.push_back(c->trans.get());
+ }
+
+ ReadAndVerifySharedWritersJoinedReadDeleteCurrentWriter(
+ kSimpleGET_Transaction, context_list);
+
+ // We should not have had to re-open the disk entry
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (auto i = context_list.begin(); i != context_list.end(); ++i) {
+ Context* c = *i;
+ delete c;
+ }
+}
+
+TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDeleteWaitingWriter) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // The first request should be a writer at this point, and the subsequent
+ // requests should be pending.
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // All requests depend on the writer, and the writer is between Start and
+ // Read, i.e. idle.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ }
+
+ std::vector<HttpTransaction*> transactions;
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ if (c->result == ERR_IO_PENDING)
+ c->result = c->callback.WaitForResult();
+ transactions.push_back(c->trans.get());
+ }
+
+ ReadAndVerifySharedWritersDeleteWaitingWriter(kSimpleGET_Transaction,
+ context_list);
+
+ // We should not have had to re-open the disk entry
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (auto i = context_list.begin(); i != context_list.end(); ++i) {
+ Context* c = *i;
+ delete c;
+ }
+}
+
+TEST(HttpCache, SimpleGET_SharedWritingJoinedReadDeleteIdleWriter) {
+ MockHttpCache cache;
+
+ MockHttpRequest request(kSimpleGET_Transaction);
+
+ std::vector<Context*> context_list;
+ const int kNumTransactions = 5;
+
+ for (int i = 0; i < kNumTransactions; ++i) {
+ context_list.push_back(new Context());
+ Context* c = context_list[i];
+
+ c->result = cache.CreateTransaction(&c->trans);
+ ASSERT_THAT(c->result, IsOk());
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+
+ c->result =
+ c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
+ }
+
+ // All requests are waiting for the active entry.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
+ }
+
+ // Allow all requests to move from the Create queue to the active entry.
+ base::RunLoop().RunUntilIdle();
+
+ // The first request should be a writer at this point, and the subsequent
+ // requests should be pending.
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // All requests depend on the writer, and the writer is between Start and
+ // Read, i.e. idle.
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
+ }
+
+ std::vector<HttpTransaction*> transactions;
+ for (int i = 0; i < kNumTransactions; ++i) {
+ Context* c = context_list[i];
+ if (c->result == ERR_IO_PENDING)
+ c->result = c->callback.WaitForResult();
+ transactions.push_back(c->trans.get());
+ }
+
+ ReadAndVerifySharedWritersDeleteIdleWriter(kSimpleGET_Transaction,
+ context_list);
+ // We should not have had to re-open the disk entry
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ for (auto i = context_list.begin(); i != context_list.end(); ++i) {
+ Context* c = *i;
+ delete c;
+ }
+}
+
+} // namespace net
« no previous file with comments | « net/http/http_cache_shared_writers.cc ('k') | net/http/http_cache_transaction.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698