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 #include <gtest/gtest.h>
16 #include <thread>
17 #include <cstring>
18 #include <algorithm>
19 #include <sched.h>
20 #include <unistd.h>
21 #include <sys/syscall.h>
22 #include <sys/resource.h>
23 #include "c/thread.h"
24 #include "sync/perf_counter.h"
25 #include "sync/wait_queue.h"
26 
27 #define private public
28 #include "eu/worker_thread.h"
29 #undef private
30 #include "../common.h"
31 
32 using namespace testing;
33 #ifdef HWTEST_TESTING_EXT_ENABLE
34 using namespace testing::ext;
35 #endif
36 using namespace ffrt;
37 
38 class ThreadTest : public testing::Test {
39 protected:
SetUpTestCase()40     static void SetUpTestCase()
41     {
42     }
43 
TearDownTestCase()44     static void TearDownTestCase()
45     {
46     }
47 
SetUp()48     virtual void SetUp()
49     {
50     }
51 
TearDown()52     virtual void TearDown()
53     {
54     }
55 
MockStart(void * arg)56     static void* MockStart(void* arg)
57     {
58         int* inc = reinterpret_cast<int*>(arg);
59         (*inc) += 1;
60         return nullptr;
61     }
62 };
63 
64 static int counter = 0;
simple_thd_func(void *)65 int simple_thd_func(void *)
66 {
67     counter++;
68     return 0;
69 }
70 
71 HWTEST_F(ThreadTest, IdleTest, TestSize.Level1)
72 {
73     WorkerThread* wt = new WorkerThread(QoS(6));
74     bool ret = wt->Idle();
75     EXPECT_FALSE(ret);
76 }
77 
78 HWTEST_F(ThreadTest, SetIdleTest, TestSize.Level1)
79 {
80     WorkerThread* wt = new WorkerThread(QoS(6));
81     bool var = false;
82     wt->SetIdle(var);
83     EXPECT_FALSE(wt->idle);
84 }
85 
86 HWTEST_F(ThreadTest, ExitedTest, TestSize.Level1)
87 {
88     WorkerThread* wt = new WorkerThread(QoS(6));
89     bool ret = wt->Exited();
90     EXPECT_FALSE(ret);
91 }
92 
93 HWTEST_F(ThreadTest, SetExitedTest, TestSize.Level1)
94 {
95     WorkerThread* wt = new WorkerThread(QoS(6));
96     bool var = false;
97     wt->SetExited(var);
98     EXPECT_FALSE(wt->exited);
99 }
100 
101 HWTEST_F(ThreadTest, GetQosTest, TestSize.Level1)
102 {
103     WorkerThread* wt = new WorkerThread(QoS(6));
104     QoS ret = wt->GetQos();
105 }
106 
107 HWTEST_F(ThreadTest, JoinTest, TestSize.Level1)
108 {
109     WorkerThread* wt = new WorkerThread(QoS(6));
110     wt->Join();
111 }
112 
113 HWTEST_F(ThreadTest, DetachTest, TestSize.Level1)
114 {
115     WorkerThread* wt = new WorkerThread(QoS(6));
116     wt->Detach();
117 }
118 
119 HWTEST_F(ThreadTest, set_worker_stack_size, TestSize.Level1)
120 {
121     int inc = 0;
122     size_t stackSize = 0;
123     WorkerThread* wt = new WorkerThread(QoS(6));
124     wt->NativeConfig();
125     wt->Start(MockStart, &inc);
126     wt->Join();
127     EXPECT_EQ(inc, 1);
128     delete wt;
129 
130     ffrt_error_t ret = ffrt_set_worker_stack_size(6, 10);
131     EXPECT_EQ(ret, ffrt_error_inval);
132 
133     ret = ffrt_set_worker_stack_size(6, 10 * 1024 * 1024);
134     wt = new WorkerThread(QoS(6));
135     wt->NativeConfig();
136     wt->Start(MockStart, &inc);
137     wt->Join();
138     EXPECT_EQ(inc, 2);
139     pthread_attr_getstacksize(&wt->attr_, &stackSize);
140     EXPECT_EQ(stackSize, 131072); // 蓝区stack size
141     delete wt;
142 }
143 
144 HWTEST_F(ThreadTest, c_api_thread_simple_test, TestSize.Level1)
145 {
146     ffrt_thread_t thread;
147     ffrt_thread_create(&thread, nullptr, nullptr, nullptr);
148     ffrt_thread_detach(nullptr);
149     ffrt_thread_join(nullptr, nullptr);
150 }
151 
152 HWTEST_F(ThreadTest, wait_queue_test, TestSize.Level1)
153 {
154     TaskWithNode node = TaskWithNode();
155 }
156