Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 1155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1180 FieldTrialList field_trial_list(nullptr); | 1181 FieldTrialList field_trial_list(nullptr); |
| 1181 FieldTrialList::CreateFieldTrial("Trial1", "Group1"); | 1182 FieldTrialList::CreateFieldTrial("Trial1", "Group1"); |
| 1182 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | 1183 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| 1183 FieldTrialList::AllStatesToString(&save_string); | 1184 FieldTrialList::AllStatesToString(&save_string); |
| 1184 handle = base::SharedMemory::DuplicateHandle( | 1185 handle = base::SharedMemory::DuplicateHandle( |
| 1185 field_trial_list.field_trial_allocator_->shared_memory()->handle()); | 1186 field_trial_list.field_trial_allocator_->shared_memory()->handle()); |
| 1186 } | 1187 } |
| 1187 | 1188 |
| 1188 FieldTrialList field_trial_list2(nullptr); | 1189 FieldTrialList field_trial_list2(nullptr); |
| 1189 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); | 1190 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); |
| 1190 shm.get()->Map(4 << 10); // Hardcoded, equal to kFieldTrialAllocationSize. | 1191 shm.get()->Map(64 << 10); // Hardcoded, equal to kFieldTrialAllocationSize. |
|
Alexei Svitkine (slow)
2016/11/22 17:46:38
Nit: Remove this diff.
lawrencewu
2016/11/22 20:25:44
Done.
| |
| 1191 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); | 1192 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); |
| 1192 std::string check_string; | 1193 std::string check_string; |
| 1193 FieldTrialList::AllStatesToString(&check_string); | 1194 FieldTrialList::AllStatesToString(&check_string); |
| 1194 EXPECT_EQ(save_string, check_string); | 1195 EXPECT_EQ(save_string, check_string); |
| 1195 } | 1196 } |
| 1196 | 1197 |
| 1198 TEST(FieldTrialListTest, AssociateFieldTrialParams) { | |
| 1199 std::string trial_name("Trial1"); | |
| 1200 std::string group_name("Group1"); | |
| 1201 | |
| 1202 // Create a field trial with some params. | |
| 1203 FieldTrialList field_trial_list(nullptr); | |
| 1204 FieldTrialList::CreateFieldTrial(trial_name, group_name); | |
| 1205 std::map<std::string, std::string> params; | |
| 1206 params["key1"] = "value1"; | |
| 1207 params["key2"] = "value2"; | |
| 1208 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( | |
| 1209 trial_name, group_name, params); | |
| 1210 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | |
| 1211 | |
| 1212 // Clear all cached params from the associator. | |
| 1213 FieldTrialParamAssociator::GetInstance()->ClearAllCachedParamsForTesting(); | |
| 1214 | |
| 1215 // Check that we fetch the param from shared memory properly. | |
| 1216 std::map<std::string, std::string> new_params; | |
| 1217 FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name, | |
| 1218 &new_params); | |
| 1219 EXPECT_EQ(new_params["key1"], "value1"); | |
|
Alexei Svitkine (slow)
2016/11/22 17:46:37
The expected value should be on the left.
lawrencewu
2016/11/22 20:25:44
Fixed.
| |
| 1220 EXPECT_EQ(new_params["key2"], "value2"); | |
| 1221 } | |
| 1222 | |
| 1197 } // namespace base | 1223 } // namespace base |
| OLD | NEW |