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