1 /**
2 * Copyright (c) 2021-2022 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
18 #include "libpandabase/utils/utf.h"
19 #include "runtime/include/class_linker.h"
20 #include "runtime/include/runtime.h"
21 #include "runtime/scheduler/worker_thread.h"
22 #include "runtime/scheduler/task.h"
23 #include "runtime/tests/class_linker_test_extension.h"
24
25 namespace panda::scheduler::test {
26
27 class WorkerThreadTest : public testing::Test {
28 public:
WorkerThreadTest()29 WorkerThreadTest()
30 {
31 RuntimeOptions options;
32 auto exec_path = panda::os::file::File::GetExecutablePath();
33 std::string panda_std_lib = exec_path.Value() + "/../pandastdlib/arkstdlib.abc";
34 options.SetBootPandaFiles({panda_std_lib});
35 Runtime::Create(options);
36 mainThread = Thread::GetCurrent();
37 Thread::SetCurrent(nullptr);
38 }
39
~WorkerThreadTest()40 ~WorkerThreadTest()
41 {
42 Thread::SetCurrent(mainThread);
43 Runtime::Destroy();
44 }
45
46 private:
47 Thread *mainThread = nullptr;
48 };
49
TEST_F(WorkerThreadTest,SwitchTasksInOneWorkerThread)50 TEST_F(WorkerThreadTest, SwitchTasksInOneWorkerThread)
51 {
52 auto wt = WorkerThread::AttachThread();
53 ASSERT_EQ(wt, WorkerThread::GetCurrent());
54 auto tk1 = Task::Create(Runtime::GetCurrent()->GetPandaVM());
55
56 tk1->SwitchFromWorkerThread();
57 ASSERT_EQ(tk1, Task::GetCurrent());
58 auto tk2 = Task::Create(Runtime::GetCurrent()->GetPandaVM());
59 Task::SuspendCurrent();
60
61 tk2->SwitchFromWorkerThread();
62 ASSERT_EQ(tk2, Task::GetCurrent());
63 Task::EndCurrent();
64
65 tk1->SwitchFromWorkerThread();
66 Task::EndCurrent();
67
68 WorkerThread::DetachThread();
69 }
70
71 } // namespace panda::scheduler::test
72