• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, updateEntries, deleteEntries;
1089     EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1090     EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, UPDATE_LIST, updateEntries));
1091     EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ZERO_TIME, DELETE_LIST, deleteEntries));
1092     observer1.Clear();
1093     DBStatus statusCommit = delegate1->Commit();
1094     EXPECT_TRUE(statusCommit == DBStatus::OK);
1095     insertEntries.clear();
1096     for (auto eachEntry : entryBatches) {
1097         insertEntries.push_back(eachEntry);
1098     }
1099     EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, INSERT_LIST, insertEntries));
1100     updateEntries.clear();
1101     EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, UPDATE_LIST, updateEntries));
1102     deleteEntries.clear();
1103     EXPECT_TRUE(VerifyObserverResult(observer1, CHANGED_ONE_TIME, DELETE_LIST, deleteEntries));
1104     observer1.Clear();
1105     return entryBatches;
1106 }
1107 
ObserverSnapVerifyTransactionRollback(KvStoreDelegate * & delegate2,KvStoreObserverImpl & observer2)1108 void ObserverSnapVerifyTransactionRollback(KvStoreDelegate *&delegate2, KvStoreObserverImpl &observer2)
1109 {
1110     vector<Entry> entriesBatch;
1111     vector<Key> allKeys2;
1112     GenerateRecords(TEN_RECORDS, DEFAULT_ANOTHER_START, allKeys2, entriesBatch);
1113 
1114     DBStatus statusStart = delegate2->StartTransaction();
1115     EXPECT_TRUE(statusStart == DBStatus::OK);
1116     DBStatus statusPutBatch = DistributedTestTools::PutBatch(*delegate2, entriesBatch);
1117     EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1118     DBStatus statusDelete = DistributedTestTools::Delete(*delegate2, entriesBatch[0].key);
1119     EXPECT_TRUE(statusDelete == DBStatus::OK);
1120     DBStatus statusPut = DistributedTestTools::Put(*delegate2, entriesBatch[1].key, VALUE_3);
1121     EXPECT_TRUE(statusPut == DBStatus::OK);
1122 
1123     vector<Key> keys2;
1124     for (int time = DELETE_CNT_START; time < static_cast<int>(DELETE_CNT_END); ++time) {
1125         keys2.push_back(entriesBatch.at(time).key);
1126     }
1127     statusDelete = DistributedTestTools::DeleteBatch(*delegate2, keys2);
1128     EXPECT_TRUE(statusDelete == DBStatus::OK);
1129 
1130     vector<DistributedDB::Entry> insertEntries;
1131     EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1132     vector<DistributedDB::Entry> updateEntries;
1133     EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, UPDATE_LIST, updateEntries));
1134     vector<DistributedDB::Entry> deleteEntries;
1135     EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, DELETE_LIST, deleteEntries));
1136     DBStatus statusRollback = delegate2->Rollback();
1137     EXPECT_TRUE(statusRollback == DBStatus::OK);
1138 
1139     // step4: check the result.
1140     insertEntries.clear();
1141     EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1142     updateEntries.clear();
1143     EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, UPDATE_LIST, updateEntries));
1144     deleteEntries.clear();
1145     EXPECT_TRUE(VerifyObserverResult(observer2, CHANGED_ZERO_TIME, DELETE_LIST, deleteEntries));
1146     observer2.Clear();
1147 }
1148 
1149 /*
1150  * @tc.name: DataChange 011
1151  * @tc.desc: verify that can observer for transaction operating changing.
1152  * @tc.type: FUNC
1153  * @tc.require: SR000BUH3K,SR000BVRNE
1154  * @tc.author: luqianfu
1155  */
1156 HWTEST_F(DistributeddbKvObserverSnapTest, DataChange011, TestSize.Level2)
1157 {
1158     DistributedTestTools::Clear(*g_observerSnapDelegate);
1159 
1160     KvStoreObserverImpl observer1;
1161     KvStoreObserverImpl observer2;
1162 
1163     KvStoreDelegate *delegate1 = nullptr;
1164     KvStoreDelegateManager *transactionManager1 = nullptr;
1165     delegate1 = DistributedTestTools::GetDelegateSuccess(transactionManager1,
1166         g_kvdbParameter2, g_kvOption);
1167     ASSERT_TRUE(transactionManager1 != nullptr && delegate1 != nullptr);
1168 
1169     /**
1170      * @tc.steps: step1. register observer1 bases on kv db snap.
1171      * @tc.expected: step1. register successfully.
1172      */
1173     KvStoreSnapshotDelegate *snapshot1 = DistributedTestTools::RegisterSnapObserver(delegate1, &observer1);
1174     EXPECT_TRUE(snapshot1 != nullptr);
1175 
1176     /**
1177      * @tc.steps: step2. start transaction, putBatch 10(keys,values), delete(k1), put(k2,v3), deleteBatch(k9)(k8).
1178      * @tc.expected: step2. operate successfully and observer1 return corresponding changed data info.
1179      */
1180     vector<Entry> entriesBatch = ObserverSnapVerifyTransactionCommit(delegate1, observer1);
1181 
1182     /**
1183      * @tc.steps: step3. unregister observer1.
1184      * @tc.expected: step3. unregister successfully.
1185      */
1186     DBStatus statusRelease = delegate1->ReleaseKvStoreSnapshot(snapshot1);
1187     snapshot1 = nullptr;
1188     EXPECT_EQ(statusRelease, DBStatus::OK);
1189     EXPECT_TRUE(transactionManager1->CloseKvStore(delegate1) == OK);
1190     delegate1 = nullptr;
1191 
1192     KvStoreDelegate *delegate2 = nullptr;
1193     KvStoreDelegateManager *transactionManager2 = nullptr;
1194     delegate2 = DistributedTestTools::GetDelegateSuccess(transactionManager2,
1195         g_kvdbParameter1_2_2, g_kvOption);
1196     ASSERT_TRUE(transactionManager2 != nullptr && delegate2 != nullptr);
1197 
1198     /**
1199      * @tc.steps: step4. register observer2 bases on kv db snap.
1200      * @tc.expected: step4. register successfully.
1201      */
1202     KvStoreSnapshotDelegate *snapshot2 = DistributedTestTools::RegisterSnapObserver(delegate2, &observer2);
1203     EXPECT_TRUE(snapshot2 != nullptr);
1204 
1205     /**
1206      * @tc.steps: step5. repeat step2 but don'commit transaction ,rollback it.
1207      * @tc.expected: step5. operate successfully and observer2 return nothing.
1208      */
1209     ObserverSnapVerifyTransactionRollback(delegate2, observer2);
1210 
1211     statusRelease = delegate2->ReleaseKvStoreSnapshot(snapshot2);
1212     snapshot2 = nullptr;
1213     EXPECT_EQ(statusRelease, DBStatus::OK);
1214     EXPECT_TRUE(transactionManager2->CloseKvStore(delegate2) == OK);
1215     delegate2 = nullptr;
1216     DBStatus status = transactionManager2->DeleteKvStore(STORE_ID_2);
1217     EXPECT_TRUE(status == DistributedDB::DBStatus::OK);
1218     delete transactionManager1;
1219     transactionManager1 = nullptr;
1220     delete transactionManager2;
1221     transactionManager2 = nullptr;
1222 }
1223 
1224 /*
1225  * @tc.name: Performance 001
1226  * @tc.desc: test performance of subscribing for inserting, deleting, updating one record.
1227  * @tc.type: Performance
1228  * @tc.require: SR000BUH3K,SR000BVRNE
1229  * @tc.author: luqianfu
1230  */
1231 HWTEST_F(DistributeddbKvObserverSnapTest, Performance001, TestSize.Level3)
1232 {
1233     DistributedTestTools::Clear(*g_observerSnapDelegate);
1234     KvStoreObserverImpl observer;
1235     /**
1236      * @tc.steps: step1. register an observer bases on kv db snap.
1237      * @tc.expected: step1. register successfully.
1238      */
1239     KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1240     EXPECT_TRUE(snapshot != nullptr);
1241     auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1242 
1243     /**
1244      * @tc.steps: step2. put (k1,v1) to db and caclute the time of observer return info.
1245      * @tc.expected: step2. put successfully, printf the time of observer return info after insert data.
1246      */
1247     DBStatus status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_1);
1248     EXPECT_TRUE(status == DBStatus::OK);
1249     vector<DistributedDB::Entry> insertEntries;
1250     insertEntries.push_back(ENTRY_1);
1251     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, insertEntries));
1252     observer.Clear();
1253     double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1254     MST_LOG(" Getting notice from subscribing to insert a record costs: %lldus.",
1255         static_cast<long long int>(duration));
1256 
1257     /**
1258      * @tc.steps: step3. put (k1,v2) to db and caclute the time of observer return info.
1259      * @tc.expected: step3. put successfully, printf the time of observer return info after update data.
1260      */
1261     tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1262     status = DistributedTestTools::Put(*g_observerSnapDelegate, KEY_1, VALUE_2);
1263     EXPECT_TRUE(status == DBStatus::OK);
1264     vector<DistributedDB::Entry> updateEntries;
1265     updateEntries.push_back(ENTRY_1_2);
1266     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, updateEntries));
1267     observer.Clear();
1268     duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1269     MST_LOG(" Getting notice from subscribing to update a record costs: %lldus.",
1270         static_cast<long long int>(duration));
1271 
1272     /**
1273      * @tc.steps: step4. delete (k1) from db and caclute the time of observer return info.
1274      * @tc.expected: step4. delete successfully, printf the time of observer return info after delete data.
1275      */
1276     tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1277     status = DistributedTestTools::Delete(*g_observerSnapDelegate, KEY_1);
1278     EXPECT_TRUE(status == DBStatus::OK);
1279     vector<DistributedDB::Entry> deleteEntries;
1280     deleteEntries.push_back(ENTRY_1_2);
1281     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, deleteEntries));
1282     observer.Clear();
1283     duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1284     MST_LOG(" Getting notice from subscribing to delete a record costs: %lldus.",
1285         static_cast<long long int>(duration));
1286 
1287     DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1288     snapshot = nullptr;
1289     EXPECT_EQ(statusRelease, DBStatus::OK);
1290 }
1291 
ObserverSnapBatchPerformance(KvStoreObserverImpl & observer,vector<Entry> & entriesBatch)1292 void ObserverSnapBatchPerformance(KvStoreObserverImpl &observer, vector<Entry> &entriesBatch)
1293 {
1294     for (auto itEntriesBatch = entriesBatch.begin(); itEntriesBatch != entriesBatch.end(); ++itEntriesBatch) {
1295         itEntriesBatch->value.push_back('A');
1296     }
1297     auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1298     DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1299     EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1300     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, entriesBatch));
1301     observer.Clear();
1302 
1303     double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1304     MST_LOG(" Getting notice from subscribing to update 10 records costs: %lldus.",
1305         static_cast<long long int>(duration));
1306     observer.Clear();
1307 }
1308 
1309 /*
1310  * @tc.name: Performance 002
1311  * @tc.desc: test performance of subscribing for batch inserting, deleting, updating.
1312  * @tc.type: Performance
1313  * @tc.require: SR000BUH3K,SR000BVRNE
1314  * @tc.author: luqianfu
1315  */
1316 HWTEST_F(DistributeddbKvObserverSnapTest, Performance002, TestSize.Level3)
1317 {
1318     DistributedTestTools::Clear(*g_observerSnapDelegate);
1319 
1320     KvStoreObserverImpl observer;
1321 
1322     /**
1323      * @tc.steps: step1. register an observer bases on kv db snap.
1324      * @tc.expected: step1. register successfully.
1325      */
1326     KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1327     EXPECT_TRUE(snapshot != nullptr);
1328 
1329     /**
1330      * @tc.steps: step2. putBatch 10 items (keys,values) to db and caclute the time of observer return info.
1331      * @tc.expected: step2. putBatch successfully,printf the time of observer return info after insert 10 data.
1332      */
1333     vector<Entry> entriesBatch;
1334     vector<Key> allKeys;
1335     GenerateRecords(TEN_RECORDS, DEFAULT_START, allKeys, entriesBatch);
1336 
1337     auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1338     DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1339     EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1340     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, entriesBatch));
1341     observer.Clear();
1342     double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1343     MST_LOG(" Getting notice from subscribing to insert 10 records costs: %lldus.",
1344         static_cast<long long int>(duration));
1345 
1346     /**
1347      * @tc.steps: step3. updateBatch (keys,values) and caclute the time of observer return info.
1348      * @tc.expected: step3. updateBatch successfully,printf the time of observer return info after updateBatch data.
1349      */
1350     observer.Clear();
1351     ObserverSnapBatchPerformance(observer, entriesBatch);
1352 
1353     /**
1354      * @tc.steps: step4. deleteBatch (keys) from db and caclute the time of observer return info.
1355      * @tc.expected: step4. deleteBatch successfully,printf the time of observer return info after deleteBatch data.
1356      */
1357     tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1358     DBStatus status = DistributedTestTools::DeleteBatch(*g_observerSnapDelegate, allKeys);
1359     EXPECT_TRUE(status == DBStatus::OK);
1360     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, entriesBatch));
1361     observer.Clear();
1362 
1363     duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1364     MST_LOG(" Getting notice from subscribing to delete 10 records costs: %lldus.",
1365         static_cast<long long int>(duration));
1366 
1367     DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1368     snapshot = nullptr;
1369     EXPECT_EQ(statusRelease, DBStatus::OK);
1370 }
1371 
ObserverSnapTransactionPerformance(vector<Entry> & entriesBatch,vector<Key> & allKeys,KvStoreObserverImpl & observer)1372 void ObserverSnapTransactionPerformance(vector<Entry> &entriesBatch,
1373     vector<Key> &allKeys, KvStoreObserverImpl &observer)
1374 {
1375     for (auto itEntriesBatch = entriesBatch.begin(); itEntriesBatch != entriesBatch.end(); ++itEntriesBatch) {
1376         itEntriesBatch->value.push_back('A');
1377     }
1378     auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1379     DBStatus statusStart = g_observerSnapDelegate->StartTransaction();
1380     EXPECT_TRUE(statusStart == DBStatus::OK);
1381     DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1382     EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1383     DBStatus statusCommit = g_observerSnapDelegate->Commit();
1384     EXPECT_TRUE(statusCommit == DBStatus::OK);
1385     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, UPDATE_LIST, entriesBatch));
1386     observer.Clear();
1387 
1388     double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1389     MST_LOG(" Getting notice from subscribing a transaction to update 10 records costs: %lldus.",
1390         static_cast<long long int>(duration));
1391 
1392     tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1393     statusStart = g_observerSnapDelegate->StartTransaction();
1394     EXPECT_TRUE(statusStart == DBStatus::OK);
1395     DBStatus status = DistributedTestTools::DeleteBatch(*g_observerSnapDelegate, allKeys);
1396     EXPECT_TRUE(status == DBStatus::OK);
1397     statusCommit = g_observerSnapDelegate->Commit();
1398     EXPECT_TRUE(statusCommit == DBStatus::OK);
1399     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, DELETE_LIST, entriesBatch));
1400     observer.Clear();
1401     duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1402     MST_LOG("Getting notice from subscribing to delete a record costs: %lldus.",
1403         static_cast<long long int>(duration));
1404 }
1405 
1406 /*
1407  * @tc.name: Performance 003
1408  * @tc.desc: test performance of subscribing for transaction operation.
1409  * @tc.type: Performance
1410  * @tc.require: SR000BUH3K,SR000BVRNE
1411  * @tc.author: luqianfu
1412  */
1413 HWTEST_F(DistributeddbKvObserverSnapTest, Performance003, TestSize.Level3)
1414 {
1415     DistributedTestTools::Clear(*g_observerSnapDelegate);
1416 
1417     KvStoreObserverImpl observer;
1418 
1419     /**
1420      * @tc.steps: step1. register an observer bases on kv db snap.
1421      * @tc.expected: step1. register successfully.
1422      */
1423     KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1424     EXPECT_TRUE(snapshot != nullptr);
1425 
1426     /**
1427      * @tc.steps: step2. start transaction and putBatch 10 items (keys,values) to db then commit.
1428      * @tc.expected: step2. operate successfully.
1429      */
1430     vector<Entry> entriesBatch;
1431     vector<Key> allKeys;
1432     GenerateRecords(TEN_RECORDS, DEFAULT_START, allKeys, entriesBatch);
1433     auto tick = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1434     DBStatus statusStart = g_observerSnapDelegate->StartTransaction();
1435     EXPECT_TRUE(statusStart == DBStatus::OK);
1436     DBStatus statusPutBatch = DistributedTestTools::PutBatch(*g_observerSnapDelegate, entriesBatch);
1437     EXPECT_TRUE(statusPutBatch == DBStatus::OK);
1438     DBStatus statusCommit = g_observerSnapDelegate->Commit();
1439     EXPECT_TRUE(statusCommit == DBStatus::OK);
1440 
1441     /**
1442      * @tc.steps: step3. check collback and calculate the duration of step2.
1443      * @tc.expected: step3. observer return batch inserting data info.
1444      */
1445     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ONE_TIME, INSERT_LIST, entriesBatch));
1446     observer.Clear();
1447 
1448     double duration = std::chrono::duration_cast<microseconds>(observer.GetOnChangeTime() - tick).count();
1449     MST_LOG(" Getting notice from subscribing a transaction to insert 10 records costs: %lldus.",
1450         static_cast<long long int>(duration));
1451 
1452     ObserverSnapTransactionPerformance(entriesBatch, allKeys, observer);
1453 
1454     DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1455     snapshot = nullptr;
1456     EXPECT_EQ(statusRelease, DBStatus::OK);
1457 }
1458 
1459 /*
1460  * @tc.name: Performance 004
1461  * @tc.desc: test system info of subscribing for inserting, deleting, updating one record.
1462  * @tc.type: Performance
1463  * @tc.require: SR000BUH3K,SR000BVRNE
1464  * @tc.author: luqianfu
1465  */
1466 HWTEST_F(DistributeddbKvObserverSnapTest, Performance004, TestSize.Level3)
1467 {
1468     DistributedTestTools::Clear(*g_observerSnapDelegate);
1469 
1470     KvStoreObserverImpl observer;
1471 
1472     /**
1473      * @tc.steps: step1. register an observer bases on kv db snap.
1474      * @tc.expected: step1. register successfully.
1475      */
1476     KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1477     EXPECT_TRUE(snapshot != nullptr);
1478 
1479     vector<DistributedDB::Entry> insertEntries;
1480     insertEntries.clear();
1481     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1482     observer.Clear();
1483 
1484     DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1485     snapshot = nullptr;
1486     EXPECT_EQ(statusRelease, DBStatus::OK);
1487 }
1488 
1489 /*
1490  * @tc.name: Performance 005
1491  * @tc.desc: test system info of subscribing for batch inserting, deleting, updating.
1492  * @tc.type: Performance
1493  * @tc.require: SR000BUH3K,SR000BVRNE
1494  * @tc.author: luqianfu
1495  */
1496 HWTEST_F(DistributeddbKvObserverSnapTest, Performance005, TestSize.Level3)
1497 {
1498     DistributedTestTools::Clear(*g_observerSnapDelegate);
1499 
1500     /**
1501      * @tc.steps: step1. register an observer bases on kv db snap.
1502      * @tc.expected: step1. register successfully.
1503      */
1504     KvStoreObserverImpl observer;
1505     KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1506     EXPECT_TRUE(snapshot != nullptr);
1507 
1508     vector<DistributedDB::Entry> insertEntries;
1509     insertEntries.clear();
1510     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1511     observer.Clear();
1512 
1513     DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1514     snapshot = nullptr;
1515     EXPECT_EQ(statusRelease, DBStatus::OK);
1516 }
1517 
1518 /*
1519  * @tc.name: Performance 006
1520  * @tc.desc: test system info of subscribing for transaction operation.
1521  * @tc.type: Performance
1522  * @tc.require: SR000BUH3K,SR000BVRNE
1523  * @tc.author: luqianfu
1524  */
1525 HWTEST_F(DistributeddbKvObserverSnapTest, Performance006, TestSize.Level3)
1526 {
1527     DistributedTestTools::Clear(*g_observerSnapDelegate);
1528     /**
1529      * @tc.steps: step1. register an observer bases on kv db snap.
1530      * @tc.expected: step1. register successfully.
1531      */
1532     KvStoreObserverImpl observer;
1533     KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(g_observerSnapDelegate, &observer);
1534     EXPECT_TRUE(snapshot != nullptr);
1535 
1536     vector<DistributedDB::Entry> insertEntries;
1537     insertEntries.clear();
1538     EXPECT_TRUE(VerifyObserverResult(observer, CHANGED_ZERO_TIME, INSERT_LIST, insertEntries));
1539     observer.Clear();
1540 
1541     DBStatus statusRelease = g_observerSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1542     snapshot = nullptr;
1543     EXPECT_EQ(statusRelease, DBStatus::OK);
1544 }
1545 
1546 /*
1547  * @tc.name: RekeyDb 001
1548  * @tc.desc: verify that Rekey will return busy when there are registered snapshot.
1549  * @tc.type: FUNC
1550  * @tc.require: SR000CQDT4
1551  * @tc.author: fengxiaoyun
1552  */
1553 HWTEST_F(DistributeddbKvObserverSnapTest, RekeyDb001, TestSize.Level1)
1554 {
1555     KvStoreDelegate *kvObserverSnapDelegate = nullptr;
1556     KvStoreDelegateManager *manager = nullptr;
1557     KvOption option;
1558 
1559     kvObserverSnapDelegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter2, option);
1560     ASSERT_TRUE(manager != nullptr && kvObserverSnapDelegate != nullptr);
1561 
1562     /**
1563      * @tc.steps: step1. register an observer bases on kv db snap.
1564      * @tc.expected: step1. register successfully.
1565      */
1566     KvStoreObserverImpl observer;
1567     KvStoreSnapshotDelegate *snapshot = DistributedTestTools::RegisterSnapObserver(kvObserverSnapDelegate, &observer);
1568     EXPECT_TRUE(snapshot != nullptr);
1569 
1570     /**
1571      * @tc.steps: step2. call Rekey to update passwd to passwd_1.
1572      * @tc.expected: step2. Rekey returns BUSY.
1573      */
1574     EXPECT_TRUE(kvObserverSnapDelegate->Rekey(g_passwd1) == BUSY);
1575 
1576     DBStatus statusRelease = kvObserverSnapDelegate->ReleaseKvStoreSnapshot(snapshot);
1577     snapshot = nullptr;
1578     EXPECT_EQ(statusRelease, DBStatus::OK);
1579 
1580     EXPECT_TRUE(manager->CloseKvStore(kvObserverSnapDelegate) == OK);
1581     kvObserverSnapDelegate = nullptr;
1582 
1583     DBStatus status = manager->DeleteKvStore(STORE_ID_2);
1584     EXPECT_TRUE(status == DBStatus::OK) << "fail to delete exist kvdb";
1585     delete manager;
1586     manager = nullptr;
1587 }
1588 }
1589 #endif // OMIT_MULTI_VER