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 WritersTest : public testing::Test { | |
25 public: | |
26 WritersTest() : request_(kSimpleGET_Transaction) {} | |
27 | |
28 ~WritersTest() override { cache_.disk_cache()->ReleaseAll(); } | |
jkarlin
2017/06/28 15:50:10
This call to ReleaseAll isn't necessary, the mock
shivanisha
2017/06/28 19:38:21
Yes, removed it
| |
29 | |
30 // Since ActiveEntry and Transaction are private classes of HttpCache, | |
31 // accessing those only in this class instead of in tests. | |
jkarlin
2017/06/28 15:50:10
This comment is kind of floating and I don't think
shivanisha
2017/06/28 19:38:20
removed
| |
32 | |
33 void CreateWriters(const std::string& url) { | |
34 disk_cache::Entry* disk_entry = nullptr; | |
jkarlin
2017/06/28 15:50:10
You need to Close() each entry when you're done wi
shivanisha
2017/06/28 19:38:20
done
| |
35 cache_.CreateBackendEntry(kSimpleGET_Transaction.url, &disk_entry, nullptr); | |
36 writers_ = base::MakeUnique<HttpCache::Writers>(disk_entry); | |
Randy Smith (Not in Mondays)
2017/06/27 18:50:56
Sorry, missed this on the last round. Josh sugges
shivanisha
2017/06/27 19:30:41
cache_.CreateBackendEntry is creating a MockDiskEn
Randy Smith (Not in Mondays)
2017/06/28 15:48:27
Ooops. Ok, ignore this note :-}.
| |
37 } | |
38 | |
39 std::unique_ptr<HttpTransaction> CreateNetworkTransaction() { | |
40 std::unique_ptr<HttpTransaction> transaction; | |
41 MockNetworkLayer* network_layer = cache_.network_layer(); | |
42 network_layer->CreateTransaction(DEFAULT_PRIORITY, &transaction); | |
43 return transaction; | |
44 } | |
45 | |
46 void CreateWritersAddTransaction(bool is_exclusive = false) { | |
47 std::unique_ptr<HttpTransaction> transaction; | |
48 int rv = cache_.CreateTransaction(&transaction); | |
49 DCHECK_EQ(rv, OK); | |
jkarlin
2017/06/28 15:50:10
Avoid using DCHECK* macros in tests, as they crash
shivanisha
2017/06/28 19:38:20
done
| |
50 DCHECK(transaction.get()); | |
jkarlin
2017/06/28 15:50:10
s/transaction.get()/transaction/
shivanisha
2017/06/28 19:38:21
done
| |
51 | |
52 TestCompletionCallback callback; | |
53 | |
54 CreateWriters(kSimpleGET_Transaction.url); | |
55 | |
56 // Create and Start a mock network transaction. | |
57 std::unique_ptr<HttpTransaction> network_transaction; | |
58 network_transaction = CreateNetworkTransaction(); | |
59 network_transaction->Start(&request_, callback.callback(), | |
60 NetLogWithSource()); | |
61 base::RunLoop().RunUntilIdle(); | |
62 | |
63 DCHECK(writers_->IsEmpty()); | |
64 writers_->AddTransaction((HttpCache::Transaction*)(transaction.get()), | |
jkarlin
2017/06/28 15:50:10
This cast (here and elsewhere) makes me nervous, a
shivanisha
2017/06/28 19:38:21
But MockHttpCache::CreateTransaction invokes HttpC
jkarlin
2017/06/30 18:20:39
Ah.. because we're passing unique_ptrs to things a
| |
65 std::move(network_transaction), is_exclusive); | |
66 transactions_.push_back(std::move(transaction)); | |
67 } | |
68 | |
69 void AddTransactionToExistingWriters() { | |
70 std::unique_ptr<HttpTransaction> transaction; | |
71 int rv = cache_.CreateTransaction(&transaction); | |
72 DCHECK_EQ(rv, OK); | |
73 DCHECK(transaction.get()); | |
74 | |
75 DCHECK(writers_); | |
76 writers_->AddTransaction((HttpCache::Transaction*)(transaction.get()), | |
77 nullptr, false); | |
78 transactions_.push_back(std::move(transaction)); | |
79 } | |
80 | |
81 int Read(std::string* result) { | |
82 DCHECK_GE(transactions_.size(), (size_t)1); | |
83 HttpCache::Transaction* transaction = | |
84 (HttpCache::Transaction*)(transactions_.begin()->get()); | |
85 TestCompletionCallback callback; | |
86 | |
87 std::string content; | |
88 int rv = 0; | |
89 do { | |
90 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | |
91 rv = writers_->Read(buf.get(), 256, callback.callback(), transaction); | |
92 if (rv == ERR_IO_PENDING) { | |
93 rv = callback.WaitForResult(); | |
94 base::RunLoop().RunUntilIdle(); | |
95 } | |
96 | |
97 if (rv > 0) | |
98 content.append(buf->data(), rv); | |
99 else if (rv < 0) | |
100 return rv; | |
101 } while (rv > 0); | |
102 | |
103 result->swap(content); | |
104 return OK; | |
105 } | |
106 | |
107 int ReadAll(std::vector<std::string>& results) { | |
jkarlin
2017/06/28 15:50:10
output parameters should be raw pointers here and
shivanisha
2017/06/28 19:38:21
done
| |
108 int rv = 0; | |
109 do { | |
110 std::vector<scoped_refptr<IOBuffer>> bufs; | |
111 std::vector<TestCompletionCallback> callbacks(results.size()); | |
112 | |
113 // Multiple transactions should be able to read. | |
114 for (size_t i = 0; i < transactions_.size(); i++) { | |
115 bufs.push_back(new IOBuffer(256)); | |
116 rv = writers_->Read(bufs[i].get(), 256, callbacks[i].callback(), | |
117 (HttpCache::Transaction*)transactions_[i].get()); | |
118 EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. | |
119 } | |
120 | |
121 if (rv == ERR_IO_PENDING) { | |
122 int prev_rv = callbacks[0].WaitForResult(); | |
123 for (size_t i = 1; i < callbacks.size(); i++) { | |
124 rv = callbacks[i].WaitForResult(); | |
125 EXPECT_EQ(rv, prev_rv); | |
126 prev_rv = rv; | |
127 } | |
128 } | |
129 | |
130 if (rv > 0) { | |
131 for (size_t i = 0; i < results.size(); i++) | |
132 results[i].append(bufs[i]->data(), rv); | |
133 } else if (rv <= 0) { | |
134 return rv; | |
135 } | |
136 } while (rv > 0); | |
137 | |
138 return OK; | |
139 } | |
140 | |
141 int ReadAllDeleteActiveTransaction(std::vector<std::string>& results) { | |
142 int rv = 0; | |
143 bool first_iter = true; | |
144 do { | |
145 std::vector<scoped_refptr<IOBuffer>> bufs; | |
146 std::vector<TestCompletionCallback> callbacks(results.size()); | |
147 | |
148 // Multiple transactions should be able to read. | |
149 for (size_t i = 0; i < transactions_.size(); i++) { | |
150 bufs.push_back(new IOBuffer(256)); | |
151 if (!first_iter && i == 0) | |
152 continue; | |
153 | |
154 rv = writers_->Read(bufs[i].get(), 256, callbacks[i].callback(), | |
155 (HttpCache::Transaction*)transactions_[i].get()); | |
156 EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. | |
157 } | |
158 | |
159 if (rv == ERR_IO_PENDING) { | |
160 if (first_iter) { | |
161 // Delete active transaction. | |
162 RemoveFirstTransaction(); | |
163 } | |
164 int prev_rv = callbacks[1].WaitForResult(); | |
165 for (size_t i = 2; i < callbacks.size(); i++) { | |
166 rv = callbacks[i].WaitForResult(); | |
167 EXPECT_EQ(rv, prev_rv); | |
168 prev_rv = rv; | |
169 } | |
170 } | |
171 | |
172 if (rv > 0) { | |
173 for (size_t i = 0; i < results.size(); i++) | |
174 results[i].append(bufs[i]->data(), rv); | |
175 } | |
176 first_iter = false; | |
177 } while (rv > 0); | |
178 | |
179 return rv; | |
180 } | |
181 | |
182 int ReadAllDeleteWaitingTransaction(std::vector<std::string>& results, | |
jkarlin
2017/06/28 15:50:10
output params come after input params here and bel
shivanisha
2017/06/28 19:38:21
done
| |
183 size_t waiting_index) { | |
184 int rv = 0; | |
185 bool first_iter = true; | |
186 do { | |
187 std::vector<scoped_refptr<IOBuffer>> bufs; | |
188 std::vector<TestCompletionCallback> callbacks(results.size()); | |
189 | |
190 // Multiple transactions should be able to read. | |
191 for (size_t i = 0; i < transactions_.size(); i++) { | |
192 bufs.push_back(new IOBuffer(256)); | |
193 if (!first_iter && i == waiting_index) | |
194 continue; | |
195 | |
196 rv = writers_->Read(bufs[i].get(), 256, callbacks[i].callback(), | |
197 (HttpCache::Transaction*)transactions_[i].get()); | |
198 EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. | |
199 } | |
200 | |
201 if (rv == ERR_IO_PENDING) { | |
202 if (first_iter) { | |
203 // Delete waiting transaction. | |
204 writers_->RemoveTransaction( | |
205 (HttpCache::Transaction*)transactions_.at(waiting_index).get()); | |
206 } | |
207 for (size_t j = 0; j < callbacks.size(); j++) { | |
208 if (j == waiting_index) | |
209 continue; | |
210 rv = callbacks[j].WaitForResult(); | |
Randy Smith (Not in Mondays)
2017/06/27 18:49:37
Confused--this looks like we throw away all rvs ex
shivanisha
2017/06/28 18:02:33
Added asserts to check that same rv is returned by
| |
211 } | |
212 } | |
213 | |
214 if (rv > 0) { | |
215 size_t i = 0; | |
216 for (auto& result : results) { | |
Randy Smith (Not in Mondays)
2017/06/27 18:49:37
Missed a case on the "Increment i && use for (auto
shivanisha
2017/06/28 18:02:33
This was missed, changed now.
| |
217 if (i == waiting_index) { | |
218 i++; | |
219 continue; | |
220 } | |
221 result.append(bufs[i]->data(), rv); | |
222 i++; | |
223 } | |
224 } | |
225 first_iter = false; | |
226 } while (rv > 0); | |
227 | |
228 return rv; | |
229 } | |
230 | |
231 int ReadAllDeleteIdleTransaction(std::vector<std::string>& results, | |
232 size_t idle_index) { | |
233 int rv = 0; | |
234 bool first_iter = true; | |
235 do { | |
236 std::vector<scoped_refptr<IOBuffer>> bufs; | |
237 std::vector<TestCompletionCallback> callbacks(results.size()); | |
238 | |
239 // Multiple transactions should be able to read. | |
240 for (size_t i = 0; i < transactions_.size(); i++) { | |
241 bufs.push_back(new IOBuffer(256)); | |
242 if (i == idle_index) | |
243 continue; | |
244 | |
245 rv = writers_->Read(bufs[i].get(), 256, callbacks[i].callback(), | |
246 (HttpCache::Transaction*)transactions_[i].get()); | |
247 EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. | |
248 } | |
249 | |
250 if (rv == ERR_IO_PENDING) { | |
251 if (first_iter) { | |
252 // Delete idle transaction. | |
253 writers_->RemoveTransaction( | |
254 (HttpCache::Transaction*)transactions_.at(idle_index).get()); | |
255 } | |
256 for (size_t j = 0; j < callbacks.size(); j++) { | |
257 if (j == idle_index) | |
258 continue; | |
259 rv = callbacks[j].WaitForResult(); | |
260 } | |
261 } | |
262 | |
263 if (rv > 0) { | |
264 size_t i = 0; | |
265 for (auto& result : results) { | |
Randy Smith (Not in Mondays)
2017/06/27 18:49:37
Missed a case on the "Increment i && use for (auto
shivanisha
2017/06/28 18:02:33
Changed. Also did a search on all uses of auto to
| |
266 if (i == idle_index) { | |
267 i++; | |
268 continue; | |
269 } | |
270 result.append(bufs[i]->data(), rv); | |
271 i++; | |
272 } | |
273 } | |
274 first_iter = false; | |
275 } while (rv > 0); | |
276 | |
277 return rv; | |
278 } | |
279 | |
280 int ReadCacheWriteFailure(std::vector<std::string>& results) { | |
281 int rv = 0; | |
282 bool first_iter = true; | |
283 do { | |
284 std::vector<scoped_refptr<IOBuffer>> bufs; | |
285 std::vector<TestCompletionCallback> callbacks(results.size()); | |
286 | |
287 // Fail the request. | |
288 cache_.disk_cache()->set_soft_failures(true); | |
289 | |
290 // We have to open the entry again to propagate the failure flag. | |
291 disk_cache::Entry* en; | |
292 cache_.OpenBackendEntry(kSimpleGET_Transaction.url, &en); | |
293 en->Close(); | |
294 | |
295 for (size_t i = 0; i < transactions_.size(); i++) { | |
296 bufs.push_back(new IOBuffer(30)); | |
297 | |
298 if (!first_iter && i > 0) | |
299 break; | |
300 rv = writers_->Read(bufs[i].get(), 30, callbacks[i].callback(), | |
301 (HttpCache::Transaction*)transactions_[i].get()); | |
302 EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. | |
303 } | |
304 | |
305 if (rv == ERR_IO_PENDING) { | |
306 int i = 0; | |
307 for (auto& callback : callbacks) { | |
Randy Smith (Not in Mondays)
2017/06/27 18:49:37
Missed a case on the "Increment i && use for (auto
shivanisha
2017/06/28 18:02:33
Changed to use index in for loop
| |
308 // Only active transaction should succeed. | |
309 if (i == 0) { | |
310 rv = callback.WaitForResult(); | |
311 EXPECT_TRUE(rv >= 0); | |
312 results[0].append(bufs[i]->data(), rv); | |
313 if (rv == 0) | |
314 return rv; | |
Randy Smith (Not in Mondays)
2017/06/27 18:49:37
Is this because we don't expect to get an rv of ze
shivanisha
2017/06/28 18:02:33
rv == 0 implies the end of reading by the active t
| |
315 } else if (first_iter) { | |
316 rv = callback.WaitForResult(); | |
317 EXPECT_EQ(ERR_CACHE_WRITE_FAILURE, rv); | |
318 } | |
319 i++; | |
320 } | |
321 } | |
322 | |
323 first_iter = false; | |
324 } while (true); | |
325 | |
326 return OK; | |
327 } | |
328 | |
329 int ReadNetworkFailure(std::vector<std::string>& results) { | |
330 int rv = 0; | |
331 std::vector<scoped_refptr<IOBuffer>> bufs; | |
332 std::vector<TestCompletionCallback> callbacks(results.size()); | |
333 | |
334 // Fail the request. | |
335 MockNetworkTransaction* network_transaction = | |
336 (MockNetworkTransaction*)(writers_->network_transaction()); | |
337 network_transaction->SetReadError(ERR_INTERNET_DISCONNECTED); | |
338 | |
339 for (size_t i = 0; i < transactions_.size(); i++) { | |
340 bufs.push_back(new IOBuffer(30)); | |
341 | |
342 rv = writers_->Read(bufs[i].get(), 30, callbacks[i].callback(), | |
343 (HttpCache::Transaction*)transactions_[i].get()); | |
344 EXPECT_EQ(rv, ERR_IO_PENDING); // Since the default is asynchronous. | |
345 } | |
346 | |
347 if (rv == ERR_IO_PENDING) { | |
348 for (auto& callback : callbacks) { | |
349 rv = callback.WaitForResult(); | |
350 EXPECT_EQ(ERR_INTERNET_DISCONNECTED, rv); | |
351 } | |
352 } | |
353 | |
354 return ERR_INTERNET_DISCONNECTED; | |
355 } | |
356 | |
357 bool StopCaching() { | |
358 HttpCache::Transaction* transaction = | |
359 (HttpCache::Transaction*)(transactions_.begin()->get()); | |
360 DCHECK(transaction); | |
361 return writers_->StopCaching(transaction); | |
362 } | |
363 | |
364 void RemoveFirstTransaction() { | |
365 HttpCache::Transaction* transaction = | |
366 (HttpCache::Transaction*)(transactions_.begin()->get()); | |
367 DCHECK(transaction); | |
368 writers_->RemoveTransaction(transaction); | |
369 } | |
370 | |
371 MockHttpCache cache_; | |
372 std::unique_ptr<HttpCache::Writers> writers_; | |
373 | |
374 // Should be before transactions_ since it is accessed in the network | |
375 // transaction's destructor. | |
376 MockHttpRequest request_; | |
377 | |
378 std::vector<std::unique_ptr<HttpTransaction>> transactions_; | |
379 }; | |
380 | |
381 // Tests successful addition of a transaction. | |
382 TEST_F(WritersTest, AddTransaction) { | |
383 CreateWritersAddTransaction(); | |
384 EXPECT_FALSE(writers_->IsEmpty()); | |
385 } | |
386 | |
387 // Tests successful addition of multiple transaction. | |
388 TEST_F(WritersTest, AddManyTransactions) { | |
389 CreateWritersAddTransaction(); | |
390 EXPECT_FALSE(writers_->IsEmpty()); | |
391 | |
392 for (int i = 0; i < 5; i++) | |
393 AddTransactionToExistingWriters(); | |
394 | |
395 EXPECT_EQ(writers_->CountTransactionsForTesting(), 6); | |
396 } | |
397 | |
jkarlin
2017/06/28 15:50:10
I don't see a test for the public IsPresent functi
shivanisha
2017/06/28 19:38:20
Added in CreateWritersAddTransaction
| |
398 // Tests that CanAddWriters should return false if it is exclusive writing. | |
399 TEST_F(WritersTest, AddTransactionsExclusive) { | |
400 CreateWritersAddTransaction(true); | |
jkarlin
2017/06/28 15:50:10
nit: true /* is_exclusive */
shivanisha
2017/06/28 19:38:21
done
| |
401 EXPECT_FALSE(writers_->IsEmpty()); | |
402 | |
403 EXPECT_FALSE(writers_->CanAddWriters()); | |
404 } | |
405 | |
406 // Tests StopCaching should not stop caching if there are multiple writers. | |
407 TEST_F(WritersTest, StopCachingMultipleWriters) { | |
408 CreateWritersAddTransaction(); | |
409 EXPECT_FALSE(writers_->IsEmpty()); | |
410 | |
411 EXPECT_TRUE(writers_->CanAddWriters()); | |
412 AddTransactionToExistingWriters(); | |
413 | |
414 EXPECT_FALSE(StopCaching()); | |
415 EXPECT_TRUE(writers_->CanAddWriters()); | |
416 } | |
417 | |
jkarlin
2017/06/28 15:50:10
It'd be good to add a test for UpdatePriority. Sin
shivanisha
2017/06/28 19:38:21
Added in RemoveIdleTransaction
| |
418 // Tests StopCaching should stop caching if there is a single writer. | |
419 TEST_F(WritersTest, StopCaching) { | |
420 CreateWritersAddTransaction(); | |
421 EXPECT_FALSE(writers_->IsEmpty()); | |
422 | |
423 EXPECT_TRUE(StopCaching()); | |
424 EXPECT_FALSE(writers_->CanAddWriters()); | |
425 } | |
426 | |
427 // Tests removing of an idle transaction. | |
428 TEST_F(WritersTest, RemoveIdleTransaction) { | |
429 CreateWritersAddTransaction(); | |
430 EXPECT_FALSE(writers_->IsEmpty()); | |
431 | |
432 RemoveFirstTransaction(); | |
433 EXPECT_TRUE(writers_->IsEmpty()); | |
434 } | |
435 | |
436 // Tests that Read is successful. | |
437 TEST_F(WritersTest, Read) { | |
438 CreateWritersAddTransaction(); | |
439 EXPECT_FALSE(writers_->IsEmpty()); | |
440 | |
441 std::string content; | |
442 int rv = Read(&content); | |
443 | |
444 EXPECT_THAT(rv, IsOk()); | |
445 std::string expected(kSimpleGET_Transaction.data); | |
446 EXPECT_EQ(expected, content); | |
jkarlin
2017/06/28 15:50:10
nit: seems reasonable to combine these two lines h
| |
447 } | |
448 | |
449 // Tests that multiple transactions can read the same data simultaneously. | |
450 TEST_F(WritersTest, ReadMultiple) { | |
451 CreateWritersAddTransaction(); | |
452 EXPECT_FALSE(writers_->IsEmpty()); | |
453 | |
454 EXPECT_TRUE(writers_->CanAddWriters()); | |
455 AddTransactionToExistingWriters(); | |
456 | |
457 std::vector<std::string> contents(2); | |
458 int rv = ReadAll(contents); | |
459 | |
460 EXPECT_THAT(rv, IsOk()); | |
461 std::string expected(kSimpleGET_Transaction.data); | |
462 | |
463 for (auto content : contents) | |
464 EXPECT_EQ(expected, content); | |
465 } | |
466 | |
467 // Tests that ongoing Read completes even when active transaction is deleted | |
468 // mid-read. Any transactions waiting should be able to get the read buffer. | |
469 TEST_F(WritersTest, ReadMultipleDeleteActiveTransaction) { | |
470 CreateWritersAddTransaction(); | |
471 EXPECT_FALSE(writers_->IsEmpty()); | |
472 | |
473 EXPECT_TRUE(writers_->CanAddWriters()); | |
474 AddTransactionToExistingWriters(); | |
475 AddTransactionToExistingWriters(); | |
476 | |
477 std::vector<std::string> contents(3); | |
478 int rv = ReadAllDeleteActiveTransaction(contents); | |
479 | |
480 EXPECT_THAT(rv, IsOk()); | |
481 std::string expected(kSimpleGET_Transaction.data); | |
482 | |
483 for (auto content : contents) | |
484 EXPECT_EQ(expected, content); | |
485 } | |
486 | |
487 // Tests that removing a waiting for read transaction does not impact other | |
488 // transactions. | |
489 TEST_F(WritersTest, ReadMultipleDeleteWaitingTransaction) { | |
490 CreateWritersAddTransaction(); | |
491 EXPECT_FALSE(writers_->IsEmpty()); | |
492 | |
493 EXPECT_TRUE(writers_->CanAddWriters()); | |
494 AddTransactionToExistingWriters(); | |
495 AddTransactionToExistingWriters(); | |
496 AddTransactionToExistingWriters(); | |
497 | |
498 std::vector<std::string> contents(4); | |
499 size_t waiting_index = 1; | |
500 int rv = ReadAllDeleteWaitingTransaction(contents, waiting_index); | |
501 | |
502 EXPECT_THAT(rv, IsOk()); | |
503 std::string expected(kSimpleGET_Transaction.data); | |
504 | |
505 size_t i = 0; | |
506 for (auto content : contents) { | |
507 if (i == waiting_index) | |
508 EXPECT_EQ("", content); | |
509 else | |
510 EXPECT_EQ(expected, content); | |
511 i++; | |
512 } | |
513 } | |
514 | |
515 // Tests that removing an idle transaction does not impact other transactions. | |
516 TEST_F(WritersTest, ReadMultipleDeleteIdleTransaction) { | |
517 CreateWritersAddTransaction(); | |
518 EXPECT_FALSE(writers_->IsEmpty()); | |
519 | |
520 EXPECT_TRUE(writers_->CanAddWriters()); | |
521 AddTransactionToExistingWriters(); | |
522 AddTransactionToExistingWriters(); | |
523 | |
524 std::vector<std::string> contents(3); | |
525 size_t idle_index = 1; | |
526 int rv = ReadAllDeleteIdleTransaction(contents, idle_index); | |
527 | |
528 EXPECT_THAT(rv, IsOk()); | |
529 std::string expected(kSimpleGET_Transaction.data); | |
530 | |
531 size_t i = 0; | |
532 for (auto content : contents) { | |
533 if (i == idle_index) { | |
534 i++; | |
535 continue; | |
536 } | |
537 EXPECT_EQ(expected, content); | |
538 i++; | |
539 } | |
540 } | |
541 | |
542 // Tests cache write failure. | |
543 TEST_F(WritersTest, ReadMultipleCacheWriteFailed) { | |
544 CreateWritersAddTransaction(); | |
545 EXPECT_FALSE(writers_->IsEmpty()); | |
546 | |
547 EXPECT_TRUE(writers_->CanAddWriters()); | |
548 AddTransactionToExistingWriters(); | |
549 AddTransactionToExistingWriters(); | |
550 | |
551 std::vector<std::string> contents(3); | |
552 int rv = ReadCacheWriteFailure(contents); | |
553 | |
554 EXPECT_THAT(rv, IsOk()); | |
555 std::string expected(kSimpleGET_Transaction.data); | |
556 | |
557 // Only active_transaction_ should succeed. | |
558 EXPECT_EQ(expected, contents.at(0)); | |
559 | |
560 // No new transactions should now be added. | |
561 EXPECT_FALSE(writers_->CanAddWriters()); | |
562 } | |
563 | |
564 // Tests that network read failure fails all transactions: active, waiting and | |
565 // idle. | |
566 TEST_F(WritersTest, ReadMultipleNetworkReadFailed) { | |
567 CreateWritersAddTransaction(); | |
568 EXPECT_FALSE(writers_->IsEmpty()); | |
569 | |
570 EXPECT_TRUE(writers_->CanAddWriters()); | |
571 AddTransactionToExistingWriters(); | |
572 AddTransactionToExistingWriters(); | |
573 | |
574 std::vector<std::string> contents(3); | |
575 int rv = ReadNetworkFailure(contents); | |
576 | |
577 EXPECT_EQ(rv, ERR_INTERNET_DISCONNECTED); | |
578 } | |
579 | |
580 // Tests moving idle writers to readers. | |
581 TEST_F(WritersTest, MoveIdleWritersToReaders) { | |
582 CreateWritersAddTransaction(); | |
583 EXPECT_FALSE(writers_->IsEmpty()); | |
584 | |
585 EXPECT_TRUE(writers_->CanAddWriters()); | |
586 AddTransactionToExistingWriters(); | |
587 AddTransactionToExistingWriters(); | |
588 | |
589 EXPECT_FALSE(writers_->IsEmpty()); | |
590 writers_->RemoveAllIdleWriters(); | |
591 EXPECT_TRUE(writers_->IsEmpty()); | |
592 } | |
593 | |
594 // Tests GetWriterLoadState. | |
595 TEST_F(WritersTest, GetWriterLoadState) { | |
596 CreateWritersAddTransaction(); | |
597 EXPECT_FALSE(writers_->IsEmpty()); | |
598 | |
599 EXPECT_EQ(LOAD_STATE_IDLE, writers_->GetWriterLoadState()); | |
600 } | |
601 | |
602 // Tests truncating the entry via Writers. | |
603 TEST_F(WritersTest, TruncateEntry) { | |
604 CreateWritersAddTransaction(); | |
605 EXPECT_FALSE(writers_->IsEmpty()); | |
jkarlin
2017/06/28 15:50:10
These are the first two lines of every test. How a
shivanisha
2017/06/28 19:38:21
I feel it will make the test body less contained s
jkarlin
2017/06/30 18:20:39
Okay, up to you.
| |
606 | |
607 std::string content; | |
608 int rv = Read(&content); | |
609 | |
610 EXPECT_THAT(rv, IsOk()); | |
611 std::string expected(kSimpleGET_Transaction.data); | |
612 EXPECT_EQ(expected, content); | |
613 | |
614 writers_->TruncateEntry(); | |
615 base::RunLoop().RunUntilIdle(); | |
616 EXPECT_TRUE(writers_->IsTruncatedForTesting()); | |
617 } | |
618 | |
619 } // namespace net | |
OLD | NEW |