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

Side by Side Diff: net/http/http_cache_writers_unittest.cc

Issue 2886483002: Adds a new class HttpCache::Writers for multiple cache transactions reading from the network. (Closed)
Patch Set: Feedback addressed Created 3 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/http/http_cache_writers.h"
6
7 #include "base/run_loop.h"
8 #include "net/http/http_cache.h"
9 #include "net/http/http_cache_transaction.h"
10 #include "net/http/http_transaction.h"
11 #include "net/http/http_transaction_test_util.h"
12 #include "net/http/mock_http_cache.h"
13 #include "net/test/gtest_util.h"
14
15 using net::test::IsError;
16 using net::test::IsOk;
17
18 namespace net {
19
20 class HttpCache::WritersTest {
21 public:
22 WritersTest() {}
23
24 // Since ActiveEntry and Transaction are private classes of HttpCache,
25 // accessing those only in this class instead of in tests.
26
27 void CreateWriters(const std::string& url) {
28 HttpCache* cache = cache_.http_cache();
29 ActiveEntry* entry = cache->FindActiveEntry(url);
30 writers_ = base::MakeUnique<Writers>(cache, entry);
31 }
32
33 void CreateNetworkTransaction(std::unique_ptr<HttpTransaction>* transaction) {
34 MockNetworkLayer* network_layer = cache_.network_layer();
35 network_layer->CreateTransaction(DEFAULT_PRIORITY, transaction);
36 }
37
38 void CreateAddTransactionToWriters(MockTransaction transaction_info) {
39 MockHttpRequest request(transaction_info);
40 std::unique_ptr<HttpTransaction> transaction;
41 int rv = cache_.CreateTransaction(&transaction);
42 DCHECK_EQ(rv, OK);
43 DCHECK(transaction.get());
44
45 // Create an ActiveEntry.
46 TestCompletionCallback callback;
47 rv = transaction->Start(&request, callback.callback(), NetLogWithSource());
48 base::RunLoop().RunUntilIdle();
49
50 CreateWriters(kSimpleGET_Transaction.url);
51
52 // Create and Start a mock network transaction.
53 std::unique_ptr<HttpTransaction> network_transaction;
54 CreateNetworkTransaction(&network_transaction);
55 network_transaction->Start(&request, callback.callback(),
56 NetLogWithSource());
57 base::RunLoop().RunUntilIdle();
58
59 DCHECK(writers_->IsEmpty());
60 writers_->AddTransaction((Transaction*)(transaction.get()),
61 std::move(network_transaction));
62 transactions_.insert(std::move(transaction));
63 }
64
65 void AddTransactionToExistingWriters() {
66 std::unique_ptr<HttpTransaction> transaction;
67 int rv = cache_.CreateTransaction(&transaction);
68 DCHECK_EQ(rv, OK);
69 DCHECK(transaction.get());
70
71 DCHECK(writers_);
72 writers_->AddTransaction((Transaction*)(transaction.get()), nullptr);
73 transactions_.insert(std::move(transaction));
74 }
75
76 int Read(std::string* result) {
77 Transaction* transaction = (Transaction*)(transactions_.begin()->get());
78 TestCompletionCallback callback;
79
80 std::string content;
81 int rv;
82 do {
83 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
84 rv = writers_->Read(buf.get(), 256, callback.callback(), transaction);
85 if (rv == ERR_IO_PENDING) {
86 rv = callback.WaitForResult();
87 base::RunLoop().RunUntilIdle();
88 }
89
90 if (rv > 0)
91 content.append(buf->data(), rv);
92 else if (rv < 0)
93 return rv;
94 } while (rv > 0);
95
96 result->swap(content);
97 return OK;
98 }
99
100 bool StopCaching() {
101 Transaction* transaction = (Transaction*)(transactions_.begin()->get());
102 return writers_->StopCaching(transaction);
103 }
104
105 void RemoveTransaction() {
106 Transaction* transaction = (Transaction*)(transactions_.begin()->get());
107 writers_->RemoveTransaction(transaction);
108 }
109
110 MockHttpCache cache_;
111 std::unique_ptr<Writers> writers_;
112 std::unordered_set<std::unique_ptr<HttpTransaction>> transactions_;
113 };
114
115 // Tests successful addition of a transaction.
116 TEST(HttpCacheWriters, AddTransaction) {
117 HttpCache::WritersTest writersTest;
118
119 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
120 EXPECT_FALSE(writersTest.writers_->IsEmpty());
121 }
122
123 // Tests successful addition of multiple transaction.
124 TEST(HttpCacheWriters, AddManyTransactions) {
125 HttpCache::WritersTest writersTest;
126
127 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
128 EXPECT_FALSE(writersTest.writers_->IsEmpty());
129
130 for (int i = 0; i < 5; i++)
131 writersTest.AddTransactionToExistingWriters();
132
133 EXPECT_EQ(writersTest.writers_->CountTransactionsForTesting(), 6);
134 }
135
136 // Tests that CanAddWriters should return false if it is exclusive writing.
137 TEST(HttpCacheWriters, AddTransactionsExclusive) {
138 HttpCache::WritersTest writersTest;
139
140 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
141 EXPECT_FALSE(writersTest.writers_->IsEmpty());
142
143 writersTest.writers_->SetExclusive();
144 EXPECT_FALSE(writersTest.writers_->CanAddWriters());
145 }
146
147 // Tests StopCaching should not stop caching if there are multiple writers.
148 TEST(HttpCacheWriters, StopCachingMultipleWriters) {
149 HttpCache::WritersTest writersTest;
150
151 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
152 EXPECT_FALSE(writersTest.writers_->IsEmpty());
153
154 EXPECT_TRUE(writersTest.writers_->CanAddWriters());
155 writersTest.AddTransactionToExistingWriters();
156
157 EXPECT_FALSE(writersTest.StopCaching());
158 EXPECT_TRUE(writersTest.writers_->CanAddWriters());
159 }
160
161 // Tests StopCaching should stop caching if there is a single writer.
162 TEST(HttpCacheWriters, StopCaching) {
163 HttpCache::WritersTest writersTest;
164
165 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
166 EXPECT_FALSE(writersTest.writers_->IsEmpty());
167
168 EXPECT_TRUE(writersTest.StopCaching());
169 EXPECT_FALSE(writersTest.writers_->CanAddWriters());
170 }
171
172 // Tests removing of an idle transaction.
173 TEST(HttpCacheWriters, RemoveIdleTransaction) {
174 HttpCache::WritersTest writersTest;
175
176 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
177 EXPECT_FALSE(writersTest.writers_->IsEmpty());
178
179 writersTest.RemoveTransaction();
180 EXPECT_TRUE(writersTest.writers_->IsEmpty());
181 }
182
183 // Tests that Read is successful.
184 TEST(HttpCacheWriters, Read) {
185 HttpCache::WritersTest writersTest;
186
187 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
188 EXPECT_FALSE(writersTest.writers_->IsEmpty());
189
190 std::string content;
191 int rv = writersTest.Read(&content);
192
193 EXPECT_THAT(rv, IsOk());
194 std::string expected(kSimpleGET_Transaction.data);
195 EXPECT_EQ(expected, content);
196 }
197
198 // Tests truncating the entry via Writers.
199 TEST(HttpCacheWriters, TruncateEntry) {
200 HttpCache::WritersTest writersTest;
201
202 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
203 EXPECT_FALSE(writersTest.writers_->IsEmpty());
204
205 std::string content;
206 int rv = writersTest.Read(&content);
207
208 EXPECT_THAT(rv, IsOk());
209 std::string expected(kSimpleGET_Transaction.data);
210 EXPECT_EQ(expected, content);
211
212 writersTest.writers_->TruncateEntry();
213 base::RunLoop().RunUntilIdle();
214 EXPECT_TRUE(writersTest.writers_->IsTruncatedForTesting());
215 }
216
217 } // namespace net
OLDNEW
« net/http/http_cache_writers.cc ('K') | « net/http/http_cache_writers.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698