1 /*
2 * Copyright (c) 2025 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 "kv_general_ut.h"
17
18 namespace DistributedDB {
19 using namespace testing::ext;
20 using namespace DistributedDB;
21 using namespace DistributedDBUnitTest;
22
23 class DistributedDBKVCompressTest : public KVGeneralUt {
24 public:
25 void SetUp() override;
26 protected:
27 void PrepareEnv(bool isNeedCompressOnSync);
28 void TriggerPutAndSync();
29 static constexpr const char *DEVICE_A = "DEVICE_A";
30 static constexpr const char *DEVICE_B = "DEVICE_B";
31 };
32
SetUp()33 void DistributedDBKVCompressTest::SetUp()
34 {
35 KVGeneralUt::SetUp();
36 }
37
PrepareEnv(bool isNeedCompressOnSync)38 void DistributedDBKVCompressTest::PrepareEnv(bool isNeedCompressOnSync)
39 {
40 KVGeneralUt::CloseAllDelegate();
41 KvStoreNbDelegate::Option option;
42 option.isNeedCompressOnSync = isNeedCompressOnSync;
43 option.compressionRate = 100; // compress rate is 100
44 SetOption(option);
45 auto storeInfo1 = GetStoreInfo1();
46 ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo1, DEVICE_A), E_OK);
47 auto storeInfo2 = GetStoreInfo2();
48 ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo2, DEVICE_B), E_OK);
49 }
50
TriggerPutAndSync()51 void DistributedDBKVCompressTest::TriggerPutAndSync()
52 {
53 auto storeInfo1 = GetStoreInfo1();
54 auto storeInfo2 = GetStoreInfo2();
55 auto store1 = GetDelegate(storeInfo1);
56 ASSERT_NE(store1, nullptr);
57 auto store2 = GetDelegate(storeInfo2);
58 ASSERT_NE(store2, nullptr);
59 Value expectValue(DBConstant::MAX_VALUE_SIZE, 'v');
60 EXPECT_EQ(store1->Put({'k'}, expectValue), OK);
61 BlockPush(storeInfo1, storeInfo2);
62 Value actualValue;
63 EXPECT_EQ(store2->Get({'k'}, actualValue), OK);
64 EXPECT_EQ(actualValue, expectValue);
65 }
66
67 /**
68 * @tc.name: SyncTest001
69 * @tc.desc: Test sync with compress.
70 * @tc.type: FUNC
71 * @tc.author: zqq
72 */
73 HWTEST_F(DistributedDBKVCompressTest, SyncTest001, TestSize.Level0)
74 {
75 /**
76 * @tc.steps: step1. Open store with compress
77 * @tc.expected: step1. Open ok
78 */
79 ASSERT_NO_FATAL_FAILURE(PrepareEnv(true));
80 /**
81 * @tc.steps: step2. dev1 put (k,v) and sync to dev2
82 * @tc.expected: step2. sync should return OK and dev2 exist (k,v).
83 */
84 auto size1 = GetAllSendMsgSize();
85 ASSERT_NO_FATAL_FAILURE(TriggerPutAndSync());
86 auto size2 = GetAllSendMsgSize();
87 /**
88 * @tc.steps: step3. Open store without compress
89 * @tc.expected: step3. Open ok
90 */
91 ASSERT_NO_FATAL_FAILURE(PrepareEnv(false));
92 /**
93 * @tc.steps: step4. dev1 put (k,v) and sync to dev2
94 * @tc.expected: step4. sync should return OK and dev2 exist (k,v).
95 */
96 auto size3 = GetAllSendMsgSize();
97 ASSERT_NO_FATAL_FAILURE(TriggerPutAndSync());
98 auto size4 = GetAllSendMsgSize();
99 EXPECT_LE(size2 - size1, size4 - size3);
100 }
101 }