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 | |
nhiroki
2014/08/14 08:59:30
nit: extra blank line
peria
2014/08/14 09:01:53
Done.
| |
159 EXPECT_EQ(3, GetDB()->num_puts()); | |
160 EXPECT_EQ(0, GetDB()->num_deletes()); | |
154 } | 161 } |
155 | 162 |
156 TEST_F(LevelDBWrapperTest, PutTest) { | 163 TEST_F(LevelDBWrapperTest, PutTest) { |
157 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"ab", "0"}, | 164 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"ab", "0"}, |
158 {"bb", "new2"}, {"c", "new1"}, {"d", "4"}}; | 165 {"bb", "new2"}, {"c", "new1"}, {"d", "4"}}; |
159 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; | 166 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; |
160 | 167 |
161 CreateDefaultDatabase(); | 168 CreateDefaultDatabase(); |
162 | 169 |
163 // Add pending transactions. | 170 // Add pending transactions. |
164 GetDB()->Put("aa", "new0"); | 171 GetDB()->Put("aa", "new0"); |
165 GetDB()->Put("c", "new1"); | 172 GetDB()->Put("c", "new1"); |
166 GetDB()->Put("bb", "new2"); // Overwrite an entry. | 173 GetDB()->Put("bb", "new2"); // Overwrite an entry. |
167 | 174 |
168 SCOPED_TRACE("PutTest_Pending"); | 175 SCOPED_TRACE("PutTest_Pending"); |
169 CheckDBContents(merged_data, arraysize(merged_data)); | 176 CheckDBContents(merged_data, arraysize(merged_data)); |
170 | 177 |
178 | |
nhiroki
2014/08/14 08:59:30
nit: ditto.
peria
2014/08/14 09:01:53
Done.
| |
179 EXPECT_EQ(3, GetDB()->num_puts()); | |
171 // Remove all pending transactions. | 180 // Remove all pending transactions. |
172 GetDB()->Clear(); | 181 GetDB()->Clear(); |
182 EXPECT_EQ(0, GetDB()->num_puts()); | |
173 | 183 |
174 SCOPED_TRACE("PutTest_Clear"); | 184 SCOPED_TRACE("PutTest_Clear"); |
175 CheckDBContents(orig_data, arraysize(orig_data)); | 185 CheckDBContents(orig_data, arraysize(orig_data)); |
176 | 186 |
177 // Add pending transactions again, with commiting. | 187 // Add pending transactions again, with commiting. |
178 GetDB()->Put("aa", "new0"); | 188 GetDB()->Put("aa", "new0"); |
179 GetDB()->Put("c", "new1"); | 189 GetDB()->Put("c", "new1"); |
180 GetDB()->Put("bb", "new2"); | 190 GetDB()->Put("bb", "new2"); |
191 EXPECT_EQ(3, GetDB()->num_puts()); | |
181 GetDB()->Commit(); | 192 GetDB()->Commit(); |
193 EXPECT_EQ(0, GetDB()->num_puts()); | |
182 GetDB()->Clear(); // Clear just in case. | 194 GetDB()->Clear(); // Clear just in case. |
183 | 195 |
184 SCOPED_TRACE("PutTest_Commit"); | 196 SCOPED_TRACE("PutTest_Commit"); |
185 CheckDBContents(merged_data, arraysize(merged_data)); | 197 CheckDBContents(merged_data, arraysize(merged_data)); |
186 } | 198 } |
187 | 199 |
188 TEST_F(LevelDBWrapperTest, DeleteTest) { | 200 TEST_F(LevelDBWrapperTest, DeleteTest) { |
189 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"bb", "new2"}, | 201 TestData merged_data[] = {{"a", "1"}, {"aa", "new0"}, {"bb", "new2"}, |
190 {"d", "4"}}; | 202 {"d", "4"}}; |
191 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; | 203 TestData orig_data[] = {{"a", "1"}, {"ab", "0"}, {"bb", "3"}, {"d", "4"}}; |
192 | 204 |
193 CreateDefaultDatabase(); | 205 CreateDefaultDatabase(); |
194 | 206 |
195 // Add pending transactions. | 207 // Add pending transactions. |
196 GetDB()->Put("aa", "new0"); | 208 GetDB()->Put("aa", "new0"); |
197 GetDB()->Put("c", "new1"); | 209 GetDB()->Put("c", "new1"); |
198 GetDB()->Put("bb", "new2"); // Overwrite an entry. | 210 GetDB()->Put("bb", "new2"); // Overwrite an entry. |
199 GetDB()->Delete("c"); // Remove a pending entry. | 211 GetDB()->Delete("c"); // Remove a pending entry. |
200 GetDB()->Delete("ab"); // Remove a committed entry. | 212 GetDB()->Delete("ab"); // Remove a committed entry. |
201 | 213 |
214 EXPECT_EQ(3, GetDB()->num_puts()); | |
215 EXPECT_EQ(2, GetDB()->num_deletes()); | |
216 | |
202 SCOPED_TRACE("DeleteTest_Pending"); | 217 SCOPED_TRACE("DeleteTest_Pending"); |
203 CheckDBContents(merged_data, arraysize(merged_data)); | 218 CheckDBContents(merged_data, arraysize(merged_data)); |
204 | 219 |
205 // Remove all pending transactions. | 220 // Remove all pending transactions. |
206 GetDB()->Clear(); | 221 GetDB()->Clear(); |
207 | 222 |
208 SCOPED_TRACE("DeleteTest_Clear"); | 223 SCOPED_TRACE("DeleteTest_Clear"); |
209 CheckDBContents(orig_data, arraysize(orig_data)); | 224 CheckDBContents(orig_data, arraysize(orig_data)); |
210 | 225 |
211 // Add pending transactions again, with commiting. | 226 // Add pending transactions again, with commiting. |
212 GetDB()->Put("aa", "new0"); | 227 GetDB()->Put("aa", "new0"); |
213 GetDB()->Put("c", "new1"); | 228 GetDB()->Put("c", "new1"); |
214 GetDB()->Put("bb", "new2"); | 229 GetDB()->Put("bb", "new2"); |
215 GetDB()->Delete("c"); | 230 GetDB()->Delete("c"); |
216 GetDB()->Delete("ab"); | 231 GetDB()->Delete("ab"); |
217 GetDB()->Commit(); | 232 GetDB()->Commit(); |
218 GetDB()->Clear(); | 233 GetDB()->Clear(); |
219 | 234 |
235 EXPECT_EQ(0, GetDB()->num_puts()); | |
236 EXPECT_EQ(0, GetDB()->num_deletes()); | |
237 | |
220 SCOPED_TRACE("DeleteTest_Commit"); | 238 SCOPED_TRACE("DeleteTest_Commit"); |
221 CheckDBContents(merged_data, arraysize(merged_data)); | 239 CheckDBContents(merged_data, arraysize(merged_data)); |
222 } | 240 } |
223 | 241 |
224 } // namespace drive_backend | 242 } // namespace drive_backend |
225 } // namespace sync_file_system | 243 } // namespace sync_file_system |
OLD | NEW |