1 /*
2  * Copyright (c) 2024 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 <iostream>
17 
18 #include <gtest/gtest.h>
19 
20 #include "softbus_conn_flow_control.h"
21 
22 using namespace testing::ext;
23 
24 namespace OHOS::SoftBus {
25 class ConnFlowControlTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase() { }
TearDownTestCase()28     static void TearDownTestCase() { }
SetUp()29     void SetUp() override { }
TearDown()30     void TearDown() override { }
31 };
32 
33 /*
34  * @tc.name: ConstructAndDestruct
35  * @tc.desc: check construct and destruct flow controller
36  * @tc.type: FUNC
37  * @tc.require:
38  */
39 HWTEST_F(ConnFlowControlTest, ConstructAndDestruct, TestSize.Level1)
40 {
41     {
42         struct ConnSlideWindowController controller { };
43         auto ret = ConnSlideWindowControllerConstructor(&controller);
44         EXPECT_EQ(ret, SOFTBUS_OK);
45     }
46 
47     {
48         auto controller = ConnSlideWindowControllerNew();
49         EXPECT_NE(controller, nullptr);
50         ConnSlideWindowControllerDelete(controller);
51     }
52 }
53 
54 /*
55  * @tc.name: FlowControlWhenDefault
56  * @tc.desc: check flow control when default case
57  * @tc.type: FUNC
58  * @tc.require:
59  */
60 HWTEST_F(ConnFlowControlTest, FlowControlWhenDefault, TestSize.Level1)
61 {
62     auto controller = ConnSlideWindowControllerNew();
63     EXPECT_NE(controller, nullptr);
64 
65     for (int i = 0; i < 100; ++i) {
66         int32_t expect = 512;
67         uint64_t now = SoftBusGetSysTimeMs();
68         auto value = controller->apply(controller, expect);
69         uint64_t delta = SoftBusGetSysTimeMs() - now;
70         EXPECT_EQ(value, expect);
71         // less than 10ms
72         EXPECT_TRUE(delta < 10);
73     }
74     ConnSlideWindowControllerDelete(controller);
75 }
76 
77 /*
78  * @tc.name: FlowControlWhenEnable
79  * @tc.desc: check flow control when enable case
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(ConnFlowControlTest, FlowControlWhenEnable, TestSize.Level1)
84 {
85     auto controller = ConnSlideWindowControllerNew();
86     EXPECT_NE(controller, nullptr);
87 
88     auto ret = controller->enable(controller, 1, MIN_QUOTA_IN_BYTES);
89     EXPECT_NE(ret, SOFTBUS_OK);
90     ret = controller->enable(controller, MIN_WINDOW_IN_MILLIS, 1);
91     EXPECT_NE(ret, SOFTBUS_OK);
92 
93     int32_t windowInMillis = MIN_WINDOW_IN_MILLIS;
94     int32_t quotaInBytes = MIN_QUOTA_IN_BYTES;
95     ret = controller->enable(controller, windowInMillis, quotaInBytes);
96     EXPECT_EQ(ret, SOFTBUS_OK);
97 
98     int32_t applyValue = quotaInBytes / 2;
99     uint64_t startTimestamp = SoftBusGetSysTimeMs();
100     auto got = controller->apply(controller, applyValue);
101     uint64_t delta = SoftBusGetSysTimeMs() - startTimestamp;
102     EXPECT_EQ(got, applyValue);
103     // less than 10ms
104     EXPECT_TRUE(delta < 10);
105 
106     int32_t remain = quotaInBytes - got;
107     startTimestamp = SoftBusGetSysTimeMs();
108     got = controller->apply(controller, remain + 1);
109     delta = SoftBusGetSysTimeMs() - startTimestamp;
110     EXPECT_EQ(remain, got);
111     // less than 10ms
112     EXPECT_TRUE(delta < 10);
113 
114     startTimestamp = SoftBusGetSysTimeMs();
115     got = controller->apply(controller, applyValue);
116     delta = SoftBusGetSysTimeMs() - startTimestamp;
117     EXPECT_EQ(got, applyValue);
118     // more than 10ms, as it wait in controller
119     EXPECT_TRUE(delta > 10);
120 
121     ret = controller->disable(controller);
122     EXPECT_EQ(ret, SOFTBUS_OK);
123 
124     applyValue = quotaInBytes + 1;
125     for (int i = 0; i < 10; ++i) {
126         startTimestamp = SoftBusGetSysTimeMs();
127         got = controller->apply(controller, applyValue);
128         delta = SoftBusGetSysTimeMs() - startTimestamp;
129         EXPECT_EQ(got, applyValue);
130         // less than 10ms
131         EXPECT_TRUE(delta < 10);
132     }
133     ConnSlideWindowControllerDelete(controller);
134 }
135 
136 } // namespace OHOS::SoftBus