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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/metrics/field_trial_unittest.cc
diff --git a/base/metrics/field_trial_unittest.cc b/base/metrics/field_trial_unittest.cc
index fcf5480d7fd9b6b10e7ebd1fbd7eac612a8e9060..6c070268f1c2fb057d85c28c2418474d37de32dc 100644
--- a/base/metrics/field_trial_unittest.cc
+++ b/base/metrics/field_trial_unittest.cc
@@ -12,6 +12,7 @@
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
+#include "base/metrics/field_trial_param_associator.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
@@ -1228,4 +1229,82 @@ TEST(FieldTrialListTest, DoNotAddSimulatedFieldTrialsToAllocator) {
ASSERT_EQ(check_string.find("Simulated"), std::string::npos);
}
+TEST(FieldTrialListTest, AssociateFieldTrialParams) {
+ std::string trial_name("Trial1");
+ std::string group_name("Group1");
+
+ // Create a field trial with some params.
+ FieldTrialList field_trial_list(nullptr);
+ FieldTrialList::CreateFieldTrial(trial_name, group_name);
+ std::map<std::string, std::string> params;
+ params["key1"] = "value1";
+ params["key2"] = "value2";
+ FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
+ trial_name, group_name, params);
+ FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
+
+ // Clear all cached params from the associator.
+ FieldTrialParamAssociator::GetInstance()->ClearAllCachedParamsForTesting();
+ // Check that the params have been cleared from the cache.
+ std::map<std::string, std::string> cached_params;
+ FieldTrialParamAssociator::GetInstance()->GetFieldTrialParamsWithoutFallback(
+ trial_name, group_name, &cached_params);
+ EXPECT_EQ(0U, cached_params.size());
+
+ // Check that we fetch the param from shared memory properly.
+ std::map<std::string, std::string> new_params;
+ FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name,
+ &new_params);
+ EXPECT_EQ("value1", new_params["key1"]);
+ EXPECT_EQ("value2", new_params["key2"]);
+ EXPECT_EQ(2U, new_params.size());
+}
+
+TEST(FieldTrialListTest, ClearParamsFromSharedMemory) {
+ std::string trial_name("Trial1");
+ std::string group_name("Group1");
+
+ base::SharedMemoryHandle handle;
+ {
+ // Create a field trial with some params.
+ FieldTrialList field_trial_list(nullptr);
+ FieldTrial* trial =
+ FieldTrialList::CreateFieldTrial(trial_name, group_name);
+ std::map<std::string, std::string> params;
+ params["key1"] = "value1";
+ params["key2"] = "value2";
+ FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
+ trial_name, group_name, params);
+ FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded();
+
+ // Clear all params from the associator AND shared memory. The allocated
+ // segments should be different.
+ FieldTrial::FieldTrialRef old_ref = trial->ref_;
+ FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
+ FieldTrial::FieldTrialRef new_ref = trial->ref_;
+ EXPECT_NE(old_ref, new_ref);
+
+ // Check that there are no params associated with the field trial anymore.
+ std::map<std::string, std::string> new_params;
+ FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name,
+ &new_params);
+ EXPECT_EQ(0U, new_params.size());
+
+ // Now duplicate the handle so we can easily check that the trial is still
+ // in shared memory via AllStatesToString.
+ handle = base::SharedMemory::DuplicateHandle(
+ field_trial_list.field_trial_allocator_->shared_memory()->handle());
+ }
+
+ // Check that we have the trial.
+ FieldTrialList field_trial_list2(nullptr);
+ std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true));
+ // 4 KiB is enough to hold the trials only created for this test.
+ shm.get()->Map(4 << 10);
+ FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm));
+ std::string check_string;
+ FieldTrialList::AllStatesToString(&check_string);
+ EXPECT_EQ("*Trial1/Group1/", check_string);
+}
+
} // namespace base
« 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