1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include <thread>
18 #include "c/deadline.h"
19 #include "sched/interval.h"
20 #include "sched/sched_deadline.h"
21 #include "tm/cpu_task.h"
22 #include "../common.h"
23 
24 using namespace testing;
25 #ifdef HWTEST_TESTING_EXT_ENABLE
26 using namespace testing::ext;
27 #endif
28 using namespace ffrt;
29 
30 class DeadlineTest : public testing::Test {
31 protected:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34     }
35 
TearDownTestCase()36     static void TearDownTestCase()
37     {
38     }
39 
SetUp()40     virtual void SetUp()
41     {
42     }
43 
TearDown()44     virtual void TearDown()
45     {
46     }
47 };
48 
49 
50 HWTEST_F(DeadlineTest, interval_deadline_test, TestSize.Level1)
51 {
52     auto it = qos_interval_create(100, static_cast<int>(ffrt::qos_user_interactive));
53     int ret = qos_interval_begin(it);
54     EXPECT_EQ(ret, 0);
55 
56     qos_interval_end(it);
57 
58     ret = qos_interval_begin(it);
59     EXPECT_EQ(ret, 0);
60 
61     qos_interval_update(it, 50);
62     qos_interval_end(it);
63     qos_interval_destroy(it);
64 }
65 
66 HWTEST_F(DeadlineTest, interval_join_test, TestSize.Level1)
67 {
68     auto it = qos_interval_create(100, static_cast<int>(ffrt::qos_user_interactive));
69     int ret = qos_interval_begin(it);
70     EXPECT_EQ(ret, 0);
71 
72     qos_interval_join(it);
73     qos_interval_leave(it);
74 
75     qos_interval_end(it);
76     qos_interval_destroy(it);
77 }
78 
79 HWTEST_F(DeadlineTest, interval_exception_test, TestSize.Level1)
80 {
81     auto it = qos_interval_create(100, static_cast<int>(ffrt::qos_user_interactive));
82     int ret = qos_interval_begin(it);
83     EXPECT_EQ(ret, 0);
84 
85     ret = qos_interval_begin(it);
86     EXPECT_EQ(ret, -1);
87 
88     qos_interval_end(it);
89 
90     ret = qos_interval_begin(it);
91     EXPECT_EQ(ret, 0);
92     qos_interval_destroy(it);
93 }
94 
95 HWTEST_F(DeadlineTest, interval_exception2_test, TestSize.Level1)
96 {
97     auto it = qos_interval_create(100, static_cast<int>(ffrt::qos_user_interactive));
98 
99     qos_interval_update(it, 50);
100     qos_interval_end(it);
101 
102     int ret = qos_interval_begin(it);
103     EXPECT_EQ(ret, 0);
104 
105     qos_interval_end(it);
106     qos_interval_destroy(it);
107 }
108 
109 HWTEST_F(DeadlineTest, interval_exception3_test, TestSize.Level1)
110 {
111     auto it = qos_interval_create(100, static_cast<int>(ffrt::qos_default));
112 
113     qos_interval_update(it, 50);
114     qos_interval_end(it);
115 
116     int ret = qos_interval_begin(it);
117     EXPECT_EQ(ret, -1);
118 
119     qos_interval_join(it);
120     qos_interval_leave(it);
121 
122     qos_interval_end(it);
123     qos_interval_destroy(it);
124 }
125 
126 HWTEST_F(DeadlineTest, sched_deadline_test, TestSize.Level1)
127 {
128     CPUEUTask *ctx = ExecuteCtx::Cur()->task;
129     TaskLoadTracking::Begin(ctx);
130     TaskLoadTracking::End(ctx);
131 
132     uint64_t load = TaskLoadTracking::GetLoad(ctx);
133 }
134 
135