1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef OMIT_MULTI_VER
16 #include <gtest/gtest.h>
17 #include <ctime>
18 #include <cmath>
19 #include <random>
20 #include <chrono>
21 #include <thread>
22 #include <cstdio>
23 #include <string>
24
25 #include "distributeddb_data_generator.h"
26 #include "distributed_test_tools.h"
27 #include "kv_store_delegate.h"
28 #include "kv_store_delegate_manager.h"
29
30 using namespace std;
31 using namespace chrono;
32 using namespace testing;
33 #if defined TESTCASES_USING_GTEST_EXT
34 using namespace testing::ext;
35 #endif
36 using namespace DistributedDB;
37 using namespace DistributedDBDataGenerator;
38
39 namespace DistributedDBKvObserverSnap {
40 const bool IS_LOCAL = false;
41 const int CHANGED_ZERO_TIME = 0;
42 const int CHANGED_ONE_TIME = 1;
43 const int OPER_CNT_START = 0;
44 const int OPER_CNT_END = 5;
45 const int TEN_RECORDS = 10;
46 const int DELETE_CNT_START = 0;
47 const int DELETE_CNT_END = 2;
48
49 DistributedDB::CipherPassword g_passwd1;
50 DistributedDB::CipherPassword g_passwd2;
51 DistributedDB::CipherPassword g_filePasswd1;
52 DistributedDB::CipherPassword g_filePasswd2;
53
54 class DistributeddbKvObserverSnapTest : public testing::Test {
55 public:
56 static void SetUpTestCase(void);
57 static void TearDownTestCase(void);
58 void SetUp();
59 void TearDown();
60 private:
61 };
62
63 KvStoreDelegate *g_observerSnapDelegate = nullptr; // the delegate used in this suit.
64 KvStoreDelegateManager *g_observerSnapManager = nullptr;
SetUpTestCase(void)65 void DistributeddbKvObserverSnapTest::SetUpTestCase(void)
66 {
67 MST_LOG("SetUpTestCase before all cases local[%d].", IS_LOCAL);
68 RemoveDir(DIRECTOR);
69 g_observerSnapDelegate = DistributedTestTools::GetDelegateSuccess(g_observerSnapManager,
70 g_kvdbParameter1, g_kvOption);
71 ASSERT_TRUE(g_observerSnapManager != nullptr && g_observerSnapDelegate != nullptr);
72 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
73 (void)g_passwd2.SetValue(PASSWD_VECTOR_2.data(), PASSWD_VECTOR_2.size());
74 (void)g_filePasswd1.SetValue(FILE_PASSWD_VECTOR_1.data(), FILE_PASSWD_VECTOR_1.size());
75 (void)g_filePasswd2.SetValue(FILE_PASSWD_VECTOR_2.data(), FILE_PASSWD_VECTOR_2.size());
76 }
77
TearDownTestCase(void)78 void DistributeddbKvObserverSnapTest::TearDownTestCase(void)
79 {
80 EXPECT_EQ(g_observerSnapManager->CloseKvStore(g_observerSnapDelegate), OK);
81 g_observerSnapDelegate = nullptr;
82 DBStatus status = g_observerSnapManager->DeleteKvStore(STORE_ID_1);
83 EXPECT_EQ(status, DistributedDB::DBStatus::OK) << "fail to delete exist kvdb";
84 delete g_observerSnapManager;
85 g_observerSnapManager = nullptr;
86 RemoveDir(DIRECTOR);
87 }
88
SetUp(void)89 void DistributeddbKvObserverSnapTest::SetUp(void)
90 {
91 ASSERT_TRUE(g_observerSnapDelegate != nullptr);
92 UnitTest *test = UnitTest::GetInstance();
93 ASSERT_NE(test, nullptr);
94 const TestInfo *testinfo = test->current_test_info();
95 ASSERT_NE(testinfo, nullptr);
96 string testCaseName = string(testinfo->name());
97 MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
98 }
99
TearDown(void)100 void DistributeddbKvObserverSnapTest::TearDown(void)
101 {
102 }
103
104 /*
105 * @tc.name: Register 001
106 * @tc.desc: Verify that can register a fresh observer base on kv db snap.
107 * @tc.type: FUNC
108 * @tc.require: SR000BUH3K,SR000BVRNE
109 * @tc.author: luqianfu
110 */
111 HWTEST_F(DistributeddbKvObserverSnapTest, Register001, TestSize.Level1)
112 {
113 DistributedTestTools::Clear(*g_observerSnapDelegate);
114
115 KvStoreObserverImpl observer;
116
117 /**
118 * @tc.steps: step1. RegisterSnapObserver base on kv db snap.
119 * @tc.expected: step1. Register successfully.
120 */
121 KvStoreSnapshotDelegate *snap = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
122 EXPECT_NE(snap, nullptr);
123
124 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snap);
125 snap = nullptr;
126 EXPECT_EQ(statusRelease, DBStatus::OK);
127 }
128
129 /*
130 * @tc.name: Register 002
131 * @tc.desc: Verify that can register a null observer on kv db snap.
132 * @tc.type: FUNC
133 * @tc.require: SR000BUH3K
134 * @tc.author: luqianfu
135 */
136 HWTEST_F(DistributeddbKvObserverSnapTest, Register002, TestSize.Level1)
137 {
138 DistributedTestTools::Clear(*g_observerSnapDelegate);
139
140 /**
141 * @tc.steps: step1. Register a null observer base on kv db snap.
142 * @tc.expected: step1. Register successfully.
143 */
144 KvStoreSnapshotDelegate *snap = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, nullptr);
145 EXPECT_NE(snap, nullptr);
146
147 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snap);
148 snap = nullptr;
149 EXPECT_EQ(statusRelease, DBStatus::OK);
150 }
151
152 /*
153 * @tc.name: Register 003
154 * @tc.desc: Verify that can register only once with the same observer.
155 * @tc.type: FUNC
156 * @tc.require: SR000BUH3K,SR000BVRNE
157 * @tc.author: luqianfu
158 */
159 HWTEST_F(DistributeddbKvObserverSnapTest, Register003, TestSize.Level1)
160 {
161 DistributedTestTools::Clear(*g_observerSnapDelegate);
162
163 KvStoreObserverImpl observer;
164
165 /**
166 * @tc.steps: step1. Register observer 6 times base on kv db snap.
167 * @tc.expected: step1. First register is successful and 5 times later return null.
168 */
169 KvStoreSnapshotDelegate *snapShot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
170 EXPECT_NE(snapShot, nullptr);
171 KvStoreSnapshotDelegate *snap = nullptr;
172 for (int time = OPER_CNT_START; time < static_cast<int>(OPER_CNT_END); ++time) {
173 snap = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
174 EXPECT_EQ(snap, nullptr);
175 }
176
177 /**
178 * @tc.steps: step2. Register Put (k1,v1) to db.
179 * @tc.expected: step2. First put successfully and the observer return inserting data once.
180 */
181 DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_1);
182 EXPECT_TRUE(status == DBStatus::OK);
183 vector<DistributedDB::Entry> insertList;
184 insertList.push_back(ENTRY_1);
185 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, insertList));
186 observer.Clear();
187
188 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShot);
189 snapShot = nullptr;
190 EXPECT_EQ(statusRelease, DBStatus::OK);
191 }
192
193 /*
194 * @tc.name: Register 004
195 * @tc.desc: Verify that can register different observers.
196 * @tc.type: FUNC
197 * @tc.require: SR000BUH3K,SR000BVRNE
198 * @tc.author: luqianfu
199 */
200 HWTEST_F(DistributeddbKvObserverSnapTest, Register004, TestSize.Level1)
201 {
202 DistributedTestTools::Clear(*g_observerSnapDelegate);
203
204 KvStoreObserverImpl observer1, observer2;
205
206 /**
207 * @tc.steps: step1. Register observer1 bases on kv db snap.
208 * @tc.expected: step1. Register successfully.
209 */
210 KvStoreSnapshotDelegate *snapShot1 = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer1);
211 EXPECT_NE(snapShot1, nullptr);
212
213 /**
214 * @tc.steps: step2. put (k1,v1) to db.
215 * @tc.expected: step2. put successfully and the observer1 return 1 inserting data info.
216 */
217 DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_1);
218 EXPECT_TRUE(status == DBStatus::OK);
219 vector<DistributedDB::Entry> insertEntries1;
220 insertEntries1.push_back(ENTRY_1);
221 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, INSERT_LIST, insertEntries1));
222 observer1.Clear();
223
224 /**
225 * @tc.steps: step3. register a null observer bases on kv db snap.
226 * @tc.expected: step3. register successfully.
227 */
228 KvStoreSnapshotDelegate *snapShotNull = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, nullptr);
229 EXPECT_NE(snapShotNull, nullptr);
230
231 /**
232 * @tc.steps: step4. put (k2,v2) to db.
233 * @tc.expected: step4. register successfully.
234 */
235 status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_2, VALUE_2);
236 EXPECT_TRUE(status == DBStatus::OK);
237 insertEntries1.clear();
238 insertEntries1.push_back(ENTRY_2);
239 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, INSERT_LIST, insertEntries1));
240 observer1.Clear();
241
242 /**
243 * @tc.steps: step5. register observer2 bases on kv db snap.
244 * @tc.expected: step5. register successfully.
245 */
246 KvStoreSnapshotDelegate *snapShot2 = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer2);
247 EXPECT_NE(snapShot2, nullptr);
248
249 /**
250 * @tc.steps: step6. put (k3,v3) to db.
251 * @tc.expected: step6. put successfully and the observer1,2 return 1 inserting data info individually.
252 */
253 status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_3, VALUE_3);
254 EXPECT_TRUE(status == DBStatus::OK);
255 insertEntries1.clear();
256 insertEntries1.push_back(ENTRY_3);
257 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, INSERT_LIST, insertEntries1));
258 observer1.Clear();
259 EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ONE_TIME, INSERT_LIST, insertEntries1));
260 observer2.Clear();
261
262 EXPECT_EQ((g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShot1)), DBStatus::OK);
263 snapShot1 = nullptr;
264 EXPECT_EQ((g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShotNull)), DBStatus::OK);
265 snapShotNull = nullptr;
266 EXPECT_EQ((g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShot2)), DBStatus::OK);
267 snapShot2 = nullptr;
268 }
269
270 /*
271 * @tc.name: Register 005
272 * @tc.desc: Verify that unregister observer (release snapshot) won't return capture data info.
273 * @tc.type: FUNC
274 * @tc.require: SR000BUH3K,SR000BVRNE
275 * @tc.author: luqianfu
276 */
277 HWTEST_F(DistributeddbKvObserverSnapTest, Register005, TestSize.Level1)
278 {
279 DistributedTestTools::Clear(*g_observerSnapDelegate);
280
281 KvStoreObserverImpl observer;
282
283 /**
284 * @tc.steps: step1. Register observer1 bases on kv db snap.
285 * @tc.expected: step1. Register successfully to construct observer exist.
286 */
287 KvStoreSnapshotDelegate *snapShot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
288 EXPECT_NE(snapShot, nullptr);
289
290 /**
291 * @tc.steps: step2. put (k1,v1) to db.
292 * @tc.expected: step2. put successfully and the observer1 return 1 inserting data info.
293 */
294 DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_1);
295 EXPECT_TRUE(status == DBStatus::OK);
296 vector<DistributedDB::Entry> insertEntries;
297 insertEntries.push_back(ENTRY_1);
298 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, insertEntries));
299 observer.Clear();
300
301 /**
302 * @tc.steps: step3. unregister observer1 (release snapshot).
303 * @tc.expected: step3. operate successfully.
304 */
305 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShot);
306 snapShot = nullptr;
307 EXPECT_TRUE(statusRelease == DBStatus::OK);
308
309 /**
310 * @tc.steps: step4. put (k2,v2) to db.
311 * @tc.expected: step4. put successfully and has nothing changed data info returned.
312 */
313 status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_2, VALUE_2);
314 EXPECT_TRUE(status == DBStatus::OK);
315 insertEntries.clear();
316 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
317 observer.Clear();
318 }
319
320 /*
321 * @tc.name: Register 006
322 * @tc.desc: Verify that register observer, and then unregister it many times will success only firstly.
323 * @tc.type: FUNC
324 * @tc.require: SR000BUH3K,SR000BVRNE
325 * @tc.author: luqianfu
326 */
327 HWTEST_F(DistributeddbKvObserverSnapTest, Register006, TestSize.Level1)
328 {
329 DistributedTestTools::Clear(*g_observerSnapDelegate);
330
331 KvStoreObserverImpl observer;
332
333 /**
334 * @tc.steps: step1. Register observer1 bases on kv db snap.
335 * @tc.expected: step1. Register successfully to construct observer exist.
336 */
337 KvStoreSnapshotDelegate *snapShot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
338 EXPECT_NE(snapShot, nullptr);
339
340 /**
341 * @tc.steps: step2. unregister observer1 for 5 times.
342 * @tc.expected: step2. Firstly register successfully and another failed.
343 */
344 DBStatus statusRelease;
345 for (int time = OPER_CNT_START; time < static_cast<int>(OPER_CNT_END); ++time) {
346 statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShot);
347 snapShot = nullptr;
348 if (time == OPER_CNT_START) {
349 EXPECT_TRUE(statusRelease == DBStatus::OK);
350 }
351 else {
352 EXPECT_TRUE(statusRelease != DBStatus::OK);
353 }
354 }
355
356 /**
357 * @tc.steps: step3. put (k1,v1) to db.
358 * @tc.expected: step3. put successfully and has nothing changed data info returned.
359 */
360 DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_1);
361 EXPECT_TRUE(status == DBStatus::OK);
362 vector<DistributedDB::Entry> insertEntries;
363 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
364 observer.Clear();
365 }
366
367 /*
368 * @tc.name: Register 007
369 * @tc.desc: Verify that the DB was not register a snapshot can't release it.
370 * @tc.type: FUNC
371 * @tc.require: SR000BUH3K,SR000BVRNE
372 * @tc.author: luqianfu
373 */
374 HWTEST_F(DistributeddbKvObserverSnapTest, Register007, TestSize.Level1)
375 {
376 DistributedTestTools::Clear(*g_observerSnapDelegate);
377
378 KvStoreDelegate *delegate = nullptr;
379 KvStoreDelegateManager *manager = nullptr;
380 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter2_1_1, g_kvOption);
381 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
382 EXPECT_TRUE(delegate != nullptr) << "fail to create exist kvdb";
383
384 /**
385 * @tc.steps: step1. Register observer1 bases on kv db1 snap and unregister it bases on kv db2.
386 * @tc.expected: step1. Register successfully and unregister failed.
387 */
388 KvStoreObserverImpl observer;
389 KvStoreSnapshotDelegate *snapShot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
390 EXPECT_NE(snapShot, nullptr);
391 DBStatus statusRelease = delegate->ReleaseKvStoreSnapshot(snapShot);
392 EXPECT_TRUE(statusRelease != DBStatus::OK);
393
394 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
395 delegate = nullptr;
396 DBStatus status = manager->DeleteKvStore(STORE_ID_2);
397 EXPECT_TRUE(status == DBStatus::OK) << "fail to delete exist kvdb";
398 delete manager;
399 manager = nullptr;
400 statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShot);
401 snapShot = nullptr;
402 EXPECT_TRUE(statusRelease == DBStatus::OK);
403 }
404
405 /*
406 * @tc.name: Register 008
407 * @tc.desc: Verify that observer will not be triggered after released.
408 * @tc.type: FUNC
409 * @tc.require: SR000BUH3K,SR000BVRNE
410 * @tc.author: luqianfu
411 */
412 HWTEST_F(DistributeddbKvObserverSnapTest, Register008, TestSize.Level1)
413 {
414 DistributedTestTools::Clear(*g_observerSnapDelegate);
415
416 KvStoreDelegate *delegate1 = nullptr;
417 KvStoreDelegate *delegate2 = nullptr;
418 KvStoreDelegateManager *manager1 = nullptr;
419 KvStoreDelegateManager *manager2 = nullptr;
420 KvOption option = g_kvOption;
421 delegate1 = DistributedTestTools::GetDelegateSuccess(manager1, g_kvdbParameter2, option);
422 ASSERT_TRUE(manager1 != nullptr && delegate1 != nullptr);
423 option.createIfNecessary = IS_NOT_NEED_CREATE;
424 delegate2 = DistributedTestTools::GetDelegateSuccess(manager2, g_kvdbParameter2, option);
425 ASSERT_TRUE(manager2 != nullptr && delegate2 != nullptr);
426
427 /**
428 * @tc.steps: step1. Register observer1 bases on kv db2 snap and close db then put(k1,v1).
429 * @tc.expected: step1. Register successfully and close failed, observer1 return a changed data info.
430 */
431 KvStoreObserverImpl observer1;
432 KvStoreSnapshotDelegate *snapShot = DistributedTestTools::RegisterSnapObserver(delegate1, &observer1);
433 EXPECT_NE(snapShot, nullptr);
434 EXPECT_TRUE(manager1->CloseKvStore(delegate1) != OK);
435 DBStatus status = DistributedTestTools::Put(*delegate2, KEY_1, VALUE_1);
436 EXPECT_TRUE(status == DBStatus::OK);
437 vector<DistributedDB::Entry> insertList;
438 insertList.push_back(ENTRY_1);
439 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, INSERT_LIST, insertList));
440 observer1.Clear();
441
442 /**
443 * @tc.steps: step2. unregister observer1 and close db then put.
444 * @tc.expected: step2. Unregister and close successfully, observer1 return nothing.
445 */
446 EXPECT_TRUE(delegate1->ReleaseKvStoreSnapshot(snapShot) == DBStatus::OK);
447 snapShot = nullptr;
448 EXPECT_TRUE(manager1->CloseKvStore(delegate1) == OK);
449 delegate1 = nullptr;
450 status = DistributedTestTools::Put(*delegate2, KEY_1, VALUE_1);
451 EXPECT_TRUE(status == DBStatus::OK);
452 insertList.clear();
453 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, INSERT_LIST, insertList));
454 observer1.Clear();
455
456 EXPECT_TRUE(manager2->CloseKvStore(delegate2) == OK);
457 delegate2 = nullptr;
458 status = manager2->DeleteKvStore(STORE_ID_2);
459 EXPECT_TRUE(status == DBStatus::OK) << "fail to delete exist kvdb";
460 delete manager1;
461 manager1 = nullptr;
462 delete manager2;
463 manager2 = nullptr;
464 }
465
466 /*
467 * @tc.name: Register 009
468 * @tc.desc: Delete kv db, and verify observer will not be activated.
469 * @tc.type: FUNC
470 * @tc.require: SR000BUH3K,SR000BVRNE
471 * @tc.author: luqianfu
472 */
473 HWTEST_F(DistributeddbKvObserverSnapTest, Register009, TestSize.Level1)
474 {
475 DistributedTestTools::Clear(*g_observerSnapDelegate);
476
477 KvStoreDelegate *delegate1 = nullptr;
478 KvStoreDelegateManager *manager1 = nullptr;
479 delegate1 = DistributedTestTools::GetDelegateSuccess(manager1, g_kvdbParameter2, g_kvOption);
480 ASSERT_TRUE(manager1 != nullptr && delegate1 != nullptr);
481
482 /**
483 * @tc.steps: step1. Register observer1 bases on kv db2 snap and close db then put(k1,v1).
484 * @tc.expected: step1. Register successfully and close failed, observer1 return a changed data info.
485 */
486 KvStoreObserverImpl observer1;
487 KvStoreSnapshotDelegate *snapShot = DistributedTestTools::RegisterSnapObserver(delegate1, &observer1);
488 EXPECT_NE(snapShot, nullptr);
489 EXPECT_TRUE(manager1->CloseKvStore(delegate1) != OK);
490
491 DBStatus status = DistributedTestTools::Put(*delegate1, KEY_1, VALUE_1);
492 EXPECT_TRUE(status == DBStatus::OK);
493 vector<DistributedDB::Entry> insertEntries;
494 DistributedDB::Entry entry = { KEY_1, VALUE_1 };
495 insertEntries.push_back(entry);
496 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, INSERT_LIST, insertEntries));
497 observer1.Clear();
498
499 /**
500 * @tc.steps: step2. Unregister observer1 and close db then delete db.
501 * @tc.expected: step2. operate successfully and observer1 return nothing.
502 */
503 EXPECT_TRUE(delegate1->ReleaseKvStoreSnapshot(snapShot) == DBStatus::OK);
504 snapShot = nullptr;
505 EXPECT_TRUE(manager1->CloseKvStore(delegate1) == OK);
506 delegate1 = nullptr;
507 EXPECT_TRUE(manager1->DeleteKvStore(STORE_ID_2) == OK);
508
509 vector<DistributedDB::Entry> deleteEntries;
510 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, DELETE_LIST, deleteEntries));
511 observer1.Clear();
512
513 EXPECT_TRUE(status == DBStatus::OK) << "fail to delete exist kvdb";
514 delete manager1;
515 manager1 = nullptr;
516 }
517
518 /*
519 * @tc.name: DataChange 001
520 * @tc.desc: verify that can observer for inserting a record.
521 * @tc.type: FUNC
522 * @tc.require: SR000BUH3K,SR000BVRNE
523 * @tc.author: luqianfu
524 */
525 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange001, TestSize.Level1)
526 {
527 DistributedTestTools::Clear(*g_observerSnapDelegate);
528
529 /**
530 * @tc.steps: step1. Register observer1 bases on kv db2 snap.
531 * @tc.expected: step1. Register successfully to construct observer exist.
532 */
533 KvStoreObserverImpl observer;
534 KvStoreSnapshotDelegate *snapShot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
535 EXPECT_TRUE(snapShot != nullptr);
536
537 /**
538 * @tc.steps: step2. put (k1,v1) to db.
539 * @tc.expected: step2. put successfully and observer1 return a insert data info.
540 */
541 DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_1);
542 EXPECT_TRUE(status == DBStatus::OK);
543 vector<DistributedDB::Entry> insertEntries;
544 insertEntries.push_back(ENTRY_1);
545 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, insertEntries));
546 observer.Clear();
547
548 status = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapShot);
549 snapShot = nullptr;
550 EXPECT_TRUE(status == DBStatus::OK);
551 }
552
553 /*
554 * @tc.name: DataChange 002
555 * @tc.desc: verify that can observer for updating a record.
556 * @tc.type: FUNC
557 * @tc.require: SR000BUH3K,SR000BVRNE
558 * @tc.author: luqianfu
559 */
560 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange002, TestSize.Level1)
561 {
562 DistributedTestTools::Clear(*g_observerSnapDelegate);
563 vector<Entry> entries1;
564 entries1.push_back(ENTRY_1);
565 entries1.push_back(ENTRY_2);
566
567 /**
568 * @tc.steps: step1. putBatch (k1,v1)(k2,v2) and register observer1 bases on kv db2 snap.
569 * @tc.expected: step1. operate successfully to construct data and observer exist.
570 */
571 DBStatus statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries1);
572 EXPECT_TRUE(statusPut == DBStatus::OK);
573 KvStoreObserverImpl observer;
574 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
575 EXPECT_TRUE(snapshot != nullptr);
576
577 /**
578 * @tc.steps: step2. update (k1,v1) to (k1,v2).
579 * @tc.expected: step2. update successfully and observer return a updating data info.
580 */
581 DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_2);
582 EXPECT_TRUE(status == DBStatus::OK);
583 vector<DistributedDB::Entry> updateEntries;
584 updateEntries.push_back(ENTRY_1_2);
585 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, updateEntries));
586 observer.Clear();
587
588 status = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
589 snapshot = nullptr;
590 EXPECT_TRUE(status == DBStatus::OK);
591 }
592
593 /*
594 * @tc.name: DataChange 003
595 * @tc.desc: verify that can observer for deleting a record.
596 * @tc.type: FUNC
597 * @tc.require: SR000BUH3K,SR000BVRNE
598 * @tc.author: luqianfu
599 */
600 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange003, TestSize.Level1)
601 {
602 DistributedTestTools::Clear(*g_observerSnapDelegate);
603 vector<Entry> entries1;
604 entries1.push_back(ENTRY_1);
605 entries1.push_back(ENTRY_2);
606
607 /**
608 * @tc.steps: step1. putBatch (k1,v1)(k2,v2) and register observer1 bases on kv db2 snap.
609 * @tc.expected: step1. operate successfully to construct data and observer exist.
610 */
611 DBStatus statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries1);
612 EXPECT_TRUE(statusPut == DBStatus::OK);
613 KvStoreObserverImpl observer;
614 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
615 EXPECT_TRUE(snapshot != nullptr);
616
617 /**
618 * @tc.steps: step2. delete from kb.
619 * @tc.expected: step2. delete successfully and observer return a deleting data info.
620 */
621 DBStatus status = DistributedTestTools::Delete(*g_observerSnapDelegate, KEY_1);
622 EXPECT_TRUE(status == DBStatus::OK);
623 vector<DistributedDB::Entry> deleteEntries;
624 deleteEntries.push_back(ENTRY_1);
625 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, deleteEntries));
626 observer.Clear();
627
628 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
629 snapshot = nullptr;
630 EXPECT_EQ(statusRelease, DBStatus::OK);
631 }
632
633 /*
634 * @tc.name: DataChange 004
635 * @tc.desc: verify that can observer for batch inserting.
636 * @tc.type: FUNC
637 * @tc.require: SR000BUH3K,SR000BVRNE
638 * @tc.author: luqianfu
639 */
640 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange004, TestSize.Level1)
641 {
642 DistributedTestTools::Clear(*g_observerSnapDelegate);
643
644 /**
645 * @tc.steps: step1. register observer1 bases on kv db2 snap and putBatch (k1,v1)(k2,v2).
646 * @tc.expected: step1. operate successfully.
647 */
648 KvStoreObserverImpl observer;
649 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
650 EXPECT_TRUE(snapshot != nullptr);
651 vector<Entry> entries1;
652 entries1.push_back(ENTRY_1);
653 entries1.push_back(ENTRY_2);
654 DBStatus statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries1);
655 EXPECT_TRUE(statusPut == DBStatus::OK);
656
657 /**
658 * @tc.steps: step2. check callback.
659 * @tc.expected: step2. observer1 return a batch inserting data info.
660 */
661 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, entries1));
662 observer.Clear();
663
664 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
665 snapshot = nullptr;
666 EXPECT_EQ(statusRelease, DBStatus::OK);
667 }
668
669 /*
670 * @tc.name: DataChange 005
671 * @tc.desc: verify that can observer for batch updating.
672 * @tc.type: FUNC
673 * @tc.require: SR000BUH3K,SR000BVRNE
674 * @tc.author: luqianfu
675 */
676 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange005, TestSize.Level1)
677 {
678 DistributedTestTools::Clear(*g_observerSnapDelegate);
679 vector<Entry> entries1;
680 entries1.push_back(ENTRY_1);
681 entries1.push_back(ENTRY_2);
682
683 /**
684 * @tc.steps: step1. putBatch (k1,v1)(k2,v2) and register observer1 bases on kv db2 snap.
685 * @tc.expected: step1. operate successfully to construct data and observer1 exist.
686 */
687 DBStatus statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries1);
688 EXPECT_TRUE(statusPut == DBStatus::OK);
689 KvStoreObserverImpl observer;
690 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
691 EXPECT_TRUE(snapshot != nullptr);
692
693 /**
694 * @tc.steps: step2. updateBatch (k1,v1)(k2,v2) to (k1,v2)(k2,v3) and check callback.
695 * @tc.expected: step2. observer1 return a batch updating data info.
696 */
697 vector<Entry> entries2;
698 entries2.push_back(ENTRY_1_2);
699 entries2.push_back(ENTRY_2_3);
700 statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries2);
701 EXPECT_TRUE(statusPut == DBStatus::OK);
702
703 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, entries2));
704 observer.Clear();
705
706 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
707 snapshot = nullptr;
708 EXPECT_EQ(statusRelease, DBStatus::OK);
709 }
710
711 /*
712 * @tc.name: DataChange 006
713 * @tc.desc: verify that can observer for batch deleting.
714 * @tc.type: FUNC
715 * @tc.require: SR000BUH3K,SR000BVRNE
716 * @tc.author: luqianfu
717 */
718 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange006, TestSize.Level1)
719 {
720 DistributedTestTools::Clear(*g_observerSnapDelegate);
721 vector<Entry> entries1;
722 entries1.push_back(ENTRY_1);
723 entries1.push_back(ENTRY_2);
724
725 /**
726 * @tc.steps: step1. putBatch (k1,v1)(k2,v2) and register observer1 bases on kv db2 snap.
727 * @tc.expected: step1. operate successfully to construct data and observer1 exist.
728 */
729 DBStatus statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries1);
730 EXPECT_TRUE(statusPut == DBStatus::OK);
731 KvStoreObserverImpl observer;
732 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
733 EXPECT_TRUE(snapshot != nullptr);
734
735 /**
736 * @tc.steps: step2. deleteBatch (k1)(k2) from db and check callback.
737 * @tc.expected: step2. observer1 return a batch deleting data info.
738 */
739 DBStatus statusDelete = DistributedTestTools::DeleteBatch(*g_observerSnapDelegate, KEYS_1);
740 EXPECT_TRUE(statusDelete == DBStatus::OK);
741
742 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, entries1));
743 observer.Clear();
744
745 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
746 snapshot = nullptr;
747 EXPECT_EQ(statusRelease, DBStatus::OK);
748 }
749
750 /*
751 * @tc.name: DataChange 007
752 * @tc.desc: verify that can observer for clearing.
753 * @tc.type: FUNC
754 * @tc.require: SR000BUH3K,SR000BVRNE
755 * @tc.author: luqianfu
756 */
757 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange007, TestSize.Level1)
758 {
759 DistributedTestTools::Clear(*g_observerSnapDelegate);
760 vector<Entry> entries1;
761 entries1.push_back(ENTRY_1);
762 entries1.push_back(ENTRY_2);
763
764 /**
765 * @tc.steps: step1. putBatch (k1,v1)(k2,v2) and register observer1 bases on kv db2 snap.
766 * @tc.expected: step1. operate successfully to construct data and observer1 exist.
767 */
768 DBStatus statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries1);
769 EXPECT_TRUE(statusPut == DBStatus::OK);
770
771 KvStoreObserverImpl observer;
772 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
773 EXPECT_TRUE(snapshot != nullptr);
774
775 /**
776 * @tc.steps: step2. deleteBatch (k1)(k2) from db and check callback.
777 * @tc.expected: step2. observer1 return a clear data info.
778 */
779 DBStatus statusClear = DistributedTestTools::Clear(*g_observerSnapDelegate);
780 EXPECT_TRUE(statusClear == DBStatus::OK);
781
782 vector<DistributedDB::Entry> deleteEntries;
783 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, deleteEntries));
784 observer.Clear();
785
786 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
787 snapshot = nullptr;
788 EXPECT_EQ(statusRelease, DBStatus::OK);
789 }
790
791 /*
792 * @tc.name: DataChange 008
793 * @tc.desc: verify that observer won't be activated if inserting null key.
794 * @tc.type: FUNC
795 * @tc.require: SR000BUH3K,SR000BVRNE
796 * @tc.author: luqianfu
797 */
798 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange008, TestSize.Level1)
799 {
800 DistributedTestTools::Clear(*g_observerSnapDelegate);
801
802 /**
803 * @tc.steps: step1. register observer1 bases on kv db2 snap and put (k=null,v=ok).
804 * @tc.expected: step1. register successfully and put failed.
805 */
806 KvStoreObserverImpl observer;
807 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
808 EXPECT_TRUE(snapshot != nullptr);
809
810 DBStatus statusPut = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_EMPTY, OK_VALUE_1);
811 EXPECT_TRUE(statusPut != DBStatus::OK);
812
813 /**
814 * @tc.steps: step2. check callback.
815 * @tc.expected: step2. observer1 return nothing.
816 */
817 vector<DistributedDB::Entry> insertEntries;
818 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
819 observer.Clear();
820
821 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
822 snapshot = nullptr;
823 EXPECT_EQ(statusRelease, DBStatus::OK);
824 }
825
826 /*
827 * @tc.name: DataChange 009
828 * @tc.desc: verify that observer won't be activated if batch inserting failed.
829 * @tc.type: FUNC
830 * @tc.require: SR000BUH3K,SR000BVRNE
831 * @tc.author: luqianfu
832 */
833 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange009, TestSize.Level1)
834 {
835 DistributedTestTools::Clear(*g_observerSnapDelegate);
836
837 /**
838 * @tc.steps: step1. register observer1 bases on kv db2 snap and putBatch (k1=null,v1)(k2,v2).
839 * @tc.expected: step1. register successfully and putBatch failed.
840 */
841 KvStoreObserverImpl observer;
842 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
843 EXPECT_TRUE(snapshot != nullptr);
844
845 vector<Entry> entries;
846 entries.push_back(ENTRY_NULL_1); // null key with VALUE_1.
847 entries.push_back(ENTRY_2);
848 DBStatus statusPut = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entries);
849 EXPECT_TRUE(statusPut != DBStatus::OK);
850
851 /**
852 * @tc.steps: step2. check callback.
853 * @tc.expected: step2. observer1 return nothing.
854 */
855 vector<DistributedDB::Entry> insertEntries;
856 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
857 observer.Clear();
858
859 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
860 snapshot = nullptr;
861 EXPECT_EQ(statusRelease, DBStatus::OK);
862 }
863
ObserverSnapVerifyInsert(KvStoreDelegate * & delegate1,vector<KvStoreObserverImpl> & observers)864 void ObserverSnapVerifyInsert(KvStoreDelegate *&delegate1,
865 vector<KvStoreObserverImpl> &observers)
866 {
867 vector<DistributedDB::Entry> insertEntries1;
868 vector<DistributedDB::Entry> insertEntries2;
869 DBStatus statusPut = DistributedTestTools::Put(*delegate1, KEY_A_1, VALUE_A_1);
870 EXPECT_TRUE(statusPut == DBStatus::OK);
871
872 insertEntries1.push_back(ENTRY_A_1);
873 EXPECT_TRUE(VerifyObserverResult(observers[0], CHANGED_ONE_TIME, INSERT_LIST, insertEntries1));
874 observers[0].Clear();
875
876 insertEntries2.push_back(ENTRY_A_1);
877 EXPECT_TRUE(VerifyObserverResult(observers[1], CHANGED_ONE_TIME, INSERT_LIST, insertEntries2));
878 observers[1].Clear();
879 }
880
ObserverSnapVerifyInsertBatch(KvStoreDelegate * & delegate1,vector<KvStoreObserverImpl> & observers)881 vector<Entry> ObserverSnapVerifyInsertBatch(KvStoreDelegate *&delegate1, vector<KvStoreObserverImpl> &observers)
882 {
883 vector<Entry> entriesBatch;
884 vector<Key> allKeys;
885 GenerateRecords(TEN_RECORDS, DEFAULT_START, allKeys, entriesBatch);
886 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*delegate1, entriesBatch);
887 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
888
889 EXPECT_TRUE(VerifyObserverResult(observers[0], CHANGED_ONE_TIME, INSERT_LIST, entriesBatch));
890 observers[0].Clear();
891 EXPECT_TRUE(VerifyObserverResult(observers[1], CHANGED_ONE_TIME, INSERT_LIST, entriesBatch));
892 observers[1].Clear();
893 return entriesBatch;
894 }
895
ObserverSnapVerifyDelete(KvStoreDelegate * & delegate1,vector<KvStoreObserverImpl> & observers,DistributedDB::Entry & entry)896 void ObserverSnapVerifyDelete(KvStoreDelegate *&delegate1, vector<KvStoreObserverImpl> &observers,
897 DistributedDB::Entry &entry)
898 {
899 vector<DistributedDB::Entry> deleteEntries;
900 DBStatus statusDelete = DistributedTestTools::Delete(*delegate1, KEY_A_1);
901 EXPECT_TRUE(statusDelete == DBStatus::OK);
902
903 deleteEntries.push_back(entry);
904 EXPECT_TRUE(VerifyObserverResult(observers[0], CHANGED_ONE_TIME, DELETE_LIST, deleteEntries));
905 observers[0].Clear();
906
907 vector<DistributedDB::Entry> deleteEntries2;
908 deleteEntries2.push_back(entry);
909 EXPECT_TRUE(VerifyObserverResult(observers[1], CHANGED_ONE_TIME, DELETE_LIST, deleteEntries2));
910 observers[1].Clear();
911 }
912
ObserverSnapVerifyUpdate(KvStoreDelegate * & delegate1,KvStoreDelegate * & delegate2,vector<KvStoreObserverImpl> & observers,vector<Entry> & entriesBatch,KvStoreSnapshotDelegate * & snapshot2)913 void ObserverSnapVerifyUpdate(KvStoreDelegate *&delegate1, KvStoreDelegate *&delegate2,
914 vector<KvStoreObserverImpl> &observers, vector<Entry> &entriesBatch,
915 KvStoreSnapshotDelegate *&snapshot2)
916 {
917 DBStatus statusPut = DistributedTestTools::Put(*delegate1, KEY_1, VALUE_2);
918 EXPECT_TRUE(statusPut == DBStatus::OK);
919 entriesBatch.front().value = VALUE_2;
920
921 vector<DistributedDB::Entry> updateEntries1;
922 updateEntries1.push_back(ENTRY_1_2);
923 EXPECT_TRUE(VerifyObserverResult(observers[0], CHANGED_ONE_TIME, UPDATE_LIST, updateEntries1));
924 observers[0].Clear();
925
926 vector<DistributedDB::Entry> updateEntries2;
927 updateEntries2.push_back(ENTRY_1_2);
928 EXPECT_TRUE(VerifyObserverResult(observers[1], CHANGED_ONE_TIME, UPDATE_LIST, updateEntries2));
929 observers[1].Clear();
930
931 DBStatus statusRelease = delegate2->ReleaseKvStoreSnapshot(snapshot2);
932 snapshot2 = nullptr;
933 EXPECT_EQ(statusRelease, DBStatus::OK);
934
935 for (auto itEntriesBatch = entriesBatch.begin(); itEntriesBatch != entriesBatch.end(); ++itEntriesBatch) {
936 itEntriesBatch->value.push_back('A');
937 }
938 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*delegate1, entriesBatch);
939 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
940 EXPECT_TRUE(VerifyObserverResult(observers[0], CHANGED_ONE_TIME, UPDATE_LIST, entriesBatch));
941 observers[0].Clear();
942 }
943
ObserverSnapVerifyDeleteBatch(KvStoreDelegate * & delegate1,vector<KvStoreObserverImpl> & observers,vector<Entry> & entriesBatch)944 void ObserverSnapVerifyDeleteBatch(KvStoreDelegate *&delegate1, vector<KvStoreObserverImpl> &observers,
945 vector<Entry> &entriesBatch)
946 {
947 vector<DistributedDB::Entry> deleteEntries1;
948 vector<Key> keys1;
949 vector<DistributedDB::Entry> deleteBatch;
950 for (int time = OPER_CNT_START; time < static_cast<int>(OPER_CNT_END); ++time) {
951 keys1.push_back(entriesBatch.back().key);
952 deleteBatch.push_back(entriesBatch.back());
953 entriesBatch.pop_back();
954 }
955 DBStatus statusDelete = DistributedTestTools::DeleteBatch(*delegate1, keys1);
956 EXPECT_TRUE(statusDelete == DBStatus::OK);
957
958 deleteEntries1.clear();
959 deleteEntries1 = deleteBatch;
960 EXPECT_TRUE(VerifyObserverResult(observers[0], CHANGED_ONE_TIME, DELETE_LIST, deleteEntries1));
961 observers[0].Clear();
962
963 DBStatus statusClear = DistributedTestTools::Clear(*delegate1);
964 EXPECT_TRUE(statusClear == DBStatus::OK);
965
966 deleteEntries1.clear();
967 EXPECT_TRUE(VerifyObserverResult(observers[0], CHANGED_ONE_TIME, DELETE_LIST, deleteEntries1));
968 observers[0].Clear();
969 }
970
971 /*
972 * @tc.name: DataChange 010
973 * @tc.desc: verify that can observer for complex data changing.
974 * @tc.type: FUNC
975 * @tc.require: SR000BUH3K,SR000BVRNE
976 * @tc.author: luqianfu
977 */
978 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange010, TestSize.Level2)
979 {
980 DistributedTestTools::Clear(*g_observerSnapDelegate);
981
982 KvStoreDelegate *delegate1 = nullptr;
983 KvStoreDelegate *delegate2 = nullptr;
984 KvStoreDelegateManager *manager1 = nullptr;
985 KvStoreDelegateManager *manager2 = nullptr;
986 delegate1 = DistributedTestTools::GetDelegateSuccess(manager1,
987 g_kvdbParameter2, g_kvOption);
988 ASSERT_TRUE(manager1 != nullptr && delegate1 != nullptr) << "fail to create exist kvdb";
989 KvOption option = g_kvOption;
990 option.createIfNecessary = IS_NOT_NEED_CREATE;
991 delegate2 = DistributedTestTools::GetDelegateSuccess(manager2,
992 g_kvdbParameter2, option);
993 ASSERT_TRUE(manager2 != nullptr && delegate2 != nullptr);
994 KvStoreObserverImpl observer1, observer2;
995 vector<KvStoreObserverImpl> observers;
996 observers.push_back(observer1);
997 observers.push_back(observer2);
998
999 /**
1000 * @tc.steps: step1. register observer1 bases on kv db snap1.
1001 * @tc.expected: step1. register successfully.
1002 */
1003 KvStoreSnapshotDelegate *snapshot1 = DistributedTestTools::RegisterSnapObserver(delegate1, &observers[0]);
1004 EXPECT_TRUE(snapshot1 != nullptr);
1005
1006 /**
1007 * @tc.steps: step2. register observer2 bases on kv db snap2.
1008 * @tc.expected: step2. register successfully.
1009 */
1010 KvStoreSnapshotDelegate *snapshot2 = DistributedTestTools::RegisterSnapObserver(delegate2, &observers[1]);
1011 EXPECT_TRUE(snapshot2 != nullptr);
1012
1013 /**
1014 * @tc.steps: step3. put (k="abc",v=a1") to db.
1015 * @tc.expected: step3. put successfully and both of observer1,2 return a putting data info.
1016 */
1017 ObserverSnapVerifyInsert(delegate1, observers);
1018
1019 /**
1020 * @tc.steps: step3. putBatch 10 items of (keys,values) to db.
1021 * @tc.expected: step3. putBatch successfully and both of observer1,2 return a batch putting data info.
1022 */
1023 vector<Entry> entriesBatch = ObserverSnapVerifyInsertBatch(delegate1, observers);
1024
1025 /**
1026 * @tc.steps: step4. delete (k="abc") from db.
1027 * @tc.expected: step4. delete successfully and both of observer1,2 return a deleting data info.
1028 */
1029 DistributedDB::Entry entry = ENTRY_A_1;
1030 ObserverSnapVerifyDelete(delegate1, observers, entry);
1031
1032 /**
1033 * @tc.steps: step5. update (k1,v1) to (k1,v2).
1034 * @tc.expected: step5. update successfully and both of observer1,2 return a updating data info.
1035 */
1036 ObserverSnapVerifyUpdate(delegate1, delegate2, observers, entriesBatch, snapshot2);
1037
1038 /**
1039 * @tc.steps: step6. deleteBatch (keys1) from db.
1040 * @tc.expected: step6. deleteBatch successfully and both of observer1,2 return a batch deleting data info.
1041 */
1042 ObserverSnapVerifyDeleteBatch(delegate1, observers, entriesBatch);
1043
1044 /**
1045 * @tc.steps: step7. unregister observer1.
1046 * @tc.expected: step7. unregister successfully.
1047 */
1048 DBStatus statusRelease = delegate1->ReleaseKvStoreSnapshot(snapshot1);
1049 snapshot1 = nullptr;
1050 EXPECT_EQ(statusRelease, DBStatus::OK);
1051 EXPECT_TRUE(manager1->CloseKvStore(delegate1) == OK);
1052 delegate1 = nullptr;
1053 EXPECT_TRUE(manager2->CloseKvStore(delegate2) == OK);
1054 delegate2 = nullptr;
1055 DBStatus status = manager2->DeleteKvStore(STORE_ID_2);
1056 EXPECT_EQ(status, DistributedDB::DBStatus::OK);
1057 delete manager1;
1058 manager1 = nullptr;
1059 delete manager2;
1060 manager2 = nullptr;
1061 }
1062
ObserverSnapVerifyTransactionCommit(KvStoreDelegate * & delegate1,KvStoreObserverImpl & observer1)1063 vector<Entry> ObserverSnapVerifyTransactionCommit(KvStoreDelegate *&delegate1, KvStoreObserverImpl &observer1)
1064 {
1065 vector<Entry> entryBatches;
1066 vector<Key> allKeys;
1067 GenerateRecords(TEN_RECORDS, DEFAULT_START, allKeys, entryBatches);
1068 list<DistributedDB::Entry> deleteBatch;
1069 DBStatus statusStart = delegate1->StartTransaction();
1070 EXPECT_TRUE(statusStart == DBStatus::OK);
1071 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*delegate1, entryBatches);
1072 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1073 DBStatus statusDelete = DistributedTestTools::Delete(*delegate1, KEY_1);
1074 EXPECT_TRUE(statusDelete == DBStatus::OK);
1075 deleteBatch.push_front(entryBatches[0]);
1076 entryBatches.erase(entryBatches.begin());
1077 DBStatus statusPut = DistributedTestTools::Put(*delegate1, KEY_2, VALUE_3);
1078 EXPECT_TRUE(statusPut == DBStatus::OK);
1079 entryBatches.front().value = VALUE_3;
1080 vector<Key> keys1;
1081 for (int time = DELETE_CNT_START; time < static_cast<int>(DELETE_CNT_END); ++time) {
1082 keys1.push_back(entryBatches.back().key);
1083 deleteBatch.push_front(entryBatches.back());
1084 entryBatches.pop_back();
1085 }
1086 statusDelete = DistributedTestTools::DeleteBatch(*delegate1, keys1);
1087 EXPECT_TRUE(statusDelete == DBStatus::OK);
1088 vector<DistributedDB::Entry> insertEntries;
1089 vector<DistributedDB::Entry> updateEntries;
1090 vector<DistributedDB::Entry> deleteEntries;
1091 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1092 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, UPDATE_LIST, updateEntries));
1093 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, DELETE_LIST, deleteEntries));
1094 observer1.Clear();
1095 DBStatus statusCommit = delegate1->Commit();
1096 EXPECT_TRUE(statusCommit == DBStatus::OK);
1097 insertEntries.clear();
1098 for (auto eachEntry : entryBatches) {
1099 insertEntries.push_back(eachEntry);
1100 }
1101 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, INSERT_LIST, insertEntries));
1102 updateEntries.clear();
1103 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, UPDATE_LIST, updateEntries));
1104 deleteEntries.clear();
1105 EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, DELETE_LIST, deleteEntries));
1106 observer1.Clear();
1107 return entryBatches;
1108 }
1109
ObserverSnapVerifyTransactionRollback(KvStoreDelegate * & delegate2,KvStoreObserverImpl & observer2)1110 void ObserverSnapVerifyTransactionRollback(KvStoreDelegate *&delegate2, KvStoreObserverImpl &observer2)
1111 {
1112 vector<Entry> entriesBatch;
1113 vector<Key> allKeys2;
1114 GenerateRecords(TEN_RECORDS, DEFAULT_ANOTHER_START, allKeys2, entriesBatch);
1115
1116 DBStatus statusStart = delegate2->StartTransaction();
1117 EXPECT_TRUE(statusStart == DBStatus::OK);
1118 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*delegate2, entriesBatch);
1119 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1120 DBStatus statusDelete = DistributedTestTools::Delete(*delegate2, entriesBatch[0].key);
1121 EXPECT_TRUE(statusDelete == DBStatus::OK);
1122 DBStatus statusPut = DistributedTestTools::Put(*delegate2, entriesBatch[1].key, VALUE_3);
1123 EXPECT_TRUE(statusPut == DBStatus::OK);
1124
1125 vector<Key> keys2;
1126 for (int time = DELETE_CNT_START; time < static_cast<int>(DELETE_CNT_END); ++time) {
1127 keys2.push_back(entriesBatch.at(time).key);
1128 }
1129 statusDelete = DistributedTestTools::DeleteBatch(*delegate2, keys2);
1130 EXPECT_TRUE(statusDelete == DBStatus::OK);
1131
1132 vector<DistributedDB::Entry> insertEntries;
1133 EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1134 vector<DistributedDB::Entry> updateEntries;
1135 EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, UPDATE_LIST, updateEntries));
1136 vector<DistributedDB::Entry> deleteEntries;
1137 EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, DELETE_LIST, deleteEntries));
1138 DBStatus statusRollback = delegate2->Rollback();
1139 EXPECT_TRUE(statusRollback == DBStatus::OK);
1140
1141 // step4: check the result.
1142 insertEntries.clear();
1143 EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1144 updateEntries.clear();
1145 EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, UPDATE_LIST, updateEntries));
1146 deleteEntries.clear();
1147 EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, DELETE_LIST, deleteEntries));
1148 observer2.Clear();
1149 }
1150
1151 /*
1152 * @tc.name: DataChange 011
1153 * @tc.desc: verify that can observer for transaction operating changing.
1154 * @tc.type: FUNC
1155 * @tc.require: SR000BUH3K,SR000BVRNE
1156 * @tc.author: luqianfu
1157 */
1158 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange011, TestSize.Level2)
1159 {
1160 DistributedTestTools::Clear(*g_observerSnapDelegate);
1161
1162 KvStoreObserverImpl observer1;
1163 KvStoreObserverImpl observer2;
1164
1165 KvStoreDelegate *delegate1 = nullptr;
1166 KvStoreDelegateManager *transactionManager1 = nullptr;
1167 delegate1 = DistributedTestTools::GetDelegateSuccess(transactionManager1,
1168 g_kvdbParameter2, g_kvOption);
1169 ASSERT_TRUE(transactionManager1 != nullptr && delegate1 != nullptr);
1170
1171 /**
1172 * @tc.steps: step1. register observer1 bases on kv db snap.
1173 * @tc.expected: step1. register successfully.
1174 */
1175 KvStoreSnapshotDelegate *snapshot1 = DistributedTestTools::RegisterSnapObserver(delegate1, &observer1);
1176 EXPECT_TRUE(snapshot1 != nullptr);
1177
1178 /**
1179 * @tc.steps: step2. start transaction, putBatch 10(keys,values), delete(k1), put(k2,v3), deleteBatch(k9)(k8).
1180 * @tc.expected: step2. operate successfully and observer1 return corresponding changed data info.
1181 */
1182 vector<Entry> entriesBatch = ObserverSnapVerifyTransactionCommit(delegate1, observer1);
1183
1184 /**
1185 * @tc.steps: step3. unregister observer1.
1186 * @tc.expected: step3. unregister successfully.
1187 */
1188 DBStatus statusRelease = delegate1->ReleaseKvStoreSnapshot(snapshot1);
1189 snapshot1 = nullptr;
1190 EXPECT_EQ(statusRelease, DBStatus::OK);
1191 EXPECT_TRUE(transactionManager1->CloseKvStore(delegate1) == OK);
1192 delegate1 = nullptr;
1193
1194 KvStoreDelegate *delegate2 = nullptr;
1195 KvStoreDelegateManager *transactionManager2 = nullptr;
1196 delegate2 = DistributedTestTools::GetDelegateSuccess(transactionManager2,
1197 g_kvdbParameter1_2_2, g_kvOption);
1198 ASSERT_TRUE(transactionManager2 != nullptr && delegate2 != nullptr);
1199
1200 /**
1201 * @tc.steps: step4. register observer2 bases on kv db snap.
1202 * @tc.expected: step4. register successfully.
1203 */
1204 KvStoreSnapshotDelegate *snapshot2 = DistributedTestTools::RegisterSnapObserver(delegate2, &observer2);
1205 EXPECT_TRUE(snapshot2 != nullptr);
1206
1207 /**
1208 * @tc.steps: step5. repeat step2 but don'commit transaction ,rollback it.
1209 * @tc.expected: step5. operate successfully and observer2 return nothing.
1210 */
1211 ObserverSnapVerifyTransactionRollback(delegate2, observer2);
1212
1213 statusRelease = delegate2->ReleaseKvStoreSnapshot(snapshot2);
1214 snapshot2 = nullptr;
1215 EXPECT_EQ(statusRelease, DBStatus::OK);
1216 EXPECT_TRUE(transactionManager2->CloseKvStore(delegate2) == OK);
1217 delegate2 = nullptr;
1218 DBStatus status = transactionManager2->DeleteKvStore(STORE_ID_2);
1219 EXPECT_TRUE(status == DistributedDB::DBStatus::OK);
1220 delete transactionManager1;
1221 transactionManager1 = nullptr;
1222 delete transactionManager2;
1223 transactionManager2 = nullptr;
1224 }
1225
1226 /*
1227 * @tc.name: Performance 001
1228 * @tc.desc: test performance of subscribing for inserting, deleting, updating one record.
1229 * @tc.type: Performance
1230 * @tc.require: SR000BUH3K,SR000BVRNE
1231 * @tc.author: luqianfu
1232 */
1233 HWTEST_F(DistributeddbKvObserverSnapTest, Performance001, TestSize.Level3)
1234 {
1235 DistributedTestTools::Clear(*g_observerSnapDelegate);
1236 KvStoreObserverImpl observer;
1237 /**
1238 * @tc.steps: step1. register an observer bases on kv db snap.
1239 * @tc.expected: step1. register successfully.
1240 */
1241 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1242 EXPECT_TRUE(snapshot != nullptr);
1243 auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1244
1245 /**
1246 * @tc.steps: step2. put (k1,v1) to db and caclute the time of observer return info.
1247 * @tc.expected: step2. put successfully, printf the time of observer return info after insert data.
1248 */
1249 DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_1);
1250 EXPECT_TRUE(status == DBStatus::OK);
1251 vector<DistributedDB::Entry> insertEntries;
1252 insertEntries.push_back(ENTRY_1);
1253 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, insertEntries));
1254 observer.Clear();
1255 double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1256 MST_LOG(" Getting notice from subscribing to insert a record costs: %lldus.",
1257 static_cast<long long int>(duration));
1258
1259 /**
1260 * @tc.steps: step3. put (k1,v2) to db and caclute the time of observer return info.
1261 * @tc.expected: step3. put successfully, printf the time of observer return info after update data.
1262 */
1263 tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1264 status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_2);
1265 EXPECT_TRUE(status == DBStatus::OK);
1266 vector<DistributedDB::Entry> updateEntries;
1267 updateEntries.push_back(ENTRY_1_2);
1268 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, updateEntries));
1269 observer.Clear();
1270 duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1271 MST_LOG(" Getting notice from subscribing to update a record costs: %lldus.",
1272 static_cast<long long int>(duration));
1273
1274 /**
1275 * @tc.steps: step4. delete (k1) from db and caclute the time of observer return info.
1276 * @tc.expected: step4. delete successfully, printf the time of observer return info after delete data.
1277 */
1278 tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1279 status = DistributedTestTools::Delete(*g_observerSnapDelegate, KEY_1);
1280 EXPECT_TRUE(status == DBStatus::OK);
1281 vector<DistributedDB::Entry> deleteEntries;
1282 deleteEntries.push_back(ENTRY_1_2);
1283 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, deleteEntries));
1284 observer.Clear();
1285 duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1286 MST_LOG(" Getting notice from subscribing to delete a record costs: %lldus.",
1287 static_cast<long long int>(duration));
1288
1289 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1290 snapshot = nullptr;
1291 EXPECT_EQ(statusRelease, DBStatus::OK);
1292 }
1293
ObserverSnapBatchPerformance(KvStoreObserverImpl & observer,vector<Entry> & entriesBatch)1294 void ObserverSnapBatchPerformance(KvStoreObserverImpl &observer, vector<Entry> &entriesBatch)
1295 {
1296 for (auto itEntriesBatch = entriesBatch.begin(); itEntriesBatch != entriesBatch.end(); ++itEntriesBatch) {
1297 itEntriesBatch->value.push_back('A');
1298 }
1299 auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1300 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1301 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1302 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, entriesBatch));
1303 observer.Clear();
1304
1305 double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1306 MST_LOG(" Getting notice from subscribing to update 10 records costs: %lldus.",
1307 static_cast<long long int>(duration));
1308 observer.Clear();
1309 }
1310
1311 /*
1312 * @tc.name: Performance 002
1313 * @tc.desc: test performance of subscribing for batch inserting, deleting, updating.
1314 * @tc.type: Performance
1315 * @tc.require: SR000BUH3K,SR000BVRNE
1316 * @tc.author: luqianfu
1317 */
1318 HWTEST_F(DistributeddbKvObserverSnapTest, Performance002, TestSize.Level3)
1319 {
1320 DistributedTestTools::Clear(*g_observerSnapDelegate);
1321
1322 KvStoreObserverImpl observer;
1323
1324 /**
1325 * @tc.steps: step1. register an observer bases on kv db snap.
1326 * @tc.expected: step1. register successfully.
1327 */
1328 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1329 EXPECT_TRUE(snapshot != nullptr);
1330
1331 /**
1332 * @tc.steps: step2. putBatch 10 items (keys,values) to db and caclute the time of observer return info.
1333 * @tc.expected: step2. putBatch successfully,printf the time of observer return info after insert 10 data.
1334 */
1335 vector<Entry> entriesBatch;
1336 vector<Key> allKeys;
1337 GenerateRecords(TEN_RECORDS, DEFAULT_START, allKeys, entriesBatch);
1338
1339 auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1340 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1341 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1342 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, entriesBatch));
1343 observer.Clear();
1344 double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1345 MST_LOG(" Getting notice from subscribing to insert 10 records costs: %lldus.",
1346 static_cast<long long int>(duration));
1347
1348 /**
1349 * @tc.steps: step3. updateBatch (keys,values) and caclute the time of observer return info.
1350 * @tc.expected: step3. updateBatch successfully,printf the time of observer return info after updateBatch data.
1351 */
1352 observer.Clear();
1353 ObserverSnapBatchPerformance(observer, entriesBatch);
1354
1355 /**
1356 * @tc.steps: step4. deleteBatch (keys) from db and caclute the time of observer return info.
1357 * @tc.expected: step4. deleteBatch successfully,printf the time of observer return info after deleteBatch data.
1358 */
1359 tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1360 DBStatus status = DistributedTestTools::DeleteBatch(*g_observerSnapDelegate, allKeys);
1361 EXPECT_TRUE(status == DBStatus::OK);
1362 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, entriesBatch));
1363 observer.Clear();
1364
1365 duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1366 MST_LOG(" Getting notice from subscribing to delete 10 records costs: %lldus.",
1367 static_cast<long long int>(duration));
1368
1369 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1370 snapshot = nullptr;
1371 EXPECT_EQ(statusRelease, DBStatus::OK);
1372 }
1373
ObserverSnapTransactionPerformance(vector<Entry> & entriesBatch,vector<Key> & allKeys,KvStoreObserverImpl & observer)1374 void ObserverSnapTransactionPerformance(vector<Entry> &entriesBatch,
1375 vector<Key> &allKeys, KvStoreObserverImpl &observer)
1376 {
1377 for (auto itEntriesBatch = entriesBatch.begin(); itEntriesBatch != entriesBatch.end(); ++itEntriesBatch) {
1378 itEntriesBatch->value.push_back('A');
1379 }
1380 auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1381 DBStatus statusStart = g_observerSnapDelegate->StartTransaction();
1382 EXPECT_TRUE(statusStart == DBStatus::OK);
1383 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1384 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1385 DBStatus statusCommit = g_observerSnapDelegate->Commit();
1386 EXPECT_TRUE(statusCommit == DBStatus::OK);
1387 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, entriesBatch));
1388 observer.Clear();
1389
1390 double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1391 MST_LOG(" Getting notice from subscribing a transaction to update 10 records costs: %lldus.",
1392 static_cast<long long int>(duration));
1393
1394 tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1395 statusStart = g_observerSnapDelegate->StartTransaction();
1396 EXPECT_TRUE(statusStart == DBStatus::OK);
1397 DBStatus status = DistributedTestTools::DeleteBatch(*g_observerSnapDelegate, allKeys);
1398 EXPECT_TRUE(status == DBStatus::OK);
1399 statusCommit = g_observerSnapDelegate->Commit();
1400 EXPECT_TRUE(statusCommit == DBStatus::OK);
1401 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, entriesBatch));
1402 observer.Clear();
1403 duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1404 MST_LOG("Getting notice from subscribing to delete a record costs: %lldus.",
1405 static_cast<long long int>(duration));
1406 }
1407
1408 /*
1409 * @tc.name: Performance 003
1410 * @tc.desc: test performance of subscribing for transaction operation.
1411 * @tc.type: Performance
1412 * @tc.require: SR000BUH3K,SR000BVRNE
1413 * @tc.author: luqianfu
1414 */
1415 HWTEST_F(DistributeddbKvObserverSnapTest, Performance003, TestSize.Level3)
1416 {
1417 DistributedTestTools::Clear(*g_observerSnapDelegate);
1418
1419 KvStoreObserverImpl observer;
1420
1421 /**
1422 * @tc.steps: step1. register an observer bases on kv db snap.
1423 * @tc.expected: step1. register successfully.
1424 */
1425 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1426 EXPECT_TRUE(snapshot != nullptr);
1427
1428 /**
1429 * @tc.steps: step2. start transaction and putBatch 10 items (keys,values) to db then commit.
1430 * @tc.expected: step2. operate successfully.
1431 */
1432 vector<Entry> entriesBatch;
1433 vector<Key> allKeys;
1434 GenerateRecords(TEN_RECORDS, DEFAULT_START, allKeys, entriesBatch);
1435 auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1436 DBStatus statusStart = g_observerSnapDelegate->StartTransaction();
1437 EXPECT_TRUE(statusStart == DBStatus::OK);
1438 DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1439 EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1440 DBStatus statusCommit = g_observerSnapDelegate->Commit();
1441 EXPECT_TRUE(statusCommit == DBStatus::OK);
1442
1443 /**
1444 * @tc.steps: step3. check collback and calculate the duration of step2.
1445 * @tc.expected: step3. observer return batch inserting data info.
1446 */
1447 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, entriesBatch));
1448 observer.Clear();
1449
1450 double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1451 MST_LOG(" Getting notice from subscribing a transaction to insert 10 records costs: %lldus.",
1452 static_cast<long long int>(duration));
1453
1454 ObserverSnapTransactionPerformance(entriesBatch, allKeys, observer);
1455
1456 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1457 snapshot = nullptr;
1458 EXPECT_EQ(statusRelease, DBStatus::OK);
1459 }
1460
1461 /*
1462 * @tc.name: Performance 004
1463 * @tc.desc: test system info of subscribing for inserting, deleting, updating one record.
1464 * @tc.type: Performance
1465 * @tc.require: SR000BUH3K,SR000BVRNE
1466 * @tc.author: luqianfu
1467 */
1468 HWTEST_F(DistributeddbKvObserverSnapTest, Performance004, TestSize.Level3)
1469 {
1470 DistributedTestTools::Clear(*g_observerSnapDelegate);
1471
1472 KvStoreObserverImpl observer;
1473
1474 /**
1475 * @tc.steps: step1. register an observer bases on kv db snap.
1476 * @tc.expected: step1. register successfully.
1477 */
1478 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1479 EXPECT_TRUE(snapshot != nullptr);
1480
1481 vector<DistributedDB::Entry> insertEntries;
1482 insertEntries.clear();
1483 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1484 observer.Clear();
1485
1486 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1487 snapshot = nullptr;
1488 EXPECT_EQ(statusRelease, DBStatus::OK);
1489 }
1490
1491 /*
1492 * @tc.name: Performance 005
1493 * @tc.desc: test system info of subscribing for batch inserting, deleting, updating.
1494 * @tc.type: Performance
1495 * @tc.require: SR000BUH3K,SR000BVRNE
1496 * @tc.author: luqianfu
1497 */
1498 HWTEST_F(DistributeddbKvObserverSnapTest, Performance005, TestSize.Level3)
1499 {
1500 DistributedTestTools::Clear(*g_observerSnapDelegate);
1501
1502 /**
1503 * @tc.steps: step1. register an observer bases on kv db snap.
1504 * @tc.expected: step1. register successfully.
1505 */
1506 KvStoreObserverImpl observer;
1507 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1508 EXPECT_TRUE(snapshot != nullptr);
1509
1510 vector<DistributedDB::Entry> insertEntries;
1511 insertEntries.clear();
1512 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1513 observer.Clear();
1514
1515 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1516 snapshot = nullptr;
1517 EXPECT_EQ(statusRelease, DBStatus::OK);
1518 }
1519
1520 /*
1521 * @tc.name: Performance 006
1522 * @tc.desc: test system info of subscribing for transaction operation.
1523 * @tc.type: Performance
1524 * @tc.require: SR000BUH3K,SR000BVRNE
1525 * @tc.author: luqianfu
1526 */
1527 HWTEST_F(DistributeddbKvObserverSnapTest, Performance006, TestSize.Level3)
1528 {
1529 DistributedTestTools::Clear(*g_observerSnapDelegate);
1530 /**
1531 * @tc.steps: step1. register an observer bases on kv db snap.
1532 * @tc.expected: step1. register successfully.
1533 */
1534 KvStoreObserverImpl observer;
1535 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1536 EXPECT_TRUE(snapshot != nullptr);
1537
1538 vector<DistributedDB::Entry> insertEntries;
1539 insertEntries.clear();
1540 EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1541 observer.Clear();
1542
1543 DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1544 snapshot = nullptr;
1545 EXPECT_EQ(statusRelease, DBStatus::OK);
1546 }
1547
1548 /*
1549 * @tc.name: RekeyDb 001
1550 * @tc.desc: verify that Rekey will return busy when there are registered snapshot.
1551 * @tc.type: FUNC
1552 * @tc.require: SR000CQDT4
1553 * @tc.author: fengxiaoyun
1554 */
1555 HWTEST_F(DistributeddbKvObserverSnapTest, RekeyDb001, TestSize.Level1)
1556 {
1557 KvStoreDelegate *kvObserverSnapDelegate = nullptr;
1558 KvStoreDelegateManager *manager = nullptr;
1559 KvOption option;
1560
1561 kvObserverSnapDelegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter2, option);
1562 ASSERT_TRUE(manager != nullptr && kvObserverSnapDelegate != nullptr);
1563
1564 /**
1565 * @tc.steps: step1. register an observer bases on kv db snap.
1566 * @tc.expected: step1. register successfully.
1567 */
1568 KvStoreObserverImpl observer;
1569 KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(kvObserverSnapDelegate, &observer);
1570 EXPECT_TRUE(snapshot != nullptr);
1571
1572 /**
1573 * @tc.steps: step2. call Rekey to update passwd to passwd_1.
1574 * @tc.expected: step2. Rekey returns BUSY.
1575 */
1576 EXPECT_TRUE(kvObserverSnapDelegate->Rekey(g_passwd1) == BUSY);
1577
1578 DBStatus statusRelease = kvObserverSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1579 snapshot = nullptr;
1580 EXPECT_EQ(statusRelease, DBStatus::OK);
1581
1582 EXPECT_TRUE(manager->CloseKvStore(kvObserverSnapDelegate) == OK);
1583 kvObserverSnapDelegate = nullptr;
1584
1585 DBStatus status = manager->DeleteKvStore(STORE_ID_2);
1586 EXPECT_TRUE(status == DBStatus::OK) << "fail to delete exist kvdb";
1587 delete manager;
1588 manager = nullptr;
1589 }
1590 }
1591 #endif // OMIT_MULTI_VER