• 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 
18 #include "flowctrl_manager/kvstore_flowctrl_manager.h"
19 #include <memory>
20 #include <thread>
21 #include <chrono>
22 using namespace testing::ext;
23 using namespace OHOS::DistributedKv;
24 using namespace OHOS;
25 using namespace std::chrono;
26 
27 class KvStoreFlowCtrlManagerTest : public testing::Test {
28 public:
29     static inline const int MANAGER_BURST_CAPACITY = 50;
30     static inline const int MANAGER_SUSTAINED_CAPACITY = 500;
31     static inline const int OPERATION_BURST_CAPACITY = 1000;
32     static inline const int OPERATION_SUSTAINED_CAPACITY = 10000;
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp();
36     void TearDown();
37 };
38 
SetUpTestCase(void)39 void KvStoreFlowCtrlManagerTest::SetUpTestCase(void)
40 {}
41 
TearDownTestCase(void)42 void KvStoreFlowCtrlManagerTest::TearDownTestCase(void)
43 {}
44 
SetUp(void)45 void KvStoreFlowCtrlManagerTest::SetUp(void)
46 {}
47 
TearDown(void)48 void KvStoreFlowCtrlManagerTest::TearDown(void)
49 {}
50 
51 /**
52 * @tc.name: KvStoreFlowCtrlManagerTest001
53 * @tc.desc: burst flow control
54 * @tc.type: FUNC
55 * @tc.require: AR000F3OP7
56 * @tc.author: jishengwu
57 */
58 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest001, TestSize.Level1)
59 {
60     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(OPERATION_BURST_CAPACITY, OPERATION_SUSTAINED_CAPACITY);
61     int arr[2] = {0, 0};
62     for (int i = 0; i < 1001; i++) {
63         arr[ptr->IsTokenEnough()]++;
64     }
65     EXPECT_EQ(1, arr[0]);
66     EXPECT_EQ(1000, arr[1]);
67 }
68 
69 /**
70 * @tc.name: KvStoreFlowCtrlManagerTest002
71 * @tc.desc: burst flow control
72 * @tc.type: FUNC
73 * @tc.require: AR000F3OP7
74 * @tc.author: jishengwu
75 */
76 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest002, TestSize.Level1)
77 {
78     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(OPERATION_BURST_CAPACITY, OPERATION_SUSTAINED_CAPACITY);
79     int arr[2] = {0, 0};
80     for (int i = 0; i < 1000; i++) {
81         arr[ptr->IsTokenEnough()]++;
82     }
83     EXPECT_EQ(0, arr[0]);
84     EXPECT_EQ(1000, arr[1]);
85 }
86 
87 /**
88 * @tc.name: KvStoreFlowCtrlManagerTest003
89 * @tc.desc: burst flow control
90 * @tc.type: FUNC
91 * @tc.require: AR000F3OP7
92 * @tc.author: jishengwu
93 */
94 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest003, TestSize.Level1)
95 {
96     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(OPERATION_BURST_CAPACITY, OPERATION_SUSTAINED_CAPACITY);
97     int arr[2] = {0, 0};
98     for (int i = 0; i < 999; i++) {
99         arr[ptr->IsTokenEnough()]++;
100     }
101     EXPECT_EQ(0, arr[0]);
102     EXPECT_EQ(999, arr[1]);
103 }
104 
105 /**
106 * @tc.name: KvStoreFlowCtrlManagerTest004
107 * @tc.desc: sustained flow control
108 * @tc.type: FUNC
109 * @tc.require: SR000F3H5U AR000F3OP8
110 * @tc.author: jishengwu
111 */
112 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest004, TestSize.Level1)
113 {
114     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(OPERATION_BURST_CAPACITY, OPERATION_SUSTAINED_CAPACITY);
115     int arr[2] = {0, 0};
116     for (int i = 0; i < 9999; i++) {
117         arr[ptr->IsTokenEnough()]++;
118         std::this_thread::sleep_for(std::chrono::milliseconds(1));
119     }
120     EXPECT_EQ(0, arr[0]);
121     EXPECT_EQ(9999, arr[1]);
122 }
123 
124 /**
125 * @tc.name: KvStoreFlowCtrlManagerTest005
126 * @tc.desc: sustained flow control
127 * @tc.type: FUNC
128 * @tc.require: AR000F3OP8
129 * @tc.author: jishengwu
130 */
131 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest005, TestSize.Level1)
132 {
133     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(OPERATION_BURST_CAPACITY, OPERATION_SUSTAINED_CAPACITY);
134     int arr[2] = {0, 0};
135     for (int i = 0; i < 10000; i++) {
136         arr[ptr->IsTokenEnough()]++;
137         std::this_thread::sleep_for(std::chrono::milliseconds(1));
138     }
139     EXPECT_EQ(0, arr[0]);
140     EXPECT_EQ(10000, arr[1]);
141 }
142 
143 /**
144 * @tc.name: KvStoreFlowCtrlManagerTest006
145 * @tc.desc: sustained flow control
146 * @tc.type: FUNC
147 * @tc.require: AR000F3OP8
148 * @tc.author: jishengwu
149 */
150 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest006, TestSize.Level1)
151 {
152     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(OPERATION_BURST_CAPACITY, OPERATION_SUSTAINED_CAPACITY);
153     int arr[2] = {0, 0};
154     uint64_t curTime = 0;
155     uint64_t lastTime = duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count();
156     for (int i = 0; i < 10001; i++) {
157         arr[ptr->IsTokenEnough()]++;
158         while (true) {
159             curTime = duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count();
160             if ((curTime - lastTime) > 1000) {
161                 lastTime = curTime;
162                 break;
163             }
164         }
165     }
166     EXPECT_EQ(1, arr[0]);
167     EXPECT_EQ(10000, arr[1]);
168 }
169 
170 /**
171 * @tc.name: KvStoreFlowCtrlManagerTest007
172 * @tc.desc: burst flow control
173 * @tc.type: FUNC
174 * @tc.require: AR000F3OP7
175 * @tc.author: jishengwu
176 */
177 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest007, TestSize.Level1)
178 {
179     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(MANAGER_BURST_CAPACITY, MANAGER_SUSTAINED_CAPACITY);
180     int arr[2] = {0, 0};
181     for (int i = 0; i < 51; i++) {
182         arr[ptr->IsTokenEnough()]++;
183     }
184     EXPECT_EQ(1, arr[0]);
185     EXPECT_EQ(50, arr[1]);
186 }
187 
188 /**
189 * @tc.name: KvStoreFlowCtrlManagerTest008
190 * @tc.desc: burst flow control
191 * @tc.type: FUNC
192 * @tc.require: AR000F3OP7
193 * @tc.author: jishengwu
194 */
195 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest008, TestSize.Level1)
196 {
197     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(MANAGER_BURST_CAPACITY, MANAGER_SUSTAINED_CAPACITY);
198     int arr[2] = {0, 0};
199     for (int i = 0; i < 50; i++) {
200         arr[ptr->IsTokenEnough()]++;
201     }
202     EXPECT_EQ(0, arr[0]);
203     EXPECT_EQ(50, arr[1]);
204 }
205 
206 /**
207 * @tc.name: KvStoreFlowCtrlManagerTest009
208 * @tc.desc: burst flow control
209 * @tc.type: FUNC
210 * @tc.require: AR000F3OP7
211 * @tc.author: jishengwu
212 */
213 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest009, TestSize.Level1)
214 {
215     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(MANAGER_BURST_CAPACITY, MANAGER_SUSTAINED_CAPACITY);
216     int arr[2] = {0, 0};
217     for (int i = 0; i < 49; i++) {
218         arr[ptr->IsTokenEnough()]++;
219     }
220     EXPECT_EQ(0, arr[0]);
221     EXPECT_EQ(49, arr[1]);
222 }
223 
224 /**
225 * @tc.name: KvStoreFlowCtrlManagerTest010
226 * @tc.desc: sustained flow control
227 * @tc.type: FUNC
228 * @tc.require: AR000F3OP8
229 * @tc.author: jishengwu
230 */
231 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest010, TestSize.Level1)
232 {
233     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(MANAGER_BURST_CAPACITY, MANAGER_SUSTAINED_CAPACITY);
234     int arr[2] = {0, 0};
235     for (int i = 0; i < 499; i++) {
236         arr[ptr->IsTokenEnough()]++;
237         std::this_thread::sleep_for(std::chrono::milliseconds(20));
238     }
239     EXPECT_EQ(0, arr[0]);
240     EXPECT_EQ(499, arr[1]);
241 }
242 
243 /**
244 * @tc.name: KvStoreFlowCtrlManagerTest011
245 * @tc.desc: sustained flow control
246 * @tc.type: FUNC
247 * @tc.require: AR000F3OP8
248 * @tc.author: jishengwu
249 */
250 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest011, TestSize.Level1)
251 {
252     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(MANAGER_BURST_CAPACITY, MANAGER_SUSTAINED_CAPACITY);
253     int arr[2] = {0, 0};
254     for (int i = 0; i < 500; i++) {
255         arr[ptr->IsTokenEnough()]++;
256         std::this_thread::sleep_for(std::chrono::milliseconds(20));
257     }
258     EXPECT_EQ(0, arr[0]);
259     EXPECT_EQ(500, arr[1]);
260 }
261 
262 /**
263 * @tc.name: KvStoreFlowCtrlManagerTest012
264 * @tc.desc: sustained flow control
265 * @tc.type: FUNC
266 * @tc.require: AR000F3OP8
267 * @tc.author: jishengwu
268 */
269 HWTEST_F(KvStoreFlowCtrlManagerTest, KvStoreFlowCtrlManagerTest012, TestSize.Level1)
270 {
271     auto ptr = std::make_shared<KvStoreFlowCtrlManager>(MANAGER_BURST_CAPACITY, MANAGER_SUSTAINED_CAPACITY);
272     int arr[2] = {0, 0};
273     for (int i = 0; i < 501; i++) {
274         arr[ptr->IsTokenEnough()]++;
275         std::this_thread::sleep_for(std::chrono::milliseconds(20));
276     }
277     EXPECT_EQ(1, arr[0]);
278     EXPECT_EQ(500, arr[1]);
279 }
280 
281