Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/cache_storage/cache_storage_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL()); | 245 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL()); |
| 246 } | 246 } |
| 247 | 247 |
| 248 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( | 248 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( |
| 249 const ServiceWorkerFetchRequest& request) { | 249 const ServiceWorkerFetchRequest& request) { |
| 250 return make_scoped_ptr(new ServiceWorkerFetchRequest( | 250 return make_scoped_ptr(new ServiceWorkerFetchRequest( |
| 251 request.url, request.method, request.headers, request.referrer, | 251 request.url, request.method, request.headers, request.referrer, |
| 252 request.is_reload)); | 252 request.is_reload)); |
| 253 } | 253 } |
| 254 | 254 |
| 255 scoped_ptr<ServiceWorkerResponse> CopyFetchResponse( | 255 CacheStorageCache::ErrorType BatchOperation( |
| 256 const ServiceWorkerResponse& response) { | 256 const std::vector<CacheStorageBatchOperation>& operations) { |
| 257 scoped_ptr<ServiceWorkerResponse> sw_response(new ServiceWorkerResponse( | |
| 258 response.url, response.status_code, response.status_text, | |
| 259 response.response_type, response.headers, response.blob_uuid, | |
| 260 response.blob_size, response.stream_url)); | |
| 261 return sw_response.Pass(); | |
| 262 } | |
| 263 | |
| 264 bool Put(const ServiceWorkerFetchRequest& request, | |
| 265 const ServiceWorkerResponse& response) { | |
| 266 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 257 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 267 | 258 |
| 268 cache_->Put( | 259 cache_->BatchOperation( |
| 269 CopyFetchRequest(request), CopyFetchResponse(response), | 260 operations, |
| 270 base::Bind(&CacheStorageCacheTest::ErrorTypeCallback, | 261 base::Bind(&CacheStorageCacheTest::ErrorTypeCallback, |
| 271 base::Unretained(this), base::Unretained(loop.get()))); | 262 base::Unretained(this), base::Unretained(loop.get()))); |
| 272 // TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle() | 263 // TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle() |
| 273 // once the cache uses a passed in MessageLoopProxy instead of the CACHE | 264 // once the cache uses a passed in MessageLoopProxy instead of the CACHE |
| 274 // thread. | 265 // thread. |
| 275 loop->Run(); | 266 loop->Run(); |
| 276 | 267 |
| 277 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; | 268 return callback_error_; |
| 269 } | |
| 270 | |
| 271 bool Put(const ServiceWorkerFetchRequest& request, | |
| 272 const ServiceWorkerResponse& response) { | |
| 273 CacheStorageBatchOperation operation; | |
| 274 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 275 operation.request = request; | |
| 276 operation.response = response; | |
| 277 | |
| 278 CacheStorageCache::ErrorType error = | |
| 279 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); | |
| 280 return error == CacheStorageCache::ERROR_TYPE_OK; | |
| 278 } | 281 } |
| 279 | 282 |
| 280 bool Match(const ServiceWorkerFetchRequest& request) { | 283 bool Match(const ServiceWorkerFetchRequest& request) { |
| 281 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 284 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 282 | 285 |
| 283 cache_->Match( | 286 cache_->Match( |
| 284 CopyFetchRequest(request), | 287 CopyFetchRequest(request), |
| 285 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, | 288 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
| 286 base::Unretained(this), base::Unretained(loop.get()))); | 289 base::Unretained(this), base::Unretained(loop.get()))); |
| 287 loop->Run(); | 290 loop->Run(); |
| 288 | 291 |
| 289 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; | 292 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; |
| 290 } | 293 } |
| 291 | 294 |
| 292 bool Delete(const ServiceWorkerFetchRequest& request) { | 295 bool Delete(const ServiceWorkerFetchRequest& request) { |
| 293 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 296 CacheStorageBatchOperation operation; |
| 297 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; | |
| 298 operation.request = request; | |
| 294 | 299 |
| 295 cache_->Delete( | 300 CacheStorageCache::ErrorType error = |
| 296 CopyFetchRequest(request), | 301 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); |
| 297 base::Bind(&CacheStorageCacheTest::ErrorTypeCallback, | 302 return error == CacheStorageCache::ERROR_TYPE_OK; |
| 298 base::Unretained(this), base::Unretained(loop.get()))); | |
| 299 loop->Run(); | |
| 300 | |
| 301 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; | |
| 302 } | 303 } |
| 303 | 304 |
| 304 bool Keys() { | 305 bool Keys() { |
| 305 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 306 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 306 | 307 |
| 307 cache_->Keys(base::Bind(&CacheStorageCacheTest::RequestsCallback, | 308 cache_->Keys(base::Bind(&CacheStorageCacheTest::RequestsCallback, |
| 308 base::Unretained(this), | 309 base::Unretained(this), |
| 309 base::Unretained(loop.get()))); | 310 base::Unretained(loop.get()))); |
| 310 loop->Run(); | 311 loop->Run(); |
| 311 | 312 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 }; | 453 }; |
| 453 | 454 |
| 454 TEST_P(CacheStorageCacheTestP, PutNoBody) { | 455 TEST_P(CacheStorageCacheTestP, PutNoBody) { |
| 455 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 456 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 456 } | 457 } |
| 457 | 458 |
| 458 TEST_P(CacheStorageCacheTestP, PutBody) { | 459 TEST_P(CacheStorageCacheTestP, PutBody) { |
| 459 EXPECT_TRUE(Put(body_request_, body_response_)); | 460 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 460 } | 461 } |
| 461 | 462 |
| 463 TEST_P(CacheStorageCacheTestP, PutBody_Multiple) { | |
| 464 CacheStorageBatchOperation operation1; | |
| 465 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 466 operation1.request = body_request_; | |
| 467 operation1.request.url = GURL("http://example.com/1"); | |
| 468 operation1.response = body_response_; | |
| 469 operation1.response.url = GURL("http://example.com/1"); | |
| 470 | |
| 471 CacheStorageBatchOperation operation2; | |
| 472 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 473 operation2.request = body_request_; | |
| 474 operation2.request.url = GURL("http://example.com/2"); | |
| 475 operation2.response = body_response_; | |
| 476 operation2.response.url = GURL("http://example.com/2"); | |
| 477 | |
| 478 CacheStorageBatchOperation operation3; | |
| 479 operation3.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 480 operation3.request = body_request_; | |
| 481 operation3.request.url = GURL("http://example.com/3"); | |
| 482 operation3.response = body_response_; | |
| 483 operation3.response.url = GURL("http://example.com/3"); | |
| 484 | |
| 485 std::vector<CacheStorageBatchOperation> operations; | |
| 486 operations.push_back(operation1); | |
| 487 operations.push_back(operation2); | |
| 488 operations.push_back(operation3); | |
| 489 | |
| 490 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_OK, BatchOperation(operations)); | |
| 491 EXPECT_TRUE(Match(operation1.request)); | |
| 492 EXPECT_TRUE(Match(operation2.request)); | |
| 493 EXPECT_TRUE(Match(operation3.request)); | |
| 494 } | |
| 495 | |
| 462 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { | 496 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { |
| 463 no_body_response_.url = GURL("http://example.com/foobar"); | 497 no_body_response_.url = GURL("http://example.com/foobar"); |
| 464 EXPECT_STRNE("http://example.com/foobar", | 498 EXPECT_STRNE("http://example.com/foobar", |
| 465 no_body_request_.url.spec().c_str()); | 499 no_body_request_.url.spec().c_str()); |
| 466 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 500 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 467 EXPECT_TRUE(Match(no_body_request_)); | 501 EXPECT_TRUE(Match(no_body_request_)); |
| 468 EXPECT_STREQ("http://example.com/foobar", | 502 EXPECT_STREQ("http://example.com/foobar", |
| 469 callback_response_->url.spec().c_str()); | 503 callback_response_->url.spec().c_str()); |
| 470 } | 504 } |
| 471 | 505 |
| 472 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { | 506 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { |
| 473 no_body_response_.url = GURL(); | 507 no_body_response_.url = GURL(); |
| 474 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); | 508 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); |
| 475 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 509 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 476 EXPECT_TRUE(Match(no_body_request_)); | 510 EXPECT_TRUE(Match(no_body_request_)); |
| 477 EXPECT_STREQ("", callback_response_->url.spec().c_str()); | 511 EXPECT_STREQ("", callback_response_->url.spec().c_str()); |
| 478 } | 512 } |
| 479 | 513 |
| 480 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { | 514 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { |
| 515 CacheStorageBatchOperation operation; | |
| 516 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 517 operation.request = body_request_; | |
| 518 operation.response = body_response_; | |
| 519 | |
| 481 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 520 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 482 cache_->Put(CopyFetchRequest(body_request_), | 521 cache_->BatchOperation( |
| 483 CopyFetchResponse(body_response_), | 522 std::vector<CacheStorageBatchOperation>(1, operation), |
| 484 base::Bind(&CacheStorageCacheTestP::ErrorTypeCallback, | 523 base::Bind(&CacheStorageCacheTestP::ErrorTypeCallback, |
| 485 base::Unretained(this), base::Unretained(loop.get()))); | 524 base::Unretained(this), base::Unretained(loop.get()))); |
| 486 // The handle should be held by the cache now so the deref here should be | 525 // The handle should be held by the cache now so the deref here should be |
| 487 // okay. | 526 // okay. |
| 488 blob_handle_.reset(); | 527 blob_handle_.reset(); |
| 489 loop->Run(); | 528 loop->Run(); |
| 490 | 529 |
| 491 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_OK, callback_error_); | 530 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_OK, callback_error_); |
| 492 } | 531 } |
| 493 | 532 |
| 494 TEST_P(CacheStorageCacheTestP, PutReplace) { | 533 TEST_P(CacheStorageCacheTestP, PutReplace) { |
| 495 EXPECT_TRUE(Put(body_request_, no_body_response_)); | 534 EXPECT_TRUE(Put(body_request_, no_body_response_)); |
| 496 EXPECT_TRUE(Match(body_request_)); | 535 EXPECT_TRUE(Match(body_request_)); |
| 497 EXPECT_FALSE(callback_response_data_); | 536 EXPECT_FALSE(callback_response_data_); |
| 498 | 537 |
| 499 EXPECT_TRUE(Put(body_request_, body_response_)); | 538 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 500 EXPECT_TRUE(Match(body_request_)); | 539 EXPECT_TRUE(Match(body_request_)); |
| 501 EXPECT_TRUE(callback_response_data_); | 540 EXPECT_TRUE(callback_response_data_); |
| 502 | 541 |
| 503 EXPECT_TRUE(Put(body_request_, no_body_response_)); | 542 EXPECT_TRUE(Put(body_request_, no_body_response_)); |
| 504 EXPECT_TRUE(Match(body_request_)); | 543 EXPECT_TRUE(Match(body_request_)); |
| 505 EXPECT_FALSE(callback_response_data_); | 544 EXPECT_FALSE(callback_response_data_); |
| 506 } | 545 } |
| 507 | 546 |
| 547 TEST_P(CacheStorageCacheTestP, PutReplcaceInBatch) { | |
| 548 CacheStorageBatchOperation operation1; | |
| 549 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 550 operation1.request = body_request_; | |
| 551 operation1.response = no_body_response_; | |
| 552 | |
| 553 CacheStorageBatchOperation operation2; | |
| 554 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 555 operation2.request = body_request_; | |
| 556 operation2.response = body_response_; | |
| 557 | |
| 558 std::vector<CacheStorageBatchOperation> operations; | |
| 559 operations.push_back(operation1); | |
| 560 operations.push_back(operation2); | |
| 561 | |
| 562 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_OK, BatchOperation(operations)); | |
| 563 | |
| 564 // |operation2| should win. | |
| 565 EXPECT_TRUE(Match(operation2.request)); | |
| 566 EXPECT_TRUE(callback_response_data_); | |
| 567 } | |
| 568 | |
|
jkarlin
2015/05/08 11:45:43
Add a test to show that you can mix delete and put
jkarlin
2015/05/08 11:45:43
Also add a test that if the first batch operation
nhiroki
2015/05/08 15:09:01
Do you have any ideas to fail only one operation i
nhiroki
2015/05/08 15:09:01
The current implementation has "DCHECK_EQ(1u, oper
jkarlin
2015/05/08 15:27:27
Yes, please add a TODO to add the test and add a b
jkarlin
2015/05/08 15:27:27
Good point. Never mind on this test.
nhiroki
2015/05/11 07:35:04
Done.
| |
| 508 TEST_P(CacheStorageCacheTestP, MatchNoBody) { | 569 TEST_P(CacheStorageCacheTestP, MatchNoBody) { |
| 509 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 570 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 510 EXPECT_TRUE(Match(no_body_request_)); | 571 EXPECT_TRUE(Match(no_body_request_)); |
| 511 EXPECT_EQ(200, callback_response_->status_code); | 572 EXPECT_EQ(200, callback_response_->status_code); |
| 512 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 573 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
| 513 EXPECT_STREQ("http://example.com/no_body.html", | 574 EXPECT_STREQ("http://example.com/no_body.html", |
| 514 callback_response_->url.spec().c_str()); | 575 callback_response_->url.spec().c_str()); |
| 515 EXPECT_STREQ("", callback_response_->blob_uuid.c_str()); | 576 EXPECT_STREQ("", callback_response_->blob_uuid.c_str()); |
| 516 EXPECT_EQ(0u, callback_response_->blob_size); | 577 EXPECT_EQ(0u, callback_response_->blob_size); |
| 517 } | 578 } |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 | 818 |
| 758 TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) { | 819 TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) { |
| 759 // Start two operations, the first one is delayed but the second isn't. The | 820 // Start two operations, the first one is delayed but the second isn't. The |
| 760 // second should wait for the first. | 821 // second should wait for the first. |
| 761 EXPECT_TRUE(Keys()); // Opens the backend. | 822 EXPECT_TRUE(Keys()); // Opens the backend. |
| 762 DelayableBackend* delayable_backend = cache_->UseDelayableBackend(); | 823 DelayableBackend* delayable_backend = cache_->UseDelayableBackend(); |
| 763 delayable_backend->set_delay_open(true); | 824 delayable_backend->set_delay_open(true); |
| 764 | 825 |
| 765 int sequence_out = -1; | 826 int sequence_out = -1; |
| 766 | 827 |
| 767 scoped_ptr<ServiceWorkerResponse> response1 = | 828 CacheStorageBatchOperation operation1; |
| 768 CopyFetchResponse(body_response_); | 829 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 830 operation1.request = body_request_; | |
| 831 operation1.response = body_response_; | |
| 832 | |
| 769 scoped_ptr<base::RunLoop> close_loop1(new base::RunLoop()); | 833 scoped_ptr<base::RunLoop> close_loop1(new base::RunLoop()); |
| 770 cache_->Put( | 834 cache_->BatchOperation( |
| 771 CopyFetchRequest(body_request_), response1.Pass(), | 835 std::vector<CacheStorageBatchOperation>(1, operation1), |
| 772 base::Bind(&CacheStorageCacheTest::SequenceCallback, | 836 base::Bind(&CacheStorageCacheTest::SequenceCallback, |
| 773 base::Unretained(this), 1, &sequence_out, close_loop1.get())); | 837 base::Unretained(this), 1, &sequence_out, close_loop1.get())); |
| 774 | 838 |
| 775 // Blocks on opening the cache entry. | 839 // Blocks on opening the cache entry. |
| 776 base::RunLoop().RunUntilIdle(); | 840 base::RunLoop().RunUntilIdle(); |
| 777 | 841 |
| 842 CacheStorageBatchOperation operation2; | |
| 843 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | |
| 844 operation2.request = body_request_; | |
| 845 operation2.response = body_response_; | |
| 846 | |
| 778 delayable_backend->set_delay_open(false); | 847 delayable_backend->set_delay_open(false); |
| 779 scoped_ptr<ServiceWorkerResponse> response2 = | |
| 780 CopyFetchResponse(body_response_); | |
| 781 scoped_ptr<base::RunLoop> close_loop2(new base::RunLoop()); | 848 scoped_ptr<base::RunLoop> close_loop2(new base::RunLoop()); |
| 782 cache_->Put( | 849 cache_->BatchOperation( |
| 783 CopyFetchRequest(body_request_), response2.Pass(), | 850 std::vector<CacheStorageBatchOperation>(1, operation2), |
| 784 base::Bind(&CacheStorageCacheTest::SequenceCallback, | 851 base::Bind(&CacheStorageCacheTest::SequenceCallback, |
| 785 base::Unretained(this), 2, &sequence_out, close_loop2.get())); | 852 base::Unretained(this), 2, &sequence_out, close_loop2.get())); |
| 786 | 853 |
| 787 // The second put operation should wait for the first to complete. | 854 // The second put operation should wait for the first to complete. |
| 788 base::RunLoop().RunUntilIdle(); | 855 base::RunLoop().RunUntilIdle(); |
| 789 EXPECT_FALSE(callback_response_); | 856 EXPECT_FALSE(callback_response_); |
| 790 | 857 |
| 791 delayable_backend->OpenEntryContinue(); | 858 delayable_backend->OpenEntryContinue(); |
| 792 close_loop1->Run(); | 859 close_loop1->Run(); |
| 793 EXPECT_EQ(1, sequence_out); | 860 EXPECT_EQ(1, sequence_out); |
| 794 close_loop2->Run(); | 861 close_loop2->Run(); |
| 795 EXPECT_EQ(2, sequence_out); | 862 EXPECT_EQ(2, sequence_out); |
| 796 } | 863 } |
| 797 | 864 |
| 798 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 865 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
| 799 CacheStorageCacheTestP, | 866 CacheStorageCacheTestP, |
| 800 ::testing::Values(false, true)); | 867 ::testing::Values(false, true)); |
| 801 | 868 |
| 802 } // namespace content | 869 } // namespace content |
| OLD | NEW |