OLD | NEW |
---|---|
(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 <memory> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/run_loop.h" | |
12 #include "net/http/http_cache.h" | |
13 #include "net/http/http_cache_transaction.h" | |
14 #include "net/http/http_transaction.h" | |
15 #include "net/http/http_transaction_test_util.h" | |
16 #include "net/http/mock_http_cache.h" | |
17 #include "net/test/gtest_util.h" | |
18 | |
19 using net::test::IsError; | |
20 using net::test::IsOk; | |
21 | |
22 namespace net { | |
23 | |
24 class MockHttpCacheTransaction : public HttpCache::Transaction { | |
jkarlin
2017/07/12 15:05:26
What's the purpose of this class? Generally Mocks
shivanisha
2017/07/13 18:35:50
Adding the overridden empty definitions.
Its more
jkarlin
2017/07/14 18:30:48
Okay, let's rename it then as it's not actually a
shivanisha
2017/07/14 20:50:27
As discussed f2f, lets keep the name as Mock since
| |
25 public: | |
26 MockHttpCacheTransaction(RequestPriority priority, HttpCache* cache) | |
27 : HttpCache::Transaction(priority, cache){}; | |
28 ~MockHttpCacheTransaction() override{}; | |
29 }; | |
30 | |
31 class WritersTest : public testing::Test { | |
32 public: | |
33 enum class DeleteTransactionType { NONE, ACTIVE, WAITING, IDLE }; | |
34 | |
35 WritersTest() : disk_entry_(nullptr), request_(kSimpleGET_Transaction) {} | |
36 | |
37 ~WritersTest() override { | |
38 if (disk_entry_) | |
39 disk_entry_->Close(); | |
40 } | |
41 | |
42 void CreateWriters(const std::string& url) { | |
43 cache_.CreateBackendEntry(kSimpleGET_Transaction.url, &disk_entry_, | |
44 nullptr); | |
45 writers_ = base::MakeUnique<HttpCache::Writers>(disk_entry_); | |
46 } | |
47 | |
48 std::unique_ptr<HttpTransaction> CreateNetworkTransaction() { | |
49 std::unique_ptr<HttpTransaction> transaction; | |
50 MockNetworkLayer* network_layer = cache_.network_layer(); | |
51 network_layer->CreateTransaction(DEFAULT_PRIORITY, &transaction); | |
52 return transaction; | |
53 } | |
54 | |
55 void CreateWritersAddTransaction(bool is_exclusive = false) { | |
56 TestCompletionCallback callback; | |
57 | |
58 // Create and Start a mock network transaction. | |
59 std::unique_ptr<HttpTransaction> network_transaction; | |
60 network_transaction = CreateNetworkTransaction(); | |
61 network_transaction->Start(&request_, callback.callback(), | |
62 NetLogWithSource()); | |
63 base::RunLoop().RunUntilIdle(); | |
64 | |
65 // Create a mock cache transaction. | |
66 std::unique_ptr<MockHttpCacheTransaction> transaction = | |
67 base::MakeUnique<MockHttpCacheTransaction>(DEFAULT_PRIORITY, | |
68 cache_.http_cache()); | |
69 | |
70 CreateWriters(kSimpleGET_Transaction.url); | |
71 EXPECT_TRUE(writers_->IsEmpty()); | |
72 writers_->AddTransaction(transaction.get(), std::move(network_transaction), | |
73 is_exclusive); | |
74 EXPECT_TRUE(writers_->HasTransaction(transaction.get())); | |
75 transactions_.push_back(std::move(transaction)); | |
76 } | |
77 | |
78 void CreateWritersAddTransactionPriority(net::RequestPriority priority, | |
79 bool is_exclusive = false) { | |
80 CreateWritersAddTransaction(is_exclusive); | |
81 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); | |
82 transaction->SetPriority(priority); | |
83 } | |
84 | |
85 void AddTransactionToExistingWriters() { | |
86 EXPECT_TRUE(writers_); | |
87 | |
88 // Create a mock cache transaction. | |
89 std::unique_ptr<MockHttpCacheTransaction> transaction = | |
90 base::MakeUnique<MockHttpCacheTransaction>(DEFAULT_PRIORITY, | |
91 cache_.http_cache()); | |
92 | |
93 writers_->AddTransaction(transaction.get(), nullptr, false); | |
94 transactions_.push_back(std::move(transaction)); | |
95 } | |
96 | |
97 int Read(std::string* result) { | |
98 EXPECT_TRUE(transactions_.size() >= (size_t)1); | |
99 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); | |
100 TestCompletionCallback callback; | |
101 | |
102 std::string content; | |
103 int rv = 0; | |
104 do { | |
105 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
nit, suggestion: 256->constant.
shivanisha
2017/07/13 18:35:51
added static const int kDefaultBufferSize = 256
| |
106 rv = writers_->Read(buf.get(), 256, callback.callback(), transaction); | |
107 if (rv == ERR_IO_PENDING) { | |
108 rv = callback.WaitForResult(); | |
109 base::RunLoop().RunUntilIdle(); | |
110 } | |
111 | |
112 if (rv > 0) | |
113 content.append(buf->data(), rv); | |
114 else if (rv < 0) | |
115 return rv; | |
116 } while (rv > 0); | |
117 | |
118 result->swap(content); | |
119 return OK; | |
120 } | |
121 | |
122 int ReadAll() { | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
nit, suggestion: I'd find this more readable if Re
shivanisha
2017/07/13 18:35:50
Moved
| |
123 return ReadAllDeleteTransaction(DeleteTransactionType::NONE); | |
124 } | |
125 | |
126 void ReadVerifyTwoDifferentBufferLengths( | |
127 const std::vector<int>& buffer_lengths) { | |
128 EXPECT_EQ(2u, buffer_lengths.size()); | |
129 EXPECT_EQ(2u, transactions_.size()); | |
130 | |
131 std::vector<std::string> results(buffer_lengths.size()); | |
132 | |
133 // Check only the 1st Read and not the complete response because the smaller | |
134 // buffer transaction will need to read the remaining response from the | |
135 // cache which will be tested when integrated with MockHttpCacheTransaction | |
136 // layer. | |
137 | |
138 int rv = 0; | |
139 | |
140 std::vector<scoped_refptr<IOBuffer>> bufs; | |
141 for (auto buffer_length : buffer_lengths) | |
142 bufs.push_back(new IOBuffer(buffer_length)); | |
143 | |
144 std::vector<TestCompletionCallback> callbacks(buffer_lengths.size()); | |
145 | |
146 // Multiple transactions should be able to read with different sized | |
147 // buffers. | |
148 for (size_t i = 0; i < transactions_.size(); i++) { | |
149 rv = writers_->Read(bufs[i].get(), buffer_lengths[i], | |
150 callbacks[i].callback(), transactions_[i].get()); | |
151 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. | |
152 } | |
153 | |
154 // If first buffer is smaller, then the second one will only read the | |
155 // smaller length as well. | |
156 std::vector<size_t> expected_lengths = { | |
jkarlin
2017/07/12 15:05:26
Why size_t? buffer_lengths is int, and you need it
shivanisha
2017/07/13 18:35:50
Changed to int.
| |
157 buffer_lengths[0], buffer_lengths[0] < buffer_lengths[1] | |
158 ? buffer_lengths[0] | |
159 : buffer_lengths[1]}; | |
160 | |
161 for (size_t i = 0; i < callbacks.size(); i++) { | |
162 rv = callbacks[i].WaitForResult(); | |
163 EXPECT_EQ(expected_lengths[i], (size_t)rv); | |
jkarlin
2017/07/12 15:05:26
static_cast, but you shouldn't need to cat if you
shivanisha
2017/07/13 18:35:50
Done
| |
164 results[i].append(bufs[i]->data(), expected_lengths[i]); | |
165 } | |
166 | |
167 if (expected_lengths[0] == expected_lengths[1]) { | |
jkarlin
2017/07/12 15:05:26
Why these checks? They're redundant with expected_
shivanisha
2017/07/13 18:35:50
Replaced with just one check:
EXPECT_EQ(results[0]
| |
168 EXPECT_EQ(results[0], results[1]); | |
169 } else { | |
170 EXPECT_EQ(results[0].substr(0, expected_lengths[1]), results[1]); | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
So it took me a moment to recognize that this was
shivanisha
2017/07/13 18:35:50
Replaced with just one check:
EXPECT_EQ(results[0]
| |
171 } | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
Since we're doing copying (i.e. touching the data)
shivanisha
2017/07/13 18:35:51
done
| |
172 } | |
173 | |
174 int ReadAllDeleteTransaction(DeleteTransactionType deleteType) { | |
jkarlin
2017/07/12 15:05:26
This method could use a comment
shivanisha
2017/07/13 18:35:50
done
| |
175 EXPECT_TRUE(transactions_.size() >= 3u); | |
176 | |
177 unsigned int delete_index = 0; | |
178 switch (deleteType) { | |
179 case DeleteTransactionType::NONE: | |
180 break; | |
181 case DeleteTransactionType::ACTIVE: | |
182 delete_index = 0; | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
Why set it explicitly for this value and not for N
shivanisha
2017/07/13 18:35:50
Changed the default value to std::numeric_limits<i
| |
183 break; | |
184 case DeleteTransactionType::WAITING: | |
185 delete_index = 1; | |
186 break; | |
187 case DeleteTransactionType::IDLE: | |
188 delete_index = 2; | |
189 break; | |
190 } | |
191 | |
192 std::vector<std::string> results(transactions_.size()); | |
193 int rv = 0; | |
194 bool first_iter = true; | |
195 do { | |
196 std::vector<scoped_refptr<IOBuffer>> bufs; | |
197 std::vector<TestCompletionCallback> callbacks(transactions_.size()); | |
198 | |
199 for (size_t i = 0; i < transactions_.size(); i++) { | |
200 bufs.push_back(new IOBuffer(256)); | |
201 if (!first_iter && deleteType != DeleteTransactionType::NONE && | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
nit, suggestion: Explanatory comment?
shivanisha
2017/07/13 18:35:51
done.
| |
202 i == delete_index) | |
203 continue; | |
204 | |
205 // For it to be an idle transaction, do not invoke Read. | |
206 if (deleteType == DeleteTransactionType::IDLE && i == delete_index) | |
207 continue; | |
208 | |
209 rv = writers_->Read(bufs[i].get(), 256, callbacks[i].callback(), | |
210 transactions_[i].get()); | |
211 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. | |
212 } | |
213 | |
214 if (first_iter && deleteType != DeleteTransactionType::NONE) { | |
215 writers_->RemoveTransaction(transactions_.at(delete_index).get()); | |
216 } | |
217 | |
218 std::vector<int> rvs; | |
219 for (size_t i = 0; i < callbacks.size(); i++) { | |
220 if (i == delete_index && deleteType != DeleteTransactionType::NONE) | |
221 continue; | |
222 rv = callbacks[i].WaitForResult(); | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
nit, suggestion: I personally wouldn't bother with
| |
223 rvs.push_back(rv); | |
224 } | |
225 | |
226 // Verify all transactions should read the same length buffer. | |
227 for (size_t i = 1; i < rvs.size(); i++) { | |
228 EXPECT_EQ(rvs[i - 1], rvs[i]); | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
Given that the code below is pretty clearly assumi
shivanisha
2017/07/13 18:35:51
Changed to an ASSERT and also had to change the re
| |
229 } | |
230 | |
231 if (rv > 0) { | |
Randy Smith (Not in Mondays)
2017/07/11 18:51:33
Given that the above is an EXPECT_EQ rather than a
shivanisha
2017/07/13 18:35:50
Changed to assert above
| |
232 for (size_t i = 0; i < results.size(); i++) { | |
233 if (i == delete_index && deleteType != DeleteTransactionType::NONE && | |
234 deleteType != DeleteTransactionType::ACTIVE) { | |
235 continue; | |
236 } | |
237 results.at(i).append(bufs[i]->data(), rv); | |
238 } | |
239 } | |
240 first_iter = false; | |
241 } while (rv > 0); | |
242 | |
243 for (size_t i = 0; i < results.size(); i++) { | |
244 if (i == delete_index && deleteType != DeleteTransactionType::NONE && | |
245 deleteType != DeleteTransactionType::ACTIVE) { | |
246 continue; | |
247 } | |
248 EXPECT_EQ(kSimpleGET_Transaction.data, results[i]); | |
249 } | |
250 | |
251 return rv; | |
252 } | |
253 | |
254 int ReadCacheWriteFailure(std::vector<std::string>* results) { | |
255 int rv = 0; | |
256 int active_transaction_rv = 0; | |
257 bool first_iter = true; | |
258 do { | |
259 std::vector<scoped_refptr<IOBuffer>> bufs; | |
260 std::vector<TestCompletionCallback> callbacks(results->size()); | |
261 | |
262 // Fail the request. | |
263 cache_.disk_cache()->set_soft_failures(true); | |
264 | |
265 // We have to open the entry again to propagate the failure flag. | |
266 disk_cache::Entry* en; | |
267 cache_.OpenBackendEntry(kSimpleGET_Transaction.url, &en); | |
268 en->Close(); | |
269 | |
270 for (size_t i = 0; i < transactions_.size(); i++) { | |
271 bufs.push_back(new IOBuffer(30)); | |
272 | |
273 if (!first_iter && i > 0) | |
274 break; | |
275 rv = writers_->Read(bufs[i].get(), 30, callbacks[i].callback(), | |
276 transactions_[i].get()); | |
277 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. | |
278 } | |
279 | |
280 for (size_t i = 0; i < callbacks.size(); i++) { | |
281 // Only active transaction should succeed. | |
282 if (i == 0) { | |
283 active_transaction_rv = callbacks[i].WaitForResult(); | |
284 EXPECT_TRUE(active_transaction_rv >= 0); | |
285 results->at(0).append(bufs[i]->data(), active_transaction_rv); | |
286 } else if (first_iter) { | |
287 rv = callbacks[i].WaitForResult(); | |
288 EXPECT_EQ(ERR_CACHE_WRITE_FAILURE, rv); | |
289 } | |
290 } | |
291 | |
292 first_iter = false; | |
293 } while (active_transaction_rv > 0); | |
294 | |
295 return active_transaction_rv; | |
296 } | |
297 | |
298 int ReadNetworkFailure(std::vector<std::string>* results, Error error) { | |
299 int rv = 0; | |
300 std::vector<scoped_refptr<IOBuffer>> bufs; | |
301 std::vector<TestCompletionCallback> callbacks(results->size()); | |
302 | |
303 for (size_t i = 0; i < transactions_.size(); i++) { | |
304 bufs.push_back(new IOBuffer(30)); | |
305 | |
306 rv = writers_->Read(bufs[i].get(), 30, callbacks[i].callback(), | |
307 transactions_[i].get()); | |
308 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. | |
309 } | |
310 | |
311 for (auto& callback : callbacks) { | |
312 rv = callback.WaitForResult(); | |
313 EXPECT_EQ(error, rv); | |
314 } | |
315 | |
316 return error; | |
317 } | |
318 | |
319 bool StopCaching() { | |
320 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); | |
321 EXPECT_TRUE(transaction); | |
322 return writers_->StopCaching(transaction); | |
323 } | |
324 | |
325 void RemoveFirstTransaction() { | |
326 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); | |
327 EXPECT_TRUE(transaction); | |
328 writers_->RemoveTransaction(transaction); | |
329 } | |
330 | |
331 void UpdateAndVerifyPriority(RequestPriority priority) { | |
332 writers_->UpdatePriority(); | |
333 EXPECT_EQ(priority, writers_->priority_); | |
334 } | |
335 | |
336 MockHttpCache cache_; | |
337 std::unique_ptr<HttpCache::Writers> writers_; | |
338 disk_cache::Entry* disk_entry_; | |
339 | |
340 // Should be before transactions_ since it is accessed in the network | |
341 // transaction's destructor. | |
342 MockHttpRequest request_; | |
343 | |
344 std::vector<std::unique_ptr<MockHttpCacheTransaction>> transactions_; | |
345 }; | |
346 | |
347 // Tests successful addition of a transaction. | |
348 TEST_F(WritersTest, AddTransaction) { | |
349 CreateWritersAddTransaction(); | |
350 EXPECT_FALSE(writers_->IsEmpty()); | |
351 } | |
352 | |
353 // Tests successful addition of multiple transactions. | |
354 TEST_F(WritersTest, AddManyTransactions) { | |
355 CreateWritersAddTransaction(); | |
356 EXPECT_FALSE(writers_->IsEmpty()); | |
357 | |
358 for (int i = 0; i < 5; i++) | |
359 AddTransactionToExistingWriters(); | |
360 | |
361 EXPECT_EQ(6, writers_->CountTransactionsForTesting()); | |
362 } | |
363 | |
364 // Tests that CanAddWriters should return false if it is writing exclusively. | |
365 TEST_F(WritersTest, AddTransactionsExclusive) { | |
366 CreateWritersAddTransaction(true /* is_exclusive */); | |
367 EXPECT_FALSE(writers_->IsEmpty()); | |
368 | |
369 EXPECT_FALSE(writers_->CanAddWriters()); | |
370 } | |
371 | |
372 // Tests StopCaching should not stop caching if there are multiple writers. | |
373 TEST_F(WritersTest, StopCachingMultipleWriters) { | |
374 CreateWritersAddTransaction(); | |
375 EXPECT_FALSE(writers_->IsEmpty()); | |
376 | |
377 EXPECT_TRUE(writers_->CanAddWriters()); | |
378 AddTransactionToExistingWriters(); | |
379 | |
380 EXPECT_FALSE(StopCaching()); | |
381 EXPECT_TRUE(writers_->CanAddWriters()); | |
382 } | |
383 | |
384 // Tests StopCaching should stop caching if there is a single writer. | |
385 TEST_F(WritersTest, StopCaching) { | |
386 CreateWritersAddTransaction(); | |
387 EXPECT_FALSE(writers_->IsEmpty()); | |
388 | |
389 EXPECT_TRUE(StopCaching()); | |
390 EXPECT_FALSE(writers_->CanAddWriters()); | |
391 } | |
392 | |
393 // Tests removing of an idle transaction and change in priority. | |
394 TEST_F(WritersTest, RemoveIdleTransaction) { | |
395 CreateWritersAddTransactionPriority(HIGHEST); | |
396 UpdateAndVerifyPriority(HIGHEST); | |
397 | |
398 AddTransactionToExistingWriters(); | |
399 UpdateAndVerifyPriority(HIGHEST); | |
400 | |
401 EXPECT_FALSE(writers_->IsEmpty()); | |
402 EXPECT_EQ(2, writers_->CountTransactionsForTesting()); | |
403 | |
404 RemoveFirstTransaction(); | |
405 EXPECT_EQ(1, writers_->CountTransactionsForTesting()); | |
406 | |
407 UpdateAndVerifyPriority(DEFAULT_PRIORITY); | |
408 } | |
409 | |
410 // Tests that Read is successful. | |
411 TEST_F(WritersTest, Read) { | |
412 CreateWritersAddTransaction(); | |
413 EXPECT_FALSE(writers_->IsEmpty()); | |
414 | |
415 std::string content; | |
416 int rv = Read(&content); | |
417 | |
418 EXPECT_THAT(rv, IsOk()); | |
419 std::string expected(kSimpleGET_Transaction.data); | |
420 EXPECT_EQ(expected, content); | |
421 } | |
422 | |
423 // Tests that multiple transactions can read the same data simultaneously. | |
424 TEST_F(WritersTest, ReadMultiple) { | |
425 CreateWritersAddTransaction(); | |
426 EXPECT_FALSE(writers_->IsEmpty()); | |
427 | |
428 EXPECT_TRUE(writers_->CanAddWriters()); | |
429 AddTransactionToExistingWriters(); | |
430 AddTransactionToExistingWriters(); | |
431 | |
432 int rv = ReadAll(); | |
433 EXPECT_THAT(rv, IsOk()); | |
434 } | |
435 | |
436 // Tests that multiple transactions can read the same data simultaneously. | |
437 TEST_F(WritersTest, ReadMultipleDifferentBufferSizes) { | |
438 CreateWritersAddTransaction(); | |
439 EXPECT_FALSE(writers_->IsEmpty()); | |
440 | |
441 EXPECT_TRUE(writers_->CanAddWriters()); | |
442 AddTransactionToExistingWriters(); | |
443 | |
444 std::vector<int> buffer_lengths{20, 10}; | |
445 ReadVerifyTwoDifferentBufferLengths(buffer_lengths); | |
446 } | |
447 | |
448 // Same as above but tests the first transaction having smaller buffer size | |
449 // than the next. | |
450 TEST_F(WritersTest, ReadMultipleDifferentBufferSizes1) { | |
451 CreateWritersAddTransaction(); | |
452 EXPECT_FALSE(writers_->IsEmpty()); | |
453 | |
454 EXPECT_TRUE(writers_->CanAddWriters()); | |
455 AddTransactionToExistingWriters(); | |
456 | |
457 std::vector<int> buffer_lengths{10, 20}; | |
458 ReadVerifyTwoDifferentBufferLengths(buffer_lengths); | |
459 } | |
460 | |
461 // Tests that ongoing Read completes even when active transaction is deleted | |
462 // mid-read. Any transactions waiting should be able to get the read buffer. | |
463 TEST_F(WritersTest, ReadMultipleDeleteActiveTransaction) { | |
464 CreateWritersAddTransaction(); | |
465 EXPECT_FALSE(writers_->IsEmpty()); | |
466 | |
467 EXPECT_TRUE(writers_->CanAddWriters()); | |
468 AddTransactionToExistingWriters(); | |
469 AddTransactionToExistingWriters(); | |
470 | |
471 int rv = ReadAllDeleteTransaction(DeleteTransactionType::ACTIVE); | |
472 EXPECT_THAT(rv, IsOk()); | |
473 } | |
474 | |
475 // Tests that removing a waiting for read transaction does not impact other | |
476 // transactions. | |
477 TEST_F(WritersTest, ReadMultipleDeleteWaitingTransaction) { | |
478 CreateWritersAddTransaction(); | |
479 EXPECT_FALSE(writers_->IsEmpty()); | |
480 | |
481 EXPECT_TRUE(writers_->CanAddWriters()); | |
482 AddTransactionToExistingWriters(); | |
483 AddTransactionToExistingWriters(); | |
484 AddTransactionToExistingWriters(); | |
485 | |
486 std::vector<std::string> contents(4); | |
487 int rv = ReadAllDeleteTransaction(DeleteTransactionType::WAITING); | |
488 EXPECT_THAT(rv, IsOk()); | |
489 } | |
490 | |
491 // Tests that removing an idle transaction does not impact other transactions. | |
492 TEST_F(WritersTest, ReadMultipleDeleteIdleTransaction) { | |
493 CreateWritersAddTransaction(); | |
494 EXPECT_FALSE(writers_->IsEmpty()); | |
495 | |
496 EXPECT_TRUE(writers_->CanAddWriters()); | |
497 AddTransactionToExistingWriters(); | |
498 AddTransactionToExistingWriters(); | |
499 | |
500 std::vector<std::string> contents(3); | |
501 int rv = ReadAllDeleteTransaction(DeleteTransactionType::IDLE); | |
502 EXPECT_THAT(rv, IsOk()); | |
503 } | |
504 | |
505 // Tests cache write failure. | |
506 TEST_F(WritersTest, ReadMultipleCacheWriteFailed) { | |
507 CreateWritersAddTransaction(); | |
508 EXPECT_FALSE(writers_->IsEmpty()); | |
509 | |
510 EXPECT_TRUE(writers_->CanAddWriters()); | |
511 AddTransactionToExistingWriters(); | |
512 AddTransactionToExistingWriters(); | |
513 | |
514 std::vector<std::string> contents(3); | |
515 int rv = ReadCacheWriteFailure(&contents); | |
516 | |
517 EXPECT_THAT(rv, IsOk()); | |
518 std::string expected(kSimpleGET_Transaction.data); | |
519 | |
520 // Only active_transaction_ should succeed. | |
521 EXPECT_EQ(expected, contents.at(0)); | |
522 | |
523 // No new transactions should now be added. | |
524 EXPECT_FALSE(writers_->CanAddWriters()); | |
525 } | |
526 | |
527 // Tests that network read failure fails all transactions: active, waiting and | |
528 // idle. | |
529 TEST_F(WritersTest, ReadMultipleNetworkReadFailed) { | |
530 ScopedMockTransaction transaction(kSimpleGET_Transaction); | |
531 transaction.read_return_code = ERR_INTERNET_DISCONNECTED; | |
532 MockHttpRequest request(transaction); | |
533 request_ = request; | |
534 | |
535 CreateWritersAddTransaction(); | |
536 EXPECT_FALSE(writers_->IsEmpty()); | |
537 | |
538 EXPECT_TRUE(writers_->CanAddWriters()); | |
539 AddTransactionToExistingWriters(); | |
540 AddTransactionToExistingWriters(); | |
541 | |
542 std::vector<std::string> contents(3); | |
543 int rv = ReadNetworkFailure(&contents, ERR_INTERNET_DISCONNECTED); | |
544 | |
545 EXPECT_EQ(ERR_INTERNET_DISCONNECTED, rv); | |
546 } | |
547 | |
548 // Tests moving idle writers to readers. | |
549 TEST_F(WritersTest, MoveIdleWritersToReaders) { | |
550 CreateWritersAddTransaction(); | |
551 EXPECT_FALSE(writers_->IsEmpty()); | |
552 | |
553 EXPECT_TRUE(writers_->CanAddWriters()); | |
554 AddTransactionToExistingWriters(); | |
555 AddTransactionToExistingWriters(); | |
556 | |
557 EXPECT_FALSE(writers_->IsEmpty()); | |
558 writers_->RemoveAllIdleWriters(); | |
559 EXPECT_TRUE(writers_->IsEmpty()); | |
560 } | |
561 | |
562 // Tests GetWriterLoadState. | |
563 TEST_F(WritersTest, GetWriterLoadState) { | |
564 CreateWritersAddTransaction(); | |
565 EXPECT_FALSE(writers_->IsEmpty()); | |
566 | |
567 EXPECT_EQ(LOAD_STATE_IDLE, writers_->GetWriterLoadState()); | |
568 } | |
569 | |
570 // Tests truncating the entry via Writers. | |
571 TEST_F(WritersTest, TruncateEntry) { | |
572 CreateWritersAddTransaction(); | |
573 EXPECT_FALSE(writers_->IsEmpty()); | |
574 | |
575 std::string content; | |
576 int rv = Read(&content); | |
577 | |
578 EXPECT_THAT(rv, IsOk()); | |
579 std::string expected(kSimpleGET_Transaction.data); | |
580 EXPECT_EQ(expected, content); | |
581 | |
582 writers_->TruncateEntry(); | |
583 base::RunLoop().RunUntilIdle(); | |
584 EXPECT_TRUE(writers_->IsTruncatedForTesting()); | |
585 } | |
586 | |
587 } // namespace net | |
OLD | NEW |