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 <random>
17 #include <csignal>
18 #include <gtest/gtest.h>
19 #include "core/entity.h"
20 #include "core/version_ctx.h"
21 #include "ffrt_inner.h"
22 #include "sched/task_state.h"
23 #include "dfx/log/ffrt_log_api.h"
24 #include "dfx/bbox/bbox.h"
25 #include "tm/cpu_task.h"
26 #include "../common.h"
27 
28 using namespace std;
29 using namespace testing;
30 #ifdef HWTEST_TESTING_EXT_ENABLE
31 using namespace testing::ext;
32 #endif
33 using namespace ffrt;
34 
35 class CoreTest : public testing::Test {
36 protected:
SetUpTestCase()37     static void SetUpTestCase()
38     {
39     }
40 
TearDownTestCase()41     static void TearDownTestCase()
42     {
43     }
44 
SetUp()45     virtual void SetUp()
46     {
47     }
48 
TearDown()49     virtual void TearDown()
50     {
51     }
52 };
53 
54 HWTEST_F(CoreTest, core_test_success_01, TestSize.Level1)
55 {
56     sync_io(0);
57 }
58 
59 HWTEST_F(CoreTest, task_ctx_success_01, TestSize.Level1)
60 {
__anon25f535c00102() 61     auto func1 = ([]() {std::cout << std::endl << " push a task " << std::endl;});
62     SCPUEUTask *task1 = new SCPUEUTask(nullptr, nullptr, 0, QoS(static_cast<int>(qos_user_interactive)));
__anon25f535c00202() 63     auto func2 = ([]() {std::cout << std::endl << " push a task " << std::endl;});
64     SCPUEUTask *task2 = new SCPUEUTask(nullptr, task1, 0, QoS());
65     QoS qos = QoS(static_cast<int>(qos_inherit));
66     task2->SetQos(qos);
67     EXPECT_EQ(task2->qos, static_cast<int>(qos_user_interactive));
68     delete task1;
69     delete task2;
70 }
71 
72 HWTEST_F(CoreTest, ffrt_submit_wait_success_01, TestSize.Level1)
73 {
74     const uint32_t sleepTime = 3 * 200;
75     int x = 0;
76     ffrt_task_attr_t* attr = (ffrt_task_attr_t *) malloc(sizeof(ffrt_task_attr_t));
77     ffrt_task_attr_init(attr);
__anon25f535c00302() 78     std::function<void()>&& basicFunc = [&]() {
79         EXPECT_EQ(x, 0);
80         usleep(sleepTime);
81         x = x + 1;
82     };
83     ffrt_function_header_t* basicFunc_ht = ffrt::create_function_wrapper((basicFunc));
84     const std::vector<ffrt_dependence_t> in_deps = {};
85     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
86     const std::vector<ffrt_dependence_t> ou_deps = {};
87     ffrt_deps_t ou{static_cast<uint32_t>(ou_deps.size()), ou_deps.data()};
88     const std::vector<ffrt_dependence_t> wait_deps = {};
89     ffrt_deps_t wait{static_cast<uint32_t>(wait_deps.size()), wait_deps.data()};
90     const ffrt_deps_t *wait_null = nullptr;
91     ffrt_submit_base(basicFunc_ht, &in, &ou, attr);
92     EXPECT_EQ(x, 0);
93     ffrt_wait_deps(wait_null);
94     EXPECT_EQ(x, 0);
95     ffrt_wait_deps(&wait);
96     EXPECT_EQ(x, 0);
97     ffrt_wait();
98     EXPECT_EQ(x, 1);
99     ffrt_task_attr_destroy(attr);
100     free(attr);
101     attr = nullptr;
102 }