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

Side by Side Diff: components/metrics/persistent_system_profile_unittest.cc

Issue 2907543003: Support persistent system profiles. (Closed)
Patch Set: addressed review comments by asvitkine Created 3 years, 6 months 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
(Empty)
1 // Copyright 2017 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
5 #include "components/metrics/persistent_system_profile.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/metrics/persistent_memory_allocator.h"
10 #include "base/rand_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace metrics {
14
15 class PersistentSystemProfileTest : public testing::Test {
16 public:
17 const int32_t kAllocatorMemorySize = 1 << 20; // 1 MiB
18
19 PersistentSystemProfileTest() {}
20 ~PersistentSystemProfileTest() override {}
21
22 void SetUp() override {
23 memory_allocator_ = base::MakeUnique<base::LocalPersistentMemoryAllocator>(
24 kAllocatorMemorySize, 0, "");
25 records_ = base::MakeUnique<PersistentSystemProfile::RecordAllocator>(
26 memory_allocator_.get());
27 persistent_profile_.RegisterPersistentAllocator(memory_allocator_.get());
28 }
29
30 void TearDown() override {
31 persistent_profile_.DeregisterPersistentAllocator(memory_allocator_.get());
32 records_.reset();
33 memory_allocator_.reset();
34 }
35
36 void WriteRecord(uint8_t type, const std::string& record) {
37 persistent_profile_.allocators_[0].Write(
38 static_cast<PersistentSystemProfile::RecordType>(type), record);
39 }
40
41 bool ReadRecord(uint8_t* type, std::string* record) {
42 PersistentSystemProfile::RecordType rec_type;
43
44 bool success = records_->Read(&rec_type, record);
45 *type = rec_type; // Convert to uint8_t for testing.
46 return success;
47 }
48
49 base::PersistentMemoryAllocator* memory_allocator() {
50 return memory_allocator_.get();
51 }
52
53 PersistentSystemProfile* persistent_profile() { return &persistent_profile_; }
54
55 private:
56 PersistentSystemProfile persistent_profile_;
57 std::unique_ptr<base::PersistentMemoryAllocator> memory_allocator_;
58 std::unique_ptr<PersistentSystemProfile::RecordAllocator> records_;
59
60 DISALLOW_COPY_AND_ASSIGN(PersistentSystemProfileTest);
61 };
62
63 TEST_F(PersistentSystemProfileTest, Create) {
64 uint32_t type;
65 base::PersistentMemoryAllocator::Iterator iter(memory_allocator());
66 base::PersistentMemoryAllocator::Reference ref = iter.GetNext(&type);
67 DCHECK(ref);
68 DCHECK_NE(0U, type);
69 }
70
71 TEST_F(PersistentSystemProfileTest, RecordSplitting) {
72 const size_t kRecordSize = 100 << 10; // 100 KiB
73 std::vector<char> buffer;
74 buffer.resize(kRecordSize);
75 base::RandBytes(&buffer[0], kRecordSize);
76
77 WriteRecord(42, std::string(&buffer[0], kRecordSize));
78
79 uint8_t type;
80 std::string record;
81 ASSERT_TRUE(ReadRecord(&type, &record));
82 EXPECT_EQ(42U, type);
83 ASSERT_EQ(kRecordSize, record.size());
84 for (size_t i = 0; i < kRecordSize; ++i)
85 EXPECT_EQ(buffer[i], record[i]);
86 }
87
88 TEST_F(PersistentSystemProfileTest, ProfileStorage) {
89 SystemProfileProto proto1;
90 SystemProfileProto::FieldTrial* trial = proto1.add_field_trial();
91 trial->set_name_id(123);
92 trial->set_group_id(456);
93
94 persistent_profile()->SetSystemProfile(proto1);
95
96 SystemProfileProto proto2;
97 ASSERT_TRUE(
98 PersistentSystemProfile::GetSystemProfile(&proto2, memory_allocator()));
99 ASSERT_EQ(1, proto2.field_trial_size());
100 EXPECT_EQ(123U, proto2.field_trial(0).name_id());
101 EXPECT_EQ(456U, proto2.field_trial(0).group_id());
102
103 // Check that the profile can be overwritten.
104
105 trial = proto1.add_field_trial();
106 trial->set_name_id(78);
107 trial->set_group_id(90);
108
109 persistent_profile()->SetSystemProfile(proto1);
110
111 ASSERT_TRUE(
112 PersistentSystemProfile::GetSystemProfile(&proto2, memory_allocator()));
113 ASSERT_EQ(2, proto2.field_trial_size());
114 EXPECT_EQ(123U, proto2.field_trial(0).name_id());
115 EXPECT_EQ(456U, proto2.field_trial(0).group_id());
116 EXPECT_EQ(78U, proto2.field_trial(1).name_id());
117 EXPECT_EQ(90U, proto2.field_trial(1).group_id());
118 }
119
120 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698