Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <stack> | 5 #include <stack> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/callback.h" | |
| 9 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 10 #include "base/pickle.h" | 13 #include "base/pickle.h" |
| 11 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 12 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 13 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 15 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webkit/appcache/appcache_response.h" | 20 #include "webkit/appcache/appcache_response.h" |
| 18 #include "webkit/appcache/mock_appcache_service.h" | 21 #include "webkit/appcache/mock_appcache_service.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 should_delete_reader_in_completion_callback_ = false; | 112 should_delete_reader_in_completion_callback_ = false; |
| 110 should_delete_writer_in_completion_callback_ = false; | 113 should_delete_writer_in_completion_callback_ = false; |
| 111 reader_deletion_count_down_ = 0; | 114 reader_deletion_count_down_ = 0; |
| 112 writer_deletion_count_down_ = 0; | 115 writer_deletion_count_down_ = 0; |
| 113 read_callback_was_called_ = false; | 116 read_callback_was_called_ = false; |
| 114 write_callback_was_called_ = false; | 117 write_callback_was_called_ = false; |
| 115 } | 118 } |
| 116 | 119 |
| 117 void TearDownTest() { | 120 void TearDownTest() { |
| 118 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 121 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 119 while (!task_stack_.empty()) { | 122 while (!task_stack_.empty()) |
| 120 delete task_stack_.top().first; | |
| 121 task_stack_.pop(); | 123 task_stack_.pop(); |
|
csilv
2011/11/10 23:47:02
(commentary: too bad std::stack doesn't have a cle
| |
| 122 } | 124 |
| 123 reader_.reset(); | 125 reader_.reset(); |
| 124 read_buffer_ = NULL; | 126 read_buffer_ = NULL; |
| 125 read_info_buffer_ = NULL; | 127 read_info_buffer_ = NULL; |
| 126 writer_.reset(); | 128 writer_.reset(); |
| 127 write_buffer_ = NULL; | 129 write_buffer_ = NULL; |
| 128 write_info_buffer_ = NULL; | 130 write_info_buffer_ = NULL; |
| 129 storage_delegate_.reset(); | 131 storage_delegate_.reset(); |
| 130 service_.reset(); | 132 service_.reset(); |
| 131 } | 133 } |
| 132 | 134 |
| 133 void TestFinished() { | 135 void TestFinished() { |
| 134 // We unwind the stack prior to finishing up to let stack | 136 // We unwind the stack prior to finishing up to let stack |
| 135 // based objects get deleted. | 137 // based objects get deleted. |
| 136 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 138 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 137 MessageLoop::current()->PostTask(FROM_HERE, | 139 MessageLoop::current()->PostTask( |
| 138 NewRunnableMethod(this, &AppCacheResponseTest::TestFinishedUnwound)); | 140 FROM_HERE, base::Bind(&AppCacheResponseTest::TestFinishedUnwound, |
| 141 base::Unretained(this))); | |
| 139 } | 142 } |
| 140 | 143 |
| 141 void TestFinishedUnwound() { | 144 void TestFinishedUnwound() { |
| 142 TearDownTest(); | 145 TearDownTest(); |
| 143 test_finished_event_->Signal(); | 146 test_finished_event_->Signal(); |
| 144 } | 147 } |
| 145 | 148 |
| 146 void PushNextTask(Task* task) { | 149 void PushNextTask(const base::Closure& task) { |
| 147 task_stack_.push(std::pair<Task*, bool>(task, false)); | 150 task_stack_.push(std::pair<base::Closure, bool>(task, false)); |
| 148 } | 151 } |
| 149 | 152 |
| 150 void PushNextTaskAsImmediate(Task* task) { | 153 void PushNextTaskAsImmediate(const base::Closure& task) { |
| 151 task_stack_.push(std::pair<Task*, bool>(task, true)); | 154 task_stack_.push(std::pair<base::Closure, bool>(task, true)); |
| 152 } | 155 } |
| 153 | 156 |
| 154 void ScheduleNextTask() { | 157 void ScheduleNextTask() { |
| 155 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 158 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 156 if (task_stack_.empty()) { | 159 if (task_stack_.empty()) { |
| 157 TestFinished(); | 160 TestFinished(); |
| 158 return; | 161 return; |
| 159 } | 162 } |
| 160 scoped_ptr<Task> task(task_stack_.top().first); | 163 base::Closure task = task_stack_.top().first; |
| 161 bool immediate = task_stack_.top().second; | 164 bool immediate = task_stack_.top().second; |
| 162 task_stack_.pop(); | 165 task_stack_.pop(); |
| 163 if (immediate) | 166 if (immediate) |
| 164 task->Run(); | 167 task.Run(); |
| 165 else | 168 else |
| 166 MessageLoop::current()->PostTask(FROM_HERE, task.release()); | 169 MessageLoop::current()->PostTask(FROM_HERE, task); |
| 167 } | 170 } |
| 168 | 171 |
| 169 // Wrappers to call AppCacheResponseReader/Writer Read and Write methods | 172 // Wrappers to call AppCacheResponseReader/Writer Read and Write methods |
| 170 | 173 |
| 171 void WriteBasicResponse() { | 174 void WriteBasicResponse() { |
| 172 static const char kHttpHeaders[] = | 175 static const char kHttpHeaders[] = |
| 173 "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; | 176 "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; |
| 174 static const char* kHttpBody = "Hello"; | 177 static const char* kHttpBody = "Hello"; |
| 175 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBody)); | 178 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBody)); |
| 176 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); | 179 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| 177 WriteResponse(MakeHttpResponseInfo(raw_headers), body, strlen(kHttpBody)); | 180 WriteResponse(MakeHttpResponseInfo(raw_headers), body, strlen(kHttpBody)); |
| 178 } | 181 } |
| 179 | 182 |
| 180 int basic_response_size() { return 5; } // should match kHttpBody above | 183 int basic_response_size() { return 5; } // should match kHttpBody above |
| 181 | 184 |
| 182 void WriteResponse(net::HttpResponseInfo* head, | 185 void WriteResponse(net::HttpResponseInfo* head, |
| 183 IOBuffer* body, int body_len) { | 186 IOBuffer* body, int body_len) { |
| 184 DCHECK(body); | 187 DCHECK(body); |
| 185 scoped_refptr<IOBuffer> body_ref(body); | 188 scoped_refptr<IOBuffer> body_ref(body); |
| 186 PushNextTask(NewRunnableMethod( | 189 PushNextTask(base::Bind(&AppCacheResponseTest::WriteResponseBody, |
| 187 this, &AppCacheResponseTest::WriteResponseBody, | 190 base::Unretained(this), body_ref, body_len)); |
| 188 body_ref, body_len)); | |
| 189 WriteResponseHead(head); | 191 WriteResponseHead(head); |
| 190 } | 192 } |
| 191 | 193 |
| 192 void WriteResponseHead(net::HttpResponseInfo* head) { | 194 void WriteResponseHead(net::HttpResponseInfo* head) { |
| 193 EXPECT_FALSE(writer_->IsWritePending()); | 195 EXPECT_FALSE(writer_->IsWritePending()); |
| 194 expected_write_result_ = GetHttpResponseInfoSize(head); | 196 expected_write_result_ = GetHttpResponseInfoSize(head); |
| 195 write_info_buffer_ = new HttpResponseInfoIOBuffer(head); | 197 write_info_buffer_ = new HttpResponseInfoIOBuffer(head); |
| 196 writer_->WriteInfo(write_info_buffer_, &write_info_callback_); | 198 writer_->WriteInfo(write_info_buffer_, &write_info_callback_); |
| 197 } | 199 } |
| 198 | 200 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 | 304 |
| 303 // ReadNonExistentResponse ------------------------------------------- | 305 // ReadNonExistentResponse ------------------------------------------- |
| 304 void ReadNonExistentResponse() { | 306 void ReadNonExistentResponse() { |
| 305 // 1. Attempt to ReadInfo | 307 // 1. Attempt to ReadInfo |
| 306 // 2. Attempt to ReadData | 308 // 2. Attempt to ReadData |
| 307 | 309 |
| 308 reader_.reset(service_->storage()->CreateResponseReader( | 310 reader_.reset(service_->storage()->CreateResponseReader( |
| 309 GURL(), 0, kNoSuchResponseId)); | 311 GURL(), 0, kNoSuchResponseId)); |
| 310 | 312 |
| 311 // Push tasks in reverse order | 313 // Push tasks in reverse order |
| 312 PushNextTask(NewRunnableMethod( | 314 PushNextTask(base::Bind(&AppCacheResponseTest::ReadNonExistentData, |
| 313 this, &AppCacheResponseTest::ReadNonExistentData)); | 315 base::Unretained(this))); |
| 314 PushNextTask(NewRunnableMethod( | 316 PushNextTask(base::Bind(&AppCacheResponseTest::ReadNonExistentInfo, |
| 315 this, &AppCacheResponseTest::ReadNonExistentInfo)); | 317 base::Unretained(this))); |
| 316 ScheduleNextTask(); | 318 ScheduleNextTask(); |
| 317 } | 319 } |
| 318 | 320 |
| 319 void ReadNonExistentInfo() { | 321 void ReadNonExistentInfo() { |
| 320 EXPECT_FALSE(reader_->IsReadPending()); | 322 EXPECT_FALSE(reader_->IsReadPending()); |
| 321 read_info_buffer_ = new HttpResponseInfoIOBuffer(); | 323 read_info_buffer_ = new HttpResponseInfoIOBuffer(); |
| 322 reader_->ReadInfo(read_info_buffer_, &read_info_callback_); | 324 reader_->ReadInfo(read_info_buffer_, &read_info_callback_); |
| 323 EXPECT_TRUE(reader_->IsReadPending()); | 325 EXPECT_TRUE(reader_->IsReadPending()); |
| 324 expected_read_result_ = net::ERR_CACHE_MISS; | 326 expected_read_result_ = net::ERR_CACHE_MISS; |
| 325 } | 327 } |
| 326 | 328 |
| 327 void ReadNonExistentData() { | 329 void ReadNonExistentData() { |
| 328 EXPECT_FALSE(reader_->IsReadPending()); | 330 EXPECT_FALSE(reader_->IsReadPending()); |
| 329 read_buffer_ = new IOBuffer(kBlockSize); | 331 read_buffer_ = new IOBuffer(kBlockSize); |
| 330 reader_->ReadData(read_buffer_, kBlockSize, &read_callback_); | 332 reader_->ReadData(read_buffer_, kBlockSize, &read_callback_); |
| 331 EXPECT_TRUE(reader_->IsReadPending()); | 333 EXPECT_TRUE(reader_->IsReadPending()); |
| 332 expected_read_result_ = net::ERR_CACHE_MISS; | 334 expected_read_result_ = net::ERR_CACHE_MISS; |
| 333 } | 335 } |
| 334 | 336 |
| 335 // LoadResponseInfo_Miss ---------------------------------------------------- | 337 // LoadResponseInfo_Miss ---------------------------------------------------- |
| 336 void LoadResponseInfo_Miss() { | 338 void LoadResponseInfo_Miss() { |
| 337 PushNextTask(NewRunnableMethod( | 339 PushNextTask(base::Bind(&AppCacheResponseTest::LoadResponseInfo_Miss_Verify, |
| 338 this, &AppCacheResponseTest::LoadResponseInfo_Miss_Verify)); | 340 base::Unretained(this))); |
| 339 service_->storage()->LoadResponseInfo(GURL(), 0, kNoSuchResponseId, | 341 service_->storage()->LoadResponseInfo(GURL(), 0, kNoSuchResponseId, |
| 340 storage_delegate_.get()); | 342 storage_delegate_.get()); |
| 341 } | 343 } |
| 342 | 344 |
| 343 void LoadResponseInfo_Miss_Verify() { | 345 void LoadResponseInfo_Miss_Verify() { |
| 344 EXPECT_EQ(kNoSuchResponseId, storage_delegate_->loaded_info_id_); | 346 EXPECT_EQ(kNoSuchResponseId, storage_delegate_->loaded_info_id_); |
| 345 EXPECT_TRUE(!storage_delegate_->loaded_info_.get()); | 347 EXPECT_TRUE(!storage_delegate_->loaded_info_.get()); |
| 346 TestFinished(); | 348 TestFinished(); |
| 347 } | 349 } |
| 348 | 350 |
| 349 // LoadResponseInfo_Hit ---------------------------------------------------- | 351 // LoadResponseInfo_Hit ---------------------------------------------------- |
| 350 void LoadResponseInfo_Hit() { | 352 void LoadResponseInfo_Hit() { |
| 351 // This tests involves multiple async steps. | 353 // This tests involves multiple async steps. |
| 352 // 1. Write a response headers and body to storage | 354 // 1. Write a response headers and body to storage |
| 353 // a. headers | 355 // a. headers |
| 354 // b. body | 356 // b. body |
| 355 // 2. Use LoadResponseInfo to read the response headers back out | 357 // 2. Use LoadResponseInfo to read the response headers back out |
| 356 PushNextTask(NewRunnableMethod( | 358 PushNextTask(base::Bind(&AppCacheResponseTest::LoadResponseInfo_Hit_Step2, |
| 357 this, &AppCacheResponseTest::LoadResponseInfo_Hit_Step2)); | 359 base::Unretained(this))); |
| 358 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 360 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 359 written_response_id_ = writer_->response_id(); | 361 written_response_id_ = writer_->response_id(); |
| 360 WriteBasicResponse(); | 362 WriteBasicResponse(); |
| 361 } | 363 } |
| 362 | 364 |
| 363 void LoadResponseInfo_Hit_Step2() { | 365 void LoadResponseInfo_Hit_Step2() { |
| 364 writer_.reset(); | 366 writer_.reset(); |
| 365 PushNextTask(NewRunnableMethod( | 367 PushNextTask(base::Bind(&AppCacheResponseTest::LoadResponseInfo_Hit_Verify, |
| 366 this, &AppCacheResponseTest::LoadResponseInfo_Hit_Verify)); | 368 base::Unretained(this))); |
| 367 service_->storage()->LoadResponseInfo(GURL(), 0, written_response_id_, | 369 service_->storage()->LoadResponseInfo(GURL(), 0, written_response_id_, |
| 368 storage_delegate_.get()); | 370 storage_delegate_.get()); |
| 369 } | 371 } |
| 370 | 372 |
| 371 void LoadResponseInfo_Hit_Verify() { | 373 void LoadResponseInfo_Hit_Verify() { |
| 372 EXPECT_EQ(written_response_id_, storage_delegate_->loaded_info_id_); | 374 EXPECT_EQ(written_response_id_, storage_delegate_->loaded_info_id_); |
| 373 EXPECT_TRUE(storage_delegate_->loaded_info_.get()); | 375 EXPECT_TRUE(storage_delegate_->loaded_info_.get()); |
| 374 EXPECT_TRUE(CompareHttpResponseInfos( | 376 EXPECT_TRUE(CompareHttpResponseInfos( |
| 375 write_info_buffer_->http_info.get(), | 377 write_info_buffer_->http_info.get(), |
| 376 storage_delegate_->loaded_info_->http_response_info())); | 378 storage_delegate_->loaded_info_->http_response_info())); |
| 377 EXPECT_EQ(basic_response_size(), | 379 EXPECT_EQ(basic_response_size(), |
| 378 storage_delegate_->loaded_info_->response_data_size()); | 380 storage_delegate_->loaded_info_->response_data_size()); |
| 379 TestFinished(); | 381 TestFinished(); |
| 380 } | 382 } |
| 381 | 383 |
| 382 // AmountWritten ---------------------------------------------------- | 384 // AmountWritten ---------------------------------------------------- |
| 383 | 385 |
| 384 void AmountWritten() { | 386 void AmountWritten() { |
| 385 static const char kHttpHeaders[] = | 387 static const char kHttpHeaders[] = |
| 386 "HTTP/1.0 200 OK\0\0"; | 388 "HTTP/1.0 200 OK\0\0"; |
| 387 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); | 389 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| 388 net::HttpResponseInfo* head = MakeHttpResponseInfo(raw_headers); | 390 net::HttpResponseInfo* head = MakeHttpResponseInfo(raw_headers); |
| 389 int expected_amount_written = | 391 int expected_amount_written = |
| 390 GetHttpResponseInfoSize(head) + kNumBlocks * kBlockSize; | 392 GetHttpResponseInfoSize(head) + kNumBlocks * kBlockSize; |
| 391 | 393 |
| 392 // Push tasks in reverse order. | 394 // Push tasks in reverse order. |
| 393 PushNextTask(NewRunnableMethod( | 395 PushNextTask(base::Bind(&AppCacheResponseTest::Verify_AmountWritten, |
| 394 this, &AppCacheResponseTest::Verify_AmountWritten, | 396 base::Unretained(this), expected_amount_written)); |
| 395 expected_amount_written)); | |
| 396 for (int i = 0; i < kNumBlocks; ++i) { | 397 for (int i = 0; i < kNumBlocks; ++i) { |
| 397 PushNextTask(NewRunnableMethod( | 398 PushNextTask(base::Bind(&AppCacheResponseTest::WriteOneBlock, |
| 398 this, &AppCacheResponseTest::WriteOneBlock, kNumBlocks - i)); | 399 base::Unretained(this), kNumBlocks - i)); |
| 399 } | 400 } |
| 400 PushNextTask(NewRunnableMethod( | 401 PushNextTask(base::Bind(&AppCacheResponseTest::WriteResponseHead, |
| 401 this, &AppCacheResponseTest::WriteResponseHead, head)); | 402 base::Unretained(this), head)); |
| 402 | 403 |
| 403 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 404 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 404 written_response_id_ = writer_->response_id(); | 405 written_response_id_ = writer_->response_id(); |
| 405 ScheduleNextTask(); | 406 ScheduleNextTask(); |
| 406 } | 407 } |
| 407 | 408 |
| 408 void Verify_AmountWritten(int expected_amount_written) { | 409 void Verify_AmountWritten(int expected_amount_written) { |
| 409 EXPECT_EQ(expected_amount_written, writer_->amount_written()); | 410 EXPECT_EQ(expected_amount_written, writer_->amount_written()); |
| 410 TestFinished(); | 411 TestFinished(); |
| 411 } | 412 } |
| 412 | 413 |
| 413 | 414 |
| 414 // WriteThenVariouslyReadResponse ------------------------------------------- | 415 // WriteThenVariouslyReadResponse ------------------------------------------- |
| 415 | 416 |
| 416 void WriteThenVariouslyReadResponse() { | 417 void WriteThenVariouslyReadResponse() { |
| 417 // This tests involves multiple async steps. | 418 // This tests involves multiple async steps. |
| 418 // 1. First, write a large body using multiple writes, we don't bother | 419 // 1. First, write a large body using multiple writes, we don't bother |
| 419 // with a response head for this test. | 420 // with a response head for this test. |
| 420 // 2. Read the entire body, using multiple reads | 421 // 2. Read the entire body, using multiple reads |
| 421 // 3. Read the entire body, using one read. | 422 // 3. Read the entire body, using one read. |
| 422 // 4. Attempt to read beyond the EOF. | 423 // 4. Attempt to read beyond the EOF. |
| 423 // 5. Read just a range. | 424 // 5. Read just a range. |
| 424 // 6. Attempt to read beyond EOF of a range. | 425 // 6. Attempt to read beyond EOF of a range. |
| 425 | 426 |
| 426 // Push tasks in reverse order | 427 // Push tasks in reverse order |
| 427 PushNextTask(NewRunnableMethod( | 428 PushNextTask(base::Bind(&AppCacheResponseTest::ReadRangeFullyBeyondEOF, |
| 428 this, &AppCacheResponseTest::ReadRangeFullyBeyondEOF)); | 429 base::Unretained(this))); |
| 429 PushNextTask(NewRunnableMethod( | 430 PushNextTask(base::Bind(&AppCacheResponseTest::ReadRangePartiallyBeyondEOF, |
| 430 this, &AppCacheResponseTest::ReadRangePartiallyBeyondEOF)); | 431 base::Unretained(this))); |
| 431 PushNextTask(NewRunnableMethod( | 432 PushNextTask(base::Bind(&AppCacheResponseTest::ReadPastEOF, |
| 432 this, &AppCacheResponseTest::ReadPastEOF)); | 433 base::Unretained(this))); |
| 433 PushNextTask(NewRunnableMethod( | 434 PushNextTask(base::Bind(&AppCacheResponseTest::ReadRange, |
| 434 this, &AppCacheResponseTest::ReadRange)); | 435 base::Unretained(this))); |
| 435 PushNextTask(NewRunnableMethod( | 436 PushNextTask(base::Bind(&AppCacheResponseTest::ReadPastEOF, |
| 436 this, &AppCacheResponseTest::ReadPastEOF)); | 437 base::Unretained(this))); |
| 437 PushNextTask(NewRunnableMethod( | 438 PushNextTask(base::Bind(&AppCacheResponseTest::ReadAllAtOnce, |
| 438 this, &AppCacheResponseTest::ReadAllAtOnce)); | 439 base::Unretained(this))); |
| 439 PushNextTask(NewRunnableMethod( | 440 PushNextTask(base::Bind(&AppCacheResponseTest::ReadInBlocks, |
| 440 this, &AppCacheResponseTest::ReadInBlocks)); | 441 base::Unretained(this))); |
| 441 PushNextTask(NewRunnableMethod( | 442 PushNextTask(base::Bind(&AppCacheResponseTest::WriteOutBlocks, |
| 442 this, &AppCacheResponseTest::WriteOutBlocks)); | 443 base::Unretained(this))); |
| 443 | 444 |
| 444 // Get them going. | 445 // Get them going. |
| 445 ScheduleNextTask(); | 446 ScheduleNextTask(); |
| 446 } | 447 } |
| 447 | 448 |
| 448 void WriteOutBlocks() { | 449 void WriteOutBlocks() { |
| 449 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 450 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 450 written_response_id_ = writer_->response_id(); | 451 written_response_id_ = writer_->response_id(); |
| 451 for (int i = 0; i < kNumBlocks; ++i) { | 452 for (int i = 0; i < kNumBlocks; ++i) { |
| 452 PushNextTask(NewRunnableMethod( | 453 PushNextTask(base::Bind(&AppCacheResponseTest::WriteOneBlock, |
| 453 this, &AppCacheResponseTest::WriteOneBlock, kNumBlocks - i)); | 454 base::Unretained(this), kNumBlocks - i)); |
| 454 } | 455 } |
| 455 ScheduleNextTask(); | 456 ScheduleNextTask(); |
| 456 } | 457 } |
| 457 | 458 |
| 458 void WriteOneBlock(int block_number) { | 459 void WriteOneBlock(int block_number) { |
| 459 scoped_refptr<IOBuffer> io_buffer( | 460 scoped_refptr<IOBuffer> io_buffer( |
| 460 new IOBuffer(kBlockSize)); | 461 new IOBuffer(kBlockSize)); |
| 461 FillData(block_number, io_buffer->data(), kBlockSize); | 462 FillData(block_number, io_buffer->data(), kBlockSize); |
| 462 WriteResponseBody(io_buffer, kBlockSize); | 463 WriteResponseBody(io_buffer, kBlockSize); |
| 463 } | 464 } |
| 464 | 465 |
| 465 void ReadInBlocks() { | 466 void ReadInBlocks() { |
| 466 writer_.reset(); | 467 writer_.reset(); |
| 467 reader_.reset(service_->storage()->CreateResponseReader( | 468 reader_.reset(service_->storage()->CreateResponseReader( |
| 468 GURL(), 0, written_response_id_)); | 469 GURL(), 0, written_response_id_)); |
| 469 for (int i = 0; i < kNumBlocks; ++i) { | 470 for (int i = 0; i < kNumBlocks; ++i) { |
| 470 PushNextTask(NewRunnableMethod( | 471 PushNextTask(base::Bind(&AppCacheResponseTest::ReadOneBlock, |
| 471 this, &AppCacheResponseTest::ReadOneBlock, kNumBlocks - i)); | 472 base::Unretained(this), kNumBlocks - i)); |
| 472 } | 473 } |
| 473 ScheduleNextTask(); | 474 ScheduleNextTask(); |
| 474 } | 475 } |
| 475 | 476 |
| 476 void ReadOneBlock(int block_number) { | 477 void ReadOneBlock(int block_number) { |
| 477 PushNextTask(NewRunnableMethod( | 478 PushNextTask(base::Bind(&AppCacheResponseTest::VerifyOneBlock, |
| 478 this, &AppCacheResponseTest::VerifyOneBlock, block_number)); | 479 base::Unretained(this), block_number)); |
| 479 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); | 480 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); |
| 480 } | 481 } |
| 481 | 482 |
| 482 void VerifyOneBlock(int block_number) { | 483 void VerifyOneBlock(int block_number) { |
| 483 EXPECT_TRUE(CheckData(block_number, read_buffer_->data(), kBlockSize)); | 484 EXPECT_TRUE(CheckData(block_number, read_buffer_->data(), kBlockSize)); |
| 484 ScheduleNextTask(); | 485 ScheduleNextTask(); |
| 485 } | 486 } |
| 486 | 487 |
| 487 void ReadAllAtOnce() { | 488 void ReadAllAtOnce() { |
| 488 PushNextTask(NewRunnableMethod( | 489 PushNextTask(base::Bind(&AppCacheResponseTest::VerifyAllAtOnce, |
| 489 this, &AppCacheResponseTest::VerifyAllAtOnce)); | 490 base::Unretained(this))); |
| 490 reader_.reset(service_->storage()->CreateResponseReader( | 491 reader_.reset(service_->storage()->CreateResponseReader( |
| 491 GURL(), 0, written_response_id_)); | 492 GURL(), 0, written_response_id_)); |
| 492 int big_size = kNumBlocks * kBlockSize; | 493 int big_size = kNumBlocks * kBlockSize; |
| 493 ReadResponseBody(new IOBuffer(big_size), big_size); | 494 ReadResponseBody(new IOBuffer(big_size), big_size); |
| 494 } | 495 } |
| 495 | 496 |
| 496 void VerifyAllAtOnce() { | 497 void VerifyAllAtOnce() { |
| 497 char* p = read_buffer_->data(); | 498 char* p = read_buffer_->data(); |
| 498 for (int i = 0; i < kNumBlocks; ++i, p += kBlockSize) | 499 for (int i = 0; i < kNumBlocks; ++i, p += kBlockSize) |
| 499 EXPECT_TRUE(CheckData(i + 1, p, kBlockSize)); | 500 EXPECT_TRUE(CheckData(i + 1, p, kBlockSize)); |
| 500 ScheduleNextTask(); | 501 ScheduleNextTask(); |
| 501 } | 502 } |
| 502 | 503 |
| 503 void ReadPastEOF() { | 504 void ReadPastEOF() { |
| 504 EXPECT_FALSE(reader_->IsReadPending()); | 505 EXPECT_FALSE(reader_->IsReadPending()); |
| 505 read_buffer_ = new IOBuffer(kBlockSize); | 506 read_buffer_ = new IOBuffer(kBlockSize); |
| 506 expected_read_result_ = 0; | 507 expected_read_result_ = 0; |
| 507 reader_->ReadData( | 508 reader_->ReadData( |
| 508 read_buffer_, kBlockSize, &read_callback_); | 509 read_buffer_, kBlockSize, &read_callback_); |
| 509 } | 510 } |
| 510 | 511 |
| 511 void ReadRange() { | 512 void ReadRange() { |
| 512 PushNextTask(NewRunnableMethod( | 513 PushNextTask(base::Bind(&AppCacheResponseTest::VerifyRange, |
| 513 this, &AppCacheResponseTest::VerifyRange)); | 514 base::Unretained(this))); |
| 514 reader_.reset(service_->storage()->CreateResponseReader( | 515 reader_.reset(service_->storage()->CreateResponseReader( |
| 515 GURL(), 0, written_response_id_)); | 516 GURL(), 0, written_response_id_)); |
| 516 reader_->SetReadRange(kBlockSize, kBlockSize); | 517 reader_->SetReadRange(kBlockSize, kBlockSize); |
| 517 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); | 518 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); |
| 518 } | 519 } |
| 519 | 520 |
| 520 void VerifyRange() { | 521 void VerifyRange() { |
| 521 EXPECT_TRUE(CheckData(2, read_buffer_->data(), kBlockSize)); | 522 EXPECT_TRUE(CheckData(2, read_buffer_->data(), kBlockSize)); |
| 522 ScheduleNextTask(); // ReadPastEOF is scheduled next | 523 ScheduleNextTask(); // ReadPastEOF is scheduled next |
| 523 } | 524 } |
| 524 | 525 |
| 525 void ReadRangePartiallyBeyondEOF() { | 526 void ReadRangePartiallyBeyondEOF() { |
| 526 PushNextTask(NewRunnableMethod( | 527 PushNextTask(base::Bind(&AppCacheResponseTest::VerifyRangeBeyondEOF, |
| 527 this, &AppCacheResponseTest::VerifyRangeBeyondEOF)); | 528 base::Unretained(this))); |
| 528 reader_.reset(service_->storage()->CreateResponseReader( | 529 reader_.reset(service_->storage()->CreateResponseReader( |
| 529 GURL(), 0, written_response_id_)); | 530 GURL(), 0, written_response_id_)); |
| 530 reader_->SetReadRange(kBlockSize, kNumBlocks * kBlockSize); | 531 reader_->SetReadRange(kBlockSize, kNumBlocks * kBlockSize); |
| 531 ReadResponseBody(new IOBuffer(kNumBlocks * kBlockSize), | 532 ReadResponseBody(new IOBuffer(kNumBlocks * kBlockSize), |
| 532 kNumBlocks * kBlockSize); | 533 kNumBlocks * kBlockSize); |
| 533 expected_read_result_ = (kNumBlocks - 1) * kBlockSize; | 534 expected_read_result_ = (kNumBlocks - 1) * kBlockSize; |
| 534 } | 535 } |
| 535 | 536 |
| 536 void VerifyRangeBeyondEOF() { | 537 void VerifyRangeBeyondEOF() { |
| 537 // Just verify the first 1k | 538 // Just verify the first 1k |
| 538 VerifyRange(); | 539 VerifyRange(); |
| 539 } | 540 } |
| 540 | 541 |
| 541 void ReadRangeFullyBeyondEOF() { | 542 void ReadRangeFullyBeyondEOF() { |
| 542 reader_.reset(service_->storage()->CreateResponseReader( | 543 reader_.reset(service_->storage()->CreateResponseReader( |
| 543 GURL(), 0, written_response_id_)); | 544 GURL(), 0, written_response_id_)); |
| 544 reader_->SetReadRange((kNumBlocks * kBlockSize) + 1, kBlockSize); | 545 reader_->SetReadRange((kNumBlocks * kBlockSize) + 1, kBlockSize); |
| 545 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); | 546 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); |
| 546 expected_read_result_ = 0; | 547 expected_read_result_ = 0; |
| 547 } | 548 } |
| 548 | 549 |
| 549 // IOChaining ------------------------------------------- | 550 // IOChaining ------------------------------------------- |
| 550 void IOChaining() { | 551 void IOChaining() { |
| 551 // 1. Write several blocks out initiating the subsequent write | 552 // 1. Write several blocks out initiating the subsequent write |
| 552 // from within the completion callback of the previous write. | 553 // from within the completion callback of the previous write. |
| 553 // 2. Read and verify several blocks in similarly chaining reads. | 554 // 2. Read and verify several blocks in similarly chaining reads. |
| 554 | 555 |
| 555 // Push tasks in reverse order | 556 // Push tasks in reverse order |
| 556 PushNextTaskAsImmediate(NewRunnableMethod( | 557 PushNextTaskAsImmediate( |
| 557 this, &AppCacheResponseTest::ReadInBlocksImmediately)); | 558 base::Bind(&AppCacheResponseTest::ReadInBlocksImmediately, |
| 558 PushNextTaskAsImmediate(NewRunnableMethod( | 559 base::Unretained(this))); |
| 559 this, &AppCacheResponseTest::WriteOutBlocksImmediately)); | 560 PushNextTaskAsImmediate( |
| 561 base::Bind(&AppCacheResponseTest::WriteOutBlocksImmediately, | |
| 562 base::Unretained(this))); | |
| 560 | 563 |
| 561 // Get them going. | 564 // Get them going. |
| 562 ScheduleNextTask(); | 565 ScheduleNextTask(); |
| 563 } | 566 } |
| 564 | 567 |
| 565 void WriteOutBlocksImmediately() { | 568 void WriteOutBlocksImmediately() { |
| 566 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 569 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 567 written_response_id_ = writer_->response_id(); | 570 written_response_id_ = writer_->response_id(); |
| 568 for (int i = 0; i < kNumBlocks; ++i) { | 571 for (int i = 0; i < kNumBlocks; ++i) { |
| 569 PushNextTaskAsImmediate(NewRunnableMethod( | 572 PushNextTaskAsImmediate( |
| 570 this, &AppCacheResponseTest::WriteOneBlock, kNumBlocks - i)); | 573 base::Bind(&AppCacheResponseTest::WriteOneBlock, |
| 574 base::Unretained(this), kNumBlocks - i)); | |
| 571 } | 575 } |
| 572 ScheduleNextTask(); | 576 ScheduleNextTask(); |
| 573 } | 577 } |
| 574 | 578 |
| 575 void ReadInBlocksImmediately() { | 579 void ReadInBlocksImmediately() { |
| 576 writer_.reset(); | 580 writer_.reset(); |
| 577 reader_.reset(service_->storage()->CreateResponseReader( | 581 reader_.reset(service_->storage()->CreateResponseReader( |
| 578 GURL(), 0, written_response_id_)); | 582 GURL(), 0, written_response_id_)); |
| 579 for (int i = 0; i < kNumBlocks; ++i) { | 583 for (int i = 0; i < kNumBlocks; ++i) { |
| 580 PushNextTaskAsImmediate(NewRunnableMethod( | 584 PushNextTaskAsImmediate( |
| 581 this, &AppCacheResponseTest::ReadOneBlockImmediately, | 585 base::Bind(&AppCacheResponseTest::ReadOneBlockImmediately, |
| 586 base::Unretained(this), | |
| 582 kNumBlocks - i)); | 587 kNumBlocks - i)); |
| 583 } | 588 } |
| 584 ScheduleNextTask(); | 589 ScheduleNextTask(); |
| 585 } | 590 } |
| 586 | 591 |
| 587 void ReadOneBlockImmediately(int block_number) { | 592 void ReadOneBlockImmediately(int block_number) { |
| 588 PushNextTaskAsImmediate(NewRunnableMethod( | 593 PushNextTaskAsImmediate(base::Bind(&AppCacheResponseTest::VerifyOneBlock, |
| 589 this, &AppCacheResponseTest::VerifyOneBlock, block_number)); | 594 base::Unretained(this), block_number)); |
| 590 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); | 595 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); |
| 591 } | 596 } |
| 592 | 597 |
| 593 // DeleteWithinCallbacks ------------------------------------------- | 598 // DeleteWithinCallbacks ------------------------------------------- |
| 594 void DeleteWithinCallbacks() { | 599 void DeleteWithinCallbacks() { |
| 595 // 1. Write out a few blocks normally, and upon | 600 // 1. Write out a few blocks normally, and upon |
| 596 // completion of the last write, delete the writer. | 601 // completion of the last write, delete the writer. |
| 597 // 2. Read in a few blocks normally, and upon completion | 602 // 2. Read in a few blocks normally, and upon completion |
| 598 // of the last read, delete the reader. | 603 // of the last read, delete the reader. |
| 599 | 604 |
| 600 should_delete_reader_in_completion_callback_ = true; | 605 should_delete_reader_in_completion_callback_ = true; |
| 601 reader_deletion_count_down_ = kNumBlocks; | 606 reader_deletion_count_down_ = kNumBlocks; |
| 602 should_delete_writer_in_completion_callback_ = true; | 607 should_delete_writer_in_completion_callback_ = true; |
| 603 writer_deletion_count_down_ = kNumBlocks; | 608 writer_deletion_count_down_ = kNumBlocks; |
| 604 | 609 |
| 605 PushNextTask(NewRunnableMethod( | 610 PushNextTask(base::Bind(&AppCacheResponseTest::ReadInBlocks, |
| 606 this, &AppCacheResponseTest::ReadInBlocks)); | 611 base::Unretained(this))); |
| 607 PushNextTask(NewRunnableMethod( | 612 PushNextTask(base::Bind(&AppCacheResponseTest::WriteOutBlocks, |
| 608 this, &AppCacheResponseTest::WriteOutBlocks)); | 613 base::Unretained(this))); |
| 609 ScheduleNextTask(); | 614 ScheduleNextTask(); |
| 610 } | 615 } |
| 611 | 616 |
| 612 // DeleteWithIOPending ------------------------------------------- | 617 // DeleteWithIOPending ------------------------------------------- |
| 613 void DeleteWithIOPending() { | 618 void DeleteWithIOPending() { |
| 614 // 1. Write a few blocks normally. | 619 // 1. Write a few blocks normally. |
| 615 // 2. Start a write, delete with it pending. | 620 // 2. Start a write, delete with it pending. |
| 616 // 3. Start a read, delete with it pending. | 621 // 3. Start a read, delete with it pending. |
| 617 PushNextTask(NewRunnableMethod( | 622 PushNextTask(base::Bind(&AppCacheResponseTest::ReadThenDelete, |
| 618 this, &AppCacheResponseTest::ReadThenDelete)); | 623 base::Unretained(this))); |
| 619 PushNextTask(NewRunnableMethod( | 624 PushNextTask(base::Bind(&AppCacheResponseTest::WriteThenDelete, |
| 620 this, &AppCacheResponseTest::WriteThenDelete)); | 625 base::Unretained(this))); |
| 621 PushNextTask(NewRunnableMethod( | 626 PushNextTask(base::Bind(&AppCacheResponseTest::WriteOutBlocks, |
| 622 this, &AppCacheResponseTest::WriteOutBlocks)); | 627 base::Unretained(this))); |
| 623 ScheduleNextTask(); | 628 ScheduleNextTask(); |
| 624 } | 629 } |
| 625 | 630 |
| 626 void WriteThenDelete() { | 631 void WriteThenDelete() { |
| 627 write_callback_was_called_ = false; | 632 write_callback_was_called_ = false; |
| 628 WriteOneBlock(5); | 633 WriteOneBlock(5); |
| 629 EXPECT_TRUE(writer_->IsWritePending()); | 634 EXPECT_TRUE(writer_->IsWritePending()); |
| 630 writer_.reset(); | 635 writer_.reset(); |
| 631 ScheduleNextTask(); | 636 ScheduleNextTask(); |
| 632 } | 637 } |
| 633 | 638 |
| 634 void ReadThenDelete() { | 639 void ReadThenDelete() { |
| 635 read_callback_was_called_ = false; | 640 read_callback_was_called_ = false; |
| 636 reader_.reset(service_->storage()->CreateResponseReader( | 641 reader_.reset(service_->storage()->CreateResponseReader( |
| 637 GURL(), 0, written_response_id_)); | 642 GURL(), 0, written_response_id_)); |
| 638 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); | 643 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); |
| 639 EXPECT_TRUE(reader_->IsReadPending()); | 644 EXPECT_TRUE(reader_->IsReadPending()); |
| 640 reader_.reset(); | 645 reader_.reset(); |
| 641 | 646 |
| 642 // Wait a moment to verify no callbacks. | 647 // Wait a moment to verify no callbacks. |
| 643 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 648 MessageLoop::current()->PostDelayedTask( |
| 644 NewRunnableMethod(this, &AppCacheResponseTest::VerifyNoCallbacks), | 649 FROM_HERE, base::Bind(&AppCacheResponseTest::VerifyNoCallbacks, |
| 650 base::Unretained(this)), | |
| 645 10); | 651 10); |
| 646 } | 652 } |
| 647 | 653 |
| 648 void VerifyNoCallbacks() { | 654 void VerifyNoCallbacks() { |
| 649 EXPECT_TRUE(!write_callback_was_called_); | 655 EXPECT_TRUE(!write_callback_was_called_); |
| 650 EXPECT_TRUE(!read_callback_was_called_); | 656 EXPECT_TRUE(!read_callback_was_called_); |
| 651 TestFinished(); | 657 TestFinished(); |
| 652 } | 658 } |
| 653 | 659 |
| 654 // Data members | 660 // Data members |
| 655 | 661 |
| 656 scoped_ptr<base::WaitableEvent> test_finished_event_; | 662 scoped_ptr<base::WaitableEvent> test_finished_event_; |
| 657 scoped_ptr<MockStorageDelegate> storage_delegate_; | 663 scoped_ptr<MockStorageDelegate> storage_delegate_; |
| 658 scoped_ptr<MockAppCacheService> service_; | 664 scoped_ptr<MockAppCacheService> service_; |
| 659 std::stack<std::pair<Task*, bool> > task_stack_; | 665 std::stack<std::pair<base::Closure, bool> > task_stack_; |
| 660 | 666 |
| 661 scoped_ptr<AppCacheResponseReader> reader_; | 667 scoped_ptr<AppCacheResponseReader> reader_; |
| 662 scoped_refptr<HttpResponseInfoIOBuffer> read_info_buffer_; | 668 scoped_refptr<HttpResponseInfoIOBuffer> read_info_buffer_; |
| 663 scoped_refptr<IOBuffer> read_buffer_; | 669 scoped_refptr<IOBuffer> read_buffer_; |
| 664 int expected_read_result_; | 670 int expected_read_result_; |
| 665 net::OldCompletionCallbackImpl<AppCacheResponseTest> read_callback_; | 671 net::OldCompletionCallbackImpl<AppCacheResponseTest> read_callback_; |
| 666 net::OldCompletionCallbackImpl<AppCacheResponseTest> read_info_callback_; | 672 net::OldCompletionCallbackImpl<AppCacheResponseTest> read_info_callback_; |
| 667 bool should_delete_reader_in_completion_callback_; | 673 bool should_delete_reader_in_completion_callback_; |
| 668 int reader_deletion_count_down_; | 674 int reader_deletion_count_down_; |
| 669 bool read_callback_was_called_; | 675 bool read_callback_was_called_; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 711 | 717 |
| 712 TEST_F(AppCacheResponseTest, DeleteWithinCallbacks) { | 718 TEST_F(AppCacheResponseTest, DeleteWithinCallbacks) { |
| 713 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithinCallbacks); | 719 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithinCallbacks); |
| 714 } | 720 } |
| 715 | 721 |
| 716 TEST_F(AppCacheResponseTest, DeleteWithIOPending) { | 722 TEST_F(AppCacheResponseTest, DeleteWithIOPending) { |
| 717 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithIOPending); | 723 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithIOPending); |
| 718 } | 724 } |
| 719 | 725 |
| 720 } // namespace appcache | 726 } // namespace appcache |
| 721 | |
| 722 // AppCacheResponseTest is expected to always live longer than the | |
| 723 // runnable methods. This lets us call NewRunnableMethod on its instances. | |
| 724 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheResponseTest); | |
| OLD | NEW |