1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <gtest/gtest.h>
17 #include "plugin_service.ipc.h"
18 #include "service_entry.h"
19 
20 using namespace testing::ext;
21 namespace {
22 class ServiceEntryTest : public testing::Test {
23 public:
SetUpTestCase()24     static void SetUpTestCase() {}
TearDownTestCase()25     static void TearDownTestCase() {}
SetUp()26     void SetUp() {}
TearDown()27     void TearDown() {}
28 };
29 
30 namespace {
31 const int MS_PER_S = 1000;
32 const int US_PER_S = 1000000;
33 const int NS_PER_US = 1000;
34 const int NS_PER_S = 1000000000;
35 const int NS_PER_MS = 1000000;
36 const int SLEEP_TIME = 30000;
37 } // namespace
38 
39 /**
40  * @tc.name: Service
41  * @tc.desc: Server process monitoring.
42  * @tc.type: FUNC
43  */
HWTEST_F(ServiceEntryTest, AllCase, TestSize.Level1)44 HWTEST_F(ServiceEntryTest, AllCase, TestSize.Level1)
45 {
46     ServiceEntry serviceEntry;
47     IPluginServiceServer pluginService;
48     serviceEntry.StartServer("test_unix_socket_service_entry");
49     serviceEntry.RegisterService(pluginService);
50     serviceEntry.FindServiceByName(pluginService.serviceName_);
51 
52     usleep(SLEEP_TIME);
53 
54     IPluginServiceClient pluginClient;
55     ASSERT_TRUE(pluginClient.Connect("test_unix_socket_service_entry"));
56 
57     usleep(SLEEP_TIME);
58 }
59 
60 /**
61  * @tc.name: Service
62  * @tc.desc: Gets the time in milliseconds.
63  * @tc.type: FUNC
64  */
HWTEST_F(ServiceEntryTest, GetTimeMS, TestSize.Level1)65 HWTEST_F(ServiceEntryTest, GetTimeMS, TestSize.Level1)
66 {
67     struct timespec ts;
68     clock_gettime(CLOCK_BOOTTIME, &ts);
69     long t1 = ts.tv_sec * MS_PER_S + ts.tv_nsec / NS_PER_MS;
70     long t2 = GetTimeMS();
71 
72     ASSERT_TRUE(t2 - t1 >= 0);
73 }
74 
75 /**
76  * @tc.name: Service
77  * @tc.desc: Gets the time in microseconds.
78  * @tc.type: FUNC
79  */
HWTEST_F(ServiceEntryTest, GetTimeUS, TestSize.Level1)80 HWTEST_F(ServiceEntryTest, GetTimeUS, TestSize.Level1)
81 {
82     struct timespec ts;
83     clock_gettime(CLOCK_BOOTTIME, &ts);
84     long t1= ts.tv_sec * US_PER_S + ts.tv_nsec / NS_PER_US;
85     long t2 = GetTimeUS();
86 
87     ASSERT_TRUE(t2 - t1 >= 0);
88 }
89 
90 /**
91  * @tc.name: Service
92  * @tc.desc: Gets the time in nanoseconds.
93  * @tc.type: FUNC
94  */
HWTEST_F(ServiceEntryTest, GetTimeNS, TestSize.Level1)95 HWTEST_F(ServiceEntryTest, GetTimeNS, TestSize.Level1)
96 {
97     struct timespec ts;
98     clock_gettime(CLOCK_BOOTTIME, &ts);
99     long t1 = ts.tv_sec * NS_PER_S + ts.tv_nsec;
100     long t2 = GetTimeNS();
101     ASSERT_TRUE(t2 - t1 >= 0);
102 }
103 } // namespace