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 #include "auto_launch.h"
18 #include "db_common.h"
19 #include "db_errno.h"
20 #include "distributeddb_tools_unit_test.h"
21 #include "kv_store_nb_conflict_data.h"
22 #include "kvdb_manager.h"
23 #include "kvdb_pragma.h"
24 #include "log_print.h"
25 #include "platform_specific.h"
26 #include "virtual_communicator_aggregator.h"
27
28 using namespace std;
29 using namespace testing::ext;
30 using namespace DistributedDB;
31 using namespace DistributedDBUnitTest;
32
33 namespace {
34 const std::string APP_ID = "appId";
35 const std::string USER_ID = "userId";
36 const std::string STORE_ID_0 = "storeId0";
37 const std::string STORE_ID_1 = "storeId1";
38 const std::string STORE_ID_2 = "storeId2";
39 const std::string STORE_ID_3 = "storeId3";
40 const std::string STORE_ID_4 = "storeId4";
41 const std::string STORE_ID_5 = "storeId5";
42 const std::string STORE_ID_6 = "storeId6";
43 const std::string STORE_ID_7 = "storeId7";
44 const std::string STORE_ID_8 = "storeId8";
45 string g_testDir;
46 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
47 KvStoreConfig g_config;
48 VirtualCommunicatorAggregator *g_communicatorAggregator = nullptr;
49
50 const int TEST_ENABLE_CNT = 10; // 10 time
51 const int TEST_ONLINE_CNT = 200; // 10 time
52 const int WAIT_TIME = 1000; // 1000ms
53 const int LIFE_CYCLE_TIME = 5000; // 5000ms
54 const int WAIT_SHORT_TIME = 200; // 20ms
55 const Timestamp TIME_ADD = 1000; // not zero is ok
56 const std::string REMOTE_DEVICE_ID = "remote_device";
57 const std::string THIS_DEVICE = "real_device";
58
59 const Key KEY1{'k', 'e', 'y', '1'};
60 const Key KEY2{'k', 'e', 'y', '2'};
61 const Value VALUE1{'v', 'a', 'l', 'u', 'e', '1'};
62 const Value VALUE2{'v', 'a', 'l', 'u', 'e', '2'};
63 KvDBProperties g_propA;
64 KvDBProperties g_propB;
65 KvDBProperties g_propC;
66 KvDBProperties g_propD;
67 KvDBProperties g_propE;
68 KvDBProperties g_propF;
69 KvDBProperties g_propG;
70 KvDBProperties g_propH;
71 KvDBProperties g_propI;
72 std::string g_identifierA;
73 std::string g_identifierB;
74 std::string g_identifierC;
75 std::string g_identifierD;
76 std::string g_identifierE;
77 std::string g_identifierF;
78 std::string g_identifierG;
79 std::string g_identifierH;
80 std::string g_identifierI;
81 std::string g_dualIdentifierA;
82 std::string g_dualIdentifierB;
83 std::string g_dualIdentifierC;
84 std::string g_dualIdentifierD;
85 std::string g_dualIdentifierE;
86 std::string g_dualIdentifierF;
87 std::string g_dualIdentifierG;
88 std::string g_dualIdentifierH;
89 std::string g_dualIdentifierI;
90 }
91
92 class DistributedDBAutoLaunchUnitTest : public testing::Test {
93 public:
94 static void SetUpTestCase(void);
95 static void TearDownTestCase(void);
96 void SetUp();
TearDown()97 void TearDown() {};
98 };
99
SetUpTestCase(void)100 void DistributedDBAutoLaunchUnitTest::SetUpTestCase(void)
101 {
102 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
103 g_config.dataDir = g_testDir;
104 g_mgr.SetKvStoreConfig(g_config);
105
106 string dir = g_testDir;
107 DIR *dirTmp = opendir(dir.c_str());
108 if (dirTmp == nullptr) {
109 OS::MakeDBDirectory(dir);
110 } else {
111 closedir(dirTmp);
112 }
113 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(
114 g_testDir + "/" + DBCommon::TransferStringToHex(g_identifierA) + "/single_ver") != 0) {
115 LOGE("rm test db files error!");
116 }
117 g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
118 ASSERT_TRUE(g_communicatorAggregator != nullptr);
119 RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator);
120 }
121
TearDownTestCase(void)122 void DistributedDBAutoLaunchUnitTest::TearDownTestCase(void)
123 {
124 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(
125 g_testDir + "/" + DBCommon::TransferStringToHex(g_identifierA) + "/single_ver") != 0) {
126 LOGE("rm test db files error!");
127 }
128 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
129 }
130
GetProperty(KvDBProperties & prop,std::string & identifier,std::string storeId,std::string & dualIdentifier)131 static void GetProperty(KvDBProperties &prop, std::string &identifier, std::string storeId, std::string &dualIdentifier)
132 {
133 prop.SetStringProp(KvDBProperties::USER_ID, USER_ID);
134 prop.SetStringProp(KvDBProperties::APP_ID, APP_ID);
135 prop.SetStringProp(KvDBProperties::STORE_ID, storeId);
136 identifier = DBCommon::TransferHashString(USER_ID + "-" + APP_ID + "-" + storeId);
137 prop.SetStringProp(KvDBProperties::IDENTIFIER_DATA, identifier);
138 dualIdentifier = DBCommon::TransferHashString(APP_ID + "-" + storeId);
139 prop.SetStringProp(KvDBProperties::DUAL_TUPLE_IDENTIFIER_DATA, dualIdentifier);
140 std::string identifierDirA = DBCommon::TransferStringToHex(identifier);
141 prop.SetStringProp(KvDBProperties::IDENTIFIER_DIR, identifierDirA);
142 prop.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
143 prop.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE);
144 prop.SetBoolProp(KvDBProperties::CREATE_IF_NECESSARY, true);
145 prop.SetBoolProp(KvDBProperties::SYNC_DUAL_TUPLE_MODE, false);
146 }
147
SetUp(void)148 void DistributedDBAutoLaunchUnitTest::SetUp(void)
149 {
150 DistributedDBToolsUnitTest::PrintTestCaseInfo();
151 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(
152 g_testDir + "/" + DBCommon::TransferStringToHex(g_identifierA) + "/single_ver") != 0) {
153 LOGE("rm test db files error!");
154 }
155 GetProperty(g_propA, g_identifierA, STORE_ID_0, g_dualIdentifierA);
156 GetProperty(g_propB, g_identifierB, STORE_ID_1, g_dualIdentifierB);
157 GetProperty(g_propC, g_identifierC, STORE_ID_2, g_dualIdentifierC);
158 GetProperty(g_propD, g_identifierD, STORE_ID_3, g_dualIdentifierD);
159 GetProperty(g_propE, g_identifierE, STORE_ID_4, g_dualIdentifierE);
160 GetProperty(g_propF, g_identifierF, STORE_ID_5, g_dualIdentifierF);
161 GetProperty(g_propG, g_identifierG, STORE_ID_6, g_dualIdentifierG);
162 GetProperty(g_propH, g_identifierH, STORE_ID_7, g_dualIdentifierH);
163 GetProperty(g_propI, g_identifierI, STORE_ID_8, g_dualIdentifierI);
164 }
165
PutSyncData(const KvDBProperties & prop,const Key & key,const Value & value)166 static void PutSyncData(const KvDBProperties &prop, const Key &key, const Value &value)
167 {
168 int errCode = E_OK;
169 auto kvStore = static_cast<SQLiteSingleVerNaturalStore *>(KvDBManager::OpenDatabase(prop, errCode));
170 ASSERT_NE(kvStore, nullptr);
171 auto *connection = kvStore->GetDBConnection(errCode);
172 ASSERT_NE(connection, nullptr);
173 std::vector<DataItem> vect;
174 Timestamp time;
175 kvStore->GetMaxTimestamp(time);
176 time += TIME_ADD;
177 LOGD("time:%" PRIu64, time);
178 vect.push_back({key, value, time, 0, DBCommon::TransferHashString(REMOTE_DEVICE_ID)});
179 EXPECT_EQ(DistributedDBToolsUnitTest::PutSyncDataTest(kvStore, vect, REMOTE_DEVICE_ID), E_OK);
180 RefObject::DecObjRef(kvStore);
181 connection->Close();
182 connection = nullptr;
183 }
184
SetLifeCycleTime(const KvDBProperties & prop)185 static void SetLifeCycleTime(const KvDBProperties &prop)
186 {
187 int errCode = E_OK;
188 auto kvStore = static_cast<SQLiteSingleVerNaturalStore *>(KvDBManager::OpenDatabase(prop, errCode));
189 ASSERT_NE(kvStore, nullptr);
190 auto *connection = kvStore->GetDBConnection(errCode);
191 ASSERT_NE(connection, nullptr);
192 uint32_t time = LIFE_CYCLE_TIME;
193 EXPECT_EQ(connection->Pragma(PRAGMA_SET_AUTO_LIFE_CYCLE, static_cast<PragmaData>(&time)), E_OK);
194 RefObject::DecObjRef(kvStore);
195 connection->Close();
196 connection = nullptr;
197 }
198
199 /**
200 * @tc.name: AutoLaunch001
201 * @tc.desc: basic enable/disable func
202 * @tc.type: FUNC
203 * @tc.require: AR000E8S2T
204 * @tc.author: wangchuanqing
205 */
206 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch001, TestSize.Level3)
207 {
208 /**
209 * @tc.steps: step1. right param A enable
210 * @tc.expected: step1. success.
211 */
212 AutoLaunchOption option;
213 option.notifier = nullptr;
214 int errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propA, nullptr, option);
215 EXPECT_TRUE(errCode == E_OK);
216
217 /**
218 * @tc.steps: step2. wrong param B enable
219 * @tc.expected: step2. failed.
220 */
221 g_propB.SetStringProp(KvDBProperties::IDENTIFIER_DATA, "");
222 errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propB, nullptr, option);
223 EXPECT_TRUE(errCode != E_OK);
224
225 /**
226 * @tc.steps: step3. right param C enable
227 * @tc.expected: step3. success.
228 */
229 errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propC, nullptr, option);
230 EXPECT_TRUE(errCode == E_OK);
231
232 /**
233 * @tc.steps: step4. param A disable
234 * @tc.expected: step4. E_OK.
235 */
236 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierA, g_dualIdentifierA, USER_ID);
237 EXPECT_TRUE(errCode == E_OK);
238
239 /**
240 * @tc.steps: step5. param B disable
241 * @tc.expected: step5. -E_NOT_FOUND.
242 */
243 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierB, g_dualIdentifierB, USER_ID);
244 EXPECT_TRUE(errCode == -E_NOT_FOUND);
245
246 /**
247 * @tc.steps: step6. param C disable
248 * @tc.expected: step6. E_OK.
249 */
250 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierC, g_dualIdentifierC, USER_ID);
251 EXPECT_TRUE(errCode == E_OK);
252 }
253
254 /**
255 * @tc.name: AutoLaunch002
256 * @tc.desc: online callback
257 * @tc.type: FUNC
258 * @tc.require: AR000E8S2T
259 * @tc.author: wangchuanqing
260 */
261 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch002, TestSize.Level3)
262 {
263 std::mutex cvMutex;
264 std::condition_variable cv;
265 bool finished = false;
266 std::map<const std::string, AutoLaunchStatus> statusMap;
267
268 auto notifier = [&cvMutex, &cv, &finished, &statusMap] (const std::string &userId, const std::string &appId,
__anon5ec298580202(const std::string &userId, const std::string &appId, const std::string &storeId, AutoLaunchStatus status) 269 const std::string &storeId, AutoLaunchStatus status) {
270 LOGD("int AutoLaunch002 notifier status:%d", status);
271 std::string identifier = DBCommon::TransferHashString(userId + "-" + appId + "-" + storeId);
272 std::unique_lock<std::mutex> lock(cvMutex);
273 statusMap[identifier] = status;
274 LOGD("int AutoLaunch002 notifier statusMap.size():%zu", statusMap.size());
275 if (statusMap.size() == 2) { // A and B
276 finished = true;
277 cv.notify_one();
278 }
279 };
280 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
281 ASSERT_TRUE(observer != nullptr);
282 /**
283 * @tc.steps: step1. right param A B enable
284 * @tc.expected: step1. success.
285 */
286 AutoLaunchOption option;
287 option.notifier = nullptr;
288 option.observer = observer;
289 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propA, notifier, option) == E_OK);
290 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propB, notifier, option) == E_OK);
291
292 /**
293 * @tc.steps: step2. RunOnConnectCallback
294 * @tc.expected: step2. success.
295 */
296 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, true);
297 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
298
299 /**
300 * @tc.steps: step3. PutSyncData
301 * @tc.expected: step3. notifier WRITE_OPENED
302 */
303 PutSyncData(g_propA, KEY1, VALUE1);
304 PutSyncData(g_propB, KEY1, VALUE1);
305 {
306 std::unique_lock<std::mutex> lock(cvMutex);
__anon5ec298580302null307 cv.wait(lock, [&finished] {return finished;});
308 EXPECT_TRUE(statusMap[g_identifierA] == WRITE_OPENED);
309 EXPECT_TRUE(statusMap[g_identifierB] == WRITE_OPENED);
310 statusMap.clear();
311 finished = false;
312 }
313 EXPECT_TRUE(observer->GetCallCount() == 2); // A and B
314 delete observer;
315 /**
316 * @tc.steps: step4. param A B disable
317 * @tc.expected: step4. notifier WRITE_CLOSED
318 */
319 EXPECT_TRUE(RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierA, g_dualIdentifierA, USER_ID)
320 == E_OK);
321 EXPECT_TRUE(RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierB, g_dualIdentifierB, USER_ID)
322 == E_OK);
323
324 std::unique_lock<std::mutex> lock(cvMutex);
__anon5ec298580402null325 cv.wait(lock, [&finished] {return finished;});
326 EXPECT_TRUE(statusMap[g_identifierA] == WRITE_CLOSED);
327 EXPECT_TRUE(statusMap[g_identifierB] == WRITE_CLOSED);
328 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false);
329 }
330
331 /**
332 * @tc.name: AutoLaunch003
333 * @tc.desc: CommunicatorLackCallback
334 * @tc.type: FUNC
335 * @tc.require: AR000E8S2T
336 * @tc.author: wangchuanqing
337 */
338 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch003, TestSize.Level3)
339 {
340 std::mutex cvMutex;
341 std::condition_variable cv;
342 bool finished = false;
343 std::map<const std::string, AutoLaunchStatus> statusMap;
344
345 auto notifier = [&cvMutex, &cv, &finished, &statusMap] (const std::string &userId, const std::string &appId,
__anon5ec298580502(const std::string &userId, const std::string &appId, const std::string &storeId, AutoLaunchStatus status) 346 const std::string &storeId, AutoLaunchStatus status) {
347 LOGD("int AutoLaunch002 notifier status:%d", status);
348 std::string identifier = DBCommon::TransferHashString(userId + "-" + appId + "-" + storeId);
349 std::unique_lock<std::mutex> lock(cvMutex);
350 statusMap[identifier] = status;
351 LOGD("int AutoLaunch002 notifier statusMap.size():%zu", statusMap.size());
352 finished = true;
353 cv.notify_one();
354 };
355 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
356 ASSERT_TRUE(observer != nullptr);
357
358 /**
359 * @tc.steps: step1. right param A B enable
360 * @tc.expected: step1. success.
361 */
362 AutoLaunchOption option;
363 option.notifier = nullptr;
364 option.observer = observer;
365 int errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propA, notifier, option);
366 EXPECT_TRUE(errCode == E_OK);
367 errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propB, notifier, option);
368 EXPECT_TRUE(errCode == E_OK);
369
370 /**
371 * @tc.steps: step2. RunCommunicatorLackCallback
372 * @tc.expected: step2. success.
373 */
374 LabelType label(g_identifierA.begin(), g_identifierA.end());
375 g_communicatorAggregator->RunCommunicatorLackCallback(label);
376 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
377
378 /**
379 * @tc.steps: step3. PutSyncData
380 * @tc.expected: step3. notifier WRITE_OPENED
381 */
382 PutSyncData(g_propA, KEY2, VALUE2);
383 {
384 std::unique_lock<std::mutex> lock(cvMutex);
__anon5ec298580602null385 cv.wait(lock, [&finished] {return finished;});
386 EXPECT_TRUE(statusMap[g_identifierA] == WRITE_OPENED);
387 statusMap.clear();
388 finished = false;
389 }
390 EXPECT_TRUE(observer->GetCallCount() == 1); // only A
391 delete observer;
392 /**
393 * @tc.steps: step4. param A B disable
394 * @tc.expected: step4. notifier WRITE_CLOSED
395 */
396 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierB, g_dualIdentifierB, USER_ID);
397 EXPECT_TRUE(errCode == E_OK);
398 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
399 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierA, g_dualIdentifierA, USER_ID);
400 EXPECT_TRUE(errCode == E_OK);
401
402 std::unique_lock<std::mutex> lock(cvMutex);
__anon5ec298580702null403 cv.wait(lock, [&finished] {return finished;});
404 EXPECT_TRUE(statusMap[g_identifierA] == WRITE_CLOSED);
405 EXPECT_TRUE(statusMap.size() == 1);
406 }
407
408 /**
409 * @tc.name: AutoLaunch004
410 * @tc.desc: basic enable/disable func
411 * @tc.type: FUNC
412 * @tc.require: AR000E8S2T
413 * @tc.author: wangchuanqing
414 */
415 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch004, TestSize.Level3)
416 {
417 /**
418 * @tc.steps: step1. right param A~H enable
419 * @tc.expected: step1. success.
420 */
421 AutoLaunchOption option;
422 option.notifier = nullptr;
423 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propA, nullptr, option) == E_OK);
424 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propB, nullptr, option) == E_OK);
425 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propC, nullptr, option) == E_OK);
426 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propD, nullptr, option) == E_OK);
427 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propE, nullptr, option) == E_OK);
428 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propF, nullptr, option) == E_OK);
429 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propG, nullptr, option) == E_OK);
430 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propH, nullptr, option) == E_OK);
431
432 /**
433 * @tc.steps: step2. right param I enable
434 * @tc.expected: step2. -E_MAX_LIMITS.
435 */
436 int errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propI, nullptr, option);
437 EXPECT_TRUE(errCode == -E_MAX_LIMITS);
438
439 /**
440 * @tc.steps: step3. param A disable
441 * @tc.expected: step3. E_OK.
442 */
443 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierA, g_dualIdentifierA, USER_ID);
444 EXPECT_TRUE(errCode == E_OK);
445
446 /**
447 * @tc.steps: step4. right param I enable
448 * @tc.expected: step4. E_OK.
449 */
450 errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propI, nullptr, option);
451 EXPECT_TRUE(errCode == E_OK);
452
453 /**
454 * @tc.steps: step6. param B~I disable
455 * @tc.expected: step6. E_OK.
456 */
457 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierB, g_dualIdentifierB, USER_ID);
458 EXPECT_TRUE(errCode == E_OK);
459 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierC, g_dualIdentifierC, USER_ID);
460 EXPECT_TRUE(errCode == E_OK);
461 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierD, g_dualIdentifierD, USER_ID);
462 EXPECT_TRUE(errCode == E_OK);
463 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierE, g_dualIdentifierE, USER_ID);
464 EXPECT_TRUE(errCode == E_OK);
465 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierF, g_dualIdentifierF, USER_ID);
466 EXPECT_TRUE(errCode == E_OK);
467 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierG, g_dualIdentifierG, USER_ID);
468 EXPECT_TRUE(errCode == E_OK);
469 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierH, g_dualIdentifierH, USER_ID);
470 EXPECT_TRUE(errCode == E_OK);
471 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierI, g_dualIdentifierI, USER_ID);
472 EXPECT_TRUE(errCode == E_OK);
473 }
474
475 /**
476 * @tc.name: AutoLaunch005
477 * @tc.desc: online device before enable
478 * @tc.type: FUNC
479 * @tc.require: AR000E8S2T
480 * @tc.author: wangchuanqing
481 */
482 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch005, TestSize.Level3)
483 {
484 std::mutex cvMutex;
485 std::condition_variable cv;
486 bool finished = false;
487 std::map<const std::string, AutoLaunchStatus> statusMap;
488
489 auto notifier = [&cvMutex, &cv, &finished, &statusMap] (const std::string &userId, const std::string &appId,
__anon5ec298580802(const std::string &userId, const std::string &appId, const std::string &storeId, AutoLaunchStatus status) 490 const std::string &storeId, AutoLaunchStatus status) {
491 LOGD("int AutoLaunch002 notifier status:%d", status);
492 std::string identifier = DBCommon::TransferHashString(userId + "-" + appId + "-" + storeId);
493 std::unique_lock<std::mutex> lock(cvMutex);
494 statusMap[identifier] = status;
495 LOGD("int AutoLaunch002 notifier statusMap.size():%zu", statusMap.size());
496 finished = true;
497 cv.notify_one();
498 };
499 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
500 ASSERT_TRUE(observer != nullptr);
501 /**
502 * @tc.steps: step1. RunOnConnectCallback
503 * @tc.expected: step1. success.
504 */
505 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, true);
506 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
507
508 /**
509 * @tc.steps: step2. right param A enable
510 * @tc.expected: step2. success.
511 */
512 AutoLaunchOption option;
513 option.notifier = nullptr;
514 option.observer = observer;
515 int errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propA, notifier, option);
516 EXPECT_TRUE(errCode == E_OK);
517
518 /**
519 * @tc.steps: step3. PutSyncData
520 * @tc.expected: step3. notifier WRITE_OPENED
521 */
522 PutSyncData(g_propA, KEY1, VALUE1);
523 {
524 std::unique_lock<std::mutex> lock(cvMutex);
__anon5ec298580902null525 cv.wait(lock, [&finished] {return finished;});
526 EXPECT_TRUE(statusMap[g_identifierA] == WRITE_OPENED);
527 statusMap.clear();
528 finished = false;
529 }
530 EXPECT_TRUE(observer->GetCallCount() == 1); // only A
531 /**
532 * @tc.steps: step4. param A disable
533 * @tc.expected: step4. notifier WRITE_CLOSED
534 */
535 std::string identifierA = g_propA.GetStringProp(KvDBProperties::DUAL_TUPLE_IDENTIFIER_DATA, "");
536 std::string userIdA = g_propA.GetStringProp(KvDBProperties::USER_ID, "");
537 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierA, g_dualIdentifierB, USER_ID);
538 EXPECT_TRUE(errCode == E_OK);
539
540 std::unique_lock<std::mutex> lock(cvMutex);
__anon5ec298580a02null541 cv.wait(lock, [&finished] {return finished;});
542 EXPECT_TRUE(statusMap[g_identifierA] == WRITE_CLOSED);
543 delete observer;
544 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false);
545 }
546
547 /**
548 * @tc.name: AutoLaunch006
549 * @tc.desc: online callback
550 * @tc.type: FUNC
551 * @tc.require: AR000E8S2T
552 * @tc.author: wangchuanqing
553 */
554 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch006, TestSize.Level3)
555 {
556 auto notifier = [] (const std::string &userId, const std::string &appId,
__anon5ec298580b02(const std::string &userId, const std::string &appId, const std::string &storeId, AutoLaunchStatus status) 557 const std::string &storeId, AutoLaunchStatus status) {
558 LOGD("int AutoLaunch006 notifier status:%d", status);
559 };
560 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
561 ASSERT_TRUE(observer != nullptr);
562 std::mutex cvLock;
563 std::condition_variable cv;
564 bool threadIsWorking = true;
__anon5ec298580c02() 565 thread aggregatorThread([&cvLock, &cv, &threadIsWorking]() {
566 LabelType label(g_identifierA.begin(), g_identifierA.end());
567 for (int i = 0; i < TEST_ONLINE_CNT; i++) {
568 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, true);
569 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME));
570 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false);
571 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME));
572 g_communicatorAggregator->RunCommunicatorLackCallback(label);
573 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME));
574 LOGD("AutoLaunch006 thread i:%d", i);
575 }
576 std::unique_lock<std::mutex> lock(cvLock);
577 threadIsWorking = false;
578 cv.notify_one();
579 });
580 aggregatorThread.detach();
581 AutoLaunchOption option;
582 option.notifier = nullptr;
583 option.observer = observer;
584 for (int i = 0; i < TEST_ENABLE_CNT; i++) {
585 int errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propA, notifier, option);
586 EXPECT_TRUE(errCode == E_OK);
587 errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propB, notifier, option);
588 EXPECT_TRUE(errCode == E_OK);
589
590 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierA, g_dualIdentifierA, USER_ID);
591 EXPECT_TRUE(errCode == E_OK);
592 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierB, g_dualIdentifierB, USER_ID);
593 EXPECT_TRUE(errCode == E_OK);
594 LOGD("AutoLaunch006 disable i:%d", i);
595 }
596 std::unique_lock<std::mutex> lock(cvLock);
__anon5ec298580d02null597 cv.wait(lock, [&threadIsWorking] { return !threadIsWorking; });
598
599 delete observer;
600 observer = nullptr;
601 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false);
602 }
603
604 namespace {
605 std::mutex g_cvMutex;
606 std::condition_variable g_cv;
607 bool g_finished = false;
608 std::map<const std::string, AutoLaunchStatus> g_statusMap;
ConflictNotifierCallback(const KvStoreNbConflictData & data)609 void ConflictNotifierCallback(const KvStoreNbConflictData &data)
610 {
611 LOGD("in ConflictNotifierCallback");
612 Key key;
613 Value oldValue;
614 Value newValue;
615 data.GetKey(key);
616 data.GetValue(KvStoreNbConflictData::ValueType::OLD_VALUE, oldValue);
617 data.GetValue(KvStoreNbConflictData::ValueType::NEW_VALUE, newValue);
618 EXPECT_TRUE(key == KEY1);
619 EXPECT_TRUE(oldValue == VALUE1);
620 EXPECT_TRUE(newValue == VALUE2);
621 g_finished = true;
622 g_cv.notify_one();
623 }
624
TestAutoLaunchNotifier(const std::string & userId,const std::string & appId,const std::string & storeId,AutoLaunchStatus status)625 void TestAutoLaunchNotifier(const std::string &userId, const std::string &appId, const std::string &storeId,
626 AutoLaunchStatus status)
627 {
628 LOGD("int AutoLaunchNotifier, status:%d", status);
629 std::string identifier = DBCommon::TransferHashString(userId + "-" + appId + "-" + storeId);
630 std::unique_lock<std::mutex> lock(g_cvMutex);
631 g_statusMap[identifier] = status;
632 g_finished = true;
633 g_cv.notify_one();
634 };
635
AutoLaunchCallBack(const std::string & identifier,AutoLaunchParam & param,KvStoreObserverUnitTest * observer,bool ret)636 bool AutoLaunchCallBack(const std::string &identifier, AutoLaunchParam ¶m, KvStoreObserverUnitTest *observer,
637 bool ret)
638 {
639 LOGD("int AutoLaunchCallBack");
640 EXPECT_TRUE(identifier == g_identifierA);
641 param.userId = USER_ID;
642 param.appId = APP_ID;
643 param.storeId = STORE_ID_0;
644 CipherPassword passwd;
645 param.option = {true, false, CipherType::DEFAULT, passwd, "", false, g_testDir, observer,
646 CONFLICT_FOREIGN_KEY_ONLY, ConflictNotifierCallback};
647 param.notifier = TestAutoLaunchNotifier;
648 return ret;
649 }
650
AutoLaunchCallBackBadParam(const std::string & identifier,AutoLaunchParam & param)651 bool AutoLaunchCallBackBadParam(const std::string &identifier, AutoLaunchParam ¶m)
652 {
653 LOGD("int AutoLaunchCallBack");
654 EXPECT_TRUE(identifier == g_identifierA);
655 param.notifier = TestAutoLaunchNotifier;
656 return true;
657 }
658 }
659
660 /**
661 * @tc.name: AutoLaunch007
662 * @tc.desc: enhancement callback return true
663 * @tc.type: FUNC
664 * @tc.require: AR000EPARJ
665 * @tc.author: wangchuanqing
666 */
667 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch007, TestSize.Level3)
668 {
669 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
670 ASSERT_TRUE(observer != nullptr);
671 /**
672 * @tc.steps: step1. SetAutoLaunchRequestCallback
673 * @tc.expected: step1. success.
674 */
675 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(
676 std::bind(AutoLaunchCallBack, std::placeholders::_1, std::placeholders::_2, observer, true), DBType::DB_KV);
677 /**
678 * @tc.steps: step2. RunCommunicatorLackCallback
679 * @tc.expected: step2. success.
680 */
681 LabelType label(g_identifierA.begin(), g_identifierA.end());
682 g_communicatorAggregator->RunCommunicatorLackCallback(label);
683 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
684 /**
685 * @tc.steps: step3. PutSyncData key1 value1
686 * @tc.expected: step3. notifier WRITE_OPENED
687 */
688 PutSyncData(g_propA, KEY1, VALUE1);
689 {
690 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298580f02null691 g_cv.wait(lock, [] {return g_finished;});
692 EXPECT_TRUE(g_statusMap[g_identifierA] == WRITE_OPENED);
693 g_statusMap.clear();
694 g_finished = false;
695 }
696 EXPECT_TRUE(observer->GetCallCount() == 1); // only A
697 /**
698 * @tc.steps: step4. PutSyncData key1 value2
699 * @tc.expected: step4. ConflictNotifierCallback
700 */
701 PutSyncData(g_propA, KEY1, VALUE2);
702 {
703 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581002null704 g_cv.wait(lock, [] {return g_finished;});
705 g_finished = false;
706 }
707 /**
708 * @tc.steps: step5. wait life cycle ,db close
709 * @tc.expected: step5. notifier WRITE_CLOSED
710 */
711 SetLifeCycleTime(g_propA);
712 {
713 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581102null714 g_cv.wait(lock, [] {return g_finished;});
715 EXPECT_TRUE(g_statusMap[g_identifierA] == WRITE_CLOSED);
716 g_statusMap.clear();
717 g_finished = false;
718 }
719 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(nullptr, DBType::DB_KV);
720 delete observer;
721 }
722
723 /**
724 * @tc.name: AutoLaunch008
725 * @tc.desc: enhancement callback return false
726 * @tc.type: FUNC
727 * @tc.require: AR000EPARJ
728 * @tc.author: wangchuanqing
729 */
730 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch008, TestSize.Level3)
731 {
732 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
733 ASSERT_TRUE(observer != nullptr);
734 /**
735 * @tc.steps: step1. SetAutoLaunchRequestCallback
736 * @tc.expected: step1. success.
737 */
738 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(
739 std::bind(AutoLaunchCallBack, std::placeholders::_1, std::placeholders::_2, observer, false), DBType::DB_KV);
740 /**
741 * @tc.steps: step2. RunCommunicatorLackCallback
742 * @tc.expected: step2. success.
743 */
744 LabelType label(g_identifierA.begin(), g_identifierA.end());
745 g_communicatorAggregator->RunCommunicatorLackCallback(label);
746 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
747 /**
748 * @tc.steps: step3. PutSyncData key1 value1
749 * @tc.expected: step3. db not open
750 */
751 PutSyncData(g_propA, KEY1, VALUE1);
752 PutSyncData(g_propA, KEY1, VALUE2);
753
754 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
755 EXPECT_TRUE(observer->GetCallCount() == 0);
756 EXPECT_TRUE(g_finished == false);
757 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(nullptr, DBType::DB_KV);
758 delete observer;
759 }
760
761 /**
762 * @tc.name: AutoLaunch009
763 * @tc.desc: enhancement callback return bad param
764 * @tc.type: FUNC
765 * @tc.require: AR000EPARJ
766 */
767 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch009, TestSize.Level3)
768 {
769 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
770 ASSERT_TRUE(observer != nullptr);
771 /**
772 * @tc.steps: step1. SetAutoLaunchRequestCallback
773 * @tc.expected: step1. success.
774 */
775 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(AutoLaunchCallBackBadParam, DBType::DB_KV);
776 /**
777 * @tc.steps: step2. RunCommunicatorLackCallback
778 * @tc.expected: step2. success.
779 */
780 LabelType label(g_identifierA.begin(), g_identifierA.end());
781 g_communicatorAggregator->RunCommunicatorLackCallback(label);
782 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
783 /**
784 * @tc.steps: step3. PutSyncData key1 value1
785 * @tc.expected: step3. db not open, notify INVALID_PARAM
786 */
787 PutSyncData(g_propA, KEY1, VALUE1);
788 PutSyncData(g_propA, KEY1, VALUE2);
789 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
790 EXPECT_TRUE(observer->GetCallCount() == 0);
791 {
792 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581202null793 g_cv.wait(lock, [] {return g_finished;});
794 EXPECT_TRUE(g_statusMap[DBCommon::TransferHashString("--")] == INVALID_PARAM);
795 g_statusMap.clear();
796 g_finished = false;
797 }
798 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(nullptr, DBType::DB_KV);
799 delete observer;
800 }
801
802 /**
803 * @tc.name: AutoLaunch010
804 * @tc.desc: enhancement nullptr callback
805 * @tc.type: FUNC
806 * @tc.require: AR000EPARJ
807 * @tc.author: wangchuanqing
808 */
809 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch010, TestSize.Level3)
810 {
811 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
812 ASSERT_TRUE(observer != nullptr);
813 /**
814 * @tc.steps: step1. SetAutoLaunchRequestCallback, then set nullptr
815 * @tc.expected: step1. success.
816 */
817 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(
818 std::bind(AutoLaunchCallBack, std::placeholders::_1, std::placeholders::_2, observer, false), DBType::DB_KV);
819
820 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(nullptr, DBType::DB_KV);
821 /**
822 * @tc.steps: step2. RunCommunicatorLackCallback
823 * @tc.expected: step2. success.
824 */
825 LabelType label(g_identifierA.begin(), g_identifierA.end());
826 g_communicatorAggregator->RunCommunicatorLackCallback(label);
827 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
828 /**
829 * @tc.steps: step3. PutSyncData key1 value1
830 * @tc.expected: step3. db not open
831 */
832 PutSyncData(g_propA, KEY1, VALUE1);
833 PutSyncData(g_propA, KEY1, VALUE2);
834 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
835 EXPECT_TRUE(observer->GetCallCount() == 0);
836 EXPECT_TRUE(g_finished == false);
837 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(nullptr, DBType::DB_KV);
838 delete observer;
839 }
840
841 /**
842 * @tc.name: AutoLaunch011
843 * @tc.desc: enhancement GetKvStoreIdentifier
844 * @tc.type: FUNC
845 * @tc.require: AR000EPARJ
846 * @tc.author: wangchuanqing
847 */
848 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch011, TestSize.Level3)
849 {
850 EXPECT_EQ(KvStoreDelegateManager::GetKvStoreIdentifier("", APP_ID, STORE_ID_0), "");
851 EXPECT_EQ(KvStoreDelegateManager::GetKvStoreIdentifier(
852 USER_ID, APP_ID, STORE_ID_0), DBCommon::TransferHashString(USER_ID + "-" + APP_ID + "-" + STORE_ID_0));
853 }
854
855 /**
856 * @tc.name: AutoLaunch012
857 * @tc.desc: CommunicatorLackCallback
858 * @tc.type: FUNC
859 * @tc.require: AR000E8S2T
860 * @tc.author: wangchuanqing
861 */
862 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch012, TestSize.Level3)
863 {
864 /**
865 * @tc.steps: step1. right param A B enable
866 * @tc.expected: step1. success.
867 */
868 AutoLaunchOption option;
869 option.notifier = ConflictNotifierCallback;
870 option.conflictType = CONFLICT_FOREIGN_KEY_ONLY;
871 int errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propA, TestAutoLaunchNotifier, option);
872 EXPECT_TRUE(errCode == E_OK);
873 AutoLaunchOption option1;
874 option1.notifier = nullptr;
875 errCode = RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propB, nullptr, option1);
876 EXPECT_TRUE(errCode == E_OK);
877
878 /**
879 * @tc.steps: step2. RunCommunicatorLackCallback
880 * @tc.expected: step2. success.
881 */
882 LabelType label(g_identifierA.begin(), g_identifierA.end());
883 g_communicatorAggregator->RunCommunicatorLackCallback(label);
884 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
885 PutSyncData(g_propA, KEY1, VALUE1);
886 {
887 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581302null888 g_cv.wait(lock, [] {return g_finished;});
889 EXPECT_TRUE(g_statusMap[g_identifierA] == WRITE_OPENED);
890 g_statusMap.clear();
891 g_finished = false;
892 }
893 /**
894 * @tc.steps: step3. PutSyncData key1 value2
895 * @tc.expected: step3. ConflictNotifierCallback
896 */
897 PutSyncData(g_propA, KEY1, VALUE2);
898 {
899 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581402null900 g_cv.wait(lock, [] {return g_finished;});
901 g_finished = false;
902 }
903 /**
904 * @tc.steps: step4. wait life cycle ,db close
905 * @tc.expected: step4. notifier WRITE_CLOSED
906 */
907 SetLifeCycleTime(g_propA);
908 {
909 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581502null910 g_cv.wait(lock, [] {return g_finished;});
911 EXPECT_TRUE(g_statusMap[g_identifierA] == WRITE_CLOSED);
912 g_statusMap.clear();
913 g_finished = false;
914 }
915 /**
916 * @tc.steps: step5. param A B disable
917 * @tc.expected: step5. OK
918 */
919 std::string identifierA = g_propA.GetStringProp(KvDBProperties::DUAL_TUPLE_IDENTIFIER_DATA, "");
920 std::string userIdA = g_propA.GetStringProp(KvDBProperties::USER_ID, "");
921 std::string identifierB = g_propB.GetStringProp(KvDBProperties::DUAL_TUPLE_IDENTIFIER_DATA, "");
922 std::string userIdB = g_propB.GetStringProp(KvDBProperties::USER_ID, "");
923 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierB, g_dualIdentifierB, USER_ID);
924 EXPECT_TRUE(errCode == E_OK);
925 errCode = RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierA, g_dualIdentifierA, USER_ID);
926 EXPECT_TRUE(errCode == E_OK);
927 }
928
929 /**
930 * @tc.name: AutoLaunch013
931 * @tc.desc: online callback
932 * @tc.type: FUNC
933 * @tc.require: AR000E8S2T
934 * @tc.author: wangchuanqing
935 */
936 HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch013, TestSize.Level3)
937 {
938 auto notifier = [] (const std::string &userId, const std::string &appId,
__anon5ec298581602(const std::string &userId, const std::string &appId, const std::string &storeId, AutoLaunchStatus status) 939 const std::string &storeId, AutoLaunchStatus status) {
940 LOGD("int AutoLaunch013 notifier status:%d", status);
941 };
942 /**
943 * @tc.steps: step1. right param b c enable, a SetAutoLaunchRequestCallback
944 * @tc.expected: step1. success.
945 */
946 AutoLaunchOption option;
947 option.notifier = nullptr;
948 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propB, notifier, option) == E_OK);
949 EXPECT_TRUE(RuntimeContext::GetInstance()->EnableKvStoreAutoLaunch(g_propC, notifier, option) == E_OK);
950
951 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
952 ASSERT_TRUE(observer != nullptr);
953 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(
954 std::bind(AutoLaunchCallBack, std::placeholders::_1, std::placeholders::_2, observer, true), DBType::DB_KV);
955
956 /**
957 * @tc.steps: step2. RunOnConnectCallback RunCommunicatorLackCallback
958 * @tc.expected: step2. success.
959 */
960 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, true);
961 LabelType label(g_identifierA.begin(), g_identifierA.end());
962 g_communicatorAggregator->RunCommunicatorLackCallback(label);
963 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
964
965 /**
966 * @tc.steps: step3. PutSyncData
967 * @tc.expected: step3. notifier WRITE_OPENED
968 */
969 PutSyncData(g_propA, KEY1, VALUE1);
970 PutSyncData(g_propB, KEY1, VALUE1);
971 PutSyncData(g_propC, KEY1, VALUE1);
972 {
973 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581702null974 g_cv.wait(lock, [] {return g_finished;});
975 EXPECT_TRUE(g_statusMap[g_identifierA] == WRITE_OPENED);
976 g_statusMap.clear();
977 g_finished = false;
978 }
979 /**
980 * @tc.steps: step4. PutSyncData key1 value2
981 * @tc.expected: step4. ConflictNotifierCallback
982 */
983 PutSyncData(g_propA, KEY1, VALUE2);
984 {
985 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581802null986 g_cv.wait(lock, [] {return g_finished;});
987 g_finished = false;
988 }
989 /**
990 * @tc.steps: step5. wait life cycle ,db close
991 * @tc.expected: step5. notifier WRITE_CLOSED
992 */
993 SetLifeCycleTime(g_propA);
994 {
995 std::unique_lock<std::mutex> lock(g_cvMutex);
__anon5ec298581902null996 g_cv.wait(lock, [] {return g_finished;});
997 EXPECT_TRUE(g_statusMap[g_identifierA] == WRITE_CLOSED);
998 g_statusMap.clear();
999 g_finished = false;
1000 }
1001 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(nullptr, DBType::DB_KV);
1002 delete observer;
1003 /**
1004 * @tc.steps: step4. param A B disable
1005 * @tc.expected: step4. notifier WRITE_CLOSED
1006 */
1007 EXPECT_TRUE(RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierB, g_dualIdentifierB, USER_ID)
1008 == E_OK);
1009 EXPECT_TRUE(RuntimeContext::GetInstance()->DisableKvStoreAutoLaunch(g_identifierC, g_dualIdentifierC, USER_ID)
1010 == E_OK);
1011 g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false);
1012 }