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_interface_test.h"
17
18 #include <gtest/gtest.h>
19 #include <string>
20
21 #include "xcollie.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace HiviewDFX {
SetUpTestCase(void)27 void XCollieInterfaceTest::SetUpTestCase(void)
28 {
29 }
30
TearDownTestCase(void)31 void XCollieInterfaceTest::TearDownTestCase(void)
32 {
33 }
34
SetUp(void)35 void XCollieInterfaceTest::SetUp(void)
36 {
37 }
38
TearDown(void)39 void XCollieInterfaceTest::TearDown(void)
40 {
41 }
42
43 /**
44 * @tc.name: XCollieTimerParamTest
45 * @tc.desc: Verify xcollie timer interface param
46 * @tc.type: FUNC
47 * @tc.require: SR000CPN2F AR000CTAMB
48 * @tc.author: yangjing
49 */
50 HWTEST_F(XCollieInterfaceTest, XCollieTimerParam_001, TestSize.Level1)
51 {
52 /**
53 * @tc.steps: step1. input param name include special string
54 * @tc.expected: step1. set timer successfully;
55 */
56 int id = XCollie::GetInstance().SetTimer("TimeoutTimerxce!@#$%^&*()", 1, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
57 ASSERT_GT(id, 0);
58
59 /**
60 * @tc.steps: step2. input param name include special string,cancel timer
61 * @tc.expected: step2. update timer successfully;
62 */
63 XCollie::GetInstance().CancelTimer(id);
64
65 /**
66 * @tc.steps: step3. input param timeout is invalid
67 * @tc.expected: step3. set timer failed;
68 */
69 id = XCollie::GetInstance().SetTimer("Timer", 0, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
70 ASSERT_EQ(id, INVALID_ID);
71
72 /**
73 * @tc.steps: step4. cancelTimer, input param id is invalid
74 * @tc.expected: step4. cancel timer failed;
75 */
76 XCollie::GetInstance().CancelTimer(-1);
77
78 /**
79 * @tc.steps: step5. multiple timer
80 * @tc.expected: step5. cancel timer successfully;
81 */
82 bool flag = false;
__anonb5ed1a140102(void *) 83 XCollieCallback callbackFunc = [&flag](void *) {
84 flag = true;
85 };
86 bool flag1 = false;
__anonb5ed1a140202(void *) 87 XCollieCallback callbackFunc1 = [&flag1](void *) {
88 flag1 = true;
89 };
90 id = XCollie::GetInstance().SetTimer("MyTimeout", 2, callbackFunc, nullptr, XCOLLIE_FLAG_NOOP);
91 ASSERT_GT(id, 0);
92 int id1 = XCollie::GetInstance().SetTimer("MyTimeout", 3, callbackFunc1, nullptr, XCOLLIE_FLAG_NOOP);
93 ASSERT_GT(id1, 0);
94 sleep(1);
95 XCollie::GetInstance().CancelTimer(id);
96 sleep(3);
97 ASSERT_EQ(flag, false);
98 ASSERT_EQ(flag1, true);
99
100 flag = false;
101 flag1 = false;
102 id = XCollie::GetInstance().SetTimer("MyTimeout", 2, callbackFunc, nullptr, XCOLLIE_FLAG_NOOP);
103 ASSERT_GT(id, 0);
104 id1 = XCollie::GetInstance().SetTimer("MyTimeout", 3, callbackFunc1, nullptr, XCOLLIE_FLAG_NOOP);
105 ASSERT_GT(id1, 0);
106 sleep(1);
107 XCollie::GetInstance().CancelTimer(id1);
108 sleep(3);
109 ASSERT_EQ(flag, true);
110 ASSERT_EQ(flag1, false);
111
112 /**
113 * @tc.steps: step6. log event
114 * @tc.expected: step6. log event successfully
115 */
116 id = XCollie::GetInstance().SetTimer("MyTimeout", 1, nullptr, nullptr, XCOLLIE_FLAG_LOG);
117 ASSERT_GT(id, 0);
118 sleep(2);
119 XCollie::GetInstance().CancelTimer(id);
120
121 /**
122 * @tc.steps: step7. callback test
123 * @tc.expected: step7. callback can be executed successfully
124 */
125 id = XCollie::GetInstance().SetTimer("MyTimeout", 1, callbackFunc, nullptr, XCOLLIE_FLAG_LOG);
126 ASSERT_GT(id, 0);
127 flag = false;
128 id1 = XCollie::GetInstance().SetTimer("MyTimeout", 2, callbackFunc, nullptr, XCOLLIE_FLAG_LOG);
129 ASSERT_GT(id1, 0);
130 sleep(1);
131 XCollie::GetInstance().CancelTimer(id1);
132 ASSERT_EQ(flag, false);
133
134 /**
135 * @tc.steps: step8. recover test
136 * @tc.expected: step8. recover can be executed successfully
137 */
138 id = XCollie::GetInstance().SetTimer("MyTimeout", 2, nullptr, nullptr, XCOLLIE_FLAG_RECOVERY);
139 ASSERT_GT(id, 0);
140 sleep(1);
141 }
142 } // namespace HiviewDFX
143 } // namespace OHOS
144