1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "distributeddb_data_generate_unit_test.h"
19 #include "distributeddb_tools_unit_test.h"
20 #include "isyncer.h"
21 #include "single_ver_sync_state_machine.h"
22 #include "single_ver_kv_sync_task_context.h"
23 #include "sync_types.h"
24 #include "version.h"
25 #include "virtual_single_ver_sync_db_Interface.h"
26 #include "virtual_time_sync_communicator.h"
27
28 using namespace std;
29 using namespace testing::ext;
30 using namespace DistributedDB;
31
32 namespace {
33 const std::string DEVICE_A = "deviceA";
34 const std::string DEVICE_B = "deviceB";
35 VirtualTimeSyncCommunicator *g_virtualCommunicator = nullptr;
36 std::shared_ptr<TimeSync> g_timeSyncA = nullptr;
37 std::shared_ptr<TimeSync> g_timeSyncB = nullptr;
38 VirtualSingleVerSyncDBInterface *g_syncInterfaceA = nullptr;
39 VirtualSingleVerSyncDBInterface *g_syncInterfaceB = nullptr;
40 std::shared_ptr<Metadata> g_metadataA = nullptr;
41 std::shared_ptr<Metadata> g_metadataB = nullptr;
42 SingleVerSyncTaskContext *g_syncTaskContext = nullptr;
43 const int NETWORK_DELAY = 100 * 1000; // 100ms
44 }
45
46 class DistributedDBTimeSyncTest : public testing::Test {
47 public:
48 static void SetUpTestCase(void);
49 static void TearDownTestCase(void);
50 void SetUp();
51 void TearDown();
52 };
53
SetUpTestCase(void)54 void DistributedDBTimeSyncTest::SetUpTestCase(void)
55 {
56 /**
57 * @tc.setup: NA
58 */
59 }
60
TearDownTestCase(void)61 void DistributedDBTimeSyncTest::TearDownTestCase(void)
62 {
63 /**
64 * @tc.teardown: NA
65 */
66 }
67
SetUp(void)68 void DistributedDBTimeSyncTest::SetUp(void)
69 {
70 DistributedDBUnitTest::DistributedDBToolsUnitTest::PrintTestCaseInfo();
71 /**
72 * @tc.setup: create the instance for virtual communicator, virtual storage component and time syncer
73 */
74 g_virtualCommunicator = new (std::nothrow) VirtualTimeSyncCommunicator();
75 ASSERT_TRUE(g_virtualCommunicator != nullptr);
76
77 g_syncInterfaceA = new (std::nothrow) VirtualSingleVerSyncDBInterface();
78 ASSERT_TRUE(g_syncInterfaceA != nullptr);
79
80 g_metadataA = std::make_shared<Metadata>();
81
82 g_syncInterfaceB = new (std::nothrow) VirtualSingleVerSyncDBInterface;
83 ASSERT_TRUE(g_syncInterfaceB != nullptr);
84
85 g_metadataB = std::make_shared<Metadata>();
86
87 g_timeSyncA = std::make_shared<TimeSync>();
88 ASSERT_TRUE(g_timeSyncA != nullptr);
89
90 g_timeSyncB = std::make_shared<TimeSync>();
91 ASSERT_TRUE(g_timeSyncB != nullptr);
92
93 g_syncTaskContext = new (std::nothrow) SingleVerKvSyncTaskContext();
94 ASSERT_TRUE(g_syncTaskContext != nullptr);
95 }
96
TearDown(void)97 void DistributedDBTimeSyncTest::TearDown(void)
98 {
99 /**
100 * @tc.teardown: delete the ptr for testing
101 */
102 if (g_syncTaskContext != nullptr) {
103 RefObject::DecObjRef(g_syncTaskContext);
104 g_syncTaskContext = nullptr;
105 }
106 if (g_syncInterfaceA != nullptr) {
107 delete g_syncInterfaceA;
108 g_syncInterfaceA = nullptr;
109 }
110 if (g_syncInterfaceB != nullptr) {
111 delete g_syncInterfaceB;
112 g_syncInterfaceB = nullptr;
113 }
114
115 g_metadataA = nullptr;
116 g_metadataB = nullptr;
117 if (g_timeSyncA != nullptr) {
118 g_timeSyncA = nullptr;
119 }
120 if (g_timeSyncB != nullptr) {
121 g_timeSyncB = nullptr;
122 }
123 if (g_virtualCommunicator != nullptr) {
124 RefObject::DecObjRef(g_virtualCommunicator);
125 g_virtualCommunicator = nullptr;
126 }
127 }
128
129 /**
130 * @tc.name: NormalSync001
131 * @tc.desc: Verify time sync function is normal between two time sync instance with different timestamp.
132 * @tc.type: FUNC
133 * @tc.require:
134 * @tc.author: wumin
135 */
136 HWTEST_F(DistributedDBTimeSyncTest, NormalSync001, TestSize.Level0)
137 {
138 /**
139 * @tc.steps: step1. Initialize the time sync A and B
140 * @tc.steps: step2. Write the timestamp into virtual storage component
141 * @tc.expected: step1. Initialize time sync A and B successfully
142 * @tc.expected: step2. Write the timestamp into virtual storage component successfully.
143 */
144 g_metadataA->Initialize(g_syncInterfaceA);
145 TimeOffset offsetA = 100 * 1000 * 1000; // 100 seconds
146 // set timestamp for A virtual storage component
147 g_syncInterfaceA->PutData(DistributedDBUnitTest::KEY_1, DistributedDBUnitTest::VALUE_1,
148 TimeHelper::GetSysCurrentTime() + TimeHelper::BASE_OFFSET + offsetA, 0);
149 int errCode;
150 // initialize timeSyncA
151 errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
152 EXPECT_TRUE(errCode == E_OK);
153
154 g_metadataB->Initialize(g_syncInterfaceB);
155 TimeOffset offsetB = 200 * 1000 * 1000; // 200 seconds
156 // set timestamp for B virtual storage component
157 g_syncInterfaceB->PutData(DistributedDBUnitTest::KEY_1, DistributedDBUnitTest::VALUE_1,
158 TimeHelper::GetSysCurrentTime() + TimeHelper::BASE_OFFSET + offsetB, 0);
159 // initialize timeSyncB
160 errCode = g_timeSyncB->Initialize(g_virtualCommunicator, g_metadataB, g_syncInterfaceB, DEVICE_A, "");
161 EXPECT_TRUE(errCode == E_OK);
162
163 /**
164 * @tc.steps: step3. Register the OnMessageCallback to virtual communicator
165 */
166 g_syncTaskContext->Initialize({DEVICE_B, ""}, g_syncInterfaceA, g_metadataA, g_virtualCommunicator);
167 g_virtualCommunicator->SetTimeSync(g_timeSyncA.get(), g_timeSyncB.get(), DEVICE_A, g_syncTaskContext);
168
169 /**
170 * @tc.steps: step4. Fetch timeOffset value
171 * @tc.expected: step4. (offsetB - offsetA ) - timeOffset < 100ms.
172 */
173 TimeOffset timeOffset = 0;
174 g_timeSyncA->GetTimeOffset(timeOffset, TIME_SYNC_WAIT_TIME);
175 offsetB = g_metadataB->GetLocalTimeOffset();
176 offsetA = g_metadataA->GetLocalTimeOffset();
177 EXPECT_TRUE(abs(offsetB - offsetA - timeOffset) < NETWORK_DELAY);
178 }
179
180 /**
181 * @tc.name: NormalSync002
182 * @tc.desc: Verify time sync function is normal between two time sync instance with the same timestamp.
183 * @tc.type: FUNC
184 * @tc.require:
185 * @tc.author: wumin
186 */
187 HWTEST_F(DistributedDBTimeSyncTest, NormalSync002, TestSize.Level0)
188 {
189 /**
190 * @tc.steps: step1. Initialize the time sync A and B
191 * @tc.expected: step1. Initialize time sync A and B successfully
192 */
193 g_metadataA->Initialize(g_syncInterfaceA);
194 int errCode;
195 // initialize timeSyncA
196 errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
197 EXPECT_TRUE(errCode == E_OK);
198
199 g_metadataB->Initialize(g_syncInterfaceB);
200 // initialize timeSyncB
201 errCode = g_timeSyncB->Initialize(g_virtualCommunicator, g_metadataB, g_syncInterfaceB, DEVICE_A, "");
202 EXPECT_TRUE(errCode == E_OK);
203 /**
204 * @tc.steps: step2. Register the OnMessageCallback to virtual communicator
205 */
206 g_syncTaskContext->Initialize({DEVICE_B, ""}, g_syncInterfaceA, g_metadataA, g_virtualCommunicator);
207 g_virtualCommunicator->SetTimeSync(g_timeSyncA.get(), g_timeSyncB.get(), DEVICE_A, g_syncTaskContext);
208 /**
209 * @tc.steps: step3. Fetch timeOffset value
210 * @tc.expected: step3. (offsetB - offsetA ) - timeOffset < 100ms.
211 */
212 TimeOffset timeOffset;
213 g_timeSyncA->GetTimeOffset(timeOffset, TIME_SYNC_WAIT_TIME);
214 TimeOffset offsetB = g_metadataB->GetLocalTimeOffset();
215 TimeOffset offsetA = g_metadataA->GetLocalTimeOffset();
216 EXPECT_TRUE(abs(offsetB - offsetA - timeOffset) < NETWORK_DELAY);
217 }
218
219 /**
220 * @tc.name: NormalSync003
221 * @tc.desc: Verify time sync function is normal between two time sync instance with different localTimeOffset.
222 * @tc.type: FUNC
223 * @tc.require:
224 * @tc.author: wumin
225 */
226 HWTEST_F(DistributedDBTimeSyncTest, NormalSync003, TestSize.Level0)
227 {
228 /**
229 * @tc.steps: step1. Initialize the time sync A and B
230 * @tc.steps: step2. Write the timeOffset into time sync A and B
231 * @tc.expected: step1. Initialize time sync A and B successfully
232 * @tc.expected: step2. Write the timeOffset into time sync A and B successfully.
233 */
234 g_metadataA->Initialize(g_syncInterfaceA);
235
236 // set timeOffset for timeSyncA
237 TimeOffset offsetA = 1;
238 g_metadataA->SaveLocalTimeOffset(offsetA);
239
240 int errCode;
241 // initialize timeSyncA
242 errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
243 EXPECT_TRUE(errCode == E_OK);
244
245 // set timeOffset for timeSyncA
246 g_metadataB->Initialize(g_syncInterfaceB);
247 TimeOffset offsetB = 100 * 1000 * 1000;
248 g_metadataB->SaveLocalTimeOffset(offsetB);
249
250 // initialize timeSyncB
251 errCode = g_timeSyncB->Initialize(g_virtualCommunicator, g_metadataB, g_syncInterfaceB, DEVICE_A, "");
252 EXPECT_TRUE(errCode == E_OK);
253 /**
254 * @tc.steps: step3. Register the OnMessageCallback to virtual communicator
255 */
256 g_syncTaskContext->Initialize({DEVICE_B, ""}, g_syncInterfaceA, g_metadataA, g_virtualCommunicator);
257 g_virtualCommunicator->SetTimeSync(g_timeSyncA.get(), g_timeSyncB.get(), DEVICE_A, g_syncTaskContext);
258 /**
259 * @tc.steps: step4. Fetch timeOffset value
260 * @tc.expected: step4. (offsetB - offsetA ) - timeOffset < 100ms.
261 */
262 TimeOffset timeOffset = 0;
263 g_timeSyncA->GetTimeOffset(timeOffset, TIME_SYNC_WAIT_TIME);
264
265 TimeOffset absTimeOffset = abs(timeOffset);
266 EXPECT_TRUE(abs(offsetB - offsetA - absTimeOffset) < NETWORK_DELAY);
267 }
268
269 /**
270 * @tc.name: NetDisconnetSyncTest001
271 * @tc.desc: Verify time sync function return failed when the virtual communicator disabled.
272 * @tc.type: FUNC
273 * @tc.require:
274 * @tc.author: wumin
275 */
276 HWTEST_F(DistributedDBTimeSyncTest, NetDisconnetSyncTest001, TestSize.Level0)
277 {
278 /**
279 * @tc.steps: step1. Initialize the time sync A and B
280 * @tc.expected: step1. Initialize time sync A and B successfully
281 */
282 g_metadataA->Initialize(g_syncInterfaceA);
283 int errCode;
284 // initialize timeSyncA
285 errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
286 EXPECT_TRUE(errCode == E_OK);
287
288 g_metadataB->Initialize(g_syncInterfaceB);
289 // initialize timeSyncB
290 errCode = g_timeSyncB->Initialize(g_virtualCommunicator, g_metadataB, g_syncInterfaceB, DEVICE_A, "");
291 EXPECT_TRUE(errCode == E_OK);
292
293 g_syncTaskContext->Initialize({DEVICE_B, ""}, g_syncInterfaceA, g_metadataA, g_virtualCommunicator);
294 g_virtualCommunicator->SetTimeSync(g_timeSyncA.get(), g_timeSyncB.get(), DEVICE_A, g_syncTaskContext);
295 /**
296 * @tc.steps: step2. Disable the virtual communicator
297 */
298 g_virtualCommunicator->Disable();
299 /**
300 * @tc.steps: step3. Start time sync function
301 * @tc.expected: step3. time sync return -E_PERIPHERAL_INTERFACE_FAIL
302 */
303 errCode = g_timeSyncA->SyncStart();
304 EXPECT_TRUE(errCode == -E_PERIPHERAL_INTERFACE_FAIL);
305 }
306
307 /**
308 * @tc.name: InvalidMessgeTest001
309 * @tc.desc: Verify RequestReceive() return failed with invalid input.
310 * @tc.type: FUNC
311 * @tc.require:
312 * @tc.author: wumin
313 */
314 HWTEST_F(DistributedDBTimeSyncTest, InvalidMessgeTest001, TestSize.Level0)
315 {
316 /**
317 * @tc.steps: step1. Initialize the time sync A and B
318 * @tc.expected: step1. Initialize time sync A and B successfully
319 */
320 g_metadataA->Initialize(g_syncInterfaceA);
321 int errCode;
322 // initialize timeSyncA
323 errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
324 EXPECT_TRUE(errCode == E_OK);
325
326 g_metadataB->Initialize(g_syncInterfaceB);
327 // initialize timeSyncB
328 errCode = g_timeSyncB->Initialize(g_virtualCommunicator, g_metadataB, g_syncInterfaceB, DEVICE_A, "");
329 EXPECT_TRUE(errCode == E_OK);
330
331 g_virtualCommunicator->SetTimeSync(g_timeSyncA.get(), g_timeSyncB.get(), DEVICE_A, g_syncTaskContext);
332
333 Message *msg = new (std::nothrow) Message();
334 ASSERT_TRUE(msg != nullptr);
335
336 /**
337 * @tc.steps: step2. SendMessage with id = TIME_SYNC_MESSAGE, type = TYPE_REQUEST and no data set
338 * @tc.expected: step2. RequestRecv() return -E_INVALID_ARGS
339 */
340 msg->SetMessageId(TIME_SYNC_MESSAGE);
341 msg->SetMessageType(TYPE_REQUEST);
342 SendConfig conf = {false, false, true, 0};
343 errCode = g_virtualCommunicator->SendMessage(DEVICE_B, msg, conf);
344 EXPECT_TRUE(errCode == -E_INVALID_ARGS);
345
346 TimeSyncPacket data;
347 data.SetSourceTimeBegin(0);
348 data.SetSourceTimeEnd(0);
349 data.SetTargetTimeBegin(0);
350 data.SetTargetTimeEnd(0);
351 /**
352 * @tc.steps: step3. SendMessage with id = DATA_SYNC_MESSAGE, type = TYPE_REQUEST
353 * @tc.expected: step3. RequestRecv() return -E_INVALID_ARGS
354 */
355 msg = new (std::nothrow) Message();
356 ASSERT_TRUE(msg != nullptr);
357 msg->SetMessageId(DATA_SYNC_MESSAGE);
358 msg->SetMessageType(TYPE_REQUEST);
359 msg->SetCopiedObject<>(data);
360 errCode = g_virtualCommunicator->SendMessage(DEVICE_B, msg, conf);
361 EXPECT_TRUE(errCode == -E_INVALID_ARGS);
362 /**
363 * @tc.steps: step4. SendMessage with id = TIME_SYNC_MESSAGE, type = TYPE_RESPONSE
364 * @tc.expected: step4. RequestRecv() return -E_INVALID_ARGS
365 */
366 msg = new (std::nothrow) Message();
367 ASSERT_TRUE(msg != nullptr);
368 msg->SetMessageId(TIME_SYNC_MESSAGE);
369 msg->SetMessageType(TYPE_RESPONSE);
370 msg->SetCopiedObject<>(data);
371 errCode = g_virtualCommunicator->SendMessage(DEVICE_B, msg, conf);
372 EXPECT_TRUE(errCode == -E_INVALID_ARGS);
373 }
374
375 /**
376 * @tc.name: InvalidMessgeTest002
377 * @tc.desc: Verify AckRec() return failed with invalid input.
378 * @tc.type: FUNC
379 * @tc.require:
380 * @tc.author: wumin
381 */
382 HWTEST_F(DistributedDBTimeSyncTest, InvalidMessgeTest002, TestSize.Level0)
383 {
384 /**
385 * @tc.steps: step1. Initialize the time sync A and B
386 * @tc.expected: step1. Initialize time sync A and B successfully
387 */
388 g_metadataA->Initialize(g_syncInterfaceA);
389 int errCode;
390 // initialize timeSyncA
391 errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
392 EXPECT_TRUE(errCode == E_OK);
393
394 g_metadataB->Initialize(g_syncInterfaceB);
395 // initialize timeSyncB
396 errCode = g_timeSyncB->Initialize(g_virtualCommunicator, g_metadataB, g_syncInterfaceB, DEVICE_A, "");
397 EXPECT_TRUE(errCode == E_OK);
398 g_syncTaskContext->Initialize({DEVICE_B, ""}, g_syncInterfaceA, g_metadataA, g_virtualCommunicator);
399 g_virtualCommunicator->SetTimeSync(g_timeSyncA.get(), g_timeSyncB.get(), DEVICE_A, g_syncTaskContext);
400
401 Message *msg = new (std::nothrow) Message();
402 ASSERT_TRUE(msg != nullptr);
403
404 /**
405 * @tc.steps: step2. SendMessage with id = TIME_SYNC_MESSAGE, type = TYPE_RESPONSE and no data set
406 * @tc.expected: step2. AckRecv() return -E_INVALID_ARGS
407 */
408 msg->SetMessageId(TIME_SYNC_MESSAGE);
409 msg->SetMessageType(TYPE_RESPONSE);
410 SendConfig conf = {false, false, true, 0};
411 errCode = g_virtualCommunicator->SendMessage(DEVICE_A, msg, conf);
412 EXPECT_TRUE(errCode == -E_INVALID_ARGS);
413
414 TimeSyncPacket data;
415 data.SetSourceTimeBegin(0);
416 data.SetSourceTimeEnd(0);
417 data.SetTargetTimeBegin(0);
418 data.SetTargetTimeEnd(0);
419 /**
420 * @tc.steps: step3. SendMessage with id = DATA_SYNC_MESSAGE, type = TYPE_RESPONSE and no data set
421 * @tc.expected: step3. AckRecv() return -E_INVALID_ARGS
422 */
423 msg = new (std::nothrow) Message();
424 ASSERT_TRUE(msg != nullptr);
425 msg->SetMessageId(DATA_SYNC_MESSAGE);
426 msg->SetMessageType(TYPE_RESPONSE);
427 msg->SetCopiedObject<>(data);
428 errCode = g_virtualCommunicator->SendMessage(DEVICE_A, msg, conf);
429 EXPECT_TRUE(errCode == -E_INVALID_ARGS);
430 /**
431 * @tc.steps: step4. SendMessage with id = TIME_SYNC_MESSAGE, type = TYPE_REQUEST and no data set
432 * @tc.expected: step4. AckRecv() return -E_INVALID_ARGS
433 */
434 msg = new (std::nothrow) Message();
435 ASSERT_TRUE(msg != nullptr);
436 msg->SetMessageId(TIME_SYNC_MESSAGE);
437 msg->SetMessageType(TYPE_REQUEST);
438 msg->SetCopiedObject<>(data);
439 errCode = g_virtualCommunicator->SendMessage(DEVICE_A, msg, conf);
440 EXPECT_TRUE(errCode == -E_INVALID_ARGS);
441 }
442
443 /**
444 * @tc.name: SyncTimeout001
445 * @tc.desc: Verify the timeout scenario for time sync.
446 * @tc.type: FUNC
447 * @tc.require:
448 * @tc.author: wumin
449 */
450 HWTEST_F(DistributedDBTimeSyncTest, SyncTimeout001, TestSize.Level2)
451 {
452 // initialize timeSyncA
453 g_metadataA->Initialize(g_syncInterfaceA);
454 int errCode;
455 errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
456 EXPECT_TRUE(errCode == E_OK);
457
458 /**
459 * @tc.steps: step1. Initialize the syncTaskContext
460 * @tc.expected: step1. Initialize syncTaskContext successfully
461 */
462 errCode = g_syncTaskContext->Initialize({DEVICE_B, ""}, g_syncInterfaceA, g_metadataA, g_virtualCommunicator);
463 EXPECT_TRUE(errCode == E_OK);
464 /**
465 * @tc.steps: step2. Start the time syc task invoking StartSync() method
466 * @tc.expected: step2. Start the time sync task return E_TIMEOUT
467 */
468 TimeOffset offset;
469 errCode = g_timeSyncA->GetTimeOffset(offset, TIME_SYNC_WAIT_TIME);
470 EXPECT_TRUE(errCode == -E_TIMEOUT);
471 }
472
473 /**
474 * @tc.name: CheckRemoteVersion001
475 * @tc.desc: Verify the timeout scenario for time sync.
476 * @tc.type: FUNC
477 * @tc.require:
478 * @tc.author: zhangqiquan
479 */
480 HWTEST_F(DistributedDBTimeSyncTest, CheckRemoteVersion001, TestSize.Level0)
481 {
482 // initialize timeSyncA
483 g_metadataA->Initialize(g_syncInterfaceA);
484 int errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
485 EXPECT_EQ(errCode, E_OK);
486
487 /**
488 * @tc.steps: step1. Initialize the syncTaskContext
489 * @tc.expected: step1. Initialize syncTaskContext successfully
490 */
491 errCode = g_syncTaskContext->Initialize({DEVICE_B, ""}, g_syncInterfaceA, g_metadataA, g_virtualCommunicator);
492 EXPECT_EQ(errCode, E_OK);
493 /**
494 * @tc.steps: step2. Check remote version
495 */
496 EXPECT_TRUE(g_timeSyncA->IsRemoteLowVersion(SOFTWARE_VERSION_RELEASE_9_0));
497 g_virtualCommunicator->SetRemoteVersion(SOFTWARE_VERSION_RELEASE_9_0);
498 EXPECT_FALSE(g_timeSyncA->IsRemoteLowVersion(SOFTWARE_VERSION_RELEASE_9_0));
499 }
500
501 /**
502 * @tc.name: SetTimeSyncFinish001
503 * @tc.desc: Verify set time sync finish won't write into db when cache is finish.
504 * @tc.type: FUNC
505 * @tc.require:
506 * @tc.author: zhangqiquan
507 */
508 HWTEST_F(DistributedDBTimeSyncTest, SetTimeSyncFinish001, TestSize.Level0)
509 {
510 /**
511 * @tc.steps: step1. Initialize the metadata and time sync
512 * @tc.expected: step1. Initialize successfully
513 */
514 EXPECT_EQ(g_metadataA->Initialize(g_syncInterfaceA), E_OK);
515 EXPECT_EQ(g_metadataA->SetTimeSyncFinishMark(DEVICE_B, "", true), E_OK);
516 int errCode = g_timeSyncA->Initialize(g_virtualCommunicator, g_metadataA, g_syncInterfaceA, DEVICE_B, "");
517 EXPECT_EQ(errCode, E_OK);
518 /**
519 * @tc.steps: step2. Set time sync finish
520 * @tc.expected: step2. meta is not finish because time sync cache is finish
521 */
522 EXPECT_EQ(g_metadataA->SetTimeSyncFinishMark(DEVICE_B, "", false), E_OK);
523 DeviceTimeInfo info;
524 RuntimeContext::GetInstance()->SetDeviceTimeInfo(DEVICE_B, info);
525 g_timeSyncA->SetTimeSyncFinishIfNeed();
526 EXPECT_FALSE(g_metadataA->IsTimeSyncFinish(DEVICE_B, ""));
527 RuntimeContext::GetInstance()->ClearAllDeviceTimeInfo();
528 }
529
530 /**
531 * @tc.name: TimeHelper001
532 * @tc.desc: Verify init time helper will not record offset into db.
533 * @tc.type: FUNC
534 * @tc.require:
535 * @tc.author: zhangqiquan
536 */
537 HWTEST_F(DistributedDBTimeSyncTest, TimeHelper001, TestSize.Level0)
538 {
539 /**
540 * @tc.steps: step1. Initialize the metadata
541 * @tc.expected: step1. Initialize successfully
542 */
543 EXPECT_EQ(g_metadataA->Initialize(g_syncInterfaceA), E_OK);
544 /**
545 * @tc.steps: step2. Record INT64_MAX as timestamp and init time helper
546 * @tc.expected: step2. Init without recording offset into db
547 */
548 ASSERT_EQ(g_syncInterfaceA->PutData({'k'}, {'v'}, INT64_MAX, 0), E_OK);
549 std::string keyStr(DBConstant::LOCALTIME_OFFSET_KEY);
550 Key key(keyStr.begin(), keyStr.end());
551 Value before;
552 g_syncInterfaceA->GetMetaData(key, before);
553 TimeHelper timeHelper;
554 EXPECT_EQ(timeHelper.Initialize(g_syncInterfaceA, g_metadataA), E_OK);
555 Value after;
556 g_syncInterfaceA->GetMetaData(key, after);
557 EXPECT_EQ(after, before);
558 }