• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef APP_USE_ARM
38 static const size_t WORKER_STACK_SIZE = 131072;
39 #else
40 static const size_t WORKER_STACK_SIZE = 10 * 1024 * 1024;
41 #endif
42 
43 class ThreadTest : public testing::Test {
44 protected:
SetUpTestCase()45     static void SetUpTestCase()
46     {
47     }
48 
TearDownTestCase()49     static void TearDownTestCase()
50     {
51     }
52 
SetUp()53     void SetUp() override
54     {
55     }
56 
TearDown()57     void TearDown() override
58     {
59     }
60 
MockStart(void * arg)61     static void* MockStart(void* arg)
62     {
63         int* inc = reinterpret_cast<int*>(arg);
64         (*inc) += 1;
65         return nullptr;
66     }
67 };
68 
69 static int counter = 0;
simple_thd_func(void *)70 int simple_thd_func(void *)
71 {
72     counter++;
73     return 0;
74 }
75 
tmp_func(void *)76 static void* tmp_func(void *)
77 {
78     ++counter;
79     return nullptr;
80 }
81 
82 HWTEST_F(ThreadTest, IdleTest, TestSize.Level1)
83 {
84     WorkerThread* wt = new WorkerThread(QoS(6));
85     bool ret = wt->Idle();
86     EXPECT_FALSE(ret);
87     delete wt;
88 }
89 
90 HWTEST_F(ThreadTest, SetIdleTest, TestSize.Level1)
91 {
92     WorkerThread* wt = new WorkerThread(QoS(6));
93     bool var = false;
94     wt->SetIdle(var);
95     EXPECT_FALSE(wt->idle);
96     delete wt;
97 }
98 
99 HWTEST_F(ThreadTest, ExitedTest, TestSize.Level1)
100 {
101     WorkerThread* wt = new WorkerThread(QoS(6));
102     bool ret = wt->Exited();
103     EXPECT_FALSE(ret);
104     delete wt;
105 }
106 
107 HWTEST_F(ThreadTest, SetExitedTest, TestSize.Level1)
108 {
109     WorkerThread* wt = new WorkerThread(QoS(6));
110     bool var = false;
111     wt->SetExited(var);
112     EXPECT_FALSE(wt->exited);
113     delete wt;
114 }
115 
116 HWTEST_F(ThreadTest, GetQosTest, TestSize.Level1)
117 {
118     WorkerThread* wt = new WorkerThread(QoS(6));
119     EXPECT_NE(wt, nullptr);
120     QoS ret = wt->GetQos();
121     delete wt;
122 }