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/heap/collector/collector_resources.h"
17 #include "common_components/heap/collector/gc_request.h"
18 #include "common_components/mutator/mutator_manager.h"
19 #include "common_components/tests/test_helper.h"
20 #include "common_interfaces/base_runtime.h"
21
22 using namespace common;
23
24 namespace common::test {
25 class CollectorResourcesTest : public BaseTestWithScope {
26 protected:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 BaseRuntime::GetInstance()->Init();
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 BaseRuntime::GetInstance()->Fini();
35 }
36
SetUp()37 void SetUp() override
38 {
39 MutatorManager::Instance().CreateRuntimeMutator(ThreadType::GC_THREAD);
40 }
41
TearDown()42 void TearDown() override
43 {
44 MutatorManager::Instance().DestroyRuntimeMutator(ThreadType::GC_THREAD);
45 }
46 };
47
HWTEST_F_L0(CollectorResourcesTest,RequestHeapDumpTest)48 HWTEST_F_L0(CollectorResourcesTest, RequestHeapDumpTest) {
49 Heap::GetHeap().GetCollectorResources().RequestHeapDump(
50 GCTask::GCTaskType::GC_TASK_INVALID);
51 EXPECT_TRUE(Heap::GetHeap().IsGCEnabled());
52 }
53
HWTEST_F_L0(CollectorResourcesTest,RequestGC)54 HWTEST_F_L0(CollectorResourcesTest, RequestGC) {
55 GCRequest gcRequests = { GC_REASON_BACKUP, "backup", true, false, 0, 0 };
56 Heap::GetHeap().EnableGC(false);
57 EXPECT_TRUE(!Heap::GetHeap().GetCollectorResources().IsGCActive());
58 GCReason reason = gcRequests.reason;
59 Heap::GetHeap().GetCollectorResources().RequestGC(reason, true, common::GC_TYPE_FULL);
60 }
61
HWTEST_F_L0(CollectorResourcesTest,RequestGCAndWaitTest)62 HWTEST_F_L0(CollectorResourcesTest, RequestGCAndWaitTest) {
63 GCRequest gcRequests = { GC_REASON_USER, "user", false, false, 0, 0 };
64 GCReason reason = gcRequests.reason;
65 Heap::GetHeap().EnableGC(true);
66 EXPECT_TRUE(Heap::GetHeap().GetCollectorResources().IsGCActive());
67 Heap::GetHeap().GetCollectorResources().RequestGC(reason, false, common::GC_TYPE_FULL);
68 EXPECT_TRUE(!gcRequests.IsSyncGC());
69 }
70
HWTEST_F_L0(CollectorResourcesTest,GetGCThreadCountTest)71 HWTEST_F_L0(CollectorResourcesTest, GetGCThreadCountTest) {
72 uint32_t res = Heap::GetHeap().GetCollectorResources().GetGCThreadCount(false);
73 EXPECT_EQ(res, 2u);
74 }
75 } // namespace common::test