1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/file.h>
18 #include <android-base/properties.h>
19 #include <android-base/stringprintf.h>
20 #include <gtest/gtest.h>
21 
22 #include <algorithm>
23 #include <thread>
24 
25 #include "perfmgr/PropertyNode.h"
26 
27 namespace android {
28 namespace perfmgr {
29 
30 using std::literals::chrono_literals::operator""ms;
31 
32 constexpr double kTIMING_TOLERANCE_MS = std::chrono::milliseconds(25).count();
33 constexpr auto kSLEEP_TOLERANCE_MS = 2ms;
34 
_VerifyPropertyValue(const std::string & path,const std::string & value)35 static inline void _VerifyPropertyValue(const std::string& path,
36                                         const std::string& value) {
37     std::string s = android::base::GetProperty(path, "");
38     EXPECT_EQ(value, s);
39 }
40 
_InitProperty(const std::string & path)41 static inline const std::string _InitProperty(const std::string& path) {
42     EXPECT_TRUE(android::base::SetProperty(path, ""))
43         << "failed to clear property";
44     return path;
45 }
46 
47 // Test init with no default value
TEST(PropertyNodeTest,NoInitDefaultTest)48 TEST(PropertyNodeTest, NoInitDefaultTest) {
49     std::string key = _InitProperty("test.libperfmgr.key");
50     PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
51     t.Update(false);
52     _VerifyPropertyValue(key, "");
53 }
54 
55 // Test init with default value
TEST(PropertyNodeTest,InitDefaultTest)56 TEST(PropertyNodeTest, InitDefaultTest) {
57     std::string key = _InitProperty("test.libperfmgr.key");
58     PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, true);
59     t.Update(false);
60     _VerifyPropertyValue(key, "value1");
61     std::string key2 = _InitProperty("test.libperfmgr.key2");
62     PropertyNode t2("t2", key2, {{"value0"}, {"value1"}, {"value2"}}, 0, true);
63     t2.Update(false);
64     _VerifyPropertyValue(key2, "value0");
65 }
66 
67 // Test DumpToFd
TEST(PropertyNodeTest,DumpToFdTest)68 TEST(PropertyNodeTest, DumpToFdTest) {
69     std::string key = _InitProperty("test.libperfmgr.key");
70     PropertyNode t("test_dump", key, {{"value0"}, {"value1"}, {"value2"}}, 1,
71                    true);
72     t.Update(false);
73     TemporaryFile dumptf;
74     t.DumpToFd(dumptf.fd);
75     fsync(dumptf.fd);
76     std::string buf(android::base::StringPrintf(
77             "Node Name\t"
78             "Property Name\t"
79             "Current Index\t"
80             "Current Value\n"
81             "%s\t%s\t%zu\t%s\n",
82             "test_dump", key.c_str(), static_cast<size_t>(1), "value1"));
83     std::string s;
84     EXPECT_TRUE(android::base::ReadFileToString(dumptf.path, &s))
85         << strerror(errno);
86     EXPECT_EQ(buf, s);
87 }
88 
89 // Test GetValueIndex
TEST(PropertyNodeTest,GetValueIndexTest)90 TEST(PropertyNodeTest, GetValueIndexTest) {
91     std::string key = _InitProperty("test.libperfmgr.key");
92     PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
93     std::size_t index = 0;
94     EXPECT_TRUE(t.GetValueIndex("value2", &index));
95     EXPECT_EQ(2u, index);
96     index = 1234;
97     EXPECT_FALSE(t.GetValueIndex("NON_EXIST", &index));
98     EXPECT_EQ(1234u, index);
99 }
100 
101 // Test GetValues
TEST(PropertyNodeTest,GetValuesTest)102 TEST(PropertyNodeTest, GetValuesTest) {
103     std::string key = _InitProperty("test.libperfmgr.key");
104     PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
105     std::vector values = t.GetValues();
106     EXPECT_EQ(3u, values.size());
107     EXPECT_EQ("value0", values[0]);
108     EXPECT_EQ("value1", values[1]);
109     EXPECT_EQ("value2", values[2]);
110 }
111 
112 // Test get more properties
TEST(PropertyNodeTest,GetPropertiesTest)113 TEST(PropertyNodeTest, GetPropertiesTest) {
114     std::string test_name = "TESTREQ_1";
115     std::string test_path = "TEST_PATH";
116     PropertyNode t(test_name, test_path, {}, 0, false);
117     EXPECT_EQ(test_name, t.GetName());
118     EXPECT_EQ(test_path, t.GetPath());
119     EXPECT_EQ(0u, t.GetValues().size());
120     EXPECT_EQ(0u, t.GetDefaultIndex());
121     EXPECT_FALSE(t.GetResetOnInit());
122 }
123 
124 // Test add request
TEST(PropertyNodeTest,AddRequestTest)125 TEST(PropertyNodeTest, AddRequestTest) {
126     std::string key = _InitProperty("test.libperfmgr.key");
127     PropertyNode t("t", key, {{"value0"}, {"value1"}, {""}}, 2, true);
128     auto start = std::chrono::steady_clock::now();
129     EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
130     std::chrono::milliseconds expire_time = t.Update(true);
131     // Add request @ value1
132     _VerifyPropertyValue(key, "value1");
133     EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
134                 kTIMING_TOLERANCE_MS);
135     // Add request @ value0 higher prio than value1
136     EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
137     expire_time = t.Update(true);
138     _VerifyPropertyValue(key, "value0");
139     EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
140                 kTIMING_TOLERANCE_MS);
141     // Let high prio request timeout, now only request @ value1 active
142     std::this_thread::sleep_for(expire_time + kSLEEP_TOLERANCE_MS);
143     expire_time = t.Update(true);
144     _VerifyPropertyValue(key, "value1");
145     EXPECT_NEAR(std::chrono::milliseconds(300).count(), expire_time.count(),
146                 kTIMING_TOLERANCE_MS);
147     // Let all requests timeout, now default value2
148     std::this_thread::sleep_for(expire_time + kSLEEP_TOLERANCE_MS);
149     expire_time = t.Update(true);
150     _VerifyPropertyValue(key, "");
151     EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
152 }
153 
154 // Test remove request
TEST(PropertyNodeTest,RemoveRequestTest)155 TEST(PropertyNodeTest, RemoveRequestTest) {
156     std::string key = _InitProperty("test.libperfmgr.key");
157     PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 2, true);
158     auto start = std::chrono::steady_clock::now();
159     EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
160     std::chrono::milliseconds expire_time = t.Update(true);
161     // Add request @ value1
162     _VerifyPropertyValue(key, "value1");
163     EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
164                 kTIMING_TOLERANCE_MS);
165     // Add request @ value0 higher prio than value1
166     EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
167     expire_time = t.Update(true);
168     _VerifyPropertyValue(key, "value0");
169     EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
170                 kTIMING_TOLERANCE_MS);
171     // Remove high prio request, now only request @ value1 active
172     t.RemoveRequest("LAUNCH");
173     expire_time = t.Update(true);
174     _VerifyPropertyValue(key, "value1");
175     EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
176                 kTIMING_TOLERANCE_MS);
177     // Remove request, now default value2
178     t.RemoveRequest("INTERACTION");
179     expire_time = t.Update(true);
180     _VerifyPropertyValue(key, "value2");
181     EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
182 }
183 
184 // Test add request
TEST(PropertyNodeTest,AddRequestTestOverride)185 TEST(PropertyNodeTest, AddRequestTestOverride) {
186     std::string key = _InitProperty("test.libperfmgr.key");
187     PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 2, true);
188     auto start = std::chrono::steady_clock::now();
189     EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
190     std::chrono::milliseconds expire_time = t.Update(true);
191     // Add request @ value1
192     _VerifyPropertyValue(key, "value1");
193     EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
194                 kTIMING_TOLERANCE_MS);
195     // Add request @ value0 higher prio than value1
196     EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
197     expire_time = t.Update(true);
198     _VerifyPropertyValue(key, "value0");
199     EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
200                 kTIMING_TOLERANCE_MS);
201     // Add request @ value0 shorter
202     EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 100ms));
203     expire_time = t.Update(true);
204     _VerifyPropertyValue(key, "value0");
205     EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
206                 kTIMING_TOLERANCE_MS);
207     // Add request @ value0 longer
208     EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 300ms));
209     expire_time = t.Update(true);
210     _VerifyPropertyValue(key, "value0");
211     EXPECT_NEAR(std::chrono::milliseconds(300).count(), expire_time.count(),
212                 kTIMING_TOLERANCE_MS);
213     // Remove high prio request, now only request @ value1 active
214     t.RemoveRequest("LAUNCH");
215     expire_time = t.Update(true);
216     _VerifyPropertyValue(key, "value1");
217     EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
218                 kTIMING_TOLERANCE_MS);
219     // Remove request, now default value2
220     t.RemoveRequest("INTERACTION");
221     expire_time = t.Update(true);
222     _VerifyPropertyValue(key, "value2");
223     EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
224 }
225 
226 }  // namespace perfmgr
227 }  // namespace android
228