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

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, 6 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 {
jkarlin 2017/06/12 18:30:33 Since every test uses this class, it seems like th
Randy Smith (Not in Mondays) 2017/06/13 17:45:05 +1
shivanisha 2017/06/14 02:33:17 This class is mostly a collection of helper functi
Randy Smith (Not in Mondays) 2017/06/14 19:49:17 Sure. But if you put them in a text fixture with
shivanisha 2017/06/15 20:17:45 Since the class needs to access a private nested c
Randy Smith (Not in Mondays) 2017/06/22 21:45:05 Fair, though there are several ways around that.
shivanisha 2017/06/26 20:47:02 I will address this tomorrow but didn't want to de
shivanisha 2017/06/27 04:42:19 It works on making the test class friends to both
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 bool is_exclusive = false) {
40 MockHttpRequest request(transaction_info);
41 std::unique_ptr<HttpTransaction> transaction;
42 int rv = cache_.CreateTransaction(&transaction);
43 DCHECK_EQ(rv, OK);
44 DCHECK(transaction.get());
45
46 // Create an ActiveEntry.
47 TestCompletionCallback callback;
48 rv = transaction->Start(&request, callback.callback(), NetLogWithSource());
49 base::RunLoop().RunUntilIdle();
50
51 CreateWriters(kSimpleGET_Transaction.url);
52
53 // Create and Start a mock network transaction.
54 std::unique_ptr<HttpTransaction> network_transaction;
55 CreateNetworkTransaction(&network_transaction);
56 network_transaction->Start(&request, callback.callback(),
57 NetLogWithSource());
58 base::RunLoop().RunUntilIdle();
59
60 DCHECK(writers_->IsEmpty());
61 writers_->AddTransaction((Transaction*)(transaction.get()),
62 std::move(network_transaction), is_exclusive);
63 transactions_.insert(std::move(transaction));
64 }
65
66 void AddTransactionToExistingWriters() {
67 std::unique_ptr<HttpTransaction> transaction;
68 int rv = cache_.CreateTransaction(&transaction);
69 DCHECK_EQ(rv, OK);
70 DCHECK(transaction.get());
71
72 DCHECK(writers_);
73 writers_->AddTransaction((Transaction*)(transaction.get()), nullptr, false);
74 transactions_.insert(std::move(transaction));
75 }
76
77 int Read(std::string* result) {
78 Transaction* transaction = (Transaction*)(transactions_.begin()->get());
79 TestCompletionCallback callback;
80
81 std::string content;
82 int rv;
83 do {
84 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
85 rv = writers_->Read(buf.get(), 256, callback.callback(), transaction);
86 if (rv == ERR_IO_PENDING) {
87 rv = callback.WaitForResult();
88 base::RunLoop().RunUntilIdle();
89 }
90
91 if (rv > 0)
92 content.append(buf->data(), rv);
93 else if (rv < 0)
94 return rv;
95 } while (rv > 0);
96
97 result->swap(content);
98 return OK;
99 }
100
101 bool StopCaching() {
102 Transaction* transaction = (Transaction*)(transactions_.begin()->get());
103 return writers_->StopCaching(transaction);
104 }
105
106 void RemoveTransaction() {
107 Transaction* transaction = (Transaction*)(transactions_.begin()->get());
108 writers_->RemoveTransaction(transaction);
109 }
110
111 MockHttpCache cache_;
112 std::unique_ptr<Writers> writers_;
113 std::unordered_set<std::unique_ptr<HttpTransaction>> transactions_;
114 };
115
116 // Tests successful addition of a transaction.
117 TEST(HttpCacheWriters, AddTransaction) {
118 HttpCache::WritersTest writersTest;
119
120 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
121 EXPECT_FALSE(writersTest.writers_->IsEmpty());
122 }
123
124 // Tests successful addition of multiple transaction.
125 TEST(HttpCacheWriters, AddManyTransactions) {
126 HttpCache::WritersTest writersTest;
127
128 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction);
129 EXPECT_FALSE(writersTest.writers_->IsEmpty());
130
131 for (int i = 0; i < 5; i++)
132 writersTest.AddTransactionToExistingWriters();
133
134 EXPECT_EQ(writersTest.writers_->CountTransactionsForTesting(), 6);
135 }
136
137 // Tests that CanAddWriters should return false if it is exclusive writing.
138 TEST(HttpCacheWriters, AddTransactionsExclusive) {
139 HttpCache::WritersTest writersTest;
140
141 writersTest.CreateAddTransactionToWriters(kSimpleGET_Transaction, true);
142 EXPECT_FALSE(writersTest.writers_->IsEmpty());
143
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 }
jkarlin 2017/06/12 18:30:33 I'd like to see tests for all of the public API in
shivanisha 2017/06/14 02:33:17 Added tests for all public APIs, multiple Reads ,
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