• 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 "xcollie_timeout_test.h"
17 
18 #include "timer_ring.h"
19 #include "xcollie.h"
20 #include "xcollie_inner.h"
21 #include "xcollie_utils.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace HiviewDFX {
XCollieTimeoutTest()27 XCollieTimeoutTest::XCollieTimeoutTest():callbackCnt_(0)
28 {
29 }
30 
~XCollieTimeoutTest()31 XCollieTimeoutTest::~XCollieTimeoutTest()
32 {
33 }
34 
SetUpTestCase(void)35 void XCollieTimeoutTest::SetUpTestCase(void)
36 {
37 }
38 
TearDownTestCase(void)39 void XCollieTimeoutTest::TearDownTestCase(void)
40 {
41 }
42 
SetUp(void)43 void XCollieTimeoutTest::SetUp(void)
44 {
45 }
46 
TearDown(void)47 void XCollieTimeoutTest::TearDown(void)
48 {
49 }
50 
TimerCallback(void * data)51 void XCollieTimeoutTest::TimerCallback(void *data)
52 {
53     callbackCnt_++;
54 }
55 
SetCallbackCnt(int cnt)56 void XCollieTimeoutTest::SetCallbackCnt(int cnt)
57 {
58     callbackCnt_ = cnt;
59 }
60 
61 /**
62  * @tc.name: XCollieTimerNoTimeoutTest
63  * @tc.desc: Verify timer, add, cancel, update timer
64  * @tc.type: FUNC
65  * @tc.require: SR000CPN2F AR000CTAMB
66  * @tc.author: yangjing
67  */
68 HWTEST_F(XCollieTimeoutTest, XCollieTimerNoTimeout_001, TestSize.Level2)
69 {
70     /**
71      * @tc.steps: step1.add timer
72      * @tc.expected: step1. Add timer successfully
73      */
74     SetCallbackCnt(0);
75     int id = XCollie::GetInstance().SetTimer("NoTimeoutTimer", 1, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
76     ASSERT_NE(id, INVALID_ID);
77 
78     /**
79      * @tc.steps: step2.update timer
80      * @tc.expected: step2. update timer successfully
81      */
82     bool ret = XCollie::GetInstance().UpdateTimer(id, 3);
83     ASSERT_EQ(ret, true);
84 
85     /**
86      * @tc.steps: step3.cancel timer
87      * @tc.expected: step3. cancel timer successfully
88      */
89     XCollie::GetInstance().CancelTimer(id);
90 }
91 
92 /**
93  * @tc.name: XCollieTimerTimeoutTest
94  * @tc.desc: Verify timer, timer timeout
95  * @tc.type: FUNC
96  * @tc.require: SR000CPN2F AR000CTAMB
97  * @tc.author: yangjing
98  */
99 HWTEST_F(XCollieTimeoutTest, XCollieTimerTimeout_002, TestSize.Level2)
100 {
101     SetCallbackCnt(0);
__anon81ec7d920102(void *data) 102     auto func = [this](void *data) {
103         this->TimerCallback(data);
104     };
105     /**
106      * @tc.steps: step1.add timer
107      * @tc.expected: step1. Add timer successfully
108      */
109     int id = XCollie::GetInstance().SetTimer("TimeoutTimer", 1, func, this, XCOLLIE_FLAG_NOOP);
110     ASSERT_NE(id, INVALID_ID);
111     std::this_thread::sleep_for(std::chrono::seconds(3));
112     ASSERT_EQ(GetCallbackCnt(), 1);
113 
114     /**
115      * @tc.steps: step2. timer timeout, update timer
116      * @tc.expected: step2. update timer failed
117      */
118     bool ret = XCollie::GetInstance().UpdateTimer(id, 1);
119     ASSERT_EQ(ret, false);
120     XCollie::GetInstance().CancelTimer(id);
121 }
122 
123 /**
124  * @tc.name: XCollieTimerAddTaskOverFlow
125  * @tc.desc: Verify timer, timer task number over flow
126  * @tc.type: FUNC
127  * @tc.require: SR000CPN2F AR000CTAMB
128  * @tc.author: yangjing
129  */
130 HWTEST_F(XCollieTimeoutTest, XCollieTimerAddTaskOverFlow_003, TestSize.Level3)
131 {
132     SetCallbackCnt(0);
133     XCollieInner::GetInstance().SetRecoveryFlag(false);
__anon81ec7d920202(void *data) 134     auto func = [this](void *data) {
135         this->TimerCallback(data);
136     };
137     /**
138      * @tc.steps: step1.add MAX_XCOLLIE_NUM timer
139      * @tc.expected: step1. Add timer successfully
140      */
141     int id[MAX_XCOLLIE_NUM];
142     for (unsigned int i = 0; i < MAX_XCOLLIE_NUM; i++) {
143         id[i] = XCollie::GetInstance().SetTimer("TimeoutTimer", 1, func, this, XCOLLIE_FLAG_DEFAULT);
144         ASSERT_NE(id[i], INVALID_ID);
145     }
146 
147     /**
148      * @tc.steps: step2.add MAX_XCOLLIE_NUM+1 timer
149      * @tc.expected: step2. Add timer failed
150      */
151     int last = XCollie::GetInstance().SetTimer("TimeoutTimer", 1, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
152     ASSERT_EQ(last, INVALID_ID);
153 
154     /**
155      * @tc.steps: step3. wait MAX_XCOLLIE_NUM timer timeout
156      * @tc.expected: step3. timer timeout successfully
157      */
158     std::this_thread::sleep_for(std::chrono::seconds(4));
159     ASSERT_EQ(static_cast<unsigned int>(GetCallbackCnt()), MAX_XCOLLIE_NUM);
160 
161     /**
162      * @tc.steps: step4.add MAX_XCOLLIE_NUM+1 timer
163      * @tc.expected: step4. Add timer successfully
164      */
165     last = XCollie::GetInstance().SetTimer("TimeoutTimer", 1, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
166     ASSERT_NE(last, INVALID_ID);
167     XCollie::GetInstance().CancelTimer(last);
168 }
169 
170 /**
171  * @tc.name: XCollieTimerRoundTest
172  * @tc.desc: Verify xcollie timer round test
173  * @tc.type: FUNC
174  * @tc.require: SR000CPN2F AR000CTAMB
175  * @tc.author: yangjing
176  */
177 HWTEST_F(XCollieTimeoutTest, XCollieTimerRound_004, TestSize.Level4)
178 {
179     XCOLLIE_LOGI("XCollieTimerRoundTest start...");
180 
181     SetCallbackCnt(0);
182     XCollieInner::GetInstance().SetRecoveryFlag(false);
__anon81ec7d920302(void *data) 183     auto func = [this](void *data) {
184         this->TimerCallback(data);
185     };
186 
187     /**
188      * @tc.steps: step1.add timer, timeout larger then ring length
189      * @tc.expected: step1. Add timer successfully
190      */
191     int last = XCollie::GetInstance().SetTimer("TimeoutTimer", 61, func, nullptr, XCOLLIE_FLAG_NOOP);
192     ASSERT_NE(last, INVALID_ID);
193 
194     /**
195      * @tc.steps: step2. wait  timer timeout
196      * @tc.expected: step2. timer timeout successfully
197      */
198     std::this_thread::sleep_for(std::chrono::seconds(65));
199     ASSERT_EQ(GetCallbackCnt(), 1);
200 }
201 } // end of namespace HiviewDFX
202 } // end of namespace OHOS
203