• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include <thread>
18 #include "db_common.h"
19 #include "db_errno.h"
20 #include "distributeddb_communicator_common.h"
21 #include "distributeddb_data_generate_unit_test.h"
22 #include "distributeddb_tools_unit_test.h"
23 #include "endian_convert.h"
24 #include "log_print.h"
25 #include "runtime_config.h"
26 #include "thread_pool_test_stub.h"
27 #include "virtual_communicator_aggregator.h"
28 
29 using namespace std;
30 using namespace testing::ext;
31 using namespace DistributedDB;
32 using namespace DistributedDBUnitTest;
33 
34 namespace {
35     EnvHandle g_envDeviceA;
36     EnvHandle g_envDeviceB;
37     EnvHandle g_envDeviceC;
38 
HandleConnectChange(OnOfflineDevice & onlines,const std::string & target,bool isConnect)39 static void HandleConnectChange(OnOfflineDevice &onlines, const std::string &target, bool isConnect)
40 {
41     if (isConnect) {
42         onlines.onlineDevices.insert(target);
43         onlines.latestOnlineDevice = target;
44         onlines.latestOfflineDevice.clear();
45     } else {
46         onlines.onlineDevices.erase(target);
47         onlines.latestOnlineDevice.clear();
48         onlines.latestOfflineDevice = target;
49     }
50 }
51 
52 class DistributedDBCommunicatorTest : public testing::Test {
53 public:
54     static void SetUpTestCase(void);
55     static void TearDownTestCase(void);
56     void SetUp();
57     void TearDown();
58 };
59 
SetUpTestCase(void)60 void DistributedDBCommunicatorTest::SetUpTestCase(void)
61 {
62     /**
63      * @tc.setup: Create and init CommunicatorAggregator and AdapterStub
64      */
65     LOGI("[UT][Test][SetUpTestCase] Enter.");
66     bool errCode = SetUpEnv(g_envDeviceA, DEVICE_NAME_A);
67     ASSERT_EQ(errCode, true);
68     errCode = SetUpEnv(g_envDeviceB, DEVICE_NAME_B);
69     ASSERT_EQ(errCode, true);
70     DoRegTransformFunction();
71     CommunicatorAggregator::EnableCommunicatorNotFoundFeedback(false);
72 }
73 
TearDownTestCase(void)74 void DistributedDBCommunicatorTest::TearDownTestCase(void)
75 {
76     /**
77      * @tc.teardown: Finalize and release CommunicatorAggregator and AdapterStub
78      */
79     LOGI("[UT][Test][TearDownTestCase] Enter.");
80     std::this_thread::sleep_for(std::chrono::seconds(7)); // Wait 7 s to make sure all thread quiet and memory released
81     TearDownEnv(g_envDeviceA);
82     TearDownEnv(g_envDeviceB);
83     CommunicatorAggregator::EnableCommunicatorNotFoundFeedback(true);
84 }
85 
SetUp()86 void DistributedDBCommunicatorTest::SetUp()
87 {
88     DistributedDBUnitTest::DistributedDBToolsUnitTest::PrintTestCaseInfo();
89 }
90 
TearDown()91 void DistributedDBCommunicatorTest::TearDown()
92 {
93     /**
94      * @tc.teardown: Wait 100 ms to make sure all thread quiet
95      */
96     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Wait 100 ms
97 }
98 
99 /**
100  * @tc.name: Communicator Management 001
101  * @tc.desc: Test alloc and release communicator
102  * @tc.type: FUNC
103  * @tc.require: AR000BVDGG AR000CQE0L
104  * @tc.author: xiaozhenjian
105  */
106 HWTEST_F(DistributedDBCommunicatorTest, CommunicatorManagement001, TestSize.Level1)
107 {
108     /**
109      * @tc.steps: step1. alloc communicator A using label A
110      * @tc.expected: step1. alloc return OK.
111      */
112     int errorNo = E_OK;
113     ICommunicator *commA = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
114     EXPECT_EQ(errorNo, E_OK);
115     EXPECT_NE(commA, nullptr);
116 
117     /**
118      * @tc.steps: step2. alloc communicator B using label B
119      * @tc.expected: step2. alloc return OK.
120      */
121     errorNo = E_OK;
122     ICommunicator *commB = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_B, errorNo);
123     EXPECT_EQ(errorNo, E_OK);
124     EXPECT_NE(commA, nullptr);
125 
126     /**
127      * @tc.steps: step3. alloc communicator C using label A
128      * @tc.expected: step3. alloc return not OK.
129      */
130     errorNo = E_OK;
131     ICommunicator *commC = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
132     EXPECT_NE(errorNo, E_OK);
133     EXPECT_EQ(commC, nullptr);
134 
135     /**
136      * @tc.steps: step4. release communicator A and communicator B
137      */
138     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commA);
139     commA = nullptr;
140     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commB);
141     commB = nullptr;
142 
143     /**
144      * @tc.steps: step5. alloc communicator D using label A
145      * @tc.expected: step5. alloc return OK.
146      */
147     errorNo = E_OK;
148     ICommunicator *commD = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
149     EXPECT_EQ(errorNo, E_OK);
150     EXPECT_NE(commD, nullptr);
151 
152     /**
153      * @tc.steps: step6. release communicator D
154      */
155     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commD);
156     commD = nullptr;
157 }
158 
159 /**
160  * @tc.name: Communicator Management 002
161  * @tc.desc: Test alloc and release communicator with different userId
162  * @tc.type: FUNC
163  * @tc.require:
164  * @tc.author: liaoyonghuang
165  */
166 HWTEST_F(DistributedDBCommunicatorTest, CommunicatorManagement002, TestSize.Level1)
167 {
168     /**
169      * @tc.steps: step1. alloc communicator A using label A with USER_ID_1
170      * @tc.expected: step1. alloc return OK.
171      */
172     int errorNo = E_OK;
173     ICommunicator *commA = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo, USER_ID_1);
174     commA->Activate(USER_ID_1);
175     EXPECT_EQ(errorNo, E_OK);
176     EXPECT_NE(commA, nullptr);
177 
178     /**
179      * @tc.steps: step2. alloc communicator B using label A with USER_ID_2
180      * @tc.expected: step2. alloc return OK, and commA == commB is TRUE
181      */
182     errorNo = E_OK;
183     ICommunicator *commB = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo, USER_ID_2);
184     commB->Activate(USER_ID_2);
185     EXPECT_EQ(errorNo, E_OK);
186     EXPECT_NE(commA, nullptr);
187 
188     /**
189      * @tc.steps: step3. alloc communicator C using label A with USER_ID_1
190      * @tc.expected: step3. alloc return not OK.
191      */
192     errorNo = E_OK;
193     ICommunicator *commC = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo, USER_ID_1);
194     EXPECT_NE(errorNo, E_OK);
195     EXPECT_EQ(commC, nullptr);
196 
197     /**
198      * @tc.steps: step4. release communicator A and communicator B
199      * @tc.expected: step4. OK.
200      */
201     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commA, USER_ID_1);
202     commA = nullptr;
203     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commB, USER_ID_2);
204     commB = nullptr;
205 }
206 
ConnectWaitDisconnect()207 static void ConnectWaitDisconnect()
208 {
209     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
210     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
211     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
212 }
213 
214 /**
215  * @tc.name: Online And Offline 001
216  * @tc.desc: Test functionality triggered by physical devices online and offline
217  * @tc.type: FUNC
218  * @tc.require: AR000BVRNS AR000CQE0H
219  * @tc.author: wudongxing
220  */
221 HWTEST_F(DistributedDBCommunicatorTest, OnlineAndOffline001, TestSize.Level1)
222 {
223     /**
224      * @tc.steps: step1. device A alloc communicator AA using label A and register callback
225      * @tc.expected: step1. no callback.
226      */
227     int errorNo = E_OK;
228     ICommunicator *commAA = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
229     ASSERT_NOT_NULL_AND_ACTIVATE(commAA, "");
230     OnOfflineDevice onlineForAA;
__anonb60f125d0202(const std::string &target, bool isConnect) 231     commAA->RegOnConnectCallback([&onlineForAA](const std::string &target, bool isConnect) {
232         HandleConnectChange(onlineForAA, target, isConnect);}, nullptr);
233     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
234 
235     /**
236      * @tc.steps: step2. connect device A with device B and then disconnect
237      * @tc.expected: step2. no callback.
238      */
239     ConnectWaitDisconnect();
240     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
241 
242     /**
243      * @tc.steps: step3. device B alloc communicator BB using label B and register callback
244      * @tc.expected: step3. no callback.
245      */
246     ICommunicator *commBB = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_B, errorNo);
247     ASSERT_NOT_NULL_AND_ACTIVATE(commBB, "");
248     OnOfflineDevice onlineForBB;
__anonb60f125d0302(const std::string &target, bool isConnect) 249     commBB->RegOnConnectCallback([&onlineForBB](const std::string &target, bool isConnect) {
250         HandleConnectChange(onlineForBB, target, isConnect);}, nullptr);
251     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
252     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
253 
254     /**
255      * @tc.steps: step4. connect device A with device B and then disconnect
256      * @tc.expected: step4. no callback.
257      */
258     ConnectWaitDisconnect();
259     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
260     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
261 
262     /**
263      * @tc.steps: step5. device B alloc communicator BA using label A and register callback
264      * @tc.expected: step5. no callback.
265      */
266     ICommunicator *commBA = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
267     ASSERT_NOT_NULL_AND_ACTIVATE(commBA, "");
268     OnOfflineDevice onlineForBA;
__anonb60f125d0402(const std::string &target, bool isConnect) 269     commBA->RegOnConnectCallback([&onlineForBA](const std::string &target, bool isConnect) {
270         HandleConnectChange(onlineForBA, target, isConnect);}, nullptr);
271     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
272     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
273     EXPECT_EQ(onlineForBA.onlineDevices.size(), static_cast<size_t>(0));
274 
275     /**
276      * @tc.steps: step6. connect device A with device B
277      * @tc.expected: step6. communicator AA has callback of device B online;
278      *                      communicator BA has callback of device A online;
279      *                      communicator BB no callback
280      */
281     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
282     std::this_thread::sleep_for(std::chrono::milliseconds(100));
283     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(1));
284     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
285     EXPECT_EQ(onlineForBA.onlineDevices.size(), static_cast<size_t>(1));
286     EXPECT_EQ(onlineForAA.latestOnlineDevice, DEVICE_NAME_B);
287     EXPECT_EQ(onlineForBA.latestOnlineDevice, DEVICE_NAME_A);
288 
289     /**
290      * @tc.steps: step7. disconnect device A with device B
291      * @tc.expected: step7. communicator AA has callback of device B offline;
292      *                      communicator BA has callback of device A offline;
293      *                      communicator BB no callback
294      */
295     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
296     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
297     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
298     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
299     EXPECT_EQ(onlineForBA.onlineDevices.size(), static_cast<size_t>(0));
300     EXPECT_EQ(onlineForAA.latestOfflineDevice, DEVICE_NAME_B);
301     EXPECT_EQ(onlineForBA.latestOfflineDevice, DEVICE_NAME_A);
302 
303     // Clean up
304     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
305     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBB);
306     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBA);
307 }
308 
309 #define REG_CONNECT_CALLBACK(communicator, online) \
310 { \
311     communicator->RegOnConnectCallback([&online](const std::string &target, bool isConnect) { \
312         HandleConnectChange(online, target, isConnect); \
313     }, nullptr); \
314 }
315 
316 #define CONNECT_AND_WAIT(waitTime) \
317 { \
318     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle); \
319     std::this_thread::sleep_for(std::chrono::milliseconds(waitTime)); \
320 }
321 
322 /**
323  * @tc.name: Online And Offline 002
324  * @tc.desc: Test functionality triggered by alloc and release communicator
325  * @tc.type: FUNC
326  * @tc.require: AR000BVRNT AR000CQE0I
327  * @tc.author: wudongxing
328  */
329 HWTEST_F(DistributedDBCommunicatorTest, OnlineAndOffline002, TestSize.Level1)
330 {
331     /**
332      * @tc.steps: step1. connect device A with device B
333      */
334     CONNECT_AND_WAIT(200); // Sleep 200 ms
335 
336     /**
337      * @tc.steps: step2. device A alloc communicator AA using label A and register callback
338      * @tc.expected: step2. no callback.
339      */
340     int errorNo = E_OK;
341     ICommunicator *commAA = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
342     ASSERT_NOT_NULL_AND_ACTIVATE(commAA, "");
343     OnOfflineDevice onlineForAA;
344     REG_CONNECT_CALLBACK(commAA, onlineForAA);
345     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
346 
347     /**
348      * @tc.steps: step3. device B alloc communicator BB using label B and register callback
349      * @tc.expected: step3. no callback.
350      */
351     ICommunicator *commBB = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_B, errorNo);
352     ASSERT_NOT_NULL_AND_ACTIVATE(commBB, "");
353     OnOfflineDevice onlineForBB;
354     REG_CONNECT_CALLBACK(commBB, onlineForBB);
355     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
356     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
357 
358     /**
359      * @tc.steps: step4. device B alloc communicator BA using label A and register callback
360      * @tc.expected: step4. communicator AA has callback of device B online;
361      *                      communicator BA has callback of device A online;
362      *                      communicator BB no callback.
363      */
364     ICommunicator *commBA = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
365     ASSERT_NOT_NULL_AND_ACTIVATE(commBA, "");
366     OnOfflineDevice onlineForBA;
367     REG_CONNECT_CALLBACK(commBA, onlineForBA);
368     std::this_thread::sleep_for(std::chrono::milliseconds(100));
369     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(1));
370     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
371     EXPECT_EQ(onlineForBA.onlineDevices.size(), static_cast<size_t>(1));
372     EXPECT_EQ(onlineForAA.latestOnlineDevice, DEVICE_NAME_B);
373     EXPECT_EQ(onlineForBA.latestOnlineDevice, DEVICE_NAME_A);
374 
375     /**
376      * @tc.steps: step5. device A alloc communicator AB using label B and register callback
377      * @tc.expected: step5. communicator AB has callback of device B online;
378      *                      communicator BB has callback of device A online;
379      */
380     ICommunicator *commAB = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_B, errorNo);
381     ASSERT_NOT_NULL_AND_ACTIVATE(commAB, "");
382     OnOfflineDevice onlineForAB;
383     REG_CONNECT_CALLBACK(commAB, onlineForAB);
384     std::this_thread::sleep_for(std::chrono::milliseconds(100));
385     EXPECT_EQ(onlineForAB.onlineDevices.size(), static_cast<size_t>(1));
386     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(1));
387     EXPECT_EQ(onlineForAB.latestOnlineDevice, DEVICE_NAME_B);
388     EXPECT_EQ(onlineForBB.latestOnlineDevice, DEVICE_NAME_A);
389 
390     /**
391      * @tc.steps: step6. device A release communicator AA
392      * @tc.expected: step6. communicator BA has callback of device A offline;
393      *                      communicator AB and BB no callback;
394      */
395     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
396     std::this_thread::sleep_for(std::chrono::milliseconds(100));
397     EXPECT_EQ(onlineForBA.onlineDevices.size(), static_cast<size_t>(0));
398     EXPECT_EQ(onlineForAB.onlineDevices.size(), static_cast<size_t>(1));
399     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(1));
400     EXPECT_EQ(onlineForBA.latestOfflineDevice, DEVICE_NAME_A);
401 
402     /**
403      * @tc.steps: step7. device B release communicator BA
404      * @tc.expected: step7. communicator AB and BB no callback;
405      */
406     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBA);
407     EXPECT_EQ(onlineForAB.onlineDevices.size(), static_cast<size_t>(1));
408     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(1));
409 
410     /**
411      * @tc.steps: step8. device B release communicator BB
412      * @tc.expected: step8. communicator AB has callback of device B offline;
413      */
414     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBB);
415     std::this_thread::sleep_for(std::chrono::milliseconds(100));
416     EXPECT_EQ(onlineForAB.onlineDevices.size(), static_cast<size_t>(0));
417     EXPECT_EQ(onlineForAB.latestOfflineDevice, DEVICE_NAME_B);
418 
419     // Clean up
420     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAB);
421     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
422 }
423 
TestRemoteRestart()424 void TestRemoteRestart()
425 {
426     /**
427      * @tc.steps: step1. connect device D with device E
428      */
429     EnvHandle envDeviceD;
430     EnvHandle envDeviceE;
431     SetUpEnv(envDeviceD, "DEVICE_D");
432     SetUpEnv(envDeviceE, "DEVICE_E");
433 
434     /**
435      * @tc.steps: step2. device D alloc communicator DD using label A and register callback
436      */
437     int errorNo = E_OK;
438     ICommunicator *commDD = envDeviceD.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
439     ASSERT_NOT_NULL_AND_ACTIVATE(commDD, "");
440     OnOfflineDevice onlineForDD;
441     REG_CONNECT_CALLBACK(commDD, onlineForDD);
442 
443     /**
444      * @tc.steps: step3. device E alloc communicator EE using label A and register callback
445      */
446     ICommunicator *commEE = envDeviceE.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
447     ASSERT_NOT_NULL_AND_ACTIVATE(commEE, "");
448     OnOfflineDevice onlineForEE;
449     REG_CONNECT_CALLBACK(commEE, onlineForEE);
450     /**
451      * @tc.steps: step4. deviceD connect to deviceE
452      * @tc.expected: step4. both communicator has callback;
453      */
454     AdapterStub::ConnectAdapterStub(envDeviceD.adapterHandle, envDeviceE.adapterHandle);
455     std::this_thread::sleep_for(std::chrono::seconds(1));
456     EXPECT_EQ(onlineForDD.onlineDevices.size(), static_cast<size_t>(1));
457     EXPECT_EQ(onlineForEE.onlineDevices.size(), static_cast<size_t>(1));
458 
459     /**
460      * @tc.steps: step5. device E restart
461      */
462     envDeviceE.commAggrHandle->ReleaseCommunicator(commEE);
463     std::this_thread::sleep_for(std::chrono::seconds(1));
464     TearDownEnv(envDeviceE);
465     SetUpEnv(envDeviceE, "DEVICE_E");
466 
467     commEE = envDeviceE.commAggrHandle->AllocCommunicator(LABEL_A, errorNo);
468     ASSERT_NOT_NULL_AND_ACTIVATE(commEE, "");
469     REG_CONNECT_CALLBACK(commEE, onlineForEE);
470     onlineForEE.onlineDevices.clear();
471     /**
472      * @tc.steps: step6. deviceD connect to deviceE again
473      * @tc.expected: step6. communicatorE has callback;
474      */
475     AdapterStub::ConnectAdapterStub(envDeviceD.adapterHandle, envDeviceE.adapterHandle);
476     int reTryTimes = 5;
477     while (onlineForEE.onlineDevices.size() != 1 && reTryTimes > 0) {
478         std::this_thread::sleep_for(std::chrono::seconds(1));
479         reTryTimes--;
480     }
481     EXPECT_EQ(onlineForEE.onlineDevices.size(), static_cast<size_t>(1));
482     // Clean up and disconnect
483     envDeviceD.commAggrHandle->ReleaseCommunicator(commDD);
484     envDeviceE.commAggrHandle->ReleaseCommunicator(commEE);
485     std::this_thread::sleep_for(std::chrono::seconds(1));
486 
487     AdapterStub::DisconnectAdapterStub(envDeviceD.adapterHandle, envDeviceE.adapterHandle);
488     TearDownEnv(envDeviceD);
489     TearDownEnv(envDeviceE);
490 }
491 /**
492  * @tc.name: Online And Offline 003
493  * @tc.desc: Test functionality triggered by remote restart
494  * @tc.type: FUNC
495  * @tc.require: AR000CQE0I
496  * @tc.author: zhangqiquan
497  */
498 HWTEST_F(DistributedDBCommunicatorTest, OnlineAndOffline003, TestSize.Level1)
499 {
500     TestRemoteRestart();
501 }
502 
503 /**
504  * @tc.name: Online And Offline 004
505  * @tc.desc: Test functionality triggered by remote restart with thread pool
506  * @tc.type: FUNC
507  * @tc.require: AR000CQE0I
508  * @tc.author: zhangqiquan
509  */
510 HWTEST_F(DistributedDBCommunicatorTest, OnlineAndOffline004, TestSize.Level1)
511 {
512     auto threadPool = std::make_shared<ThreadPoolTestStub>();
513     RuntimeContext::GetInstance()->SetThreadPool(threadPool);
514     TestRemoteRestart();
515     RuntimeContext::GetInstance()->SetThreadPool(nullptr);
516 }
517 
518 /**
519  * @tc.name: Report Device Connect Change 001
520  * @tc.desc: Test CommunicatorAggregator support report device connect change event
521  * @tc.type: FUNC
522  * @tc.require: AR000DR9KV
523  * @tc.author: xiaozhenjian
524  */
525 HWTEST_F(DistributedDBCommunicatorTest, ReportDeviceConnectChange001, TestSize.Level1)
526 {
527     /**
528      * @tc.steps: step1. device A and device B register connect callback to CommunicatorAggregator
529      */
530     OnOfflineDevice onlineForA;
531     int errCode = g_envDeviceA.commAggrHandle->RegOnConnectCallback(
__anonb60f125d0502(const std::string &target, bool isConnect) 532         [&onlineForA](const std::string &target, bool isConnect) {
533             HandleConnectChange(onlineForA, target, isConnect);
534         }, nullptr);
535     EXPECT_EQ(errCode, E_OK);
536     OnOfflineDevice onlineForB;
537     errCode = g_envDeviceB.commAggrHandle->RegOnConnectCallback(
__anonb60f125d0602(const std::string &target, bool isConnect) 538         [&onlineForB](const std::string &target, bool isConnect) {
539             HandleConnectChange(onlineForB, target, isConnect);
540         }, nullptr);
541     EXPECT_EQ(errCode, E_OK);
542 
543     /**
544      * @tc.steps: step2. connect device A with device B
545      * @tc.expected: step2. device A callback B online; device B callback A online;
546      */
547     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
548     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
549     EXPECT_EQ(onlineForA.onlineDevices.size(), static_cast<size_t>(1));
550     EXPECT_EQ(onlineForB.onlineDevices.size(), static_cast<size_t>(1));
551     EXPECT_EQ(onlineForA.latestOnlineDevice, DEVICE_NAME_B);
552     EXPECT_EQ(onlineForB.latestOnlineDevice, DEVICE_NAME_A);
553 
554     /**
555      * @tc.steps: step3. connect device A with device B
556      * @tc.expected: step3. device A callback B offline; device B callback A offline;
557      */
558     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
559     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
560     EXPECT_EQ(onlineForA.onlineDevices.size(), static_cast<size_t>(0));
561     EXPECT_EQ(onlineForB.onlineDevices.size(), static_cast<size_t>(0));
562     EXPECT_EQ(onlineForA.latestOfflineDevice, DEVICE_NAME_B);
563     EXPECT_EQ(onlineForB.latestOfflineDevice, DEVICE_NAME_A);
564 
565     // Clean up
566     g_envDeviceA.commAggrHandle->RegOnConnectCallback(nullptr, nullptr);
567     g_envDeviceB.commAggrHandle->RegOnConnectCallback(nullptr, nullptr);
568 }
569 
570 namespace {
ToLabelType(uint64_t commLabel)571 LabelType ToLabelType(uint64_t commLabel)
572 {
573     uint64_t netOrderLabel = HostToNet(commLabel);
574     uint8_t *eachByte = reinterpret_cast<uint8_t *>(&netOrderLabel);
575     std::vector<uint8_t> realLabel(COMM_LABEL_LENGTH, 0);
576     for (int i = 0; i < static_cast<int>(sizeof(uint64_t)); i++) {
577         realLabel[i] = eachByte[i];
578     }
579     return realLabel;
580 }
581 }
582 
583 /**
584  * @tc.name: Report Communicator Not Found 001
585  * @tc.desc: Test CommunicatorAggregator support report communicator not found event
586  * @tc.type: FUNC
587  * @tc.require: AR000DR9KV
588  * @tc.author: xiaozhenjian
589  */
590 HWTEST_F(DistributedDBCommunicatorTest, ReportCommunicatorNotFound001, TestSize.Level1)
591 {
592     /**
593      * @tc.steps: step1. device B register communicator not found callback to CommunicatorAggregator
594      */
595     std::vector<LabelType> lackLabels;
596     int errCode = g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback(
__anonb60f125d0802(const LabelType &commLabel, const std::string &userId)597         [&lackLabels](const LabelType &commLabel, const std::string &userId)->int {
598             lackLabels.push_back(commLabel);
599             return -E_NOT_FOUND;
600         }, nullptr);
601     EXPECT_EQ(errCode, E_OK);
602 
603     /**
604      * @tc.steps: step2. connect device A with device B
605      */
606     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
607     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
608 
609     /**
610      * @tc.steps: step3. device A alloc communicator AA using label A and send message to B
611      * @tc.expected: step3. device B callback that label A not found.
612      */
613     ICommunicator *commAA = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errCode);
614     ASSERT_NOT_NULL_AND_ACTIVATE(commAA, "");
615     Message *msgForAA = BuildRegedTinyMessage();
616     ASSERT_NE(msgForAA, nullptr);
617     SendConfig conf = {true, false, 0};
618     errCode = commAA->SendMessage(DEVICE_NAME_B, msgForAA, conf);
619     EXPECT_EQ(errCode, E_OK);
620     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
621     ASSERT_EQ(lackLabels.size(), static_cast<size_t>(1));
622     EXPECT_EQ(lackLabels[0], ToLabelType(LABEL_A));
623 
624     /**
625      * @tc.steps: step4. device B alloc communicator BA using label A and register message callback
626      * @tc.expected: step4. communicator BA will not receive message.
627      */
628     ICommunicator *commBA = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_A, errCode);
629     ASSERT_NE(commBA, nullptr);
630     Message *recvMsgForBA = nullptr;
__anonb60f125d0902(const std::string &srcTarget, Message *inMsg) 631     commBA->RegOnMessageCallback([&recvMsgForBA](const std::string &srcTarget, Message *inMsg) {
632         recvMsgForBA = inMsg;
633     }, nullptr);
634     commBA->Activate(USER_ID_1);
635     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
636     EXPECT_EQ(recvMsgForBA, nullptr);
637 
638     // Clean up
639     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
640     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBA);
641     g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback(nullptr, nullptr);
642     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
643 }
644 
645 #define DO_SEND_MESSAGE(src, dst, label, session) \
646 { \
647     Message *msgFor##src##label = BuildRegedTinyMessage(); \
648     ASSERT_NE(msgFor##src##label, nullptr); \
649     msgFor##src##label->SetSessionId(session); \
650     SendConfig conf = {true, false, 0}; \
651     errCode = comm##src##label->SendMessage(DEVICE_NAME_##dst, msgFor##src##label, conf); \
652     EXPECT_EQ(errCode, E_OK); \
653 }
654 
655 #define DO_SEND_GIANT_MESSAGE(src, dst, label, size) \
656 { \
657     Message *msgFor##src##label = BuildRegedGiantMessage(size); \
658     ASSERT_NE(msgFor##src##label, nullptr); \
659     SendConfig conf = {false, false, 0}; \
660     errCode = comm##src##label->SendMessage(DEVICE_NAME_##dst, msgFor##src##label, conf); \
661     EXPECT_EQ(errCode, E_OK); \
662 }
663 
664 #define ALLOC_AND_SEND_MESSAGE(src, dst, label, session) \
665     ICommunicator *comm##src##label = g_envDevice##src.commAggrHandle->AllocCommunicator(LABEL_##label, errCode); \
666     ASSERT_NOT_NULL_AND_ACTIVATE(comm##src##label, ""); \
667     DO_SEND_MESSAGE(src, dst, label, session)
668 
669 #define REG_MESSAGE_CALLBACK(src, label) \
670     string srcTargetFor##src##label; \
671     Message *recvMsgFor##src##label = nullptr; \
672     comm##src##label->RegOnMessageCallback( \
673         [&srcTargetFor##src##label, &recvMsgFor##src##label](const std::string &srcTarget, Message *inMsg) { \
674         srcTargetFor##src##label = srcTarget; \
675         recvMsgFor##src##label = inMsg; \
676     }, nullptr);
677 
678 /**
679  * @tc.name: ReDeliver Message 001
680  * @tc.desc: Test CommunicatorAggregator support redeliver message
681  * @tc.type: FUNC
682  * @tc.require: AR000DR9KV
683  * @tc.author: xiaozhenjian
684  */
685 HWTEST_F(DistributedDBCommunicatorTest, ReDeliverMessage001, TestSize.Level1)
686 {
687     /**
688      * @tc.steps: step1. device B register communicator not found callback to CommunicatorAggregator
689      */
690     std::vector<LabelType> lackLabels;
691     int errCode = g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback(
__anonb60f125d0a02(const LabelType &commLabel, const std::string &userId)692         [&lackLabels](const LabelType &commLabel, const std::string &userId)->int {
693             lackLabels.push_back(commLabel);
694             return E_OK;
695         }, nullptr);
696     EXPECT_EQ(errCode, E_OK);
697 
698     /**
699      * @tc.steps: step2. connect device A with device B
700      */
701     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
702     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
703 
704     /**
705      * @tc.steps: step3. device A alloc communicator AA using label A and send message to B
706      * @tc.expected: step3. device B callback that label A not found.
707      */
708     ALLOC_AND_SEND_MESSAGE(A, B, A, 100); // session id 100
709     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
710     ASSERT_EQ(lackLabels.size(), static_cast<size_t>(1));
711     EXPECT_EQ(lackLabels[0], ToLabelType(LABEL_A));
712 
713     /**
714      * @tc.steps: step4. device A alloc communicator AB using label B and send message to B
715      * @tc.expected: step4. device B callback that label B not found.
716      */
717     ALLOC_AND_SEND_MESSAGE(A, B, B, 200); // session id 200
718     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
719     ASSERT_EQ(lackLabels.size(), static_cast<size_t>(2));
720     EXPECT_EQ(lackLabels[1], ToLabelType(LABEL_B)); // 1 for second element
721 
722     /**
723      * @tc.steps: step5. device B alloc communicator BA using label A and register message callback
724      * @tc.expected: step5. communicator BA will receive message.
725      */
726     ICommunicator *commBA = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_A, errCode);
727     ASSERT_NE(commBA, nullptr);
728     REG_MESSAGE_CALLBACK(B, A);
729     commBA->Activate();
730     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
731     EXPECT_EQ(srcTargetForBA, DEVICE_NAME_A);
732     ASSERT_NE(recvMsgForBA, nullptr);
733     EXPECT_EQ(recvMsgForBA->GetSessionId(), 100U); // session id 100
734     delete recvMsgForBA;
735     recvMsgForBA = nullptr;
736 
737     /**
738      * @tc.steps: step6. device B alloc communicator BB using label B and register message callback
739      * @tc.expected: step6. communicator BB will receive message.
740      */
741     ICommunicator *commBB = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_B, errCode);
742     ASSERT_NE(commBB, nullptr);
743     REG_MESSAGE_CALLBACK(B, B);
744     commBB->Activate();
745     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
746     EXPECT_EQ(srcTargetForBB, DEVICE_NAME_A);
747     ASSERT_NE(recvMsgForBB, nullptr);
748     EXPECT_EQ(recvMsgForBB->GetSessionId(), 200U); // session id 200
749     delete recvMsgForBB;
750     recvMsgForBB = nullptr;
751 
752     // Clean up
753     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
754     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAB);
755     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBA);
756     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBB);
757     g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback(nullptr, nullptr);
758     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
759 }
760 
761 /**
762  * @tc.name: ReDeliver Message 002
763  * @tc.desc: Test CommunicatorAggregator support redeliver message by order
764  * @tc.type: FUNC
765  * @tc.require: AR000DR9KV
766  * @tc.author: xiaozhenjian
767  */
768 HWTEST_F(DistributedDBCommunicatorTest, ReDeliverMessage002, TestSize.Level1)
769 {
770     /**
771      * @tc.steps: step1. device C create CommunicatorAggregator and initialize
772      */
773     bool step1 = SetUpEnv(g_envDeviceC, DEVICE_NAME_C);
774     ASSERT_EQ(step1, true);
775 
776     /**
777      * @tc.steps: step2. device B register communicator not found callback to CommunicatorAggregator
778      */
779     int errCode = g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback([](const LabelType &commLabel,
__anonb60f125d0b02(const LabelType &commLabel, const std::string &userId)780         const std::string &userId)->int {
781         return E_OK;
782     }, nullptr);
783     EXPECT_EQ(errCode, E_OK);
784 
785     /**
786      * @tc.steps: step3. connect device A with device B, then device B with device C
787      */
788     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
789     AdapterStub::ConnectAdapterStub(g_envDeviceB.adapterHandle, g_envDeviceC.adapterHandle);
790     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
791 
792     /**
793      * @tc.steps: step4. device A alloc communicator AA using label A and send message to B
794      */
795     ALLOC_AND_SEND_MESSAGE(A, B, A, 100); // session id 100
796     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
797 
798     /**
799      * @tc.steps: step5. device C alloc communicator CA using label A and send message to B
800      */
801     ALLOC_AND_SEND_MESSAGE(C, B, A, 200); // session id 200
802     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
803     DO_SEND_MESSAGE(A, B, A, 300); // session id 300
804     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
805     DO_SEND_MESSAGE(C, B, A, 400); // session id 400
806     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
807 
808     /**
809      * @tc.steps: step6. device B alloc communicator BA using label A and register message callback
810      * @tc.expected: step6. communicator BA will receive message in order of sessionid 100, 200, 300, 400.
811      */
812     ICommunicator *commBA = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_A, errCode);
813     ASSERT_NE(commBA, nullptr);
814     std::vector<std::pair<std::string, Message *>> msgCallbackForBA;
__anonb60f125d0c02(const std::string &srcTarget, Message *inMsg) 815     commBA->RegOnMessageCallback([&msgCallbackForBA](const std::string &srcTarget, Message *inMsg) {
816         msgCallbackForBA.push_back({srcTarget, inMsg});
817     }, nullptr);
818     commBA->Activate();
819     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
820     ASSERT_EQ(msgCallbackForBA.size(), static_cast<size_t>(4)); // total 4 callback
821     EXPECT_EQ(msgCallbackForBA[0].first, DEVICE_NAME_A); // the 0 order element
822     EXPECT_EQ(msgCallbackForBA[1].first, DEVICE_NAME_C); // the 1 order element
823     EXPECT_EQ(msgCallbackForBA[2].first, DEVICE_NAME_A); // the 2 order element
824     EXPECT_EQ(msgCallbackForBA[3].first, DEVICE_NAME_C); // the 3 order element
825     for (uint32_t i = 0; i < msgCallbackForBA.size(); i++) {
826         EXPECT_EQ(msgCallbackForBA[i].second->GetSessionId(), static_cast<uint32_t>((i + 1) * 100)); // 1 sessionid 100
827         delete msgCallbackForBA[i].second;
828         msgCallbackForBA[i].second = nullptr;
829     }
830 
831     // Clean up
832     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
833     g_envDeviceC.commAggrHandle->ReleaseCommunicator(commCA);
834     g_envDeviceB.commAggrHandle->ReleaseCommunicator(commBA);
835     g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback(nullptr, nullptr);
836     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
837     AdapterStub::DisconnectAdapterStub(g_envDeviceB.adapterHandle, g_envDeviceC.adapterHandle);
838     TearDownEnv(g_envDeviceC);
839 }
840 
841 /**
842  * @tc.name: ReDeliver Message 003
843  * @tc.desc: For observe memory in unusual scenario
844  * @tc.type: FUNC
845  * @tc.require: AR000DR9KV
846  * @tc.author: xiaozhenjian
847  */
848 HWTEST_F(DistributedDBCommunicatorTest, ReDeliverMessage003, TestSize.Level2)
849 {
850     /**
851      * @tc.steps: step1. device B register communicator not found callback to CommunicatorAggregator
852      */
853     int errCode = g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback([](const LabelType &commLabel,
__anonb60f125d0d02(const LabelType &commLabel, const std::string &userId)854         const std::string &userId)->int {
855         return E_OK;
856     }, nullptr);
857     EXPECT_EQ(errCode, E_OK);
858 
859     /**
860      * @tc.steps: step2. connect device A with device B
861      */
862     AdapterStub::ConnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
863     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
864 
865     /**
866      * @tc.steps: step3. device A alloc communicator AA,AB,AC using label A,B,C
867      */
868     ICommunicator *commAA = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errCode);
869     ASSERT_NOT_NULL_AND_ACTIVATE(commAA, "");
870     ICommunicator *commAB = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_B, errCode);
871     ASSERT_NOT_NULL_AND_ACTIVATE(commAB, "");
872     ICommunicator *commAC = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_C, errCode);
873     ASSERT_NOT_NULL_AND_ACTIVATE(commAC, "");
874     std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms
875 
876     /**
877      * @tc.steps: step4. device A Continuously send tiny message to B using communicator AA,AB,AC
878      */
879     for (int turn = 0; turn < 11; turn++) { // Total 11 turns
880         DO_SEND_MESSAGE(A, B, A, 0);
881         DO_SEND_MESSAGE(A, B, B, 0);
882         DO_SEND_MESSAGE(A, B, C, 0);
883     }
884 
885     /**
886      * @tc.steps: step5. device A Continuously send giant message to B using communicator AA,AB,AC
887      */
888     for (int turn = 0; turn < 5; turn++) { // Total 5 turns
889         DO_SEND_GIANT_MESSAGE(A, B, A, (3 * 1024 * 1024)); // 3 MBytes, 1024 is scale
890         DO_SEND_GIANT_MESSAGE(A, B, B, (6 * 1024 * 1024)); // 6 MBytes, 1024 is scale
891         DO_SEND_GIANT_MESSAGE(A, B, C, (7 * 1024 * 1024)); // 7 MBytes, 1024 is scale
892     }
893     DO_SEND_GIANT_MESSAGE(A, B, A, (30 * 1024 * 1024)); // 30 MBytes, 1024 is scale
894 
895     /**
896      * @tc.steps: step6. wait a long time then send last frame
897      */
898     for (int sec = 0; sec < 15; sec++) { // Total 15 s
899         std::this_thread::sleep_for(std::chrono::seconds(1)); // Sleep 1 s
900         LOGI("[UT][Test][ReDeliverMessage003] Sleep and wait=%d.", sec);
901     }
902     DO_SEND_MESSAGE(A, B, A, 0);
903     std::this_thread::sleep_for(std::chrono::seconds(1)); // Sleep 1 s
904 
905     // Clean up
906     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
907     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAB);
908     g_envDeviceA.commAggrHandle->ReleaseCommunicator(commAC);
909     g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback(nullptr, nullptr);
910     AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle);
911 }
912 
913 namespace {
SetUpEnv(const std::string & localDev,const std::string & supportDev,const std::shared_ptr<DBStatusAdapter> & adapter,bool isSupport,EnvHandle & envDevice)914     void SetUpEnv(const std::string &localDev, const std::string &supportDev,
915         const std::shared_ptr<DBStatusAdapter> &adapter, bool isSupport, EnvHandle &envDevice)
916     {
917         std::shared_ptr<DBInfoHandleTest> handle = std::make_shared<DBInfoHandleTest>();
918         adapter->SetDBInfoHandle(handle);
919         adapter->SetRemoteOptimizeCommunication(supportDev, isSupport);
920         SetUpEnv(envDevice, localDev, adapter);
921     }
922 
InitCommunicator(DBInfo & dbInfo,EnvHandle & envDevice,OnOfflineDevice & onlineCallback,ICommunicator * & comm)923     void InitCommunicator(DBInfo &dbInfo, EnvHandle &envDevice, OnOfflineDevice &onlineCallback, ICommunicator *&comm)
924     {
925         dbInfo.userId = USER_ID;
926         dbInfo.appId = APP_ID;
927         dbInfo.storeId = STORE_ID_1;
928         dbInfo.isNeedSync = true;
929         dbInfo.syncDualTupleMode = false;
930         std::string label = DBCommon::GenerateHashLabel(dbInfo);
931         std::vector<uint8_t> commLabel(label.begin(), label.end());
932         int errorNo = E_OK;
933         comm = envDevice.commAggrHandle->AllocCommunicator(commLabel, errorNo);
934         ASSERT_NOT_NULL_AND_ACTIVATE(comm, "");
935         REG_CONNECT_CALLBACK(comm, onlineCallback);
936     }
937 }
938 
939 /**
940   * @tc.name: CommunicationOptimization001
941   * @tc.desc: Test notify with isSupport true.
942   * @tc.type: FUNC
943   * @tc.require: AR000HGD0B
944   * @tc.author: zhangqiquan
945   */
946 HWTEST_F(DistributedDBCommunicatorTest, CommunicationOptimization001, TestSize.Level3)
947 {
948     auto *pAggregator = new VirtualCommunicatorAggregator();
949     ASSERT_NE(pAggregator, nullptr);
950     const std::string deviceA = "DEVICES_A";
951     const std::string deviceB = "DEVICES_B";
952     RuntimeContext::GetInstance()->SetCommunicatorAggregator(pAggregator);
953     /**
954      * @tc.steps: step1. set up env
955      */
956     EnvHandle envDeviceA;
957     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
958     SetUpEnv(deviceA, deviceB, adapterA, true, envDeviceA);
959 
960     EnvHandle envDeviceB;
961     std::shared_ptr<DBStatusAdapter> adapterB = std::make_shared<DBStatusAdapter>();
962     SetUpEnv(deviceB, deviceA, adapterB, true, envDeviceB);
963 
964     /**
965      * @tc.steps: step2. device alloc communicator using label and register callback
966      */
967     DBInfo dbInfo;
968     ICommunicator *commAA = nullptr;
969     OnOfflineDevice onlineForAA;
970     InitCommunicator(dbInfo, envDeviceA, onlineForAA, commAA);
971 
972     ICommunicator *commBB = nullptr;
973     OnOfflineDevice onlineForBB;
974     InitCommunicator(dbInfo, envDeviceB, onlineForBB, commBB);
975 
976     /**
977      * @tc.steps: step3. connect device A with device B
978      */
979     AdapterStub::ConnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
980 
981     /**
982      * @tc.steps: step4. wait for label exchange
983      * @tc.expected: step4. both communicator has no callback;
984      */
985     std::this_thread::sleep_for(std::chrono::seconds(1));
986     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
987     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
988 
989     /**
990      * @tc.steps: step5. both notify
991      * @tc.expected: step5. both has callback;
992      */
993     RuntimeConfig::NotifyDBInfos({ deviceB }, { dbInfo });
994     adapterA->NotifyDBInfos({ deviceB }, { dbInfo });
995     adapterB->NotifyDBInfos({ deviceA }, { dbInfo });
996     std::this_thread::sleep_for(std::chrono::seconds(1));
997 
998     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(1));
999 
1000     dbInfo.isNeedSync = false;
1001     adapterA->NotifyDBInfos({ deviceB }, { dbInfo });
1002     adapterB->NotifyDBInfos({ deviceA }, { dbInfo });
1003     std::this_thread::sleep_for(std::chrono::seconds(1));
1004     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
1005 
1006     // Clean up and disconnect
1007     envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
1008     envDeviceB.commAggrHandle->ReleaseCommunicator(commBB);
1009     std::this_thread::sleep_for(std::chrono::seconds(1));
1010 
1011     AdapterStub::DisconnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1012 
1013     TearDownEnv(envDeviceA);
1014     TearDownEnv(envDeviceB);
1015     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
1016 }
1017 
1018 /**
1019   * @tc.name: CommunicationOptimization002
1020   * @tc.desc: Test notify with isSupport true and can offline by device change.
1021   * @tc.type: FUNC
1022   * @tc.require: AR000HGD0B
1023   * @tc.author: zhangqiquan
1024   */
1025 HWTEST_F(DistributedDBCommunicatorTest, CommunicationOptimization002, TestSize.Level3)
1026 {
1027     auto *pAggregator = new VirtualCommunicatorAggregator();
1028     ASSERT_NE(pAggregator, nullptr);
1029     const std::string deviceA = "DEVICES_A";
1030     const std::string deviceB = "DEVICES_B";
1031     RuntimeContext::GetInstance()->SetCommunicatorAggregator(pAggregator);
1032     /**
1033      * @tc.steps: step1. set up env
1034      */
1035     EnvHandle envDeviceA;
1036     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1037     SetUpEnv(deviceA, deviceB, adapterA, true, envDeviceA);
1038 
1039     EnvHandle envDeviceB;
1040     std::shared_ptr<DBStatusAdapter> adapterB = std::make_shared<DBStatusAdapter>();
1041     SetUpEnv(deviceB, deviceA, adapterB, true, envDeviceB);
1042 
1043     /**
1044      * @tc.steps: step2. device alloc communicator using label and register callback
1045      */
1046     DBInfo dbInfo;
1047     ICommunicator *commAA = nullptr;
1048     OnOfflineDevice onlineForAA;
1049     InitCommunicator(dbInfo, envDeviceA, onlineForAA, commAA);
1050     /**
1051      * @tc.steps: step3. connect device A with device B
1052      */
1053     AdapterStub::ConnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1054     std::this_thread::sleep_for(std::chrono::seconds(1));
1055 
1056     /**
1057      * @tc.steps: step5. A notify remote
1058      * @tc.expected: step5. A has callback;
1059      */
1060     adapterA->NotifyDBInfos({ deviceB }, { dbInfo });
1061     std::this_thread::sleep_for(std::chrono::seconds(1));
1062     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(1));
1063 
1064     AdapterStub::DisconnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1065     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
1066 
1067     // Clean up and disconnect
1068     envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
1069     std::this_thread::sleep_for(std::chrono::seconds(1));
1070 
1071     TearDownEnv(envDeviceA);
1072     TearDownEnv(envDeviceB);
1073     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
1074 }
1075 
1076 /**
1077   * @tc.name: CommunicationOptimization003
1078   * @tc.desc: Test notify with isSupport false.
1079   * @tc.type: FUNC
1080   * @tc.require: AR000HGD0B
1081   * @tc.author: zhangqiquan
1082   */
1083 HWTEST_F(DistributedDBCommunicatorTest, CommunicationOptimization003, TestSize.Level3)
1084 {
1085     auto *pAggregator = new VirtualCommunicatorAggregator();
1086     ASSERT_NE(pAggregator, nullptr);
1087     const std::string deviceA = "DEVICES_A";
1088     const std::string deviceB = "DEVICES_B";
1089     RuntimeContext::GetInstance()->SetCommunicatorAggregator(pAggregator);
1090     /**
1091      * @tc.steps: step1. set up env
1092      */
1093     EnvHandle envDeviceA;
1094     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1095     SetUpEnv(deviceA, deviceB, adapterA, false, envDeviceA);
1096     EnvHandle envDeviceB;
1097     std::shared_ptr<DBStatusAdapter> adapterB = std::make_shared<DBStatusAdapter>();
1098     SetUpEnv(deviceB, deviceA, adapterB, false, envDeviceB);
1099     /**
1100      * @tc.steps: step2. device alloc communicator using label and register callback
1101      */
1102     DBInfo dbInfo;
1103     ICommunicator *commAA = nullptr;
1104     OnOfflineDevice onlineForAA;
1105     InitCommunicator(dbInfo, envDeviceA, onlineForAA, commAA);
1106     ICommunicator *commBB = nullptr;
1107     OnOfflineDevice onlineForBB;
1108     InitCommunicator(dbInfo, envDeviceB, onlineForBB, commBB);
1109     /**
1110      * @tc.steps: step3. connect device A with device B
1111      */
1112     AdapterStub::ConnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1113     /**
1114      * @tc.steps: step4. wait for label exchange
1115      * @tc.expected: step4. both communicator has no callback;
1116      */
1117     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
1118     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
1119     /**
1120      * @tc.steps: step5. A notify remote
1121      * @tc.expected: step5. B has no callback;
1122      */
1123     adapterA->NotifyDBInfos({ deviceB }, { dbInfo });
1124     std::this_thread::sleep_for(std::chrono::seconds(1));
1125     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(0));
1126     /**
1127      * @tc.steps: step6. A notify local
1128      * @tc.expected: step6. B has no callback;
1129      */
1130     dbInfo.isNeedSync = false;
1131     onlineForAA.onlineDevices.clear();
1132     adapterA->NotifyDBInfos({ deviceA }, { dbInfo });
1133     std::this_thread::sleep_for(std::chrono::seconds(1));
1134     EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast<size_t>(0));
1135     // Clean up and disconnect
1136     envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
1137     envDeviceB.commAggrHandle->ReleaseCommunicator(commBB);
1138     std::this_thread::sleep_for(std::chrono::seconds(1));
1139     AdapterStub::DisconnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1140     TearDownEnv(envDeviceA);
1141     TearDownEnv(envDeviceB);
1142     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
1143 }
1144 
1145 /**
1146   * @tc.name: CommunicationOptimization004
1147   * @tc.desc: Test notify with isSupport false and it be will changed by communication.
1148   * @tc.type: FUNC
1149   * @tc.require: AR000HGD0B
1150   * @tc.author: zhangqiquan
1151   */
1152 HWTEST_F(DistributedDBCommunicatorTest, CommunicationOptimization004, TestSize.Level3)
1153 {
1154     const std::string deviceA = "DEVICES_A";
1155     const std::string deviceB = "DEVICES_B";
1156     /**
1157      * @tc.steps: step1. set up env
1158      */
1159     EnvHandle envDeviceA;
1160     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1161     SetUpEnv(deviceA, deviceB, adapterA, false, envDeviceA);
1162     RuntimeContext::GetInstance()->SetCommunicatorAggregator(envDeviceA.commAggrHandle);
1163     EnvHandle envDeviceB;
1164     std::shared_ptr<DBStatusAdapter> adapterB = std::make_shared<DBStatusAdapter>();
1165     SetUpEnv(deviceB, deviceA, adapterB, false, envDeviceB);
1166     /**
1167      * @tc.steps: step2. device alloc communicator using label and register callback
1168      */
1169     DBInfo dbInfo;
1170     ICommunicator *commAA = nullptr;
1171     OnOfflineDevice onlineForAA;
1172     InitCommunicator(dbInfo, envDeviceA, onlineForAA, commAA);
1173     ICommunicator *commBB = nullptr;
1174     OnOfflineDevice onlineForBB;
1175     InitCommunicator(dbInfo, envDeviceB, onlineForBB, commBB);
1176     /**
1177      * @tc.steps: step3. connect device A with device B
1178      */
1179     EXPECT_EQ(adapterA->IsSupport(deviceB), false);
1180     AdapterStub::ConnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1181     std::this_thread::sleep_for(std::chrono::seconds(1));
1182     EXPECT_EQ(adapterA->IsSupport(deviceB), true);
1183     // Clean up and disconnect
1184     envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
1185     envDeviceB.commAggrHandle->ReleaseCommunicator(commBB);
1186     std::this_thread::sleep_for(std::chrono::seconds(1));
1187     AdapterStub::DisconnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1188     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
1189     envDeviceA.commAggrHandle = nullptr;
1190     TearDownEnv(envDeviceA);
1191     TearDownEnv(envDeviceB);
1192 }
1193 
1194 /**
1195   * @tc.name: CommunicationOptimization005
1196   * @tc.desc: Test notify with isSupport false and send label exchange.
1197   * @tc.type: FUNC
1198   * @tc.require: AR000HGD0B
1199   * @tc.author: zhangqiquan
1200   */
1201 HWTEST_F(DistributedDBCommunicatorTest, CommunicationOptimization005, TestSize.Level3)
1202 {
1203     const std::string deviceA = "DEVICES_A";
1204     const std::string deviceB = "DEVICES_B";
1205     /**
1206      * @tc.steps: step1. set up env
1207      */
1208     EnvHandle envDeviceA;
1209     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1210     std::shared_ptr<DBInfoHandleTest> handle = std::make_shared<DBInfoHandleTest>();
1211     handle->SetLocalIsSupport(false);
1212     adapterA->SetDBInfoHandle(handle);
1213     SetUpEnv(envDeviceA, deviceA, adapterA);
1214     RuntimeContext::GetInstance()->SetCommunicatorAggregator(envDeviceA.commAggrHandle);
1215     EnvHandle envDeviceB;
1216     SetUpEnv(envDeviceB, deviceB, nullptr);
1217     /**
1218      * @tc.steps: step2. connect device A with device B
1219      */
1220     EXPECT_EQ(adapterA->IsSupport(deviceB), false);
1221     AdapterStub::ConnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1222     std::this_thread::sleep_for(std::chrono::seconds(1));
1223     /**
1224      * @tc.steps: step3. device alloc communicator using label and register callback
1225      */
1226     DBInfo dbInfo;
1227     ICommunicator *commAA = nullptr;
1228     OnOfflineDevice onlineForAA;
1229     InitCommunicator(dbInfo, envDeviceA, onlineForAA, commAA);
1230     ICommunicator *commBB = nullptr;
1231     OnOfflineDevice onlineForBB;
1232     InitCommunicator(dbInfo, envDeviceB, onlineForBB, commBB);
1233     std::this_thread::sleep_for(std::chrono::seconds(1));
1234     EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast<size_t>(1));
1235 
1236     // Clean up and disconnect
1237     envDeviceA.commAggrHandle->ReleaseCommunicator(commAA);
1238     envDeviceB.commAggrHandle->ReleaseCommunicator(commBB);
1239     std::this_thread::sleep_for(std::chrono::seconds(1));
1240     AdapterStub::DisconnectAdapterStub(envDeviceA.adapterHandle, envDeviceB.adapterHandle);
1241     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
1242     envDeviceA.commAggrHandle = nullptr;
1243     TearDownEnv(envDeviceA);
1244     TearDownEnv(envDeviceB);
1245 }
1246 
1247 /**
1248   * @tc.name: DbStatusAdapter001
1249   * @tc.desc: Test notify with isSupport false.
1250   * @tc.type: FUNC
1251   * @tc.require: AR000HGD0B
1252   * @tc.author: zhangqiquan
1253   */
1254 HWTEST_F(DistributedDBCommunicatorTest, DbStatusAdapter001, TestSize.Level1)
1255 {
1256     auto *pAggregator = new VirtualCommunicatorAggregator();
1257     ASSERT_NE(pAggregator, nullptr);
1258     const std::string deviceA = "DEVICES_A";
1259     const std::string deviceB = "DEVICES_B";
1260     RuntimeContext::GetInstance()->SetCommunicatorAggregator(pAggregator);
1261 
1262     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1263     std::shared_ptr<DBInfoHandleTest> handle = std::make_shared<DBInfoHandleTest>();
1264     adapterA->SetDBInfoHandle(handle);
1265     adapterA->SetRemoteOptimizeCommunication(deviceB, true);
1266     std::string actualRemoteDevInfo;
1267     size_t remoteInfoCount = 0u;
1268     size_t localCount = 0u;
1269     DBInfo dbInfo;
1270     adapterA->NotifyDBInfos({ deviceA }, { dbInfo });
1271 
1272     dbInfo = {
1273         USER_ID,
1274         APP_ID,
1275         STORE_ID_1,
1276         true,
1277         false
1278     };
1279     adapterA->NotifyDBInfos({ deviceA }, { dbInfo });
1280     dbInfo.isNeedSync = false;
1281     adapterA->NotifyDBInfos({ deviceA }, { dbInfo });
1282     adapterA->NotifyDBInfos({ deviceB }, { dbInfo });
1283 
1284     size_t remoteChangeCount = 0u;
1285     adapterA->SetDBStatusChangeCallback(
__anonb60f125d0f02(const std::string &devInfo, const std::vector<DBInfo> &dbInfos) 1286         [&actualRemoteDevInfo, &remoteInfoCount](const std::string &devInfo, const std::vector<DBInfo> &dbInfos) {
1287             actualRemoteDevInfo = devInfo;
1288             remoteInfoCount = dbInfos.size();
1289         },
__anonb60f125d1002() 1290         [&localCount]() {
1291             localCount++;
1292         },
__anonb60f125d1102(const std::string &dev) 1293         [&remoteChangeCount, deviceB](const std::string &dev) {
1294             remoteChangeCount++;
1295             EXPECT_EQ(dev, deviceB);
1296         });
1297     std::this_thread::sleep_for(std::chrono::seconds(1));
1298     EXPECT_EQ(actualRemoteDevInfo, deviceB);
1299     EXPECT_EQ(remoteInfoCount, 1u);
1300     adapterA->SetRemoteOptimizeCommunication(deviceB, false);
1301     std::this_thread::sleep_for(std::chrono::seconds(1));
1302     EXPECT_EQ(remoteChangeCount, 1u);
1303     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
1304 }
1305 
1306 /**
1307   * @tc.name: DbStatusAdapter002
1308   * @tc.desc: Test adapter clear cache.
1309   * @tc.type: FUNC
1310   * @tc.require: AR000HGD0B
1311   * @tc.author: zhangqiquan
1312   */
1313 HWTEST_F(DistributedDBCommunicatorTest, DbStatusAdapter002, TestSize.Level1) {
1314     const std::string deviceB = "DEVICES_B";
1315     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1316     std::shared_ptr<DBInfoHandleTest> handle = std::make_shared<DBInfoHandleTest>();
1317     adapterA->SetDBInfoHandle(handle);
1318     adapterA->SetRemoteOptimizeCommunication(deviceB, true);
1319     EXPECT_TRUE(adapterA->IsSupport(deviceB));
1320     adapterA->SetDBInfoHandle(handle);
1321     adapterA->SetRemoteOptimizeCommunication(deviceB, false);
1322     EXPECT_FALSE(adapterA->IsSupport(deviceB));
1323 }
1324 
1325 /**
1326   * @tc.name: DbStatusAdapter003
1327   * @tc.desc: Test adapter get local dbInfo.
1328   * @tc.type: FUNC
1329   * @tc.require: AR000HGD0B
1330   * @tc.author: zhangqiquan
1331   */
1332 HWTEST_F(DistributedDBCommunicatorTest, DbStatusAdapter003, TestSize.Level1) {
1333     const std::string deviceB = "DEVICES_B";
1334     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1335     std::shared_ptr<DBInfoHandleTest> handle = std::make_shared<DBInfoHandleTest>();
1336     adapterA->SetDBInfoHandle(handle);
1337     handle->SetLocalIsSupport(true);
1338     std::vector<DBInfo> dbInfos;
1339     EXPECT_EQ(adapterA->GetLocalDBInfos(dbInfos), E_OK);
1340     handle->SetLocalIsSupport(false);
1341     EXPECT_EQ(adapterA->GetLocalDBInfos(dbInfos), E_OK);
1342     adapterA->SetDBInfoHandle(handle);
1343     EXPECT_EQ(adapterA->GetLocalDBInfos(dbInfos), -E_NOT_SUPPORT);
1344 }
1345 
1346 /**
1347   * @tc.name: DbStatusAdapter004
1348   * @tc.desc: Test adapter clear cache will get callback.
1349   * @tc.type: FUNC
1350   * @tc.require: AR000HGD0B
1351   * @tc.author: zhangqiquan
1352   */
1353 HWTEST_F(DistributedDBCommunicatorTest, DbStatusAdapter004, TestSize.Level1)
1354 {
1355     const std::string deviceB = "DEVICES_B";
1356     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1357     std::shared_ptr<DBInfoHandleTest> handle = std::make_shared<DBInfoHandleTest>();
1358     adapterA->SetDBInfoHandle(handle);
1359     handle->SetLocalIsSupport(true);
1360     std::vector<DBInfo> dbInfos;
1361     DBInfo dbInfo = {
1362         USER_ID,
1363         APP_ID,
1364         STORE_ID_1,
1365         true,
1366         true
1367     };
1368     dbInfos.push_back(dbInfo);
1369     dbInfo.storeId = STORE_ID_2;
1370     dbInfos.push_back(dbInfo);
1371     dbInfo.storeId = STORE_ID_3;
1372     dbInfo.isNeedSync = false;
1373     dbInfos.push_back(dbInfo);
1374     adapterA->NotifyDBInfos({"dev"}, dbInfos);
1375     std::this_thread::sleep_for(std::chrono::seconds(1));
1376     size_t notifyCount = 0u;
1377     adapterA->SetDBStatusChangeCallback([&notifyCount](const std::string &devInfo,
__anonb60f125d1202(const std::string &devInfo, const std::vector<DBInfo> &dbInfos) 1378         const std::vector<DBInfo> &dbInfos) {
1379         LOGD("on callback");
1380         for (const auto &dbInfo: dbInfos) {
1381             EXPECT_FALSE(dbInfo.isNeedSync);
1382         }
1383         notifyCount = dbInfos.size();
1384     }, nullptr, nullptr);
1385     adapterA->SetDBInfoHandle(nullptr);
1386     EXPECT_EQ(notifyCount, 2u); // 2 dbInfo is need sync now it should be offline
1387 }
1388 
1389 /**
1390   * @tc.name: DbStatusAdapter005
1391   * @tc.desc: Test adapter is need auto sync.
1392   * @tc.type: FUNC
1393   * @tc.require: AR000HGD0B
1394   * @tc.author: zhangqiquan
1395   */
1396 HWTEST_F(DistributedDBCommunicatorTest, DbStatusAdapter005, TestSize.Level1)
1397 {
1398     const std::string deviceB = "DEVICES_B";
1399     std::shared_ptr<DBStatusAdapter> adapterA = std::make_shared<DBStatusAdapter>();
1400     std::shared_ptr<DBInfoHandleTest> handle = std::make_shared<DBInfoHandleTest>();
1401     adapterA->SetDBInfoHandle(handle);
1402     handle->SetLocalIsSupport(true);
1403     EXPECT_EQ(adapterA->IsNeedAutoSync(USER_ID, APP_ID, STORE_ID_1, deviceB), true);
1404     handle->SetNeedAutoSync(false);
1405     EXPECT_EQ(adapterA->IsNeedAutoSync(USER_ID, APP_ID, STORE_ID_1, deviceB), false);
1406     handle->SetLocalIsSupport(false);
1407     EXPECT_EQ(adapterA->IsNeedAutoSync(USER_ID, APP_ID, STORE_ID_1, deviceB), false);
1408     adapterA->SetDBInfoHandle(handle);
1409     EXPECT_EQ(adapterA->IsNeedAutoSync(USER_ID, APP_ID, STORE_ID_1, deviceB), true);
1410     adapterA->SetDBInfoHandle(nullptr);
1411     EXPECT_EQ(adapterA->IsNeedAutoSync(USER_ID, APP_ID, STORE_ID_1, deviceB), true);
1412 }
1413 }