Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: sync/internal_api/attachments/attachment_store_test_template.h

Issue 652723003: Implementation of OnDiskAttachmentStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More updates Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #ifndef SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_
6 #define SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_
7
5 #include "sync/api/attachments/attachment_store.h" 8 #include "sync/api/attachments/attachment_store.h"
6 9
7 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
9 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
11 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
12 #include "sync/api/attachments/attachment.h" 16 #include "sync/api/attachments/attachment.h"
13 #include "sync/protocol/sync.pb.h" 17 #include "sync/protocol/sync.pb.h"
14 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
15 19
20 // AttachmentStoreTest defines tests for AttachmentStore. To instantiate these
21 // tests for a particular implementation you need to:
22 // - Include this file in test.
23 // - Create factory class for attachment store that implements factory method.
24 // - add INSTANTIATE_TYPED_TEST_CASE_P statement.
25 // Here is how to do it for MyAttachmentStore:
26 //
27 // class MyAttachmentStoreFactory {
28 // public:
29 // scoped_refptr<AttachmentStore> CreateAttachmentStore() {
30 // return new MyAttachmentStore();
31 // }
32 // };
33 //
34 // INSTANTIATE_TYPED_TEST_CASE_P(My,
35 // AttachmentStoreTest,
36 // MyAttachmentStoreFactory);
37
16 namespace syncer { 38 namespace syncer {
17 39
18 const char kTestData1[] = "test data 1"; 40 const char kTestData1[] = "test data 1";
19 const char kTestData2[] = "test data 2"; 41 const char kTestData2[] = "test data 2";
20 42
21 class InMemoryAttachmentStoreTest : public testing::Test { 43 template <typename AttachmentStoreFactory>
44 class AttachmentStoreTest : public testing::Test {
22 protected: 45 protected:
46 AttachmentStoreFactory attachment_store_factory;
23 base::MessageLoop message_loop; 47 base::MessageLoop message_loop;
24 scoped_refptr<AttachmentStore> store; 48 scoped_refptr<AttachmentStore> store;
25 AttachmentStore::Result result; 49 AttachmentStore::Result result;
26 scoped_ptr<AttachmentMap> attachments; 50 scoped_ptr<AttachmentMap> attachments;
27 scoped_ptr<AttachmentIdList> failed_attachment_ids; 51 scoped_ptr<AttachmentIdList> failed_attachment_ids;
28 52
29 AttachmentStore::ReadCallback read_callback; 53 AttachmentStore::ReadCallback read_callback;
30 AttachmentStore::WriteCallback write_callback; 54 AttachmentStore::WriteCallback write_callback;
31 AttachmentStore::DropCallback drop_callback; 55 AttachmentStore::DropCallback drop_callback;
32 56
33 scoped_refptr<base::RefCountedString> some_data1; 57 scoped_refptr<base::RefCountedString> some_data1;
34 scoped_refptr<base::RefCountedString> some_data2; 58 scoped_refptr<base::RefCountedString> some_data2;
35 59
36 InMemoryAttachmentStoreTest() 60 AttachmentStoreTest() {}
37 : store(AttachmentStore::CreateInMemoryStore()) {}
38 61
39 virtual void SetUp() { 62 virtual void SetUp() {
63 store = attachment_store_factory.CreateAttachmentStore();
64
40 Clear(); 65 Clear();
41 read_callback = 66 read_callback = base::Bind(&AttachmentStoreTest::CopyResultAttachments,
42 base::Bind(&InMemoryAttachmentStoreTest::CopyResultAttachments, 67 base::Unretained(this),
43 base::Unretained(this), 68 &result,
44 &result, 69 &attachments,
45 &attachments, 70 &failed_attachment_ids);
46 &failed_attachment_ids); 71 write_callback = base::Bind(
47 write_callback = base::Bind(&InMemoryAttachmentStoreTest::CopyResult, 72 &AttachmentStoreTest::CopyResult, base::Unretained(this), &result);
48 base::Unretained(this),
49 &result);
50 drop_callback = write_callback; 73 drop_callback = write_callback;
51 74
52 some_data1 = new base::RefCountedString; 75 some_data1 = new base::RefCountedString;
53 some_data1->data() = kTestData1; 76 some_data1->data() = kTestData1;
54 77
55 some_data2 = new base::RefCountedString; 78 some_data2 = new base::RefCountedString;
56 some_data2->data() = kTestData2; 79 some_data2->data() = kTestData2;
57 } 80 }
58 81
59 virtual void ClearAndPumpLoop() { 82 virtual void ClearAndPumpLoop() {
(...skipping 19 matching lines...) Expand all
79 scoped_ptr<AttachmentIdList>* destination_failed_attachment_ids, 102 scoped_ptr<AttachmentIdList>* destination_failed_attachment_ids,
80 const AttachmentStore::Result& source_result, 103 const AttachmentStore::Result& source_result,
81 scoped_ptr<AttachmentMap> source_attachments, 104 scoped_ptr<AttachmentMap> source_attachments,
82 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { 105 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) {
83 CopyResult(destination_result, source_result); 106 CopyResult(destination_result, source_result);
84 *destination_attachments = source_attachments.Pass(); 107 *destination_attachments = source_attachments.Pass();
85 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); 108 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass();
86 } 109 }
87 }; 110 };
88 111
112 TYPED_TEST_CASE_P(AttachmentStoreTest);
113
89 // Verify that we do not overwrite existing attachments and that we do not treat 114 // Verify that we do not overwrite existing attachments and that we do not treat
90 // it as an error. 115 // it as an error.
91 TEST_F(InMemoryAttachmentStoreTest, Write_NoOverwriteNoError) { 116 TYPED_TEST_P(AttachmentStoreTest, Write_NoOverwriteNoError) {
92 // Create two attachments with the same id but different data. 117 // Create two attachments with the same id but different data.
93 Attachment attachment1 = Attachment::Create(some_data1); 118 Attachment attachment1 = Attachment::Create(this->some_data1);
94 Attachment attachment2 = 119 Attachment attachment2 =
95 Attachment::CreateWithId(attachment1.GetId(), some_data2); 120 Attachment::CreateWithId(attachment1.GetId(), this->some_data2);
96 121
97 // Write the first one. 122 // Write the first one.
98 AttachmentList some_attachments; 123 AttachmentList some_attachments;
99 some_attachments.push_back(attachment1); 124 some_attachments.push_back(attachment1);
100 store->Write(some_attachments, write_callback); 125 this->store->Write(some_attachments, this->write_callback);
101 ClearAndPumpLoop(); 126 this->ClearAndPumpLoop();
102 EXPECT_EQ(result, AttachmentStore::SUCCESS); 127 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
103 128
104 // Write the second one. 129 // Write the second one.
105 some_attachments.clear(); 130 some_attachments.clear();
106 some_attachments.push_back(attachment2); 131 some_attachments.push_back(attachment2);
107 store->Write(some_attachments, write_callback); 132 this->store->Write(some_attachments, this->write_callback);
108 ClearAndPumpLoop(); 133 this->ClearAndPumpLoop();
109 EXPECT_EQ(result, AttachmentStore::SUCCESS); 134 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
110 135
111 // Read it back and see that it was not overwritten. 136 // Read it back and see that it was not overwritten.
112 AttachmentIdList some_attachment_ids; 137 AttachmentIdList some_attachment_ids;
113 some_attachment_ids.push_back(attachment1.GetId()); 138 some_attachment_ids.push_back(attachment1.GetId());
114 store->Read(some_attachment_ids, read_callback); 139 this->store->Read(some_attachment_ids, this->read_callback);
115 ClearAndPumpLoop(); 140 this->ClearAndPumpLoop();
116 EXPECT_EQ(result, AttachmentStore::SUCCESS); 141 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
117 EXPECT_EQ(attachments->size(), 1U); 142 EXPECT_EQ(this->attachments->size(), 1U);
118 EXPECT_EQ(failed_attachment_ids->size(), 0U); 143 EXPECT_EQ(this->failed_attachment_ids->size(), 0U);
119 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); 144 AttachmentMap::const_iterator a1 =
120 EXPECT_TRUE(a1 != attachments->end()); 145 this->attachments->find(attachment1.GetId());
146 EXPECT_TRUE(a1 != this->attachments->end());
121 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); 147 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData()));
122 } 148 }
123 149
124 // Verify that we can write some attachments and read them back. 150 // Verify that we can write some attachments and read them back.
125 TEST_F(InMemoryAttachmentStoreTest, Write_RoundTrip) { 151 TYPED_TEST_P(AttachmentStoreTest, Write_RoundTrip) {
126 Attachment attachment1 = Attachment::Create(some_data1); 152 Attachment attachment1 = Attachment::Create(this->some_data1);
127 Attachment attachment2 = Attachment::Create(some_data2); 153 Attachment attachment2 = Attachment::Create(this->some_data2);
128 AttachmentList some_attachments; 154 AttachmentList some_attachments;
129 some_attachments.push_back(attachment1); 155 some_attachments.push_back(attachment1);
130 some_attachments.push_back(attachment2); 156 some_attachments.push_back(attachment2);
131 157
132 store->Write(some_attachments, write_callback); 158 this->store->Write(some_attachments, this->write_callback);
133 ClearAndPumpLoop(); 159 this->ClearAndPumpLoop();
134 EXPECT_EQ(result, AttachmentStore::SUCCESS); 160 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
135 161
136 AttachmentIdList some_attachment_ids; 162 AttachmentIdList some_attachment_ids;
137 some_attachment_ids.push_back(attachment1.GetId()); 163 some_attachment_ids.push_back(attachment1.GetId());
138 some_attachment_ids.push_back(attachment2.GetId()); 164 some_attachment_ids.push_back(attachment2.GetId());
139 store->Read(some_attachment_ids, read_callback); 165 this->store->Read(some_attachment_ids, this->read_callback);
140 ClearAndPumpLoop(); 166 this->ClearAndPumpLoop();
141 EXPECT_EQ(result, AttachmentStore::SUCCESS); 167 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
142 EXPECT_EQ(attachments->size(), 2U); 168 EXPECT_EQ(this->attachments->size(), 2U);
143 EXPECT_EQ(failed_attachment_ids->size(), 0U); 169 EXPECT_EQ(this->failed_attachment_ids->size(), 0U);
144 170
145 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); 171 AttachmentMap::const_iterator a1 =
146 EXPECT_TRUE(a1 != attachments->end()); 172 this->attachments->find(attachment1.GetId());
173 EXPECT_TRUE(a1 != this->attachments->end());
147 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); 174 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData()));
148 175
149 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); 176 AttachmentMap::const_iterator a2 =
150 EXPECT_TRUE(a2 != attachments->end()); 177 this->attachments->find(attachment2.GetId());
178 EXPECT_TRUE(a2 != this->attachments->end());
151 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); 179 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData()));
152 } 180 }
153 181
154 // Try to read two attachments when only one exists. 182 // Try to read two attachments when only one exists.
155 TEST_F(InMemoryAttachmentStoreTest, Read_OneNotFound) { 183 TYPED_TEST_P(AttachmentStoreTest, Read_OneNotFound) {
156 Attachment attachment1 = Attachment::Create(some_data1); 184 Attachment attachment1 = Attachment::Create(this->some_data1);
157 Attachment attachment2 = Attachment::Create(some_data2); 185 Attachment attachment2 = Attachment::Create(this->some_data2);
158 186
159 AttachmentList some_attachments; 187 AttachmentList some_attachments;
160 // Write attachment1 only. 188 // Write attachment1 only.
161 some_attachments.push_back(attachment1); 189 some_attachments.push_back(attachment1);
162 store->Write(some_attachments, write_callback); 190 this->store->Write(some_attachments, this->write_callback);
163 ClearAndPumpLoop(); 191 this->ClearAndPumpLoop();
164 EXPECT_EQ(result, AttachmentStore::SUCCESS); 192 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
165 193
166 // Try to read both attachment1 and attachment2. 194 // Try to read both attachment1 and attachment2.
167 AttachmentIdList ids; 195 AttachmentIdList ids;
168 ids.push_back(attachment1.GetId()); 196 ids.push_back(attachment1.GetId());
169 ids.push_back(attachment2.GetId()); 197 ids.push_back(attachment2.GetId());
170 store->Read(ids, read_callback); 198 this->store->Read(ids, this->read_callback);
171 ClearAndPumpLoop(); 199 this->ClearAndPumpLoop();
172 200
173 // See that only attachment1 was read. 201 // See that only attachment1 was read.
174 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); 202 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR);
175 EXPECT_EQ(attachments->size(), 1U); 203 EXPECT_EQ(this->attachments->size(), 1U);
176 EXPECT_EQ(failed_attachment_ids->size(), 1U); 204 EXPECT_EQ(this->failed_attachment_ids->size(), 1U);
177 } 205 }
178 206
179 // Try to drop two attachments when only one exists. Verify that no error occurs 207 // Try to drop two attachments when only one exists. Verify that no error occurs
180 // and that the existing attachment was dropped. 208 // and that the existing attachment was dropped.
181 TEST_F(InMemoryAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { 209 TYPED_TEST_P(AttachmentStoreTest, Drop_DropTwoButOnlyOneExists) {
182 // First, create two attachments. 210 // First, create two attachments.
183 Attachment attachment1 = Attachment::Create(some_data1); 211 Attachment attachment1 = Attachment::Create(this->some_data1);
184 Attachment attachment2 = Attachment::Create(some_data2); 212 Attachment attachment2 = Attachment::Create(this->some_data2);
185 AttachmentList some_attachments; 213 AttachmentList some_attachments;
186 some_attachments.push_back(attachment1); 214 some_attachments.push_back(attachment1);
187 some_attachments.push_back(attachment2); 215 some_attachments.push_back(attachment2);
188 store->Write(some_attachments, write_callback); 216 this->store->Write(some_attachments, this->write_callback);
189 ClearAndPumpLoop(); 217 this->ClearAndPumpLoop();
190 EXPECT_EQ(result, AttachmentStore::SUCCESS); 218 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
191 219
192 // Drop attachment1 only. 220 // Drop attachment1 only.
193 AttachmentIdList ids; 221 AttachmentIdList ids;
194 ids.push_back(attachment1.GetId()); 222 ids.push_back(attachment1.GetId());
195 store->Drop(ids, drop_callback); 223 this->store->Drop(ids, this->drop_callback);
196 ClearAndPumpLoop(); 224 this->ClearAndPumpLoop();
197 EXPECT_EQ(result, AttachmentStore::SUCCESS); 225 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
198 226
199 // See that attachment1 is gone. 227 // See that attachment1 is gone.
200 store->Read(ids, read_callback); 228 this->store->Read(ids, this->read_callback);
201 ClearAndPumpLoop(); 229 this->ClearAndPumpLoop();
202 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); 230 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR);
203 EXPECT_EQ(attachments->size(), 0U); 231 EXPECT_EQ(this->attachments->size(), 0U);
204 EXPECT_EQ(failed_attachment_ids->size(), 1U); 232 EXPECT_EQ(this->failed_attachment_ids->size(), 1U);
205 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); 233 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment1.GetId());
206 234
207 // Drop both attachment1 and attachment2. 235 // Drop both attachment1 and attachment2.
208 ids.clear(); 236 ids.clear();
209 ids.push_back(attachment1.GetId()); 237 ids.push_back(attachment1.GetId());
210 ids.push_back(attachment2.GetId()); 238 ids.push_back(attachment2.GetId());
211 store->Drop(ids, drop_callback); 239 this->store->Drop(ids, this->drop_callback);
212 ClearAndPumpLoop(); 240 this->ClearAndPumpLoop();
213 EXPECT_EQ(result, AttachmentStore::SUCCESS); 241 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
214 242
215 // See that attachment2 is now gone. 243 // See that attachment2 is now gone.
216 ids.clear(); 244 ids.clear();
217 ids.push_back(attachment2.GetId()); 245 ids.push_back(attachment2.GetId());
218 store->Read(ids, read_callback); 246 this->store->Read(ids, this->read_callback);
219 ClearAndPumpLoop(); 247 this->ClearAndPumpLoop();
220 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); 248 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR);
221 EXPECT_EQ(attachments->size(), 0U); 249 EXPECT_EQ(this->attachments->size(), 0U);
222 EXPECT_EQ(failed_attachment_ids->size(), 1U); 250 EXPECT_EQ(this->failed_attachment_ids->size(), 1U);
223 EXPECT_EQ((*failed_attachment_ids)[0], attachment2.GetId()); 251 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment2.GetId());
224 } 252 }
225 253
226 // Verify that attempting to drop an attachment that does not exist is not an 254 // Verify that attempting to drop an attachment that does not exist is not an
227 // error. 255 // error.
228 TEST_F(InMemoryAttachmentStoreTest, Drop_DoesNotExist) { 256 TYPED_TEST_P(AttachmentStoreTest, Drop_DoesNotExist) {
229 Attachment attachment1 = Attachment::Create(some_data1); 257 Attachment attachment1 = Attachment::Create(this->some_data1);
230 AttachmentList some_attachments; 258 AttachmentList some_attachments;
231 some_attachments.push_back(attachment1); 259 some_attachments.push_back(attachment1);
232 store->Write(some_attachments, write_callback); 260 this->store->Write(some_attachments, this->write_callback);
233 ClearAndPumpLoop(); 261 this->ClearAndPumpLoop();
234 EXPECT_EQ(result, AttachmentStore::SUCCESS); 262 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
235 263
236 // Drop the attachment. 264 // Drop the attachment.
237 AttachmentIdList ids; 265 AttachmentIdList ids;
238 ids.push_back(attachment1.GetId()); 266 ids.push_back(attachment1.GetId());
239 store->Drop(ids, drop_callback); 267 this->store->Drop(ids, this->drop_callback);
240 ClearAndPumpLoop(); 268 this->ClearAndPumpLoop();
241 EXPECT_EQ(result, AttachmentStore::SUCCESS); 269 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
242 270
243 // See that it's gone. 271 // See that it's gone.
244 store->Read(ids, read_callback); 272 this->store->Read(ids, this->read_callback);
245 ClearAndPumpLoop(); 273 this->ClearAndPumpLoop();
246 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); 274 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR);
247 EXPECT_EQ(attachments->size(), 0U); 275 EXPECT_EQ(this->attachments->size(), 0U);
248 EXPECT_EQ(failed_attachment_ids->size(), 1U); 276 EXPECT_EQ(this->failed_attachment_ids->size(), 1U);
249 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); 277 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment1.GetId());
250 278
251 // Drop again, see that no error occurs. 279 // Drop again, see that no error occurs.
252 ids.clear(); 280 ids.clear();
253 ids.push_back(attachment1.GetId()); 281 ids.push_back(attachment1.GetId());
254 store->Drop(ids, drop_callback); 282 this->store->Drop(ids, this->drop_callback);
255 ClearAndPumpLoop(); 283 this->ClearAndPumpLoop();
256 EXPECT_EQ(result, AttachmentStore::SUCCESS); 284 EXPECT_EQ(this->result, AttachmentStore::SUCCESS);
257 } 285 }
258 286
287 REGISTER_TYPED_TEST_CASE_P(AttachmentStoreTest,
288 Write_NoOverwriteNoError,
289 Write_RoundTrip,
290 Read_OneNotFound,
291 Drop_DropTwoButOnlyOneExists,
292 Drop_DoesNotExist);
293
259 } // namespace syncer 294 } // namespace syncer
295
296 #endif // SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698