• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include <chrono>
16 #include <thread>
17 #include <gtest/gtest.h>
18 
19 #include "service_control.h"
20 #include "test_utils.h"
21 
22 using namespace testing::ext;
23 namespace initModuleTest {
24 namespace {
25 // Default wait for service status change time is 10 seconds
26 constexpr int WAIT_SERVICE_STATUS_TIMEOUT = 10;
27 }
28 
29 class ServiceControlTest : public testing::Test {
30 public:
SetUpTestCase()31     static void SetUpTestCase() {};
TearDownTestCase()32     static void TearDownTestCase() {};
SetUp()33     void SetUp() {};
TearDown()34     void TearDown() {};
35 };
36 
37 // Test service start
38 HWTEST_F(ServiceControlTest, ServiceStartTest, TestSize.Level1)
39 {
40     // Pick an unusual service for testing
41     // Try to start media_service.
42 
43     // 1) Check if media_service exist
44     std::string serviceName = "media_service";
45     auto status = GetServiceStatus(serviceName);
46     if (status == "running") {
47         int ret = ServiceControl(serviceName.c_str(), STOP);
48         ASSERT_EQ(ret, 0);
49         ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STOPPED, WAIT_SERVICE_STATUS_TIMEOUT);
50         ASSERT_EQ(ret, 0);
51     } else if (status != "created" && status != "stopped") {
52         std::cout << serviceName << " in invalid status " << status << std::endl;
53         std::cout << "Debug " << serviceName << " in unexpected status " << status << std::endl;
54         ASSERT_TRUE(0);
55     }
56 
57     // 2) Now try to start service
58     int ret = ServiceControl(serviceName.c_str(), START);
59     EXPECT_EQ(ret, 0);
60     ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STARTED, WAIT_SERVICE_STATUS_TIMEOUT);
61     EXPECT_EQ(ret, 0);
62     status = GetServiceStatus(serviceName);
63     std::cout << "Debug " << serviceName << " in status " << status << std::endl;
64     EXPECT_TRUE(status == "running");
65 }
66 
67 HWTEST_F(ServiceControlTest, NonExistServiceStartTest, TestSize.Level1)
68 {
69     std::string serviceName = "non_exist_service";
70     int ret = ServiceControl(serviceName.c_str(), START);
71     EXPECT_EQ(ret, 0); // No matter if service exist or not, ServiceControl always success.
72 
73     auto status = GetServiceStatus(serviceName);
74     EXPECT_TRUE(status == "idle");
75 }
76 
77 HWTEST_F(ServiceControlTest, ServiceStopTest, TestSize.Level1)
78 {
79     std::string serviceName = "media_service";
80     auto status = GetServiceStatus(serviceName);
81     if (status == "stopped" || status == "created") {
82         int ret = ServiceControl(serviceName.c_str(), START);
83         ASSERT_EQ(ret, 0); // start must be success
84 
85     } else if (status != "running") {
86         std::cout << serviceName << " in invalid status " << status << std::endl;
87         ASSERT_TRUE(0);
88     }
89 
90     int ret = ServiceControl(serviceName.c_str(), STOP);
91     EXPECT_EQ(ret, 0);
92     // Sleep for a while, let init handle service starting.
93     const int64_t ms = 500;
94     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
95     status = GetServiceStatus(serviceName);
96     bool isStopped = status == "stopped";
97     EXPECT_TRUE(isStopped);
98 }
99 
100 HWTEST_F(ServiceControlTest, NonExistServiceStopTest, TestSize.Level1)
101 {
102     std::string serviceName = "non_exist_service";
103     int ret = ServiceControl(serviceName.c_str(), STOP);
104     EXPECT_EQ(ret, 0); // No matter if service exist or not, ServiceControl always success.
105 
106     auto status = GetServiceStatus(serviceName);
107     EXPECT_TRUE(status == "idle");
108 }
109 
110 HWTEST_F(ServiceControlTest, ServiceTimerStartTest, TestSize.Level1)
111 {
112     uint64_t timeout = 1000; // Start service in 1 second
113     std::string serviceName = "media_service";
114     // stop this service first
115     int ret = ServiceControl(serviceName.c_str(), STOP);
116     auto oldStatus = GetServiceStatus(serviceName);
117     bool isRunning = oldStatus == "running";
118     EXPECT_FALSE(isRunning);
119 
120     ret = StartServiceByTimer(serviceName.c_str(), timeout);
121     EXPECT_EQ(ret, 0);
122 
123     // Service will be started in @timeout seconds
124     // Now we try to sleep about @timeout / 2 seconds, then check service status.
125     int64_t ms = 600;
126     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
127     // Get service status.
128     auto newStatus = GetServiceStatus(serviceName);
129     bool notChange = oldStatus == newStatus;
130     EXPECT_TRUE(notChange);
131 
132     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
133     newStatus = GetServiceStatus(serviceName);
134 
135     isRunning = newStatus == "running";
136     EXPECT_TRUE(isRunning);
137 }
138 
139 HWTEST_F(ServiceControlTest, ServiceTimerStartContinuouslyTest, TestSize.Level1)
140 {
141     uint64_t oldTimeout = 500;
142     uint64_t newTimeout = 1000;
143     std::string serviceName = "media_service";
144     int ret = ServiceControl(serviceName.c_str(), STOP);
145     EXPECT_EQ(ret, 0);
146     ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STOPPED, WAIT_SERVICE_STATUS_TIMEOUT);
147     EXPECT_EQ(ret, 0);
148     auto oldStatus = GetServiceStatus(serviceName);
149     bool isRunning = oldStatus == "running";
150     EXPECT_FALSE(isRunning);
151 
152     ret = StartServiceByTimer(serviceName.c_str(), oldTimeout); // Set timer as 500 ms
153     EXPECT_EQ(ret, 0);
154     ret = StartServiceByTimer(serviceName.c_str(), newTimeout); // Set timer as 1 second
155     EXPECT_EQ(ret, 0);
156 
157     std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int64_t>(oldTimeout)));
158     auto newStatus = GetServiceStatus(serviceName);
159     bool notChange = oldStatus == newStatus;
160     EXPECT_TRUE(notChange);
161     uint64_t margin = 20; // 20 ms margin in case of timer not that precisely
162     std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int64_t>(oldTimeout + margin)));
163     newStatus = GetServiceStatus(serviceName);
164     isRunning = newStatus == "running";
165     EXPECT_TRUE(isRunning);
166 }
167 
168 HWTEST_F(ServiceControlTest, ServiceTimerStopTest, TestSize.Level1)
169 {
170     uint64_t timeout = 1000; // set timer as 1 second
171     std::string serviceName = "media_service";
172     int ret = ServiceControl(serviceName.c_str(), STOP);
173     EXPECT_EQ(ret, 0);
174     ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STOPPED, WAIT_SERVICE_STATUS_TIMEOUT);
175     EXPECT_EQ(ret, 0);
176     auto oldStatus = GetServiceStatus(serviceName);
177     bool isRunning = oldStatus == "running";
178     EXPECT_FALSE(isRunning);
179 
180     ret = StartServiceByTimer(serviceName.c_str(), timeout);
181     EXPECT_EQ(ret, 0);
182 
183     // Now sleep for a while
184     int64_t ms = 300;
185     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
186     auto newStatus = GetServiceStatus(serviceName);
187 
188     bool notChange = oldStatus == newStatus;
189     EXPECT_TRUE(notChange);
190 
191     ret = StopServiceTimer(serviceName.c_str());
192     EXPECT_EQ(ret, 0);
193     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
194 
195     newStatus = GetServiceStatus(serviceName);
196     notChange = oldStatus == newStatus;
197     EXPECT_TRUE(notChange);
198 }
199 
200 HWTEST_F(ServiceControlTest, ServiceTimerStopLateTest, TestSize.Level1)
201 {
202     uint64_t timeout = 500; // set timer as 5 micro seconds
203     std::string serviceName = "media_service";
204     int ret = ServiceControl(serviceName.c_str(), STOP);
205     auto oldStatus = GetServiceStatus(serviceName);
206     bool isRunning = oldStatus == "running";
207     EXPECT_FALSE(isRunning);
208 
209     ret = StartServiceByTimer(serviceName.c_str(), timeout);
210     EXPECT_EQ(ret, 0);
211 
212     int64_t ms = 550;
213     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
214     ret = StopServiceTimer(serviceName.c_str());
215     EXPECT_EQ(ret, 0);
216 
217     auto newStatus = GetServiceStatus(serviceName);
218     isRunning = newStatus == "running";
219     EXPECT_TRUE(isRunning);
220 }
221 
222 HWTEST_F(ServiceControlTest, RestartServiceTest, TestSize.Level1)
223 {
224     std::string serviceName = "media_service";
225     auto status = GetServiceStatus(serviceName);
226     EXPECT_FALSE(status.empty());
227 
228     int ret = ServiceControl(serviceName.c_str(), RESTART);
229     EXPECT_EQ(ret, 0);
230 
231     ret = ServiceControl(serviceName.c_str(), STOP);
232     EXPECT_EQ(ret, 0);
233 
234     ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STOPPED, WAIT_SERVICE_STATUS_TIMEOUT);
235     EXPECT_EQ(ret, 0);
236 
237     ret = ServiceControl(serviceName.c_str(), RESTART);
238     EXPECT_EQ(ret, 0);
239 
240     status = GetServiceStatus(serviceName);
241 
242     bool isRunning = status == "running";
243     EXPECT_TRUE(isRunning);
244 }
245 
246 HWTEST_F(ServiceControlTest, WaitForServiceStatusTest, TestSize.Level1)
247 {
248     std::string serviceName = "media_service";
249     int ret = ServiceControl(serviceName.c_str(), STOP);
250     EXPECT_EQ(ret, 0);
251 
252     ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STOPPED, WAIT_SERVICE_STATUS_TIMEOUT);
253     EXPECT_EQ(ret, 0);
254 
255     auto status = GetServiceStatus(serviceName);
256     bool isStopped = status == "stopped";
257     EXPECT_TRUE(isStopped);
258 
259     // service is stopped now. try to wait a status which will not be set
260     std::cout << "Wait for service " << serviceName << " status change to start\n";
261     ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STARTED, WAIT_SERVICE_STATUS_TIMEOUT);
262     EXPECT_EQ(ret, -1);
263 
264     serviceName = "non-exist-service";
265     std::cout << "Wait for service " << serviceName << " status change to stop\n";
266     ret = ServiceWaitForStatus(serviceName.c_str(), SERVICE_STOPPED, WAIT_SERVICE_STATUS_TIMEOUT);
267     EXPECT_EQ(ret, -1);
268 }
269 } // initModuleTest
270