| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "net/disk_cache/blockfile/file_block.h" | 8 #include "net/disk_cache/blockfile/file_block.h" |
| 9 #include "net/disk_cache/blockfile/mapped_file.h" | 9 #include "net/disk_cache/blockfile/mapped_file.h" |
| 10 #include "net/disk_cache/disk_cache_test_base.h" | 10 #include "net/disk_cache/disk_cache_test_base.h" |
| 11 #include "net/disk_cache/disk_cache_test_util.h" | 11 #include "net/disk_cache/disk_cache_test_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Implementation of FileIOCallback for the tests. | 16 // Implementation of FileIOCallback for the tests. |
| 17 class FileCallbackTest: public disk_cache::FileIOCallback { | 17 class FileCallbackTest : public disk_cache::FileIOCallback { |
| 18 public: | 18 public: |
| 19 FileCallbackTest(int id, MessageLoopHelper* helper, int* max_id) | 19 FileCallbackTest(int id, MessageLoopHelper* helper, int* max_id) |
| 20 : id_(id), | 20 : id_(id), helper_(helper), max_id_(max_id) {} |
| 21 helper_(helper), | |
| 22 max_id_(max_id) { | |
| 23 } | |
| 24 virtual ~FileCallbackTest() {} | 21 virtual ~FileCallbackTest() {} |
| 25 | 22 |
| 26 virtual void OnFileIOComplete(int bytes_copied) OVERRIDE; | 23 virtual void OnFileIOComplete(int bytes_copied) OVERRIDE; |
| 27 | 24 |
| 28 private: | 25 private: |
| 29 int id_; | 26 int id_; |
| 30 MessageLoopHelper* helper_; | 27 MessageLoopHelper* helper_; |
| 31 int* max_id_; | 28 int* max_id_; |
| 32 }; | 29 }; |
| 33 | 30 |
| 34 void FileCallbackTest::OnFileIOComplete(int bytes_copied) { | 31 void FileCallbackTest::OnFileIOComplete(int bytes_copied) { |
| 35 if (id_ > *max_id_) { | 32 if (id_ > *max_id_) { |
| 36 NOTREACHED(); | 33 NOTREACHED(); |
| 37 helper_->set_callback_reused_error(true); | 34 helper_->set_callback_reused_error(true); |
| 38 } | 35 } |
| 39 | 36 |
| 40 helper_->CallbackWasCalled(); | 37 helper_->CallbackWasCalled(); |
| 41 } | 38 } |
| 42 | 39 |
| 43 class TestFileBlock : public disk_cache::FileBlock { | 40 class TestFileBlock : public disk_cache::FileBlock { |
| 44 public: | 41 public: |
| 45 TestFileBlock() { | 42 TestFileBlock() { CacheTestFillBuffer(buffer_, sizeof(buffer_), false); } |
| 46 CacheTestFillBuffer(buffer_, sizeof(buffer_), false); | |
| 47 } | |
| 48 virtual ~TestFileBlock() {} | 43 virtual ~TestFileBlock() {} |
| 49 | 44 |
| 50 // FileBlock interface. | 45 // FileBlock interface. |
| 51 virtual void* buffer() const OVERRIDE { return const_cast<char*>(buffer_); } | 46 virtual void* buffer() const OVERRIDE { return const_cast<char*>(buffer_); } |
| 52 virtual size_t size() const OVERRIDE { return sizeof(buffer_); } | 47 virtual size_t size() const OVERRIDE { return sizeof(buffer_); } |
| 53 virtual int offset() const OVERRIDE { return 1024; } | 48 virtual int offset() const OVERRIDE { return 1024; } |
| 54 | 49 |
| 55 private: | 50 private: |
| 56 char buffer_[20]; | 51 char buffer_[20]; |
| 57 }; | 52 }; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 81 | 76 |
| 82 int max_id = 0; | 77 int max_id = 0; |
| 83 MessageLoopHelper helper; | 78 MessageLoopHelper helper; |
| 84 FileCallbackTest callback(1, &helper, &max_id); | 79 FileCallbackTest callback(1, &helper, &max_id); |
| 85 | 80 |
| 86 char buffer1[20]; | 81 char buffer1[20]; |
| 87 char buffer2[20]; | 82 char buffer2[20]; |
| 88 CacheTestFillBuffer(buffer1, sizeof(buffer1), false); | 83 CacheTestFillBuffer(buffer1, sizeof(buffer1), false); |
| 89 base::strlcpy(buffer1, "the data", arraysize(buffer1)); | 84 base::strlcpy(buffer1, "the data", arraysize(buffer1)); |
| 90 bool completed; | 85 bool completed; |
| 91 EXPECT_TRUE(file->Write(buffer1, sizeof(buffer1), 1024 * 1024, &callback, | 86 EXPECT_TRUE(file->Write( |
| 92 &completed)); | 87 buffer1, sizeof(buffer1), 1024 * 1024, &callback, &completed)); |
| 93 int expected = completed ? 0 : 1; | 88 int expected = completed ? 0 : 1; |
| 94 | 89 |
| 95 max_id = 1; | 90 max_id = 1; |
| 96 helper.WaitUntilCacheIoFinished(expected); | 91 helper.WaitUntilCacheIoFinished(expected); |
| 97 | 92 |
| 98 EXPECT_TRUE(file->Read(buffer2, sizeof(buffer2), 1024 * 1024, &callback, | 93 EXPECT_TRUE( |
| 99 &completed)); | 94 file->Read(buffer2, sizeof(buffer2), 1024 * 1024, &callback, &completed)); |
| 100 if (!completed) | 95 if (!completed) |
| 101 expected++; | 96 expected++; |
| 102 | 97 |
| 103 helper.WaitUntilCacheIoFinished(expected); | 98 helper.WaitUntilCacheIoFinished(expected); |
| 104 | 99 |
| 105 EXPECT_EQ(expected, helper.callbacks_called()); | 100 EXPECT_EQ(expected, helper.callbacks_called()); |
| 106 EXPECT_FALSE(helper.callback_reused_error()); | 101 EXPECT_FALSE(helper.callback_reused_error()); |
| 107 EXPECT_STREQ(buffer1, buffer2); | 102 EXPECT_STREQ(buffer1, buffer2); |
| 108 } | 103 } |
| 109 | 104 |
| 110 TEST_F(DiskCacheTest, MappedFile_AsyncLoadStore) { | 105 TEST_F(DiskCacheTest, MappedFile_AsyncLoadStore) { |
| 111 base::FilePath filename = cache_path_.AppendASCII("a_test"); | 106 base::FilePath filename = cache_path_.AppendASCII("a_test"); |
| 112 scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile); | 107 scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile); |
| 113 ASSERT_TRUE(CreateCacheTestFile(filename)); | 108 ASSERT_TRUE(CreateCacheTestFile(filename)); |
| 114 ASSERT_TRUE(file->Init(filename, 8192)); | 109 ASSERT_TRUE(file->Init(filename, 8192)); |
| 115 | 110 |
| 116 int max_id = 0; | 111 int max_id = 0; |
| 117 MessageLoopHelper helper; | 112 MessageLoopHelper helper; |
| 118 FileCallbackTest callback(1, &helper, &max_id); | 113 FileCallbackTest callback(1, &helper, &max_id); |
| 119 | 114 |
| 120 TestFileBlock file_block1; | 115 TestFileBlock file_block1; |
| 121 TestFileBlock file_block2; | 116 TestFileBlock file_block2; |
| 122 base::strlcpy(static_cast<char*>(file_block1.buffer()), "the data", | 117 base::strlcpy( |
| 123 file_block1.size()); | 118 static_cast<char*>(file_block1.buffer()), "the data", file_block1.size()); |
| 124 bool completed; | 119 bool completed; |
| 125 EXPECT_TRUE(file->Store(&file_block1, &callback, &completed)); | 120 EXPECT_TRUE(file->Store(&file_block1, &callback, &completed)); |
| 126 int expected = completed ? 0 : 1; | 121 int expected = completed ? 0 : 1; |
| 127 | 122 |
| 128 max_id = 1; | 123 max_id = 1; |
| 129 helper.WaitUntilCacheIoFinished(expected); | 124 helper.WaitUntilCacheIoFinished(expected); |
| 130 | 125 |
| 131 EXPECT_TRUE(file->Load(&file_block2, &callback, &completed)); | 126 EXPECT_TRUE(file->Load(&file_block2, &callback, &completed)); |
| 132 if (!completed) | 127 if (!completed) |
| 133 expected++; | 128 expected++; |
| 134 | 129 |
| 135 helper.WaitUntilCacheIoFinished(expected); | 130 helper.WaitUntilCacheIoFinished(expected); |
| 136 | 131 |
| 137 EXPECT_EQ(expected, helper.callbacks_called()); | 132 EXPECT_EQ(expected, helper.callbacks_called()); |
| 138 EXPECT_FALSE(helper.callback_reused_error()); | 133 EXPECT_FALSE(helper.callback_reused_error()); |
| 139 EXPECT_STREQ(static_cast<char*>(file_block1.buffer()), | 134 EXPECT_STREQ(static_cast<char*>(file_block1.buffer()), |
| 140 static_cast<char*>(file_block2.buffer())); | 135 static_cast<char*>(file_block2.buffer())); |
| 141 } | 136 } |
| OLD | NEW |