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
16 #include <gtest/gtest.h>
17
18 #include "db_errno.h"
19 #include "db_common.h"
20 #include "distributeddb_data_generate_unit_test.h"
21 #include "log_print.h"
22 #include "platform_specific.h"
23
24 using namespace testing::ext;
25 using namespace DistributedDB;
26 using namespace DistributedDBUnitTest;
27
28 namespace {
29 std::string g_testDir;
30
31 // define some variables to init a KvStoreDelegateManager object.
32 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
33 KvStoreConfig g_config;
34
35 // define the g_kvDelegateCallback, used to get some information when open a kv store.
36 DBStatus g_kvDelegateStatus = INVALID_ARGS;
37
38 KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
39 auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
40 std::placeholders::_1, std::placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
41
42 class DistributedDBCommonTest : public testing::Test {
43 public:
44 static void SetUpTestCase(void);
45 static void TearDownTestCase(void);
46 void SetUp();
47 void TearDown();
48 };
49
SetUpTestCase(void)50 void DistributedDBCommonTest::SetUpTestCase(void)
51 {
52 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
53 g_config.dataDir = g_testDir;
54 g_mgr.SetKvStoreConfig(g_config);
55 }
56
TearDownTestCase(void)57 void DistributedDBCommonTest::TearDownTestCase(void) {}
58
SetUp(void)59 void DistributedDBCommonTest::SetUp(void)
60 {
61 DistributedDBToolsUnitTest::PrintTestCaseInfo();
62 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
63 }
64
TearDown(void)65 void DistributedDBCommonTest::TearDown(void)
66 {
67 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
68 LOGI("rm test db files error!");
69 }
70 }
71
72 /**
73 * @tc.name: RemoveAllFilesOfDirectory
74 * @tc.desc: Test delete all file and dir.
75 * @tc.type: FUNC
76 * @tc.require: AR000FN6G9
77 * @tc.author: sunpeng
78 */
79 HWTEST_F(DistributedDBCommonTest, RemoveAllFilesOfDirectory, TestSize.Level1)
80 {
81 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/dirLevel1_1/"), E_OK);
82 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/dirLevel1_1/" + "/dirLevel2_1/"), E_OK);
83 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/dirLevel1_1/" + "/dirLevel2_2/"), E_OK);
84 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/dirLevel1_1/" + "/dirLevel2_2/" + "/dirLevel3_1/"), E_OK);
85
86 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + "/fileLevel1_1"), E_OK);
87 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + "/dirLevel1_1/" + "/fileLevel2_1"), E_OK);
88 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/dirLevel1_1/" + "/dirLevel2_2/" +
89 "/dirLevel3_1/"+ "/fileLevel4_1/"), E_OK);
90
91 EXPECT_EQ(DBCommon::RemoveAllFilesOfDirectory(g_testDir), E_OK);
92
93 EXPECT_EQ(OS::CheckPathExistence(g_testDir), false);
94 }
95
96 #ifdef RUNNING_ON_LINUX
97 /**
98 * @tc.name: InvalidArgsTest001
99 * @tc.desc: Test invalid args for file operation.
100 * @tc.type: FUNC
101 * @tc.require:
102 * @tc.author: zhangshijie
103 */
104 HWTEST_F(DistributedDBCommonTest, InvalidArgsTest001, TestSize.Level1)
105 {
106 EXPECT_EQ(OS::CloseFile(nullptr), -E_INVALID_ARGS);
107 EXPECT_EQ(OS::FileLock(nullptr, false), -E_INVALID_ARGS);
108 // unlock nullptr will return E_OK
109 EXPECT_EQ(OS::FileUnlock(nullptr), E_OK);
110 }
111
112 /**
113 * @tc.name: SameProcessReLockFile
114 * @tc.desc: Test same process repeat lock same file.
115 * @tc.type: FUNC
116 * @tc.require: AR000FN6G9
117 * @tc.author: sunpeng
118 */
119 HWTEST_F(DistributedDBCommonTest, SameProcessReLockFile, TestSize.Level1)
120 {
121 // block mode
122 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + "/blockmode"), E_OK);
123 OS::FileHandle *fd = nullptr;
124 EXPECT_EQ(OS::OpenFile(g_testDir + "/blockmode", fd), E_OK);
125
126 EXPECT_EQ(OS::FileLock(fd, true), E_OK);
127 EXPECT_EQ(OS::FileLock(fd, true), E_OK);
128
129 // normal mode
130 OS::FileHandle *fd2 = nullptr;
131 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + "/normalmode"), E_OK);
132 EXPECT_EQ(OS::OpenFile(g_testDir + "/normalmode", fd2), E_OK);
133 EXPECT_EQ(OS::FileLock(fd2, true), E_OK);
134 EXPECT_EQ(OS::FileLock(fd2, true), E_OK);
135
136 // unlock
137 EXPECT_EQ(OS::FileUnlock(fd), E_OK);
138 EXPECT_EQ(OS::CloseFile(fd), E_OK);
139 EXPECT_EQ(OS::FileUnlock(fd2), E_OK);
140 EXPECT_EQ(OS::CloseFile(fd2), E_OK);
141 }
142
143 /**
144 * @tc.name: SameProcessReUnLockFile
145 * @tc.desc: Test same process repeat lock same file.
146 * @tc.type: FUNC
147 * @tc.require: AR000FN6G9
148 * @tc.author: sunpeng
149 */
150 HWTEST_F(DistributedDBCommonTest, SameProcessReUnLockFile, TestSize.Level1)
151 {
152 // unlock normal file twice
153 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + "/normalmode"), E_OK);
154 OS::FileHandle *fd = nullptr;
155 EXPECT_EQ(OS::OpenFile(g_testDir + "/normalmode", fd), E_OK);
156 EXPECT_EQ(OS::FileUnlock(fd), E_OK);
157 EXPECT_EQ(OS::CloseFile(fd), E_OK);
158
159 // block mode
160 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + "/blockmode"), E_OK);
161 EXPECT_EQ(OS::OpenFile(g_testDir + "/blockmode", fd), E_OK);
162
163 EXPECT_EQ(OS::FileLock(fd, false), E_OK);
164 EXPECT_EQ(OS::FileLock(fd, false), E_OK);
165 EXPECT_EQ(OS::FileUnlock(fd), E_OK);
166 EXPECT_EQ(OS::CloseFile(fd), E_OK);
167 }
168
169 /**
170 * @tc.name: CalcFileSizeTest
171 * @tc.desc: Test the file size for function test and the performance test.
172 * @tc.type: FUNC
173 * @tc.require: AR000FN6G9
174 * @tc.author: wangbingquan
175 */
176 HWTEST_F(DistributedDBCommonTest, CalcFileSizeTest, TestSize.Level1)
177 {
178 std::string filePath = g_testDir + "/testFileSize";
179 std::ofstream ofs(filePath, std::ofstream::out);
180 ASSERT_TRUE(ofs.good());
181 ofs << "test file size";
182 ofs.close();
183 uint64_t fileSize = 0;
184 EXPECT_EQ(OS::CalFileSize(filePath, fileSize), E_OK);
185 EXPECT_GT(fileSize, 0ULL);
186 EXPECT_EQ(OS::RemoveFile(filePath), E_OK);
187 }
188
189 // Distributed db is not recommended to use multiple processes to access
190 // This testcase only guard for some wrong use on current product
191 #if defined(RUN_MULTI_PROCESS_TEST)
192 namespace {
193 // use file sync diff process information
waitForStep(int step,int retryTimes)194 bool waitForStep(int step, int retryTimes)
195 {
196 std::this_thread::sleep_for(std::chrono::milliseconds(1));
197 while (retryTimes >= 0 && !OS::CheckPathExistence(g_testDir + "/LOCK_step_" + std::to_string(step))) {
198 retryTimes = retryTimes - 1; // wait 10ms one times
199 std::this_thread::sleep_for(std::chrono::milliseconds(10)); // once 10 ms
200 }
201 return (retryTimes > 0);
202 }
203
createStepFlag(int step)204 void createStepFlag(int step)
205 {
206 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + "/LOCK_step_" + std::to_string(step)), E_OK);
207 }
208 }
209
210 /**
211 * @tc.name: DiffProcessLockFile
212 * @tc.desc: Test different process repeat lock same file.
213 * @tc.type: FUNC
214 * @tc.require: AR000FN6G9
215 * @tc.author: sunpeng
216 */
217 HWTEST_F(DistributedDBCommonTest, DiffProcessLockFile, TestSize.Level1)
218 {
219 OS::FileHandle *fd = nullptr;
220 EXPECT_EQ(OS::OpenFile(g_testDir + DBConstant::DB_LOCK_POSTFIX, fd), E_OK);
221 EXPECT_EQ(OS::FileLock(fd, false), E_OK);
222 sleep(1);
223 LOGI("begin fork new process!!");
224 pid_t pid = fork();
225 ASSERT_TRUE(pid >= 0);
226 if (pid < 0) {
227 return;
228 }
229 else if (pid == 0) {
230 LOGI("child process begin!");
231 OS::FileHandle ChildFd;
232 EXPECT_EQ(OS::OpenFile(g_testDir + DBConstant::DB_LOCK_POSTFIX, ChildFd), E_OK);
233 ASSERT_TRUE(waitForStep(1, 10));
234 EXPECT_EQ(OS::FileLock(ChildFd, false), -E_BUSY);
235 createStepFlag(2);
236 EXPECT_EQ(OS::CloseFile(ChildFd), E_OK);
237 exit(0);
238 } else {
239 LOGI("main process begin!");
240 EXPECT_EQ(OS::FileLock(fd, false), E_OK);
241 createStepFlag(1);
242
243 ASSERT_TRUE(waitForStep(2, 100));
244 EXPECT_EQ(OS::CloseFile(fd), E_OK); // fd close, lock invalid
245 }
246 }
247
248 /**
249 * @tc.name: DiffProcessLockFileBlocked
250 * @tc.desc: Test different process repeat lock same file.
251 * @tc.type: FUNC
252 * @tc.require: AR000FN6G9
253 * @tc.author: sunpeng
254 */
255 HWTEST_F(DistributedDBCommonTest, DiffProcessLockFileBlocked, TestSize.Level1)
256 {
257 EXPECT_EQ(OS::CreateFileByFileName(g_testDir + DBConstant::DB_LOCK_POSTFIX), E_OK);
258 OS::FileHandle fd;
259 EXPECT_EQ(OS::OpenFile(g_testDir + DBConstant::DB_LOCK_POSTFIX, fd), E_OK);
260 EXPECT_EQ(OS::FileLock(fd, true), E_OK);
261 sleep(1);
262 LOGI("begin fork new process!!");
263 int count = 10; // wait 10 times 10 ms for block wait
264 pid_t pid = fork();
265 ASSERT_TRUE(pid >= 0);
266 if (pid < 0) {
267 return;
268 }
269 else if (pid == 0) {
270 LOGI("child process begin!");
271 EXPECT_FALSE(OS::CheckPathExistence(g_testDir + "/LOCK_step_1"));
272 OS::FileHandle ChildFd;
273 EXPECT_EQ(OS::OpenFile(g_testDir + DBConstant::DB_LOCK_POSTFIX, ChildFd), E_OK);
274 EXPECT_EQ(OS::FileLock(ChildFd, true), E_OK);
275 createStepFlag(1);
276 EXPECT_EQ(OS::FileUnlock(ChildFd), E_OK);
277 EXPECT_EQ(OS::CloseFile(ChildFd), E_OK);
278 LOGI("child process finish!");
279 exit(0);
280 } else {
281 LOGI("main process begin!");
282 while (count--) {
283 LOGI("main process waiting!");
284 std::this_thread::sleep_for(std::chrono::milliseconds(10)); // once 10 ms
285 }
286 ASSERT_FALSE(waitForStep(1, 10));
287 EXPECT_EQ(OS::FileUnlock(fd), E_OK);
288 EXPECT_EQ(OS::CloseFile(fd), E_OK);
289 ASSERT_TRUE(waitForStep(1, 10));
290 }
291 }
292
293 /**
294 * @tc.name: DiffProcessGetDBBlocked
295 * @tc.desc: Test block other process get kvstore when db locked.
296 * @tc.type: FUNC
297 * @tc.require: AR000CQDV7
298 * @tc.author: sunpeng
299 */
300 HWTEST_F(DistributedDBCommonTest, DiffProcessGetDBBlocked, TestSize.Level1)
301 {
302 std::string storeId = "DiffProcessGetDBBlocked";
303 std::string origId = USER_ID + "-" + APP_ID + "-" + storeId;
304 std::string identifier = DBCommon::TransferHashString(origId);
305 std::string hexDir = DBCommon::TransferStringToHex(identifier);
306 std::string lockFile = g_testDir + "/" + hexDir + DBConstant::DB_LOCK_POSTFIX;
307 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/" + hexDir), E_OK);
308 EXPECT_EQ(OS::CreateFileByFileName(lockFile), E_OK);
309 LOGI("Create lock file[%s]", lockFile.c_str());
310
311 LOGI("begin fork new process!!");
312 pid_t pid = fork();
313 OS::FileHandle fd;
314 ASSERT_TRUE(pid >= 0);
315 if (pid == 0) {
316 LOGI("child process begin!");
317 ASSERT_TRUE(waitForStep(1, 10));
318 KvStoreNbDelegate::Option option = {true, false, false};
319 option.isNeedIntegrityCheck = true;
320 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
321 EXPECT_TRUE(g_kvDelegateStatus == BUSY);
322 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
323 createStepFlag(2);
324 exit(0);
325 } else {
326 LOGI("main process begin!");
327 EXPECT_EQ(OS::OpenFile(lockFile, fd), E_OK);
328 EXPECT_EQ(OS::FileLock(fd, false), E_OK);
329 createStepFlag(1);
330 }
331
332 // Prevent the child process from not being completed, the main process ends to clean up resources
333 EXPECT_TRUE(waitForStep(2, 1000));
334 EXPECT_EQ(OS::FileUnlock(fd), E_OK);
335 EXPECT_EQ(OS::CloseFile(fd), E_OK);
336 }
337
338 /**
339 * @tc.name: DiffProcessDeleteDBBlocked
340 * @tc.desc: Test block other process delete kvstore when db locked.
341 * @tc.type: FUNC
342 * @tc.require: AR000CQDV7
343 * @tc.author: sunpeng
344 */
345 HWTEST_F(DistributedDBCommonTest, DiffProcessDeleteDBBlocked, TestSize.Level1)
346 {
347 std::string storeId = "DiffProcessDeleteDBBlocked";
348 std::string origId = USER_ID + "-" + APP_ID + "-" + storeId;
349 std::string identifier = DBCommon::TransferHashString(origId);
350 std::string hexDir = DBCommon::TransferStringToHex(identifier);
351 std::string lockFile = g_testDir + "/" + hexDir + DBConstant::DB_LOCK_POSTFIX;
352 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/" + hexDir), E_OK);
353 EXPECT_EQ(OS::CreateFileByFileName(lockFile), E_OK);
354 LOGI("Create lock file[%s]", lockFile.c_str());
355
356 KvStoreNbDelegate::Option option = {true, false, false};
357 option.isNeedIntegrityCheck = true;
358 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
359 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
360 EXPECT_TRUE(g_kvDelegateStatus == OK);
361
362 LOGI("begin fork new process!!");
363 pid_t pid = fork();
364 OS::FileHandle fd;
365 ASSERT_TRUE(pid >= 0);
366 if (pid == 0) {
367 LOGI("child process begin!");
368 ASSERT_TRUE(waitForStep(1, 10));
369 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), BUSY);
370 createStepFlag(2);
371 exit(0);
372 } else {
373 LOGI("main process begin!");
374 EXPECT_EQ(OS::OpenFile(lockFile, fd), E_OK);
375 EXPECT_EQ(OS::FileLock(fd, false), E_OK);
376 createStepFlag(1);
377 }
378
379 // Prevent the child process from not being completed, the main process ends to clean up resources
380 EXPECT_TRUE(waitForStep(2, 1000));
381 EXPECT_EQ(OS::FileUnlock(fd), E_OK);
382 EXPECT_EQ(OS::CloseFile(fd), E_OK);
383 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
384 }
385
386 /**
387 * @tc.name: DiffProcessGetDBBlocked001
388 * @tc.desc: Test block other process get kvstore when db locked.
389 * @tc.type: FUNC
390 * @tc.require: AR000CQDV7
391 * @tc.author: sunpeng
392 */
393 HWTEST_F(DistributedDBCommonTest, DiffProcessGetDBBlocked001, TestSize.Level1)
394 {
395 std::string storeId = "DiffProcessGetDBBlocked001";
396 std::string origId = USER_ID + "-" + APP_ID + "-" + storeId;
397 std::string identifier = DBCommon::TransferHashString(origId);
398 std::string hexDir = DBCommon::TransferStringToHex(identifier);
399 std::string lockFile = g_testDir + "/" + hexDir + DBConstant::DB_LOCK_POSTFIX;
400 EXPECT_EQ(DBCommon::CreateDirectory(g_testDir + "/" + hexDir), E_OK);
401 EXPECT_EQ(OS::CreateFileByFileName(lockFile), E_OK);
402 LOGI("Create lock file[%s]", lockFile.c_str());
403
404 LOGI("begin fork new process!!");
405 pid_t pid = fork();
406 OS::FileHandle fd;
407 ASSERT_TRUE(pid >= 0);
408 if (pid == 0) {
409 LOGI("child process begin!");
410 ASSERT_TRUE(waitForStep(1, 10));
411 KvStoreNbDelegate::Option option = {true, false, false};
412 option.isNeedIntegrityCheck = true;
413 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
414 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
415 EXPECT_TRUE(g_kvDelegateStatus == OK);
416 createStepFlag(2);
417 exit(0);
418 } else {
419 LOGI("main process begin!");
420 EXPECT_EQ(OS::OpenFile(lockFile, fd), E_OK);
421 EXPECT_EQ(OS::FileLock(fd, false), E_OK);
422 createStepFlag(1);
423 }
424 ASSERT_TRUE(waitForStep(1, 100));
425
426 EXPECT_EQ(OS::FileUnlock(fd), E_OK);
427 EXPECT_EQ(OS::CloseFile(fd), E_OK);
428
429 ASSERT_TRUE(waitForStep(2, 100));
430 }
431
432 /**
433 * @tc.name: DiffProcessGetDB
434 * @tc.desc: Test block other process get kvstore.
435 * @tc.type: FUNC
436 * @tc.require: AR000CQDV7
437 * @tc.author: sunpeng
438 */
439 HWTEST_F(DistributedDBCommonTest, DiffProcessGetDB, TestSize.Level1)
440 {
441 std::string storeId = "DiffProcessGetDB";
442 KvStoreNbDelegate::Option option = {true, false, false};
443 option.isNeedIntegrityCheck = true;
444 LOGI("begin fork new process!!");
445 pid_t pid = fork();
446 ASSERT_TRUE(pid >= 0);
447 if (pid == 0) {
448 LOGI("child process begin!");
449 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
450 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
451 EXPECT_TRUE(g_kvDelegateStatus == OK);
452 createStepFlag(2);
453 EXPECT_TRUE(waitForStep(1, 1000));
454 exit(0);
455 } else {
456 LOGI("main process begin!");
457 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
458 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
459 EXPECT_TRUE(g_kvDelegateStatus == OK);
460 createStepFlag(1);
461 }
462 EXPECT_TRUE(waitForStep(2, 100));
463 // Prevent the child process from not being completed, the main process ends to clean up resources
464 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
465 }
466
467 /**
468 * @tc.name: DiffProcessDeleteDB
469 * @tc.desc: Test block other process delete kvstore.
470 * @tc.type: FUNC
471 * @tc.require: AR000CQDV7
472 * @tc.author: sunpeng
473 */
474 HWTEST_F(DistributedDBCommonTest, DiffProcessDeleteDB, TestSize.Level1)
475 {
476 std::string storeId = "DiffProcessGetDB";
477 KvStoreNbDelegate::Option option = {true, false, false};
478 option.isNeedIntegrityCheck = true;
479 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
480 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
481 EXPECT_TRUE(g_kvDelegateStatus == OK);
482 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
483 LOGI("begin fork new process!!");
484 pid_t pid = fork();
485 ASSERT_TRUE(pid >= 0);
486 if (pid == 0) {
487 LOGI("child process begin!");
488 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
489 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
490 EXPECT_TRUE(g_kvDelegateStatus == OK);
491 createStepFlag(2);
492 EXPECT_TRUE(waitForStep(1, 1000));
493 exit(0);
494 } else {
495 LOGI("main process begin!");
496 g_mgr.DeleteKvStore(storeId);
497 createStepFlag(1);
498 }
499 EXPECT_TRUE(waitForStep(2, 100));
500
501 // Prevent the child process from not being completed, the main process ends to clean up resources
502 EXPECT_TRUE(waitForStep(1, 100));
503 }
504
505 /**
506 * @tc.name: DiffProcessGetAndDeleteDB
507 * @tc.desc: Test block other process delete kvstore.
508 * @tc.type: FUNC
509 * @tc.require: AR000CQDV7
510 * @tc.author: sunpeng
511 */
512 HWTEST_F(DistributedDBCommonTest, DiffProcessGetAndDeleteDB, TestSize.Level1)
513 {
514 std::string storeId = "DiffProcessGetAndDeleteDB";
515 KvStoreNbDelegate::Option option = {true, false, false};
516 option.isNeedIntegrityCheck = true;
517 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
518 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
519 EXPECT_TRUE(g_kvDelegateStatus == OK);
520 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
521 LOGI("begin fork new process!!");
522 pid_t pid = fork();
523 ASSERT_TRUE(pid >= 0);
524 if (pid == 0) {
525 LOGI("child process begin!");
526 g_mgr.DeleteKvStore(storeId); // one process OK, one process NOT_FOUND
527 createStepFlag(2);
528 EXPECT_TRUE(waitForStep(1, 1000));
529 exit(0);
530 } else {
531 LOGI("main process begin!");
532 g_mgr.DeleteKvStore(storeId);
533 createStepFlag(1);
534 }
535 EXPECT_TRUE(waitForStep(2, 100));
536
537 // Prevent the child process from not being completed, the main process ends to clean up resources
538 EXPECT_TRUE(waitForStep(1, 1000));
539 }
540 #endif
541 #endif
542
543 HWTEST_F(DistributedDBCommonTest, StringCaseTest002, TestSize.Level0)
544 {
545 EXPECT_TRUE(DBCommon::CaseInsensitiveCompare("HELLO WORLD.", "hello world."));
546 EXPECT_TRUE(DBCommon::CaseInsensitiveCompare("ABCDEFGHIJKLMN", "abcdefghijklmn"));
547 EXPECT_TRUE(DBCommon::CaseInsensitiveCompare("OPQRSTUVWXYZ", "opqrstuvwxyz"));
548 EXPECT_FALSE(DBCommon::CaseInsensitiveCompare("sqlite", "sqlite3"));
549 EXPECT_FALSE(DBCommon::CaseInsensitiveCompare("gitee", "git"));
550 }
551
552 HWTEST_F(DistributedDBCommonTest, PerformanceAnalysisTest001, TestSize.Level1)
553 {
554 int threadCount = 1000;
555 for (int i = 0; i < threadCount; i++) {
__anon2482f7ec0302null556 std::thread t1([] {
557 PerformanceAnalysis::GetInstance(20); // 20 is stepNum
558 });
559
__anon2482f7ec0402null560 std::thread t2([] {
561 PerformanceAnalysis::GetInstance(20); // 20 is stepNum
562 });
563
564 t1.join();
565 t2.join();
566 }
567 }
568
569 }