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

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

Issue 1779503002: Fix StatisticsRecorder to handle re-entry during tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: always do global UninitializeForTesting when uninitializing Created 4 years, 8 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
« no previous file with comments | « no previous file | base/metrics/sparse_histogram_unittest.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/histogram.h" 5 #include "base/metrics/histogram.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 void TearDown() override { 48 void TearDown() override {
49 if (allocator_) { 49 if (allocator_) {
50 ASSERT_FALSE(allocator_->IsFull()); 50 ASSERT_FALSE(allocator_->IsFull());
51 ASSERT_FALSE(allocator_->IsCorrupt()); 51 ASSERT_FALSE(allocator_->IsCorrupt());
52 } 52 }
53 UninitializeStatisticsRecorder(); 53 UninitializeStatisticsRecorder();
54 DestroyPersistentHistogramAllocator(); 54 DestroyPersistentHistogramAllocator();
55 } 55 }
56 56
57 void InitializeStatisticsRecorder() { 57 void InitializeStatisticsRecorder() {
58 StatisticsRecorder::ResetForTesting(); 58 DCHECK(!statistics_recorder_);
59 statistics_recorder_ = new StatisticsRecorder(); 59 statistics_recorder_.reset(new StatisticsRecorder());
60 } 60 }
61 61
62 void UninitializeStatisticsRecorder() { 62 void UninitializeStatisticsRecorder() {
63 delete statistics_recorder_; 63 statistics_recorder_.reset();
64 statistics_recorder_ = NULL;
65 } 64 }
66 65
67 void CreatePersistentHistogramAllocator() { 66 void CreatePersistentHistogramAllocator() {
68 // By getting the results-histogram before any persistent allocator 67 // By getting the results-histogram before any persistent allocator
69 // is attached, that histogram is guaranteed not to be stored in 68 // is attached, that histogram is guaranteed not to be stored in
70 // any persistent memory segment (which simplifies some tests). 69 // any persistent memory segment (which simplifies some tests).
71 GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); 70 GlobalHistogramAllocator::GetCreateHistogramResultHistogram();
72 71
73 GlobalHistogramAllocator::CreateWithLocalMemory( 72 GlobalHistogramAllocator::CreateWithLocalMemory(
74 kAllocatorMemorySize, 0, "HistogramAllocatorTest"); 73 kAllocatorMemorySize, 0, "HistogramAllocatorTest");
75 allocator_ = GlobalHistogramAllocator::Get()->memory_allocator(); 74 allocator_ = GlobalHistogramAllocator::Get()->memory_allocator();
76 } 75 }
77 76
78 void DestroyPersistentHistogramAllocator() { 77 void DestroyPersistentHistogramAllocator() {
79 allocator_ = nullptr; 78 allocator_ = nullptr;
80 GlobalHistogramAllocator::ReleaseForTesting(); 79 GlobalHistogramAllocator::ReleaseForTesting();
81 } 80 }
82 81
83 const bool use_persistent_histogram_allocator_; 82 const bool use_persistent_histogram_allocator_;
84 83
85 StatisticsRecorder* statistics_recorder_ = nullptr; 84 std::unique_ptr<StatisticsRecorder> statistics_recorder_;
86 std::unique_ptr<char[]> allocator_memory_; 85 std::unique_ptr<char[]> allocator_memory_;
87 PersistentMemoryAllocator* allocator_ = nullptr; 86 PersistentMemoryAllocator* allocator_ = nullptr;
88 87
89 private: 88 private:
90 DISALLOW_COPY_AND_ASSIGN(HistogramTest); 89 DISALLOW_COPY_AND_ASSIGN(HistogramTest);
91 }; 90 };
92 91
93 // Run all HistogramTest cases with both heap and persistent memory. 92 // Run all HistogramTest cases with both heap and persistent memory.
94 INSTANTIATE_TEST_CASE_P(HeapAndPersistent, HistogramTest, testing::Bool()); 93 INSTANTIATE_TEST_CASE_P(HeapAndPersistent, HistogramTest, testing::Bool());
95 94
96 95
97 // Check for basic syntax and use. 96 // Check for basic syntax and use.
98 TEST_P(HistogramTest, BasicTest) { 97 TEST_P(HistogramTest, BasicTest) {
99 // Try basic construction 98 // Try basic construction
100 HistogramBase* histogram = Histogram::FactoryGet( 99 HistogramBase* histogram = Histogram::FactoryGet(
101 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 100 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
102 EXPECT_TRUE(histogram); 101 EXPECT_TRUE(histogram);
103 102
104 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( 103 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
105 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 104 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
106 EXPECT_TRUE(linear_histogram); 105 EXPECT_TRUE(linear_histogram);
107 106
108 std::vector<int> custom_ranges; 107 std::vector<int> custom_ranges;
109 custom_ranges.push_back(1); 108 custom_ranges.push_back(1);
110 custom_ranges.push_back(5); 109 custom_ranges.push_back(5);
111 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( 110 HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
112 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags); 111 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
113 EXPECT_TRUE(custom_histogram); 112 EXPECT_TRUE(custom_histogram);
114 113
114 // Macros that create hitograms have an internal static variable which will
115 // continue to point to those from the very first run of this method even
116 // during subsequent runs.
117 static bool already_run = false;
118 if (already_run)
119 return;
120 already_run = true;
121
115 // Use standard macros (but with fixed samples) 122 // Use standard macros (but with fixed samples)
116 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); 123 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
117 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30); 124 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30);
118 125
119 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); 126 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130);
120 } 127 }
121 128
122 // Check that the macro correctly matches histograms by name and records their 129 // Check that the macro correctly matches histograms by name and records their
123 // data together. 130 // data together.
124 TEST_P(HistogramTest, NameMatchTest) { 131 TEST_P(HistogramTest, NameMatchTest) {
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 power_of_2 *= 2; 436 power_of_2 *= 2;
430 } 437 }
431 438
432 // Check to see that the bucket counts reflect our additions. 439 // Check to see that the bucket counts reflect our additions.
433 std::unique_ptr<SampleVector> samples = histogram->SnapshotSampleVector(); 440 std::unique_ptr<SampleVector> samples = histogram->SnapshotSampleVector();
434 for (int i = 0; i < 8; i++) 441 for (int i = 0; i < 8; i++)
435 EXPECT_EQ(i + 1, samples->GetCountAtIndex(i)); 442 EXPECT_EQ(i + 1, samples->GetCountAtIndex(i));
436 } 443 }
437 444
438 TEST_P(HistogramTest, CorruptSampleCounts) { 445 TEST_P(HistogramTest, CorruptSampleCounts) {
446 // The internal code creates histograms via macros and thus keeps static
447 // pointers to them. If those pointers are to persistent memory which will
448 // be free'd then any following calls to that code will crash with a
449 // segmentation violation.
450 if (use_persistent_histogram_allocator_)
451 return;
452
439 Histogram* histogram = static_cast<Histogram*>( 453 Histogram* histogram = static_cast<Histogram*>(
440 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags)); 454 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
441 455
442 // Add some samples. 456 // Add some samples.
443 histogram->Add(20); 457 histogram->Add(20);
444 histogram->Add(40); 458 histogram->Add(40);
445 459
446 std::unique_ptr<SampleVector> snapshot = histogram->SnapshotSampleVector(); 460 std::unique_ptr<SampleVector> snapshot = histogram->SnapshotSampleVector();
447 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES, 461 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES,
448 histogram->FindCorruption(*snapshot)); 462 histogram->FindCorruption(*snapshot));
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 // CustomHistogram needs at least 1 valid range. 716 // CustomHistogram needs at least 1 valid range.
703 custom_ranges.clear(); 717 custom_ranges.clear();
704 custom_ranges.push_back(0); 718 custom_ranges.push_back(0);
705 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, 719 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges,
706 HistogramBase::kNoFlags), 720 HistogramBase::kNoFlags),
707 ""); 721 "");
708 } 722 }
709 #endif 723 #endif
710 724
711 } // namespace base 725 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/metrics/sparse_histogram_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698