store_update_data_unittest.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/optimization_guide/core/store_update_data.h"
  5. #include <string>
  6. #include "base/time/time.h"
  7. #include "base/version.h"
  8. #include "components/optimization_guide/core/optimization_guide_features.h"
  9. #include "components/optimization_guide/proto/hint_cache.pb.h"
  10. #include "components/optimization_guide/proto/hints.pb.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace optimization_guide {
  13. namespace {
  14. TEST(StoreUpdateDataTest, BuildComponentStoreUpdateData) {
  15. // Verify creating a Component Hint update package.
  16. base::Version v1("1.2.3.4");
  17. proto::Hint hint1;
  18. hint1.set_key("foo.org");
  19. hint1.set_key_representation(proto::HOST);
  20. proto::PageHint* page_hint1 = hint1.add_page_hints();
  21. page_hint1->set_page_pattern("slowpage");
  22. proto::Hint hint2;
  23. hint2.set_key("bar.com");
  24. hint2.set_key_representation(proto::HOST);
  25. proto::PageHint* page_hint2 = hint2.add_page_hints();
  26. page_hint2->set_page_pattern("slowpagealso");
  27. std::unique_ptr<StoreUpdateData> component_update =
  28. StoreUpdateData::CreateComponentStoreUpdateData(v1);
  29. component_update->MoveHintIntoUpdateData(std::move(hint1));
  30. component_update->MoveHintIntoUpdateData(std::move(hint2));
  31. EXPECT_TRUE(component_update->component_version().has_value());
  32. EXPECT_FALSE(component_update->update_time().has_value());
  33. EXPECT_EQ(v1, *component_update->component_version());
  34. // Verify there are 3 store entries: 1 for the metadata entry plus
  35. // the 2 added hint entries.
  36. EXPECT_EQ(3ul, component_update->TakeUpdateEntries()->size());
  37. }
  38. TEST(StoreUpdateDataTest, BuildFetchUpdateDataUsesDefaultCacheDuration) {
  39. // Verify creating a Fetched Hint update package.
  40. base::Time update_time = base::Time::Now();
  41. proto::Hint hint1;
  42. hint1.set_key("foo.org");
  43. hint1.set_key_representation(proto::HOST);
  44. proto::PageHint* page_hint1 = hint1.add_page_hints();
  45. page_hint1->set_page_pattern("slowpage");
  46. std::unique_ptr<StoreUpdateData> fetch_update =
  47. StoreUpdateData::CreateFetchedStoreUpdateData(update_time);
  48. fetch_update->MoveHintIntoUpdateData(std::move(hint1));
  49. EXPECT_FALSE(fetch_update->component_version().has_value());
  50. EXPECT_TRUE(fetch_update->update_time().has_value());
  51. EXPECT_EQ(update_time, *fetch_update->update_time());
  52. // Verify there are 2 store entries: 1 for the metadata entry plus
  53. // the 1 added hint entries.
  54. const auto update_entries = fetch_update->TakeUpdateEntries();
  55. EXPECT_EQ(2ul, update_entries->size());
  56. // Verify expiry time taken from hint rather than the default expiry time of
  57. // the store update data.
  58. for (const auto& entry : *update_entries) {
  59. proto::StoreEntry store_entry = entry.second;
  60. if (store_entry.entry_type() == proto::FETCHED_HINT) {
  61. base::Time expected_expiry_time =
  62. base::Time::Now() + features::StoredFetchedHintsFreshnessDuration();
  63. EXPECT_EQ(expected_expiry_time.ToDeltaSinceWindowsEpoch().InSeconds(),
  64. store_entry.expiry_time_secs());
  65. break;
  66. }
  67. }
  68. }
  69. TEST(StoreUpdateDataTest,
  70. BuildFetchUpdateDataUsesCacheDurationFromHintIfAvailable) {
  71. // Verify creating a Fetched Hint update package.
  72. int max_cache_duration_secs = 60;
  73. base::Time update_time = base::Time::Now();
  74. proto::Hint hint1;
  75. hint1.set_key("foo.org");
  76. hint1.set_key_representation(proto::HOST);
  77. hint1.mutable_max_cache_duration()->set_seconds(max_cache_duration_secs);
  78. proto::PageHint* page_hint1 = hint1.add_page_hints();
  79. page_hint1->set_page_pattern("slowpage");
  80. std::unique_ptr<StoreUpdateData> fetch_update =
  81. StoreUpdateData::CreateFetchedStoreUpdateData(update_time);
  82. fetch_update->MoveHintIntoUpdateData(std::move(hint1));
  83. EXPECT_FALSE(fetch_update->component_version().has_value());
  84. EXPECT_TRUE(fetch_update->update_time().has_value());
  85. EXPECT_EQ(update_time, *fetch_update->update_time());
  86. // Verify there are 2 store entries: 1 for the metadata entry plus
  87. // the 1 added hint entries.
  88. const auto update_entries = fetch_update->TakeUpdateEntries();
  89. EXPECT_EQ(2ul, update_entries->size());
  90. // Verify expiry time taken from hint rather than the default expiry time of
  91. // the store update data.
  92. for (const auto& entry : *update_entries) {
  93. proto::StoreEntry store_entry = entry.second;
  94. if (store_entry.entry_type() == proto::FETCHED_HINT) {
  95. base::Time expected_expiry_time =
  96. base::Time::Now() + base::Seconds(max_cache_duration_secs);
  97. EXPECT_EQ(expected_expiry_time.ToDeltaSinceWindowsEpoch().InSeconds(),
  98. store_entry.expiry_time_secs());
  99. break;
  100. }
  101. }
  102. }
  103. TEST(StoreUpdateDataTest, BuildPredictionModelUpdateData) {
  104. // Verify creating a Prediction Model update data.
  105. proto::PredictionModel prediction_model;
  106. proto::ModelInfo* model_info = prediction_model.mutable_model_info();
  107. model_info->set_version(1);
  108. model_info->set_optimization_target(
  109. proto::OPTIMIZATION_TARGET_PAINFUL_PAGE_LOAD);
  110. model_info->add_supported_model_engine_versions(
  111. proto::ModelEngineVersion::MODEL_ENGINE_VERSION_DECISION_TREE);
  112. model_info->set_keep_beyond_valid_duration(false);
  113. model_info->mutable_valid_duration()->set_seconds(3);
  114. base::Time expected_expiry_time = base::Time::Now() + base::Seconds(3);
  115. std::unique_ptr<StoreUpdateData> prediction_model_update =
  116. StoreUpdateData::CreatePredictionModelStoreUpdateData(
  117. expected_expiry_time);
  118. prediction_model_update->CopyPredictionModelIntoUpdateData(prediction_model);
  119. EXPECT_FALSE(prediction_model_update->component_version().has_value());
  120. EXPECT_FALSE(prediction_model_update->update_time().has_value());
  121. // Verify there is 1 store entry.
  122. const auto update_entries = prediction_model_update->TakeUpdateEntries();
  123. EXPECT_EQ(1ul, update_entries->size());
  124. // Verify expiry time taken from model rather than the default expiry time of
  125. // the store update data.
  126. bool found_prediction_model_entry = false;
  127. for (const auto& entry : *update_entries) {
  128. proto::StoreEntry store_entry = entry.second;
  129. if (store_entry.entry_type() == proto::PREDICTION_MODEL) {
  130. found_prediction_model_entry = true;
  131. EXPECT_EQ(expected_expiry_time.ToDeltaSinceWindowsEpoch().InSeconds(),
  132. store_entry.expiry_time_secs());
  133. EXPECT_EQ(store_entry.keep_beyond_valid_duration(),
  134. model_info->keep_beyond_valid_duration());
  135. break;
  136. }
  137. }
  138. EXPECT_TRUE(found_prediction_model_entry);
  139. }
  140. TEST(StoreUpdateDataTest, DefaultExpiryPredictionModelUpdateData) {
  141. // Verify creating a Prediction Model update data.
  142. proto::PredictionModel prediction_model;
  143. proto::ModelInfo* model_info = prediction_model.mutable_model_info();
  144. model_info->set_version(1);
  145. model_info->set_optimization_target(
  146. proto::OPTIMIZATION_TARGET_PAINFUL_PAGE_LOAD);
  147. model_info->add_supported_model_engine_versions(
  148. proto::ModelEngineVersion::MODEL_ENGINE_VERSION_DECISION_TREE);
  149. model_info->set_keep_beyond_valid_duration(false);
  150. std::unique_ptr<StoreUpdateData> prediction_model_update =
  151. StoreUpdateData::CreatePredictionModelStoreUpdateData(base::Time::Now());
  152. prediction_model_update->CopyPredictionModelIntoUpdateData(prediction_model);
  153. EXPECT_FALSE(prediction_model_update->component_version().has_value());
  154. EXPECT_FALSE(prediction_model_update->update_time().has_value());
  155. // Verify there is 1 store entry.
  156. const auto update_entries = prediction_model_update->TakeUpdateEntries();
  157. EXPECT_EQ(1ul, update_entries->size());
  158. // Verify expiry time taken from the default expiry time of model.
  159. bool found_prediction_model_entry = false;
  160. for (const auto& entry : *update_entries) {
  161. proto::StoreEntry store_entry = entry.second;
  162. if (store_entry.entry_type() == proto::PREDICTION_MODEL) {
  163. found_prediction_model_entry = true;
  164. base::Time expected_expiry_time =
  165. base::Time::Now() + features::StoredModelsValidDuration();
  166. EXPECT_EQ(expected_expiry_time.ToDeltaSinceWindowsEpoch().InSeconds(),
  167. store_entry.expiry_time_secs());
  168. EXPECT_EQ(store_entry.keep_beyond_valid_duration(),
  169. model_info->keep_beyond_valid_duration());
  170. break;
  171. }
  172. }
  173. EXPECT_TRUE(found_prediction_model_entry);
  174. }
  175. } // namespace
  176. } // namespace optimization_guide