diff options
Diffstat (limited to 'odrefresh/odr_metrics_record_test.cc')
-rw-r--r-- | odrefresh/odr_metrics_record_test.cc | 124 |
1 files changed, 82 insertions, 42 deletions
diff --git a/odrefresh/odr_metrics_record_test.cc b/odrefresh/odr_metrics_record_test.cc index dd739d68f2..fbb5b99063 100644 --- a/odrefresh/odr_metrics_record_test.cc +++ b/odrefresh/odr_metrics_record_test.cc @@ -20,6 +20,8 @@ #include <fstream> +#include "android-base/result-gmock.h" +#include "android-base/stringprintf.h" #include "base/common_art_test.h" namespace art { @@ -27,86 +29,124 @@ namespace odrefresh { class OdrMetricsRecordTest : public CommonArtTest {}; +using android::base::testing::Ok; +using android::base::testing::HasError; +using android::base::testing::WithMessage; + TEST_F(OdrMetricsRecordTest, HappyPath) { - const OdrMetricsRecord expected { + const OdrMetricsRecord expected{ + .odrefresh_metrics_version = art::odrefresh::kOdrefreshMetricsVersion, .art_apex_version = 0x01233456'789abcde, .trigger = 0x01020304, .stage_reached = 0x11121314, .status = 0x21222324, - .primary_bcp_compilation_seconds = 0x31323334, - .secondary_bcp_compilation_seconds = 0x41424344, - .system_server_compilation_seconds = 0x51525354, .cache_space_free_start_mib = 0x61626364, - .cache_space_free_end_mib = 0x71727374 + .cache_space_free_end_mib = 0x71727374, + .primary_bcp_compilation_millis = 0x31323334, + .secondary_bcp_compilation_millis = 0x41424344, + .system_server_compilation_millis = 0x51525354 }; ScratchDir dir(/*keep_files=*/false); - std::string file_path = dir.GetPath() + "/metrics-record.txt"; - - { - std::ofstream ofs(file_path); - ofs << expected; - ASSERT_FALSE(ofs.fail()); - ofs.close(); - } + std::string file_path = dir.GetPath() + "/metrics-record.xml"; + ASSERT_THAT(expected.WriteToFile(file_path), Ok()); OdrMetricsRecord actual {}; - { - std::ifstream ifs(file_path); - ifs >> actual; - ASSERT_TRUE(ifs.eof()); - } + ASSERT_THAT(actual.ReadFromFile(file_path), Ok()); + ASSERT_EQ(expected.odrefresh_metrics_version, actual.odrefresh_metrics_version); ASSERT_EQ(expected.art_apex_version, actual.art_apex_version); ASSERT_EQ(expected.trigger, actual.trigger); ASSERT_EQ(expected.stage_reached, actual.stage_reached); ASSERT_EQ(expected.status, actual.status); - ASSERT_EQ(expected.primary_bcp_compilation_seconds, actual.primary_bcp_compilation_seconds); - ASSERT_EQ(expected.secondary_bcp_compilation_seconds, actual.secondary_bcp_compilation_seconds); - ASSERT_EQ(expected.system_server_compilation_seconds, actual.system_server_compilation_seconds); ASSERT_EQ(expected.cache_space_free_start_mib, actual.cache_space_free_start_mib); ASSERT_EQ(expected.cache_space_free_end_mib, actual.cache_space_free_end_mib); + ASSERT_EQ(expected.primary_bcp_compilation_millis, actual.primary_bcp_compilation_millis); + ASSERT_EQ(expected.secondary_bcp_compilation_millis, actual.secondary_bcp_compilation_millis); + ASSERT_EQ(expected.system_server_compilation_millis, actual.system_server_compilation_millis); ASSERT_EQ(0, memcmp(&expected, &actual, sizeof(expected))); } TEST_F(OdrMetricsRecordTest, EmptyInput) { ScratchDir dir(/*keep_files=*/false); - std::string file_path = dir.GetPath() + "/metrics-record.txt"; - - std::ifstream ifs(file_path); - OdrMetricsRecord record; - ifs >> record; + std::string file_path = dir.GetPath() + "/metrics-record.xml"; - ASSERT_TRUE(ifs.fail()); - ASSERT_TRUE(!ifs); + OdrMetricsRecord record{}; + ASSERT_THAT(record.ReadFromFile(file_path), testing::Not(Ok())); } -TEST_F(OdrMetricsRecordTest, ClosedInput) { +TEST_F(OdrMetricsRecordTest, UnexpectedInput) { ScratchDir dir(/*keep_files=*/false); - std::string file_path = dir.GetPath() + "/metrics-record.txt"; + std::string file_path = dir.GetPath() + "/metrics-record.xml"; - std::ifstream ifs(file_path); - ifs.close(); + std::ofstream ofs(file_path); + ofs << "<not_odrefresh_metrics></not_odrefresh_metrics>"; + ofs.close(); - OdrMetricsRecord record; - ifs >> record; + OdrMetricsRecord record{}; + ASSERT_THAT( + record.ReadFromFile(file_path), + HasError(WithMessage("odrefresh_metrics element not found in " + file_path))); +} + +TEST_F(OdrMetricsRecordTest, ExpectedElementNotFound) { + ScratchDir dir(/*keep_files=*/false); + std::string file_path = dir.GetPath() + "/metrics-record.xml"; - ASSERT_TRUE(ifs.fail()); - ASSERT_TRUE(!ifs); + std::ofstream ofs(file_path); + ofs << "<odrefresh_metrics>"; + ofs << "<not_valid_metric>25</not_valid_metric>"; + ofs << "</odrefresh_metrics>"; + ofs.close(); + + OdrMetricsRecord record{}; + ASSERT_THAT( + record.ReadFromFile(file_path), + HasError(WithMessage("Expected Odrefresh metric odrefresh_metrics_version not found"))); } -TEST_F(OdrMetricsRecordTest, ClosedOutput) { +TEST_F(OdrMetricsRecordTest, UnexpectedOdrefreshMetricsVersion) { ScratchDir dir(/*keep_files=*/false); - std::string file_path = dir.GetPath() + "/metrics-record.txt"; + std::string file_path = dir.GetPath() + "/metrics-record.xml"; std::ofstream ofs(file_path); + ofs << "<odrefresh_metrics>"; + ofs << "<odrefresh_metrics_version>0</odrefresh_metrics_version>"; + ofs << "</odrefresh_metrics>"; ofs.close(); - OdrMetricsRecord record {}; - ofs << record; + OdrMetricsRecord record{}; + std::string expected_error = android::base::StringPrintf( + "odrefresh_metrics_version 0 is different than expected (%d)", + kOdrefreshMetricsVersion); + ASSERT_THAT(record.ReadFromFile(file_path), + HasError(WithMessage(expected_error))); +} + +TEST_F(OdrMetricsRecordTest, UnexpectedType) { + ScratchDir dir(/*keep_files=*/false); + std::string file_path = dir.GetPath() + "/metrics-record.xml"; + + std::ofstream ofs(file_path); + ofs << "<odrefresh_metrics>"; + ofs << "<odrefresh_metrics_version>" << kOdrefreshMetricsVersion + << "</odrefresh_metrics_version>"; + ofs << "<art_apex_version>81966764218039518</art_apex_version>"; + ofs << "<trigger>16909060</trigger>"; + ofs << "<stage_reached>286397204</stage_reached>"; + ofs << "<status>abcd</status>"; // It should be an int32. + ofs << "<cache_space_free_start_mib>1633837924</cache_space_free_start_mib>"; + ofs << "<cache_space_free_end_mib>1903326068</cache_space_free_end_mib>"; + ofs << "<primary_bcp_compilation_millis>825373492</primary_bcp_compilation_millis>"; + ofs << "<secondary_bcp_compilation_millis>1094861636</secondary_bcp_compilation_millis>"; + ofs << "<system_server_compilation_millis>1364349780</system_server_compilation_millis>"; + ofs << "</odrefresh_metrics>"; + ofs.close(); - ASSERT_TRUE(ofs.fail()); - ASSERT_TRUE(!ofs.good()); + OdrMetricsRecord record{}; + ASSERT_THAT( + record.ReadFromFile(file_path), + HasError(WithMessage("Odrefresh metric status is not an int32"))); } } // namespace odrefresh |