| 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 "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 121 |
| 122 itr->Delete(); | 122 itr->Delete(); |
| 123 EXPECT_TRUE(itr->Valid()); | 123 EXPECT_TRUE(itr->Valid()); |
| 124 EXPECT_EQ("ab", itr->key().ToString()); | 124 EXPECT_EQ("ab", itr->key().ToString()); |
| 125 EXPECT_EQ("0", itr->value().ToString()); | 125 EXPECT_EQ("0", itr->value().ToString()); |
| 126 | 126 |
| 127 itr->SeekToFirst(); | 127 itr->SeekToFirst(); |
| 128 EXPECT_TRUE(itr->Valid()); | 128 EXPECT_TRUE(itr->Valid()); |
| 129 EXPECT_EQ("ab", itr->key().ToString()); | 129 EXPECT_EQ("ab", itr->key().ToString()); |
| 130 EXPECT_EQ("0", itr->value().ToString()); | 130 EXPECT_EQ("0", itr->value().ToString()); |
| 131 |
| 132 EXPECT_EQ(0, GetDB()->num_puts()); |
| 133 EXPECT_EQ(1, GetDB()->num_deletes()); |
| 131 } | 134 } |
| 132 | 135 |
| 133 TEST_F(LevelDBWrapperTest, Iterator2Test) { | 136 TEST_F(LevelDBWrapperTest, Iterator2Test) { |
| 134 GetDB()->Put("a", "1"); | 137 GetDB()->Put("a", "1"); |
| 135 GetDB()->Put("b", "2"); | 138 GetDB()->Put("b", "2"); |
| 136 GetDB()->Put("c", "3"); | 139 GetDB()->Put("c", "3"); |
| 137 // Keep pending transanctions on memory. | 140 // Keep pending transanctions on memory. |
| 138 | 141 |
| 139 scoped_ptr<LevelDBWrapper::Iterator> itr = GetDB()->NewIterator(); | 142 scoped_ptr<LevelDBWrapper::Iterator> itr = GetDB()->NewIterator(); |
| 140 | 143 |
| 141 std::string prev_key; | 144 std::string prev_key; |
| 142 std::string prev_value; | 145 std::string prev_value; |
| 143 int loop_counter = 0; | 146 int loop_counter = 0; |
| 144 for (itr->SeekToFirst(); itr->Valid(); itr->Next()) { | 147 for (itr->SeekToFirst(); itr->Valid(); itr->Next()) { |
| 145 ASSERT_NE(prev_key, itr->key().ToString()); | 148 ASSERT_NE(prev_key, itr->key().ToString()); |
| 146 ASSERT_NE(prev_value, itr->value().ToString()); | 149 ASSERT_NE(prev_value, itr->value().ToString()); |
| 147 prev_key = itr->key().ToString(); | 150 prev_key = itr->key().ToString(); |
| 148 prev_value = itr->value().ToString(); | 151 prev_value = itr->value().ToString(); |
| 149 ++loop_counter; | 152 ++loop_counter; |
| 150 } | 153 } |
| 151 EXPECT_EQ(3, loop_counter); | 154 EXPECT_EQ(3, loop_counter); |
| 152 EXPECT_EQ("c", prev_key); | 155 EXPECT_EQ("c", prev_key); |
| 153 EXPECT_EQ("3", prev_value); | 156 EXPECT_EQ("3", prev_value); |
| 157 |
| 158 EXPECT_EQ(3, GetDB()->num_puts()); |
| 159 EXPECT_EQ(0, GetDB()->num_deletes()); |
| 154 } | 160 } |
| 155 | 161 |
| 156 TEST_F(LevelDBWrapperTest, PutTest) { | 162 TEST_F(LevelDBWrapperTest, PutTest) { |
| 157 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"ab", "0"}, | 163 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"ab", "0"}, |
| 158 {"bb", "new2"}, {"c", "new1"}, {"d", "4"}}; | 164 {"bb", "new2"}, {"c", "new1"}, {"d", "4"}}; |
| 159 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; | 165 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; |
| 160 | 166 |
| 161 CreateDefaultDatabase(); | 167 CreateDefaultDatabase(); |
| 162 | 168 |
| 163 // Add pending transactions. | 169 // Add pending transactions. |
| 164 GetDB()->Put("aa", "new0"); | 170 GetDB()->Put("aa", "new0"); |
| 165 GetDB()->Put("c", "new1"); | 171 GetDB()->Put("c", "new1"); |
| 166 GetDB()->Put("bb", "new2"); // Overwrite an entry. | 172 GetDB()->Put("bb", "new2"); // Overwrite an entry. |
| 167 | 173 |
| 168 SCOPED_TRACE("PutTest_Pending"); | 174 SCOPED_TRACE("PutTest_Pending"); |
| 169 CheckDBContents(merged_data, arraysize(merged_data)); | 175 CheckDBContents(merged_data, arraysize(merged_data)); |
| 170 | 176 |
| 177 EXPECT_EQ(3, GetDB()->num_puts()); |
| 171 // Remove all pending transactions. | 178 // Remove all pending transactions. |
| 172 GetDB()->Clear(); | 179 GetDB()->Clear(); |
| 180 EXPECT_EQ(0, GetDB()->num_puts()); |
| 173 | 181 |
| 174 SCOPED_TRACE("PutTest_Clear"); | 182 SCOPED_TRACE("PutTest_Clear"); |
| 175 CheckDBContents(orig_data, arraysize(orig_data)); | 183 CheckDBContents(orig_data, arraysize(orig_data)); |
| 176 | 184 |
| 177 // Add pending transactions again, with commiting. | 185 // Add pending transactions again, with commiting. |
| 178 GetDB()->Put("aa", "new0"); | 186 GetDB()->Put("aa", "new0"); |
| 179 GetDB()->Put("c", "new1"); | 187 GetDB()->Put("c", "new1"); |
| 180 GetDB()->Put("bb", "new2"); | 188 GetDB()->Put("bb", "new2"); |
| 189 EXPECT_EQ(3, GetDB()->num_puts()); |
| 181 GetDB()->Commit(); | 190 GetDB()->Commit(); |
| 191 EXPECT_EQ(0, GetDB()->num_puts()); |
| 182 GetDB()->Clear(); // Clear just in case. | 192 GetDB()->Clear(); // Clear just in case. |
| 183 | 193 |
| 184 SCOPED_TRACE("PutTest_Commit"); | 194 SCOPED_TRACE("PutTest_Commit"); |
| 185 CheckDBContents(merged_data, arraysize(merged_data)); | 195 CheckDBContents(merged_data, arraysize(merged_data)); |
| 186 } | 196 } |
| 187 | 197 |
| 188 TEST_F(LevelDBWrapperTest, DeleteTest) { | 198 TEST_F(LevelDBWrapperTest, DeleteTest) { |
| 189 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"bb", "new2"}, | 199 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"bb", "new2"}, |
| 190 {"d", "4"}}; | 200 {"d", "4"}}; |
| 191 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; | 201 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; |
| 192 | 202 |
| 193 CreateDefaultDatabase(); | 203 CreateDefaultDatabase(); |
| 194 | 204 |
| 195 // Add pending transactions. | 205 // Add pending transactions. |
| 196 GetDB()->Put("aa", "new0"); | 206 GetDB()->Put("aa", "new0"); |
| 197 GetDB()->Put("c", "new1"); | 207 GetDB()->Put("c", "new1"); |
| 198 GetDB()->Put("bb", "new2"); // Overwrite an entry. | 208 GetDB()->Put("bb", "new2"); // Overwrite an entry. |
| 199 GetDB()->Delete("c"); // Remove a pending entry. | 209 GetDB()->Delete("c"); // Remove a pending entry. |
| 200 GetDB()->Delete("ab"); // Remove a committed entry. | 210 GetDB()->Delete("ab"); // Remove a committed entry. |
| 201 | 211 |
| 212 EXPECT_EQ(3, GetDB()->num_puts()); |
| 213 EXPECT_EQ(2, GetDB()->num_deletes()); |
| 214 |
| 202 SCOPED_TRACE("DeleteTest_Pending"); | 215 SCOPED_TRACE("DeleteTest_Pending"); |
| 203 CheckDBContents(merged_data, arraysize(merged_data)); | 216 CheckDBContents(merged_data, arraysize(merged_data)); |
| 204 | 217 |
| 205 // Remove all pending transactions. | 218 // Remove all pending transactions. |
| 206 GetDB()->Clear(); | 219 GetDB()->Clear(); |
| 207 | 220 |
| 208 SCOPED_TRACE("DeleteTest_Clear"); | 221 SCOPED_TRACE("DeleteTest_Clear"); |
| 209 CheckDBContents(orig_data, arraysize(orig_data)); | 222 CheckDBContents(orig_data, arraysize(orig_data)); |
| 210 | 223 |
| 211 // Add pending transactions again, with commiting. | 224 // Add pending transactions again, with commiting. |
| 212 GetDB()->Put("aa", "new0"); | 225 GetDB()->Put("aa", "new0"); |
| 213 GetDB()->Put("c", "new1"); | 226 GetDB()->Put("c", "new1"); |
| 214 GetDB()->Put("bb", "new2"); | 227 GetDB()->Put("bb", "new2"); |
| 215 GetDB()->Delete("c"); | 228 GetDB()->Delete("c"); |
| 216 GetDB()->Delete("ab"); | 229 GetDB()->Delete("ab"); |
| 217 GetDB()->Commit(); | 230 GetDB()->Commit(); |
| 218 GetDB()->Clear(); | 231 GetDB()->Clear(); |
| 219 | 232 |
| 233 EXPECT_EQ(0, GetDB()->num_puts()); |
| 234 EXPECT_EQ(0, GetDB()->num_deletes()); |
| 235 |
| 220 SCOPED_TRACE("DeleteTest_Commit"); | 236 SCOPED_TRACE("DeleteTest_Commit"); |
| 221 CheckDBContents(merged_data, arraysize(merged_data)); | 237 CheckDBContents(merged_data, arraysize(merged_data)); |
| 222 } | 238 } |
| 223 | 239 |
| 224 } // namespace drive_backend | 240 } // namespace drive_backend |
| 225 } // namespace sync_file_system | 241 } // namespace sync_file_system |
| OLD | NEW |