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

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

Issue 2463223002: Store field trial parameters in shared memory (Closed)
Patch Set: check that cache has been cleared in test 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
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/rand_util.h" 16 #include "base/rand_util.h"
16 #include "base/run_loop.h" 17 #include "base/run_loop.h"
17 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
19 #include "base/test/gtest_util.h" 20 #include "base/test/gtest_util.h"
20 #include "base/test/mock_entropy_provider.h" 21 #include "base/test/mock_entropy_provider.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 namespace base { 24 namespace base {
24 25
(...skipping 1196 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 FieldTrialList field_trial_list2(nullptr); 1222 FieldTrialList field_trial_list2(nullptr);
1222 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); 1223 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true));
1223 // 4 KiB is enough to hold the trials only created for this test. 1224 // 4 KiB is enough to hold the trials only created for this test.
1224 shm.get()->Map(4 << 10); 1225 shm.get()->Map(4 << 10);
1225 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); 1226 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm));
1226 std::string check_string; 1227 std::string check_string;
1227 FieldTrialList::AllStatesToString(&check_string); 1228 FieldTrialList::AllStatesToString(&check_string);
1228 ASSERT_EQ(check_string.find("Simulated"), std::string::npos); 1229 ASSERT_EQ(check_string.find("Simulated"), std::string::npos);
1229 } 1230 }
1230 1231
1232 TEST(FieldTrialListTest, AssociateFieldTrialParams) {
1233 std::string trial_name("Trial1");
1234 std::string group_name("Group1");
1235
1236 // Create a field trial with some params.
1237 FieldTrialList field_trial_list(nullptr);
1238 FieldTrialList::CreateFieldTrial(trial_name, group_name);
1239 std::map<std::string, std::string> params;
1240 params["key1"] = "value1";
1241 params["key2"] = "value2";
1242 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
1243 trial_name, group_name, params);
1244 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
1245
1246 // Clear all cached params from the associator.
1247 FieldTrialParamAssociator::GetInstance()->ClearAllCachedParamsForTesting();
1248 // Check that the params have been cleared from the cache.
1249 std::map<std::string, std::string> cached_params;
1250 FieldTrialParamAssociator::GetInstance()->GetFieldTrialParamsWithoutFallback(
1251 trial_name, group_name, &cached_params);
1252 EXPECT_EQ(0U, cached_params.size());
1253
1254 // Check that we fetch the param from shared memory properly.
1255 std::map<std::string, std::string> new_params;
1256 FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name,
1257 &new_params);
1258 EXPECT_EQ("value1", new_params["key1"]);
1259 EXPECT_EQ("value2", new_params["key2"]);
1260 EXPECT_EQ(2U, new_params.size());
1261 }
1262
1263 TEST(FieldTrialListTest, ClearParamsFromSharedMemory) {
1264 std::string trial_name("Trial1");
1265 std::string group_name("Group1");
1266
1267 base::SharedMemoryHandle handle;
1268 {
1269 // Create a field trial with some params.
1270 FieldTrialList field_trial_list(nullptr);
1271 FieldTrial* trial =
1272 FieldTrialList::CreateFieldTrial(trial_name, group_name);
1273 std::map<std::string, std::string> params;
1274 params["key1"] = "value1";
1275 params["key2"] = "value2";
1276 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
1277 trial_name, group_name, params);
1278 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
1279
1280 // Clear all params from the associator AND shared memory. The allocated
1281 // segments should be different.
1282 FieldTrial::FieldTrialRef old_ref = trial->ref_;
1283 FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
1284 FieldTrial::FieldTrialRef new_ref = trial->ref_;
1285 EXPECT_NE(old_ref, new_ref);
1286
1287 // Check that there are no params associated with the field trial anymore.
1288 std::map<std::string, std::string> new_params;
1289 FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name,
1290 &new_params);
1291 EXPECT_EQ(0U, new_params.size());
1292
1293 // Now duplicate the handle so we can easily check that the trial is still
1294 // in shared memory via AllStatesToString.
1295 handle = base::SharedMemory::DuplicateHandle(
1296 field_trial_list.field_trial_allocator_->shared_memory()->handle());
1297 }
1298
1299 // Check that we have the trial.
1300 FieldTrialList field_trial_list2(nullptr);
1301 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true));
1302 // 4 KiB is enough to hold the trials only created for this test.
1303 shm.get()->Map(4 << 10);
1304 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm));
1305 std::string check_string;
1306 FieldTrialList::AllStatesToString(&check_string);
1307 EXPECT_EQ("*Trial1/Group1/", check_string);
1308 }
1309
1231 } // namespace base 1310 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/field_trial_param_associator.cc ('k') | components/variations/variations_seed_processor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698