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/gc_request.h"
17 #include "common_components/heap/collector/gc_stats.h"
18 #include "common_components/base/time_utils.h"
19
20 #include "common_components/tests/test_helper.h"
21
22 using namespace common;
23
24 namespace common {
25 class GCRequestTest : public common::test::BaseTestWithScope {
26 };
27
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Heu_Test1)28 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Heu_Test1) {
29 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
30 GCStats::SetPrevGCFinishTime(now);
31 GCRequest req = { GC_REASON_HEU, "heuristic", false, true, 0, 0 };
32 req.SetMinInterval(now + 1000);
33
34 bool result = req.ShouldBeIgnored();
35 EXPECT_TRUE(result);
36 }
37
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Heu_Test2)38 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Heu_Test2) {
39 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
40 GCStats::SetPrevGCFinishTime(1000);
41 GCRequest req = { GC_REASON_HEU, "heuristic", false, true, 0, 0 };
42 req.SetMinInterval(1000);
43
44 bool result = req.ShouldBeIgnored();
45 EXPECT_FALSE(result);
46 }
47
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Young_Test1)48 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Young_Test1) {
49 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
50 GCStats::SetPrevGCFinishTime(now);
51 GCRequest req = { GC_REASON_YOUNG, "young", false, true, 0, 0 };
52 req.SetMinInterval(now + 1000);
53
54 bool result = req.ShouldBeIgnored();
55 EXPECT_TRUE(result);
56 }
57
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Young_Test2)58 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Young_Test2) {
59 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
60 GCStats::SetPrevGCFinishTime(1000);
61 GCRequest req = { GC_REASON_YOUNG, "young", false, true, 0, 0 };
62 req.SetMinInterval(1000);
63
64 bool result = req.ShouldBeIgnored();
65 EXPECT_FALSE(result);
66 }
67
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Native_Test1)68 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Native_Test1) {
69 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
70 GCStats::SetPrevGCFinishTime(now);
71 GCRequest req = { GC_REASON_NATIVE, "native", false, true, 0, 0 };
72 req.SetMinInterval(now + 1000);
73
74 bool result = req.ShouldBeIgnored();
75 EXPECT_TRUE(result);
76 }
77
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Native_Test2)78 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Native_Test2) {
79 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
80 GCStats::SetPrevGCFinishTime(1000);
81 GCRequest req = { GC_REASON_NATIVE, "native", false, true, 0, 0 };
82 req.SetMinInterval(1000);
83
84 bool result = req.ShouldBeIgnored();
85 EXPECT_FALSE(result);
86 }
87
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Oom_Test1)88 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Oom_Test1) {
89 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
90 GCRequest req = { GC_REASON_OOM, "oom", false, true, 0, 0 };
91 req.SetMinInterval(now + 1000);
92 req.SetPrevRequestTime(now);
93
94 bool result = req.ShouldBeIgnored();
95 EXPECT_TRUE(result);
96 }
97
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Oom_Test2)98 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Oom_Test2) {
99 GCRequest req = { GC_REASON_OOM, "oom", false, true, 0, 0 };
100 req.SetMinInterval(0);
101 req.SetPrevRequestTime(1000);
102
103 bool result = req.ShouldBeIgnored();
104 EXPECT_FALSE(result);
105 }
106
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Oom_Test3)107 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Oom_Test3) {
108 GCRequest req = { GC_REASON_OOM, "oom", false, true, 0, 0 };
109 req.SetMinInterval(1000);
110 req.SetPrevRequestTime(1000);
111
112 bool result = req.ShouldBeIgnored();
113 EXPECT_FALSE(result);
114 }
115
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Force_Test1)116 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Force_Test1) {
117 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
118 GCRequest req = { GC_REASON_FORCE, "force", false, true, 0, 0 };
119 req.SetMinInterval(now + 1000);
120 req.SetPrevRequestTime(now);
121
122 bool result = req.ShouldBeIgnored();
123 EXPECT_TRUE(result);
124 }
125
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Force_Test2)126 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Force_Test2) {
127 GCRequest req = { GC_REASON_FORCE, "force", false, true, 0, 0 };
128 req.SetMinInterval(0);
129 req.SetPrevRequestTime(1000);
130
131 bool result = req.ShouldBeIgnored();
132 EXPECT_FALSE(result);
133 }
134
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_Force_Test3)135 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Force_Test3) {
136 GCRequest req = { GC_REASON_FORCE, "force", false, true, 0, 0 };
137 req.SetMinInterval(1000);
138 req.SetPrevRequestTime(1000);
139
140 bool result = req.ShouldBeIgnored();
141 EXPECT_FALSE(result);
142 }
143
HWTEST_F_L0(GCRequestTest,ShouldBeIgnored_User_Test1)144 HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_User_Test1) {
145 GCRequest req = { GC_REASON_USER, "user", false, true, 0, 0 };
146 bool result = req.ShouldBeIgnored();
147 EXPECT_FALSE(result);
148 }
149 } // namespace common::test