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 #include <gtest/gtest.h>
16
17 #include <random>
18 #include <string>
19 #include <sys/time.h>
20
21 #include "common.h"
22 #include "grd_api_manager.h"
23 #include "rdb_errno.h"
24 #include "rdb_helper.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::NativeRdb;
28
29 class RdbRdDataAgingTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 void InsertData(uint64_t second, int startId, int endId);
36
37 static const std::string databaseName;
38 static std::shared_ptr<RdbStore> store;
39 };
40
41 class ExecuteTestOpenRdCallback : public RdbOpenCallback {
42 public:
43 int OnCreate(RdbStore &store) override;
44 int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override;
45 };
46
47 const std::string RdbRdDataAgingTest::databaseName = RDB_TEST_PATH + "data_aging_test.db";
48 std::shared_ptr<RdbStore> RdbRdDataAgingTest::store = nullptr;
49
SetUpTestCase(void)50 void RdbRdDataAgingTest::SetUpTestCase(void)
51 {
52 }
53
TearDownTestCase(void)54 void RdbRdDataAgingTest::TearDownTestCase(void)
55 {
56 }
57
SetUp(void)58 void RdbRdDataAgingTest::SetUp(void)
59 {
60 if (!IsUsingArkData()) {
61 GTEST_SKIP() << "Current testcase is not compatible from current rdb";
62 }
63 int errCode = E_OK;
64 RdbHelper::DeleteRdbStore(RdbRdDataAgingTest::databaseName);
65 RdbStoreConfig config(RdbRdDataAgingTest::databaseName);
66 config.SetIsVector(true);
67 ExecuteTestOpenRdCallback helper;
68 RdbRdDataAgingTest::store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
69 ASSERT_NE(RdbRdDataAgingTest::store, nullptr);
70 ASSERT_EQ(errCode, E_OK);
71 string sql1 = "create table test (id integer, start_time integer not null) with "
72 "(time_col='start_time', ttl='1 hour', data_limit='100 KB', interval='5 second', max_num='100');";
73 store->Execute(sql1);
74 }
75
TearDown(void)76 void RdbRdDataAgingTest::TearDown(void)
77 {
78 RdbRdDataAgingTest::store = nullptr;
79 RdbHelper::DeleteRdbStore(RdbRdDataAgingTest::databaseName);
80 }
81
InsertData(uint64_t second,int startId,int endId)82 void RdbRdDataAgingTest::InsertData(uint64_t second, int startId, int endId)
83 {
84 for (int i = startId; i <= endId; i++) {
85 struct timeval timestamp;
86 (void)gettimeofday(×tamp, nullptr);
87 uint64_t startTime = timestamp.tv_sec - second;
88 string sql = "insert into test values(" + std::to_string(i) + ", " + std::to_string(startTime) + ");";
89 auto ret = store->Execute(sql);
90 ASSERT_EQ(ret.first, E_OK);
91 }
92 }
93
94 /**
95 @tc.name: RdbStore_Data_Aging_001
96 @tc.desc: test RdbStore_Data_Aging
97 @tc.type: FUNC
98 */
99 HWTEST_F(RdbRdDataAgingTest, RdbStore_Data_Aging_001, TestSize.Level1)
100 {
101 InsertData(3595, 1, 100);
102 sleep(2);
103 InsertData(0, 101, 101);
104 sleep(2);
105 auto resultSet = store->QueryByStep("select * from test;");
106 int count = 0;
107 resultSet->GetRowCount(count);
108 resultSet->Close();
109 ASSERT_EQ(count, 101);
110 }
111
112 /**
113 @tc.name: RdbStore_Data_Aging_002
114 @tc.desc: test RdbStore_Data_Aging
115 @tc.type: FUNC
116 */
117 HWTEST_F(RdbRdDataAgingTest, RdbStore_Data_Aging_002, TestSize.Level1)
118 {
119 InsertData(3600, 1, 99);
120 sleep(5);
121 InsertData(0, 100, 100);
122 sleep(2);
123 auto resultSet = store->QueryByStep("select * from test;");
124 int count = 0;
125 resultSet->GetRowCount(count);
126 resultSet->Close();
127 ASSERT_EQ(count, 1);
128 }
129
130 /**
131 @tc.name: RdbStore_Data_Aging_003
132 @tc.desc: test RdbStore_Data_Aging
133 @tc.type: FUNC
134 */
135 HWTEST_F(RdbRdDataAgingTest, RdbStore_Data_Aging_003, TestSize.Level1)
136 {
137 InsertData(3595, 1, 50);
138 InsertData(0, 51, 100);
139 sleep(5);
140 auto resultSet = store->QueryByStep("select * from test;");
141 int count = 0;
142 resultSet->GetRowCount(count);
143 resultSet->Close();
144 ASSERT_EQ(count, 100);
145 }
146
147 /**
148 @tc.name: RdbStore_Data_Aging_004
149 @tc.desc: test RdbStore_Data_Aging
150 @tc.type: FUNC
151 */
152 HWTEST_F(RdbRdDataAgingTest, RdbStore_Data_Aging_004, TestSize.Level1)
153 {
154 InsertData(3595, 1, 100);
155 InsertData(0, 101, 149);
156 sleep(5);
157 auto resultSet = store->QueryByStep("select * from test;");
158 int count = 0;
159 resultSet->GetRowCount(count);
160 resultSet->Close();
161 ASSERT_EQ(count, 149);
162
163 InsertData(0, 150, 150);
164 sleep(2);
165
166 resultSet = store->QueryByStep("select * from test;");
167 resultSet->GetRowCount(count);
168 resultSet->Close();
169 ASSERT_EQ(count, 50);
170 }
171
172 /**
173 @tc.name: RdbStore_Data_Aging_005
174 @tc.desc: test RdbStore_Data_Aging
175 @tc.type: FUNC
176 */
177 HWTEST_F(RdbRdDataAgingTest, RdbStore_Data_Aging_005, TestSize.Level1)
178 {
179 InsertData(3595, 1, 100);
180 InsertData(3592, 101, 130);
181 InsertData(3590, 131, 200);
182
183 auto resultSet = store->QueryByStep("select * from test;");
184 int count = 0;
185 resultSet->GetRowCount(count);
186 resultSet->Close();
187 ASSERT_EQ(count, 200);
188 sleep(5);
189 InsertData(0, 201, 201);
190
191 sleep(2);
192 resultSet = store->QueryByStep("select * from test;");
193 resultSet->GetRowCount(count);
194 resultSet->Close();
195 ASSERT_EQ(count, 71);
196
197 sleep(5);
198 InsertData(0, 202, 202);
199
200 sleep(2);
201 resultSet = store->QueryByStep("select * from test;");
202 resultSet->GetRowCount(count);
203 resultSet->Close();
204 ASSERT_EQ(count, 2);
205 }
206
207 /**
208 @tc.name: RdbStore_Data_Aging_006
209 @tc.desc: test RdbStore_Data_Aging
210 @tc.type: FUNC
211 */
212 HWTEST_F(RdbRdDataAgingTest, RdbStore_Data_Aging_006, TestSize.Level1)
213 {
214 InsertData(3600, 1, 99);
215 InsertData(0, 100, 100);
216 auto ret = store->Execute("drop table test;");
217 ASSERT_EQ(ret.first, E_OK);
218 }
219