1 /*
2 * Copyright (c) 2025 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 "common_components/tests/test_helper.h"
17 #include "common_components/mutator/mutator.h"
18 #include "common_components/mutator/mutator_manager.h"
19 #include "common_interfaces/base_runtime.h"
20 #include "common_interfaces/thread/thread_holder-inl.h"
21 #include "common_interfaces/thread/thread_holder.h"
22 #include "common_interfaces/thread/thread_holder_manager.h"
23
24 using namespace common;
25
26 namespace ark {
27 class Coroutine;
28 }
29
30 namespace common::test {
31 using Coroutine = ark::Coroutine;
32 namespace {
SetCurrentThreadHolder(ThreadHolder * holder)33 void SetCurrentThreadHolder(ThreadHolder* holder)
34 {
35 ThreadHolder::SetCurrent(holder);
36 }
37 }
38
39 class ThreadHolderTest : public BaseTestWithScope {
40 protected:
SetUpTestCase()41 static void SetUpTestCase()
42 {
43 BaseRuntime::GetInstance()->Init();
44 }
45
TearDownTestCase()46 static void TearDownTestCase()
47 {
48 BaseRuntime::GetInstance()->Fini();
49 }
50
SetUp()51 void SetUp() override {}
52
TearDown()53 void TearDown() override {}
54 };
55
56 size_t callCount;
CallBackVisitor(void * root)57 void CallBackVisitor(void *root)
58 {
59 callCount++;
60 }
61
HWTEST_F_L0(ThreadHolderTest,VisitAllThreadsTest)62 HWTEST_F_L0(ThreadHolderTest, VisitAllThreadsTest) {
63 ThreadHolder *curHolder = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr);
64 ASSERT_TRUE(curHolder != nullptr);
65 SetCurrentThreadHolder(curHolder);
66
67 curHolder->WaitSuspension();
68
69 CommonRootVisitor visitor = nullptr;
70 curHolder->VisitAllThreads(visitor);
71
72 ThreadHolder* result = ThreadHolder::GetCurrent();
73 EXPECT_EQ(result, curHolder);
74 }
75
HWTEST_F_L0(ThreadHolderTest,UnregisterThreadHolderTest)76 HWTEST_F_L0(ThreadHolderTest, UnregisterThreadHolderTest) {
77 ThreadHolder *curHolder = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr);
78 Mutator *mutator = static_cast<Mutator *>(curHolder->GetMutator());
79
80 auto& mutator_manager = MutatorManager::Instance();
81 auto& list = mutator_manager.allMutatorList_;
82 list.clear();
83
84 ThreadHolderManager thManager;
85 thManager.UnregisterThreadHolder(curHolder);
86 EXPECT_EQ(mutator->PopRawObject(), nullptr);
87 }
88
HWTEST_F_L0(ThreadHolderTest,TryBindMutatorTest)89 HWTEST_F_L0(ThreadHolderTest, TryBindMutatorTest) {
90 ThreadHolder *curHolder = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr);
91 ASSERT_TRUE(curHolder != nullptr);
92 SetCurrentThreadHolder(curHolder);
93
94 ThreadLocal::SetProcessorFlag(true);
95 ThreadHolder::TryBindMutatorScope scope(curHolder);
96 EXPECT_EQ(ThreadLocal::GetAllocBuffer(), nullptr);
97 }
98 } // namespace panda::test