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 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 using net::test::IsError; |
| 22 using net::test::IsOk; |
| 23 |
| 24 namespace net { |
| 25 |
| 26 class MockHttpCacheTransaction : public HttpCache::Transaction { |
| 27 typedef WebSocketHandshakeStreamBase::CreateHelper CreateHelper; |
| 28 |
| 29 public: |
| 30 MockHttpCacheTransaction(RequestPriority priority, HttpCache* cache) |
| 31 : HttpCache::Transaction(priority, cache){}; |
| 32 ~MockHttpCacheTransaction() override{}; |
| 33 |
| 34 // HttpCache::Transaction: |
| 35 |
| 36 int Start(const HttpRequestInfo* request, |
| 37 const CompletionCallback& callback, |
| 38 const NetLogWithSource& net_log) override { |
| 39 return OK; |
| 40 } |
| 41 |
| 42 int RestartIgnoringLastError(const CompletionCallback& callback) override { |
| 43 return OK; |
| 44 } |
| 45 |
| 46 int RestartWithCertificate(scoped_refptr<X509Certificate> client_cert, |
| 47 scoped_refptr<SSLPrivateKey> client_private_key, |
| 48 const CompletionCallback& callback) override { |
| 49 return OK; |
| 50 } |
| 51 |
| 52 int RestartWithAuth(const AuthCredentials& credentials, |
| 53 const CompletionCallback& callback) override { |
| 54 return OK; |
| 55 } |
| 56 |
| 57 bool IsReadyToRestartForAuth() override { return true; } |
| 58 |
| 59 int Read(IOBuffer* buf, |
| 60 int buf_len, |
| 61 const CompletionCallback& callback) override { |
| 62 return OK; |
| 63 } |
| 64 |
| 65 void PopulateNetErrorDetails(NetErrorDetails* details) const override {} |
| 66 |
| 67 void StopCaching() override {} |
| 68 |
| 69 bool GetFullRequestHeaders(HttpRequestHeaders* headers) const override { |
| 70 return true; |
| 71 } |
| 72 |
| 73 int64_t GetTotalReceivedBytes() const override { return OK; } |
| 74 |
| 75 int64_t GetTotalSentBytes() const override { return OK; } |
| 76 |
| 77 void DoneReading() override {} |
| 78 |
| 79 const HttpResponseInfo* GetResponseInfo() const override { return nullptr; } |
| 80 |
| 81 LoadState GetLoadState() const override { return LOAD_STATE_IDLE; } |
| 82 |
| 83 void SetQuicServerInfo(QuicServerInfo* quic_server_info) override {} |
| 84 |
| 85 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override { |
| 86 return true; |
| 87 } |
| 88 |
| 89 bool GetRemoteEndpoint(IPEndPoint* endpoint) const override { return true; } |
| 90 |
| 91 void SetWebSocketHandshakeStreamCreateHelper( |
| 92 CreateHelper* create_helper) override {} |
| 93 |
| 94 void SetBeforeNetworkStartCallback( |
| 95 const BeforeNetworkStartCallback& callback) override {} |
| 96 |
| 97 void SetBeforeHeadersSentCallback( |
| 98 const BeforeHeadersSentCallback& callback) override {} |
| 99 |
| 100 int ResumeNetworkStart() override { return OK; } |
| 101 |
| 102 void GetConnectionAttempts(ConnectionAttempts* out) const override {} |
| 103 |
| 104 CreateHelper* websocket_handshake_stream_create_helper() { return nullptr; } |
| 105 }; |
| 106 |
| 107 class WritersTest : public testing::Test { |
| 108 public: |
| 109 enum class DeleteTransactionType { NONE, ACTIVE, WAITING, IDLE }; |
| 110 |
| 111 WritersTest() : disk_entry_(nullptr), request_(kSimpleGET_Transaction) {} |
| 112 |
| 113 ~WritersTest() override { |
| 114 if (disk_entry_) |
| 115 disk_entry_->Close(); |
| 116 } |
| 117 |
| 118 void CreateWriters(const std::string& url) { |
| 119 cache_.CreateBackendEntry(kSimpleGET_Transaction.url, &disk_entry_, |
| 120 nullptr); |
| 121 writers_ = base::MakeUnique<HttpCache::Writers>(disk_entry_); |
| 122 } |
| 123 |
| 124 std::unique_ptr<HttpTransaction> CreateNetworkTransaction() { |
| 125 std::unique_ptr<HttpTransaction> transaction; |
| 126 MockNetworkLayer* network_layer = cache_.network_layer(); |
| 127 network_layer->CreateTransaction(DEFAULT_PRIORITY, &transaction); |
| 128 return transaction; |
| 129 } |
| 130 |
| 131 void CreateWritersAddTransaction(bool is_exclusive = false) { |
| 132 TestCompletionCallback callback; |
| 133 |
| 134 // Create and Start a mock network transaction. |
| 135 std::unique_ptr<HttpTransaction> network_transaction; |
| 136 network_transaction = CreateNetworkTransaction(); |
| 137 network_transaction->Start(&request_, callback.callback(), |
| 138 NetLogWithSource()); |
| 139 base::RunLoop().RunUntilIdle(); |
| 140 |
| 141 // Create a mock cache transaction. |
| 142 std::unique_ptr<MockHttpCacheTransaction> transaction = |
| 143 base::MakeUnique<MockHttpCacheTransaction>(DEFAULT_PRIORITY, |
| 144 cache_.http_cache()); |
| 145 |
| 146 CreateWriters(kSimpleGET_Transaction.url); |
| 147 EXPECT_TRUE(writers_->IsEmpty()); |
| 148 writers_->AddTransaction(transaction.get(), std::move(network_transaction), |
| 149 is_exclusive); |
| 150 EXPECT_TRUE(writers_->HasTransaction(transaction.get())); |
| 151 transactions_.push_back(std::move(transaction)); |
| 152 } |
| 153 |
| 154 void CreateWritersAddTransactionPriority(net::RequestPriority priority, |
| 155 bool is_exclusive = false) { |
| 156 CreateWritersAddTransaction(is_exclusive); |
| 157 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); |
| 158 transaction->SetPriority(priority); |
| 159 } |
| 160 |
| 161 void AddTransactionToExistingWriters() { |
| 162 EXPECT_TRUE(writers_); |
| 163 |
| 164 // Create a mock cache transaction. |
| 165 std::unique_ptr<MockHttpCacheTransaction> transaction = |
| 166 base::MakeUnique<MockHttpCacheTransaction>(DEFAULT_PRIORITY, |
| 167 cache_.http_cache()); |
| 168 |
| 169 writers_->AddTransaction(transaction.get(), nullptr, false); |
| 170 transactions_.push_back(std::move(transaction)); |
| 171 } |
| 172 |
| 173 int Read(std::string* result) { |
| 174 EXPECT_TRUE(transactions_.size() >= (size_t)1); |
| 175 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); |
| 176 TestCompletionCallback callback; |
| 177 |
| 178 std::string content; |
| 179 int rv = 0; |
| 180 do { |
| 181 scoped_refptr<IOBuffer> buf(new IOBuffer(kDefaultBufferSize)); |
| 182 rv = writers_->Read(buf.get(), kDefaultBufferSize, callback.callback(), |
| 183 transaction); |
| 184 if (rv == ERR_IO_PENDING) { |
| 185 rv = callback.WaitForResult(); |
| 186 base::RunLoop().RunUntilIdle(); |
| 187 } |
| 188 |
| 189 if (rv > 0) |
| 190 content.append(buf->data(), rv); |
| 191 else if (rv < 0) |
| 192 return rv; |
| 193 } while (rv > 0); |
| 194 |
| 195 result->swap(content); |
| 196 return OK; |
| 197 } |
| 198 |
| 199 void ReadVerifyTwoDifferentBufferLengths( |
| 200 const std::vector<int>& buffer_lengths) { |
| 201 EXPECT_EQ(2u, buffer_lengths.size()); |
| 202 EXPECT_EQ(2u, transactions_.size()); |
| 203 |
| 204 std::vector<std::string> results(buffer_lengths.size()); |
| 205 |
| 206 // Check only the 1st Read and not the complete response because the smaller |
| 207 // buffer transaction will need to read the remaining response from the |
| 208 // cache which will be tested when integrated with MockHttpCacheTransaction |
| 209 // layer. |
| 210 |
| 211 int rv = 0; |
| 212 |
| 213 std::vector<scoped_refptr<IOBuffer>> bufs; |
| 214 for (auto buffer_length : buffer_lengths) |
| 215 bufs.push_back(new IOBuffer(buffer_length)); |
| 216 |
| 217 std::vector<TestCompletionCallback> callbacks(buffer_lengths.size()); |
| 218 |
| 219 // Multiple transactions should be able to read with different sized |
| 220 // buffers. |
| 221 for (size_t i = 0; i < transactions_.size(); i++) { |
| 222 rv = writers_->Read(bufs[i].get(), buffer_lengths[i], |
| 223 callbacks[i].callback(), transactions_[i].get()); |
| 224 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. |
| 225 } |
| 226 |
| 227 // If first buffer is smaller, then the second one will only read the |
| 228 // smaller length as well. |
| 229 std::vector<int> expected_lengths = {buffer_lengths[0], |
| 230 buffer_lengths[0] < buffer_lengths[1] |
| 231 ? buffer_lengths[0] |
| 232 : buffer_lengths[1]}; |
| 233 |
| 234 for (size_t i = 0; i < callbacks.size(); i++) { |
| 235 rv = callbacks[i].WaitForResult(); |
| 236 EXPECT_EQ(expected_lengths[i], rv); |
| 237 results[i].append(bufs[i]->data(), expected_lengths[i]); |
| 238 } |
| 239 |
| 240 EXPECT_EQ(results[0].substr(0, expected_lengths[1]), results[1]); |
| 241 |
| 242 std::string expected(kSimpleGET_Transaction.data); |
| 243 EXPECT_EQ(expected.substr(0, expected_lengths[1]), results[1]); |
| 244 } |
| 245 |
| 246 // Each transaction invokes Read simultaneously. If |deleteType| is not NONE, |
| 247 // then it deletes the transaction of given type during the read process. |
| 248 void ReadAllDeleteTransaction(DeleteTransactionType deleteType) { |
| 249 EXPECT_TRUE(transactions_.size() >= 3u); |
| 250 |
| 251 unsigned int delete_index = std::numeric_limits<unsigned int>::max(); |
| 252 switch (deleteType) { |
| 253 case DeleteTransactionType::NONE: |
| 254 break; |
| 255 case DeleteTransactionType::ACTIVE: |
| 256 delete_index = 0; |
| 257 break; |
| 258 case DeleteTransactionType::WAITING: |
| 259 delete_index = 1; |
| 260 break; |
| 261 case DeleteTransactionType::IDLE: |
| 262 delete_index = 2; |
| 263 break; |
| 264 } |
| 265 |
| 266 std::vector<std::string> results(transactions_.size()); |
| 267 int rv = 0; |
| 268 bool first_iter = true; |
| 269 do { |
| 270 std::vector<scoped_refptr<IOBuffer>> bufs; |
| 271 std::vector<TestCompletionCallback> callbacks(transactions_.size()); |
| 272 |
| 273 for (size_t i = 0; i < transactions_.size(); i++) { |
| 274 bufs.push_back(new IOBuffer(kDefaultBufferSize)); |
| 275 |
| 276 // If we have deleted a transaction in the first iteration, then do not |
| 277 // invoke Read on it, in subsequent iterations. |
| 278 if (!first_iter && deleteType != DeleteTransactionType::NONE && |
| 279 i == delete_index) |
| 280 continue; |
| 281 |
| 282 // For it to be an idle transaction, do not invoke Read. |
| 283 if (deleteType == DeleteTransactionType::IDLE && i == delete_index) |
| 284 continue; |
| 285 |
| 286 rv = writers_->Read(bufs[i].get(), kDefaultBufferSize, |
| 287 callbacks[i].callback(), transactions_[i].get()); |
| 288 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. |
| 289 } |
| 290 |
| 291 if (first_iter && deleteType != DeleteTransactionType::NONE) { |
| 292 writers_->RemoveTransaction(transactions_.at(delete_index).get()); |
| 293 } |
| 294 |
| 295 std::vector<int> rvs; |
| 296 for (size_t i = 0; i < callbacks.size(); i++) { |
| 297 if (i == delete_index && deleteType != DeleteTransactionType::NONE) |
| 298 continue; |
| 299 rv = callbacks[i].WaitForResult(); |
| 300 rvs.push_back(rv); |
| 301 } |
| 302 |
| 303 // Verify all transactions should read the same length buffer. |
| 304 for (size_t i = 1; i < rvs.size(); i++) { |
| 305 ASSERT_EQ(rvs[i - 1], rvs[i]); |
| 306 } |
| 307 |
| 308 if (rv > 0) { |
| 309 for (size_t i = 0; i < results.size(); i++) { |
| 310 if (i == delete_index && deleteType != DeleteTransactionType::NONE && |
| 311 deleteType != DeleteTransactionType::ACTIVE) { |
| 312 continue; |
| 313 } |
| 314 results.at(i).append(bufs[i]->data(), rv); |
| 315 } |
| 316 } |
| 317 first_iter = false; |
| 318 } while (rv > 0); |
| 319 |
| 320 for (size_t i = 0; i < results.size(); i++) { |
| 321 if (i == delete_index && deleteType != DeleteTransactionType::NONE && |
| 322 deleteType != DeleteTransactionType::ACTIVE) { |
| 323 continue; |
| 324 } |
| 325 EXPECT_EQ(kSimpleGET_Transaction.data, results[i]); |
| 326 } |
| 327 |
| 328 EXPECT_EQ(OK, rv); |
| 329 } |
| 330 |
| 331 void ReadAll() { ReadAllDeleteTransaction(DeleteTransactionType::NONE); } |
| 332 |
| 333 int ReadCacheWriteFailure(std::vector<std::string>* results) { |
| 334 int rv = 0; |
| 335 int active_transaction_rv = 0; |
| 336 bool first_iter = true; |
| 337 do { |
| 338 std::vector<scoped_refptr<IOBuffer>> bufs; |
| 339 std::vector<TestCompletionCallback> callbacks(results->size()); |
| 340 |
| 341 // Fail the request. |
| 342 cache_.disk_cache()->set_soft_failures(true); |
| 343 |
| 344 // We have to open the entry again to propagate the failure flag. |
| 345 disk_cache::Entry* en; |
| 346 cache_.OpenBackendEntry(kSimpleGET_Transaction.url, &en); |
| 347 en->Close(); |
| 348 |
| 349 for (size_t i = 0; i < transactions_.size(); i++) { |
| 350 bufs.push_back(new IOBuffer(30)); |
| 351 |
| 352 if (!first_iter && i > 0) |
| 353 break; |
| 354 rv = writers_->Read(bufs[i].get(), 30, callbacks[i].callback(), |
| 355 transactions_[i].get()); |
| 356 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. |
| 357 } |
| 358 |
| 359 for (size_t i = 0; i < callbacks.size(); i++) { |
| 360 // Only active transaction should succeed. |
| 361 if (i == 0) { |
| 362 active_transaction_rv = callbacks[i].WaitForResult(); |
| 363 EXPECT_TRUE(active_transaction_rv >= 0); |
| 364 results->at(0).append(bufs[i]->data(), active_transaction_rv); |
| 365 } else if (first_iter) { |
| 366 rv = callbacks[i].WaitForResult(); |
| 367 EXPECT_EQ(ERR_CACHE_WRITE_FAILURE, rv); |
| 368 } |
| 369 } |
| 370 |
| 371 first_iter = false; |
| 372 } while (active_transaction_rv > 0); |
| 373 |
| 374 return active_transaction_rv; |
| 375 } |
| 376 |
| 377 int ReadNetworkFailure(std::vector<std::string>* results, Error error) { |
| 378 int rv = 0; |
| 379 std::vector<scoped_refptr<IOBuffer>> bufs; |
| 380 std::vector<TestCompletionCallback> callbacks(results->size()); |
| 381 |
| 382 for (size_t i = 0; i < transactions_.size(); i++) { |
| 383 bufs.push_back(new IOBuffer(30)); |
| 384 |
| 385 rv = writers_->Read(bufs[i].get(), 30, callbacks[i].callback(), |
| 386 transactions_[i].get()); |
| 387 EXPECT_EQ(ERR_IO_PENDING, rv); // Since the default is asynchronous. |
| 388 } |
| 389 |
| 390 for (auto& callback : callbacks) { |
| 391 rv = callback.WaitForResult(); |
| 392 EXPECT_EQ(error, rv); |
| 393 } |
| 394 |
| 395 return error; |
| 396 } |
| 397 |
| 398 bool StopCaching() { |
| 399 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); |
| 400 EXPECT_TRUE(transaction); |
| 401 return writers_->StopCaching(transaction); |
| 402 } |
| 403 |
| 404 void RemoveFirstTransaction() { |
| 405 MockHttpCacheTransaction* transaction = transactions_.begin()->get(); |
| 406 EXPECT_TRUE(transaction); |
| 407 writers_->RemoveTransaction(transaction); |
| 408 } |
| 409 |
| 410 void UpdateAndVerifyPriority(RequestPriority priority) { |
| 411 writers_->UpdatePriority(); |
| 412 EXPECT_EQ(priority, writers_->priority_); |
| 413 } |
| 414 |
| 415 MockHttpCache cache_; |
| 416 std::unique_ptr<HttpCache::Writers> writers_; |
| 417 disk_cache::Entry* disk_entry_; |
| 418 |
| 419 // Should be before transactions_ since it is accessed in the network |
| 420 // transaction's destructor. |
| 421 MockHttpRequest request_; |
| 422 |
| 423 static const int kDefaultBufferSize = 256; |
| 424 |
| 425 std::vector<std::unique_ptr<MockHttpCacheTransaction>> transactions_; |
| 426 }; |
| 427 |
| 428 // Tests successful addition of a transaction. |
| 429 TEST_F(WritersTest, AddTransaction) { |
| 430 CreateWritersAddTransaction(); |
| 431 EXPECT_FALSE(writers_->IsEmpty()); |
| 432 } |
| 433 |
| 434 // Tests successful addition of multiple transactions. |
| 435 TEST_F(WritersTest, AddManyTransactions) { |
| 436 CreateWritersAddTransaction(); |
| 437 EXPECT_FALSE(writers_->IsEmpty()); |
| 438 |
| 439 for (int i = 0; i < 5; i++) |
| 440 AddTransactionToExistingWriters(); |
| 441 |
| 442 EXPECT_EQ(6, writers_->CountTransactionsForTesting()); |
| 443 } |
| 444 |
| 445 // Tests that CanAddWriters should return false if it is writing exclusively. |
| 446 TEST_F(WritersTest, AddTransactionsExclusive) { |
| 447 CreateWritersAddTransaction(true /* is_exclusive */); |
| 448 EXPECT_FALSE(writers_->IsEmpty()); |
| 449 |
| 450 EXPECT_FALSE(writers_->CanAddWriters()); |
| 451 } |
| 452 |
| 453 // Tests StopCaching should not stop caching if there are multiple writers. |
| 454 TEST_F(WritersTest, StopCachingMultipleWriters) { |
| 455 CreateWritersAddTransaction(); |
| 456 EXPECT_FALSE(writers_->IsEmpty()); |
| 457 |
| 458 EXPECT_TRUE(writers_->CanAddWriters()); |
| 459 AddTransactionToExistingWriters(); |
| 460 |
| 461 EXPECT_FALSE(StopCaching()); |
| 462 EXPECT_TRUE(writers_->CanAddWriters()); |
| 463 } |
| 464 |
| 465 // Tests StopCaching should stop caching if there is a single writer. |
| 466 TEST_F(WritersTest, StopCaching) { |
| 467 CreateWritersAddTransaction(); |
| 468 EXPECT_FALSE(writers_->IsEmpty()); |
| 469 |
| 470 EXPECT_TRUE(StopCaching()); |
| 471 EXPECT_FALSE(writers_->CanAddWriters()); |
| 472 } |
| 473 |
| 474 // Tests removing of an idle transaction and change in priority. |
| 475 TEST_F(WritersTest, RemoveIdleTransaction) { |
| 476 CreateWritersAddTransactionPriority(HIGHEST); |
| 477 UpdateAndVerifyPriority(HIGHEST); |
| 478 |
| 479 AddTransactionToExistingWriters(); |
| 480 UpdateAndVerifyPriority(HIGHEST); |
| 481 |
| 482 EXPECT_FALSE(writers_->IsEmpty()); |
| 483 EXPECT_EQ(2, writers_->CountTransactionsForTesting()); |
| 484 |
| 485 RemoveFirstTransaction(); |
| 486 EXPECT_EQ(1, writers_->CountTransactionsForTesting()); |
| 487 |
| 488 UpdateAndVerifyPriority(DEFAULT_PRIORITY); |
| 489 } |
| 490 |
| 491 // Tests that Read is successful. |
| 492 TEST_F(WritersTest, Read) { |
| 493 CreateWritersAddTransaction(); |
| 494 EXPECT_FALSE(writers_->IsEmpty()); |
| 495 |
| 496 std::string content; |
| 497 int rv = Read(&content); |
| 498 |
| 499 EXPECT_THAT(rv, IsOk()); |
| 500 std::string expected(kSimpleGET_Transaction.data); |
| 501 EXPECT_EQ(expected, content); |
| 502 } |
| 503 |
| 504 // Tests that multiple transactions can read the same data simultaneously. |
| 505 TEST_F(WritersTest, ReadMultiple) { |
| 506 CreateWritersAddTransaction(); |
| 507 EXPECT_FALSE(writers_->IsEmpty()); |
| 508 |
| 509 EXPECT_TRUE(writers_->CanAddWriters()); |
| 510 AddTransactionToExistingWriters(); |
| 511 AddTransactionToExistingWriters(); |
| 512 |
| 513 ReadAll(); |
| 514 } |
| 515 |
| 516 // Tests that multiple transactions can read the same data simultaneously. |
| 517 TEST_F(WritersTest, ReadMultipleDifferentBufferSizes) { |
| 518 CreateWritersAddTransaction(); |
| 519 EXPECT_FALSE(writers_->IsEmpty()); |
| 520 |
| 521 EXPECT_TRUE(writers_->CanAddWriters()); |
| 522 AddTransactionToExistingWriters(); |
| 523 |
| 524 std::vector<int> buffer_lengths{20, 10}; |
| 525 ReadVerifyTwoDifferentBufferLengths(buffer_lengths); |
| 526 } |
| 527 |
| 528 // Same as above but tests the first transaction having smaller buffer size |
| 529 // than the next. |
| 530 TEST_F(WritersTest, ReadMultipleDifferentBufferSizes1) { |
| 531 CreateWritersAddTransaction(); |
| 532 EXPECT_FALSE(writers_->IsEmpty()); |
| 533 |
| 534 EXPECT_TRUE(writers_->CanAddWriters()); |
| 535 AddTransactionToExistingWriters(); |
| 536 |
| 537 std::vector<int> buffer_lengths{10, 20}; |
| 538 ReadVerifyTwoDifferentBufferLengths(buffer_lengths); |
| 539 } |
| 540 |
| 541 // Tests that ongoing Read completes even when active transaction is deleted |
| 542 // mid-read. Any transactions waiting should be able to get the read buffer. |
| 543 TEST_F(WritersTest, ReadMultipleDeleteActiveTransaction) { |
| 544 CreateWritersAddTransaction(); |
| 545 EXPECT_FALSE(writers_->IsEmpty()); |
| 546 |
| 547 EXPECT_TRUE(writers_->CanAddWriters()); |
| 548 AddTransactionToExistingWriters(); |
| 549 AddTransactionToExistingWriters(); |
| 550 |
| 551 ReadAllDeleteTransaction(DeleteTransactionType::ACTIVE); |
| 552 } |
| 553 |
| 554 // Tests that removing a waiting for read transaction does not impact other |
| 555 // transactions. |
| 556 TEST_F(WritersTest, ReadMultipleDeleteWaitingTransaction) { |
| 557 CreateWritersAddTransaction(); |
| 558 EXPECT_FALSE(writers_->IsEmpty()); |
| 559 |
| 560 EXPECT_TRUE(writers_->CanAddWriters()); |
| 561 AddTransactionToExistingWriters(); |
| 562 AddTransactionToExistingWriters(); |
| 563 AddTransactionToExistingWriters(); |
| 564 |
| 565 std::vector<std::string> contents(4); |
| 566 ReadAllDeleteTransaction(DeleteTransactionType::WAITING); |
| 567 } |
| 568 |
| 569 // Tests that removing an idle transaction does not impact other transactions. |
| 570 TEST_F(WritersTest, ReadMultipleDeleteIdleTransaction) { |
| 571 CreateWritersAddTransaction(); |
| 572 EXPECT_FALSE(writers_->IsEmpty()); |
| 573 |
| 574 EXPECT_TRUE(writers_->CanAddWriters()); |
| 575 AddTransactionToExistingWriters(); |
| 576 AddTransactionToExistingWriters(); |
| 577 |
| 578 std::vector<std::string> contents(3); |
| 579 ReadAllDeleteTransaction(DeleteTransactionType::IDLE); |
| 580 } |
| 581 |
| 582 // Tests cache write failure. |
| 583 TEST_F(WritersTest, ReadMultipleCacheWriteFailed) { |
| 584 CreateWritersAddTransaction(); |
| 585 EXPECT_FALSE(writers_->IsEmpty()); |
| 586 |
| 587 EXPECT_TRUE(writers_->CanAddWriters()); |
| 588 AddTransactionToExistingWriters(); |
| 589 AddTransactionToExistingWriters(); |
| 590 |
| 591 std::vector<std::string> contents(3); |
| 592 int rv = ReadCacheWriteFailure(&contents); |
| 593 |
| 594 EXPECT_THAT(rv, IsOk()); |
| 595 std::string expected(kSimpleGET_Transaction.data); |
| 596 |
| 597 // Only active_transaction_ should succeed. |
| 598 EXPECT_EQ(expected, contents.at(0)); |
| 599 |
| 600 // No new transactions should now be added. |
| 601 EXPECT_FALSE(writers_->CanAddWriters()); |
| 602 } |
| 603 |
| 604 // Tests that network read failure fails all transactions: active, waiting and |
| 605 // idle. |
| 606 TEST_F(WritersTest, ReadMultipleNetworkReadFailed) { |
| 607 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 608 transaction.read_return_code = ERR_INTERNET_DISCONNECTED; |
| 609 MockHttpRequest request(transaction); |
| 610 request_ = request; |
| 611 |
| 612 CreateWritersAddTransaction(); |
| 613 EXPECT_FALSE(writers_->IsEmpty()); |
| 614 |
| 615 EXPECT_TRUE(writers_->CanAddWriters()); |
| 616 AddTransactionToExistingWriters(); |
| 617 AddTransactionToExistingWriters(); |
| 618 |
| 619 std::vector<std::string> contents(3); |
| 620 int rv = ReadNetworkFailure(&contents, ERR_INTERNET_DISCONNECTED); |
| 621 |
| 622 EXPECT_EQ(ERR_INTERNET_DISCONNECTED, rv); |
| 623 } |
| 624 |
| 625 // Tests moving idle writers to readers. |
| 626 TEST_F(WritersTest, MoveIdleWritersToReaders) { |
| 627 CreateWritersAddTransaction(); |
| 628 EXPECT_FALSE(writers_->IsEmpty()); |
| 629 |
| 630 EXPECT_TRUE(writers_->CanAddWriters()); |
| 631 AddTransactionToExistingWriters(); |
| 632 AddTransactionToExistingWriters(); |
| 633 |
| 634 EXPECT_FALSE(writers_->IsEmpty()); |
| 635 writers_->RemoveAllIdleWriters(); |
| 636 EXPECT_TRUE(writers_->IsEmpty()); |
| 637 } |
| 638 |
| 639 // Tests GetWriterLoadState. |
| 640 TEST_F(WritersTest, GetWriterLoadState) { |
| 641 CreateWritersAddTransaction(); |
| 642 EXPECT_FALSE(writers_->IsEmpty()); |
| 643 |
| 644 EXPECT_EQ(LOAD_STATE_IDLE, writers_->GetWriterLoadState()); |
| 645 } |
| 646 |
| 647 // Tests truncating the entry via Writers. |
| 648 TEST_F(WritersTest, TruncateEntry) { |
| 649 CreateWritersAddTransaction(); |
| 650 EXPECT_FALSE(writers_->IsEmpty()); |
| 651 |
| 652 std::string content; |
| 653 int rv = Read(&content); |
| 654 |
| 655 EXPECT_THAT(rv, IsOk()); |
| 656 std::string expected(kSimpleGET_Transaction.data); |
| 657 EXPECT_EQ(expected, content); |
| 658 |
| 659 writers_->TruncateEntry(); |
| 660 base::RunLoop().RunUntilIdle(); |
| 661 EXPECT_TRUE(writers_->IsTruncatedForTesting()); |
| 662 } |
| 663 |
| 664 } // namespace net |
OLD | NEW |