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 <random>
18 #include <csignal>
19 #include <cstdlib>
20 #include "core/entity.h"
21 #include "core/version_ctx.h"
22 #include "ffrt_inner.h"
23 #include "sched/task_state.h"
24 #include "dfx/log/ffrt_log_api.h"
25 #include "dfx/bbox/bbox.h"
26 #include "tm/cpu_task.h"
27 #include "../common.h"
28 
29 using namespace std;
30 using namespace testing;
31 #ifdef HWTEST_TESTING_EXT_ENABLE
32 using namespace testing::ext;
33 #endif
34 using namespace ffrt;
35 
36 class CoreTest : public testing::Test {
37 protected:
SetUpTestCase()38     static void SetUpTestCase()
39     {
40     }
41 
TearDownTestCase()42     static void TearDownTestCase()
43     {
44     }
45 
SetUp()46     virtual void SetUp()
47     {
48     }
49 
TearDown()50     virtual void TearDown()
51     {
52     }
53 };
54 
55 HWTEST_F(CoreTest, core_test_success_01, TestSize.Level1)
56 {
57     sync_io(0);
58 }
59 
60 HWTEST_F(CoreTest, task_ctx_success_01, TestSize.Level1)
61 {
__anon451a775f0102() 62     auto func1 = ([]() {cout << endl << "push a task" << endl;});
63     SCPUEUTask *task1 = new SCPUEUTask(nullptr, nullptr, 0, QoS(static_cast<int>(qos_user_interactive)));
__anon451a775f0202() 64     auto func2 = ([]() {cout << endl << "push a task" << endl;});
65     SCPUEUTask *task2 = new SCPUEUTask(nullptr, task1, 0, QoS());
66     QoS qos = QoS(static_cast<int>(qos_inherit));
67     task2->SetQos(qos);
68     EXPECT_EQ(task2->qos, static_cast<int>(qos_user_interactive));
69     delete task1;
70     delete task2;
71 }
72 
73 HWTEST_F(CoreTest, ThreadWaitAndNotifyMode, TestSize.Level1)
74 {
75     SCPUEUTask* task = new SCPUEUTask(nullptr, nullptr, 0, QoS());
76 
77     // when executing task is nullptr
78     EXPECT_EQ(ThreadWaitMode(nullptr), true);
79 
80     // when executing task is root
81     EXPECT_EQ(ThreadWaitMode(task), true);
82 
83     //when executing task in legacy mode
84     SCPUEUTask* parent = new SCPUEUTask(nullptr, nullptr, 0, QoS());
85     task->parent = parent;
86     task->legacyCountNum = 1;
87     EXPECT_EQ(ThreadWaitMode(task), true);
88 
89     // when task is valid and not legacy mode
90     task->legacyCountNum = 0;
91     EXPECT_EQ(ThreadWaitMode(task), false);
92 
93     // when block thread is false
94     EXPECT_EQ(ThreadNotifyMode(task), false);
95 
96     // when block thread is true
97     task->blockType = BlockType::BLOCK_THREAD;
98     EXPECT_EQ(ThreadNotifyMode(task), true);
99 
100     delete parent;
101     delete task;
102 }