Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: base/metrics/field_trial_unittest.cc

Issue 2546653002: Store and retrieve features from shared memory (Closed)
Patch Set: add comment Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/field_trial.cc ('k') | content/app/content_main_runner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/metrics/field_trial.h" 5 #include "base/metrics/field_trial.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/base_switches.h" 9 #include "base/base_switches.h"
10 #include "base/build_time.h" 10 #include "base/build_time.h"
11 #include "base/feature_list.h" 11 #include "base/feature_list.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/field_trial_param_associator.h" 15 #include "base/metrics/field_trial_param_associator.h"
16 #include "base/rand_util.h" 16 #include "base/rand_util.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
20 #include "base/test/gtest_util.h" 20 #include "base/test/gtest_util.h"
21 #include "base/test/mock_entropy_provider.h" 21 #include "base/test/mock_entropy_provider.h"
22 #include "base/test/scoped_feature_list.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 namespace base { 25 namespace base {
25 26
26 namespace { 27 namespace {
27 28
28 // Default group name used by several tests. 29 // Default group name used by several tests.
29 const char kDefaultGroupName[] = "DefaultGroup"; 30 const char kDefaultGroupName[] = "DefaultGroup";
30 31
31 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. 32 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date.
(...skipping 1106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 } 1139 }
1139 1140
1140 #if defined(OS_WIN) 1141 #if defined(OS_WIN)
1141 TEST(FieldTrialListTest, TestCopyFieldTrialStateToFlags) { 1142 TEST(FieldTrialListTest, TestCopyFieldTrialStateToFlags) {
1142 base::FieldTrialList field_trial_list( 1143 base::FieldTrialList field_trial_list(
1143 base::MakeUnique<base::MockEntropyProvider>()); 1144 base::MakeUnique<base::MockEntropyProvider>());
1144 base::FieldTrialList::CreateFieldTrial("Trial1", "Group1"); 1145 base::FieldTrialList::CreateFieldTrial("Trial1", "Group1");
1145 base::FilePath test_file_path = base::FilePath(FILE_PATH_LITERAL("Program")); 1146 base::FilePath test_file_path = base::FilePath(FILE_PATH_LITERAL("Program"));
1146 base::CommandLine cmd_line = base::CommandLine(test_file_path); 1147 base::CommandLine cmd_line = base::CommandLine(test_file_path);
1147 const char field_trial_handle[] = "test-field-trial-handle"; 1148 const char field_trial_handle[] = "test-field-trial-handle";
1149 const char enable_features_switch[] = "test-enable-features";
1150 const char disable_features_switch[] = "test-disable-features";
1148 1151
1149 base::FieldTrialList::CopyFieldTrialStateToFlags(field_trial_handle, 1152 base::FieldTrialList::CopyFieldTrialStateToFlags(
1150 &cmd_line); 1153 field_trial_handle, enable_features_switch, disable_features_switch,
1154 &cmd_line);
1151 EXPECT_TRUE(cmd_line.HasSwitch(field_trial_handle) || 1155 EXPECT_TRUE(cmd_line.HasSwitch(field_trial_handle) ||
1152 cmd_line.HasSwitch(switches::kForceFieldTrials)); 1156 cmd_line.HasSwitch(switches::kForceFieldTrials));
1153 } 1157 }
1154 #endif 1158 #endif
1155 1159
1156 TEST(FieldTrialListTest, InstantiateAllocator) { 1160 TEST(FieldTrialListTest, InstantiateAllocator) {
1161 test::ScopedFeatureList scoped_feature_list;
1162 scoped_feature_list.Init();
1163
1157 FieldTrialList field_trial_list(nullptr); 1164 FieldTrialList field_trial_list(nullptr);
1158 FieldTrialList::CreateFieldTrial("Trial1", "Group1"); 1165 FieldTrialList::CreateFieldTrial("Trial1", "Group1");
1159 1166
1160 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); 1167 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
1161 void* memory = field_trial_list.field_trial_allocator_->shared_memory(); 1168 void* memory = field_trial_list.field_trial_allocator_->shared_memory();
1162 size_t used = field_trial_list.field_trial_allocator_->used(); 1169 size_t used = field_trial_list.field_trial_allocator_->used();
1163 1170
1164 // Ensure that the function is idempotent. 1171 // Ensure that the function is idempotent.
1165 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); 1172 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
1166 void* new_memory = field_trial_list.field_trial_allocator_->shared_memory(); 1173 void* new_memory = field_trial_list.field_trial_allocator_->shared_memory();
1167 size_t new_used = field_trial_list.field_trial_allocator_->used(); 1174 size_t new_used = field_trial_list.field_trial_allocator_->used();
1168 EXPECT_EQ(memory, new_memory); 1175 EXPECT_EQ(memory, new_memory);
1169 EXPECT_EQ(used, new_used); 1176 EXPECT_EQ(used, new_used);
1170 } 1177 }
1171 1178
1172 TEST(FieldTrialListTest, AddTrialsToAllocator) { 1179 TEST(FieldTrialListTest, AddTrialsToAllocator) {
1173 std::string save_string; 1180 std::string save_string;
1174 base::SharedMemoryHandle handle; 1181 base::SharedMemoryHandle handle;
1175 1182
1176 // Scoping the first FieldTrialList, as we need another one to test that it 1183 // Scoping the first FieldTrialList, as we need another one to test that it
1177 // matches. 1184 // matches.
1178 { 1185 {
1186 test::ScopedFeatureList scoped_feature_list;
1187 scoped_feature_list.Init();
1188
1179 FieldTrialList field_trial_list(nullptr); 1189 FieldTrialList field_trial_list(nullptr);
1180 FieldTrialList::CreateFieldTrial("Trial1", "Group1"); 1190 FieldTrialList::CreateFieldTrial("Trial1", "Group1");
1181 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); 1191 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
1182 FieldTrialList::AllStatesToString(&save_string); 1192 FieldTrialList::AllStatesToString(&save_string);
1183 handle = base::SharedMemory::DuplicateHandle( 1193 handle = base::SharedMemory::DuplicateHandle(
1184 field_trial_list.field_trial_allocator_->shared_memory()->handle()); 1194 field_trial_list.field_trial_allocator_->shared_memory()->handle());
1185 } 1195 }
1186 1196
1187 FieldTrialList field_trial_list2(nullptr); 1197 FieldTrialList field_trial_list2(nullptr);
1188 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); 1198 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true));
1189 // 4 KiB is enough to hold the trials only created for this test. 1199 // 4 KiB is enough to hold the trials only created for this test.
1190 shm.get()->Map(4 << 10); 1200 shm.get()->Map(4 << 10);
1191 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); 1201 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm));
1192 std::string check_string; 1202 std::string check_string;
1193 FieldTrialList::AllStatesToString(&check_string); 1203 FieldTrialList::AllStatesToString(&check_string);
1194 EXPECT_EQ(save_string, check_string); 1204 EXPECT_EQ(save_string, check_string);
1195 } 1205 }
1196 1206
1197 TEST(FieldTrialListTest, DoNotAddSimulatedFieldTrialsToAllocator) { 1207 TEST(FieldTrialListTest, DoNotAddSimulatedFieldTrialsToAllocator) {
1198 constexpr char kTrialName[] = "trial"; 1208 constexpr char kTrialName[] = "trial";
1199 base::SharedMemoryHandle handle; 1209 base::SharedMemoryHandle handle;
1200 { 1210 {
1211 test::ScopedFeatureList scoped_feature_list;
1212 scoped_feature_list.Init();
1213
1201 // Create a simulated trial and a real trial and call group() on them, which 1214 // Create a simulated trial and a real trial and call group() on them, which
1202 // should only add the real trial to the field trial allocator. 1215 // should only add the real trial to the field trial allocator.
1203 FieldTrialList field_trial_list(nullptr); 1216 FieldTrialList field_trial_list(nullptr);
1204 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); 1217 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
1205 1218
1206 // This shouldn't add to the allocator. 1219 // This shouldn't add to the allocator.
1207 scoped_refptr<FieldTrial> simulated_trial = 1220 scoped_refptr<FieldTrial> simulated_trial =
1208 FieldTrial::CreateSimulatedFieldTrial(kTrialName, 100, "Simulated", 1221 FieldTrial::CreateSimulatedFieldTrial(kTrialName, 100, "Simulated",
1209 0.95); 1222 0.95);
1210 simulated_trial->group(); 1223 simulated_trial->group();
(...skipping 12 matching lines...) Expand all
1223 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); 1236 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true));
1224 // 4 KiB is enough to hold the trials only created for this test. 1237 // 4 KiB is enough to hold the trials only created for this test.
1225 shm.get()->Map(4 << 10); 1238 shm.get()->Map(4 << 10);
1226 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); 1239 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm));
1227 std::string check_string; 1240 std::string check_string;
1228 FieldTrialList::AllStatesToString(&check_string); 1241 FieldTrialList::AllStatesToString(&check_string);
1229 ASSERT_EQ(check_string.find("Simulated"), std::string::npos); 1242 ASSERT_EQ(check_string.find("Simulated"), std::string::npos);
1230 } 1243 }
1231 1244
1232 TEST(FieldTrialListTest, AssociateFieldTrialParams) { 1245 TEST(FieldTrialListTest, AssociateFieldTrialParams) {
1246 test::ScopedFeatureList scoped_feature_list;
1247 scoped_feature_list.Init();
1248
1233 std::string trial_name("Trial1"); 1249 std::string trial_name("Trial1");
1234 std::string group_name("Group1"); 1250 std::string group_name("Group1");
1235 1251
1236 // Create a field trial with some params. 1252 // Create a field trial with some params.
1237 FieldTrialList field_trial_list(nullptr); 1253 FieldTrialList field_trial_list(nullptr);
1238 FieldTrialList::CreateFieldTrial(trial_name, group_name); 1254 FieldTrialList::CreateFieldTrial(trial_name, group_name);
1239 std::map<std::string, std::string> params; 1255 std::map<std::string, std::string> params;
1240 params["key1"] = "value1"; 1256 params["key1"] = "value1";
1241 params["key2"] = "value2"; 1257 params["key2"] = "value2";
1242 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( 1258 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
(...skipping 16 matching lines...) Expand all
1259 EXPECT_EQ("value2", new_params["key2"]); 1275 EXPECT_EQ("value2", new_params["key2"]);
1260 EXPECT_EQ(2U, new_params.size()); 1276 EXPECT_EQ(2U, new_params.size());
1261 } 1277 }
1262 1278
1263 TEST(FieldTrialListTest, ClearParamsFromSharedMemory) { 1279 TEST(FieldTrialListTest, ClearParamsFromSharedMemory) {
1264 std::string trial_name("Trial1"); 1280 std::string trial_name("Trial1");
1265 std::string group_name("Group1"); 1281 std::string group_name("Group1");
1266 1282
1267 base::SharedMemoryHandle handle; 1283 base::SharedMemoryHandle handle;
1268 { 1284 {
1285 test::ScopedFeatureList scoped_feature_list;
1286 scoped_feature_list.Init();
1287
1269 // Create a field trial with some params. 1288 // Create a field trial with some params.
1270 FieldTrialList field_trial_list(nullptr); 1289 FieldTrialList field_trial_list(nullptr);
1271 FieldTrial* trial = 1290 FieldTrial* trial =
1272 FieldTrialList::CreateFieldTrial(trial_name, group_name); 1291 FieldTrialList::CreateFieldTrial(trial_name, group_name);
1273 std::map<std::string, std::string> params; 1292 std::map<std::string, std::string> params;
1274 params["key1"] = "value1"; 1293 params["key1"] = "value1";
1275 params["key2"] = "value2"; 1294 params["key2"] = "value2";
1276 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( 1295 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
1277 trial_name, group_name, params); 1296 trial_name, group_name, params);
1278 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); 1297 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
(...skipping 22 matching lines...) Expand all
1301 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); 1320 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true));
1302 // 4 KiB is enough to hold the trials only created for this test. 1321 // 4 KiB is enough to hold the trials only created for this test.
1303 shm.get()->Map(4 << 10); 1322 shm.get()->Map(4 << 10);
1304 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); 1323 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm));
1305 std::string check_string; 1324 std::string check_string;
1306 FieldTrialList::AllStatesToString(&check_string); 1325 FieldTrialList::AllStatesToString(&check_string);
1307 EXPECT_EQ("*Trial1/Group1/", check_string); 1326 EXPECT_EQ("*Trial1/Group1/", check_string);
1308 } 1327 }
1309 1328
1310 } // namespace base 1329 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/field_trial.cc ('k') | content/app/content_main_runner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698