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/task_queue.h"
17 #include "common_components/heap/collector/collector_proxy.h"
18 #include "common_components/heap/collector/gc_request.h"
19 #include "common_components/tests/test_helper.h"
20
21 namespace common {
22 class StubAllocator : public Allocator {
23 public:
Allocate(size_t size,AllocType allocType)24 HeapAddress Allocate(size_t size, AllocType allocType) override { return 0; }
AllocateNoGC(size_t size,AllocType allocType)25 HeapAddress AllocateNoGC(size_t size, AllocType allocType) override { return 0; }
ForEachObject(const std::function<void (BaseObject *)> &,bool safe) const26 bool ForEachObject(const std::function<void(BaseObject*)>&, bool safe) const override { return true; }
ReclaimGarbageMemory(bool releaseAll)27 size_t ReclaimGarbageMemory(bool releaseAll) override { return 0; }
LargeObjectSize() const28 size_t LargeObjectSize() const override { return 0; }
GetAllocatedBytes() const29 size_t GetAllocatedBytes() const override { return 0; }
Init(const RuntimeParam & param)30 void Init(const RuntimeParam& param) override {}
GetMaxCapacity() const31 size_t GetMaxCapacity() const override { return 0; }
GetCurrentCapacity() const32 size_t GetCurrentCapacity() const override { return 0; }
GetUsedPageSize() const33 size_t GetUsedPageSize() const override { return 0; }
GetSpaceStartAddress() const34 HeapAddress GetSpaceStartAddress() const override { return 0; }
GetSpaceEndAddress() const35 HeapAddress GetSpaceEndAddress() const override { return 0; }
36 #ifndef NDEBUG
IsHeapObject(HeapAddress) const37 bool IsHeapObject(HeapAddress) const override { return false; }
38 #endif
FeedHungryBuffers()39 void FeedHungryBuffers() override {}
GetSurvivedSize() const40 size_t GetSurvivedSize() const override { return 0; }
41 };
42
43 class StubCollectorProxy : public CollectorProxy {
44 public:
StubCollectorProxy(Allocator & allocator,CollectorResources & resources)45 explicit StubCollectorProxy(Allocator& allocator, CollectorResources& resources)
46 : CollectorProxy(allocator, resources) {}
47
RunGarbageCollection(uint64_t gcIndex,GCReason reason,GCType gcType)48 void RunGarbageCollection(uint64_t gcIndex, GCReason reason, GCType gcType) override {}
49 };
50 }
51
52 namespace common {
53 class DummyCollectorProxy : public CollectorProxy {
54 public:
DummyCollectorProxy(Allocator & alloc,CollectorResources & res)55 explicit DummyCollectorProxy(Allocator& alloc, CollectorResources& res)
56 : CollectorProxy(alloc, res) {}
RunGarbageCollection(uint64_t gcIndex,GCReason reason,GCType gcType)57 void RunGarbageCollection(uint64_t gcIndex, GCReason reason, GCType gcType) override {}
58 };
59
60 class DummyCollectorResources : public CollectorResources {
61 private:
62 DummyCollectorProxy proxy_;
63
64 public:
DummyCollectorResources(Allocator & alloc)65 explicit DummyCollectorResources(Allocator& alloc)
66 : CollectorResources(proxy_),
67 proxy_(alloc, *this) {}
68 };
69 }
70
71 namespace common::test {
72 class GCRunnerTest : public common::test::BaseTestWithScope {
73 protected:
SetUp()74 void SetUp() override
75 {
76 allocator_ = std::make_unique<common::StubAllocator>();
77 dummyResources_ = std::make_unique<common::DummyCollectorResources>(*allocator_);
78 proxy_ = std::make_unique<common::StubCollectorProxy>(*allocator_, *dummyResources_);
79 proxyStorage_ = std::make_unique<common::StubCollectorProxy>(*allocator_, *dummyResources_);
80 }
81
TearDown()82 void TearDown() override
83 {
84 proxyStorage_.reset();
85 dummyResources_.reset();
86 proxy_.reset();
87 allocator_.reset();
88 }
89
90 std::unique_ptr<common::StubAllocator> allocator_;
91 std::unique_ptr<common::StubCollectorProxy> proxy_;
92 std::unique_ptr<common::StubCollectorProxy> proxyStorage_;
93 std::unique_ptr<common::DummyCollectorResources> dummyResources_;
94 };
95
HWTEST_F_L0(GCRunnerTest,Execute_TerminateGC)96 HWTEST_F_L0(GCRunnerTest, Execute_TerminateGC) {
97 common::GCRunner runner(common::GCTask::GCTaskType::GC_TASK_TERMINATE_GC);
98 bool result = runner.Execute(proxyStorage_.get());
99 EXPECT_FALSE(result);
100 }
101
HWTEST_F_L0(GCRunnerTest,Execute_InvokeGC)102 HWTEST_F_L0(GCRunnerTest, Execute_InvokeGC) {
103 common::GCRunner runner(common::GCTask::GCTaskType::GC_TASK_INVOKE_GC, GC_REASON_BACKUP);
104 bool result = runner.Execute(proxyStorage_.get());
105 EXPECT_TRUE(result);
106 }
107
HWTEST_F_L0(GCRunnerTest,Execute_InvalidTaskType)108 HWTEST_F_L0(GCRunnerTest, Execute_InvalidTaskType) {
109 common::GCRunner runner(static_cast<common::GCTask::GCTaskType>(
110 static_cast<uint32_t>(common::GCTask::GCTaskType::GC_TASK_DUMP_HEAP_IDE) + 1));
111 bool result = runner.Execute(proxyStorage_.get());
112 EXPECT_TRUE(result);
113 }
114 } // namespace common::test
115