• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "softbus_conn_bytes_delivery.h"
17 
18 #include <random>
19 
20 #include <gtest/gtest.h>
21 
22 #include "conn_log.h"
23 #include "softbus_adapter_timer.h"
24 
25 #include "softbus_conn_common_mock.h"
26 
27 using namespace testing::ext;
28 using namespace testing;
29 
30 namespace OHOS::SoftBus {
31 class ConnBytesDeliveryTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase() { }
34 
TearDownTestCase()35     static void TearDownTestCase() { }
36 
SetUp()37     void SetUp() override { }
38 
TearDown()39     void TearDown() override { }
40 };
41 
42 /*
43  * @tc.name: CreateDestroyDeliveryTest
44  * @tc.desc: test create and destroy delivery instance
45  * @tc.type: FUNC
46  * @tc.require:
47  */
48 HWTEST_F(ConnBytesDeliveryTest, CreateDestoryDeliveryTest, TestSize.Level1)
49 {
50     ConnBytesDelivery *delivery = ConnCreateBytesDelivery(NULL);
51     ASSERT_EQ(delivery, nullptr);
52 
53     struct ConnBytesDeliveryConfig config = {
54         .name = "test_deliver",
55         .unitNum = 1 << 3,
56         .waitTimeoutMs = 500,
57         .idleTimeoutMs = 500,
58         .errorRetryWaitMs = 100,
59         .handler = nullptr,
60     };
61     delivery = ConnCreateBytesDelivery(&config);
62     ASSERT_EQ(delivery, nullptr);
63 
64     ConnBytesHandler noopHandler = [](uint32_t connectionId, uint8_t *data, uint32_t length,
__anonf6b2e7db0102(uint32_t connectionId, uint8_t *data, uint32_t length, struct ConnBytesAddition addition) 65                                        struct ConnBytesAddition addition) {};
66     config.handler = noopHandler;
67     delivery = ConnCreateBytesDelivery(&config);
68     ASSERT_NE(delivery, nullptr);
69     ConnDestroyBytesDelivery(delivery);
70 }
71 
72 /*
73  * @tc.name: DeliverTest
74  * @tc.desc: test deliver bytes
75  * @tc.type: FUNC
76  * @tc.require:
77  */
78 HWTEST_F(ConnBytesDeliveryTest, DeliverTest, TestSize.Level1)
79 {
80     ConnCommonTestMock mock;
81     struct ConnBytesDeliveryConfig config = {
82         .name = "test_deliver",
83         .unitNum = 2,
84         .waitTimeoutMs = 500,
85         .idleTimeoutMs = 500,
86         .errorRetryWaitMs = 100,
87         .handler = ConnCommonTestMock::bytesHandler_,
88     };
89     ConnBytesDelivery *delivery = ConnCreateBytesDelivery(&config);
90     ASSERT_NE(delivery, nullptr);
91 
92     auto got = std::vector<std::pair<uint32_t, int64_t>>();
93     EXPECT_CALL(mock, BytesHandlerHook)
__anonf6b2e7db0202(uint32_t id, const uint8_t *data, uint32_t length, struct ConnBytesAddition addition) 94         .WillRepeatedly([&got](uint32_t id, const uint8_t *data, uint32_t length, struct ConnBytesAddition addition) {
95             auto p = std::pair<uint32_t, int64_t>(id, addition.seq);
96             got.push_back(p);
97         });
98 
99     std::random_device rd;
100     std::mt19937 gen(rd());
101     std::uniform_int_distribution<> distSeq(0, 1000);
102     auto delivered = std::vector<std::pair<uint32_t, int64_t>>();
103     for (int i = 0; i < 10; i++) {
104         int64_t seq = distSeq(gen);
105         auto p = std::pair<uint32_t, int64_t>(i, seq);
106         delivered.push_back(p);
107 
108         auto ret = ConnDeliver(delivery, i, nullptr, 0, {0, 0, 0, seq});
109         CONN_LOGI(CONN_TEST, "delivery done, connection id=%{public}d, "
110             "L/P/F/M/S=0/0/0/0/%{public}" PRId64 ", ret=%{public}d", i, seq, ret);
111         EXPECT_EQ(ret, SOFTBUS_OK) << "the " << i << "th deliver failed";
112     }
113 
114     // delivery task should keep running util idle timeout, check it here
115     int32_t interval = 100;
116     for (int32_t duration = 0; duration < config.idleTimeoutMs; duration += interval) {
117         if (!ConnIsDeliveryTaskRunning(delivery)) {
118             ADD_FAILURE() << "duration " << duration << "ms check delivery task is not running";
119         }
120         SoftBusSleepMs(interval);
121     }
122     SoftBusSleepMs(interval);
123     EXPECT_FALSE(ConnIsDeliveryTaskRunning(delivery));
124 
125     EXPECT_EQ(got.size(), delivered.size());
126     for (int i = 0; i < got.size() && i < delivered.size(); i++) {
127         EXPECT_EQ(got[i].first, delivered[i].first) << "the " << i << "th id is not equal";
128         EXPECT_EQ(got[i].second, delivered[i].second) << "the " << i << "th seq is not equal";
129     }
130     ConnDestroyBytesDelivery(delivery);
131 }
132 } // namespace OHOS::SoftBus